1 / 19

Graphs

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 other. Facebook Graph. Vertices: people Edges: friends. June. Katie. Susan. Sarah. Judy. Lila. Robert. Rebecca. John.

gefen
Download Presentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs Trees Without Rules

  2. 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

  3. Facebook Graph Vertices: people Edges: friends June Katie Susan Sarah Judy Lila Robert Rebecca John

  4. Airline Routes Graph

  5. Course Prerequisites Graph

  6. 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

  7. 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

  8. 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 here vector <nodeT *> connected; };

  9. 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

  10. Circular Reference

  11. 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 here vector <arcT *> outgoing; //arcT already defined };

  12. Making the Gears Work struct nodeT; //forward reference struct arcT { //arc fields go here nodeT *start, *end; }; struct nodeT { //node fields go here vector <arcT *> outgoing; };

  13. 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

  14. 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?

  15. 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); }

  16. Trace A H B D G C E A B C D E F G H F

  17. 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

  18. 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); }

  19. 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?

More Related