GRAPHS Trees Without Rules. Graph A data structure that consists of a set of nodes (called...

19
GRAPHS Trees Without Rules

Transcript of GRAPHS Trees Without Rules. Graph A data structure that consists of a set of nodes (called...

Page 1: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

GRAPHS

Trees Without Rules

Page 2: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Graph

A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each other

Page 3: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Facebook Graph

June

SarahSusan

Robert

Judy

Katie

Lila

Rebecca

John

Vertices: peopleEdges: friends

Page 4: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Airline Routes Graph

Page 5: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Course Prerequisites Graph

Page 6: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Graphs

Another recursive data structure Node with any number of pointers to other nodes No restrictions on connectivity

Allows disconnected nodes, multiple paths, and cycles Terminology

Node Arc Directed Connected Path Cycle

Page 7: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Implementation Strategies

Create a set of all arcs (a->b, a->c, b->d, c->d, d->a, d->b)

Adjacency List a: {b, c} b: {d} c:{d} d:{a, b}

Adjacency matrix

Consider the data Sparse vs. Dense Space vs. Time

a

c b

d

Page 8: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Coding Graphs

Graph itself is set or vector of node * Why not just pointer to a root, like a tree? Could you designate an arbitrary node as

root?

Struct nodeT {//data for node goes herevector <nodeT *> connected;

};

Page 9: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Coding Graphs

Often graphs have data associated with the arc itself. Unlike trees and lists where links are only

for connecting nodes Arcs may have information like distance or

cost. Add struct to hold arc info

Arc has pointers to start/end node and a node has a collection of arcs Circular reference

Page 10: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Circular Reference

nodeT

arcT

Page 11: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Arcs have Nodes and Nodes have Arcs

struct arcT {//arc fields go here

nodeT *start, *end; //error! nodeT not defined

};struct nodeT {

//node fields go herevector <arcT *> outgoing; //arcT already defined

};

Page 12: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Making the Gears Work

struct nodeT; //forward referencestruct arcT {

//arc fields go here nodeT *start, *end;};struct nodeT {

//node fields go herevector <arcT *> outgoing;

};

Page 13: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Graph Traversals

Traverse reachable nodes Start from a node and follow arcs to other nodes Some graphs not fully connected, so not all nodes

are reachable Depth-first and Breadth-first

Similar to tree’s post-order, pre-order and in-order Both visit all reachable nodes, but in a different

order Possibility of cycles means you must track “visited”

nodes to avoid infinite loop Could maintain a visited flag per node or set of visited

nodes

Page 14: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Depth-First Traversal

Choose a starting node No root node in graph, may have specific start in

mind or just choose one randomly Go Deep

Pick a neighbor, explore all reachable from there Backtrack

After fully exploring everything reachable from first neighbor, choose another neighbor and go again

Continue until all neighbors are exhausted Base case?

Page 15: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Depth-First Pseudocode

void DepthFirstSeach(node *cur, set<nodeT *> & visited)

{//if visited contains cur, do nothing.//otherwise, add cur to visited set//for all arcT’s in the graph vector named outgoing

DepthFirstSearch(cur->outgoing[i]->end, visited);

}

Page 16: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Trace

A

B

C

D

E

F

G

H

A B C D E F G H

Page 17: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Breadth-First Traversal

Choose starting node Visit all immediate neighbors

Those directly connected to start node Branch out to all neighbors 2 hops away Again to 3 hops and so on

Until all reachable nodes are visited How to manage nodes to vist

Perfect for the queue What about cycles/multiple paths?

Need to track visited status

Page 18: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Breadth-First Pseudocode

void BreadthFirstSearch(nodeT *start){

queue<nodeT *> q;set<nodeT *> visited;q.enqueue(start);//while q is not empty

nodeT *cur = q.dequeue();//if visited does not contain cur, add cur to visited

and//for all arcTs in vector named outgoing...q.enqueue(cur->outgoing[i]->end);

}

Page 19: GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Graph Search Algorithms

Many interesting questions are really just graph searches Which nodes are reachable from this node? Does the graph have a cycle? Is the graph fully connected? Longest path without a cycle? Is there a continuous path that visits all

nodes once and exactly once?