1 / 33

Graph Data Structures

Graph Data Structures. 1843. ORD. SFO. 802. 1743. 337. 1233. LAX. DFW. Vertices and edges are positions store elements Accessor methods endVertices (e): an array of the two endvertices of e opposite (v, e): the vertex opposite of v on e

lweldon
Download Presentation

Graph Data Structures

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. Graph Data Structures 1843 ORD SFO 802 1743 337 1233 LAX DFW Graphs

  2. Vertices and edges are positions store elements Accessor methods endVertices(e): an array of the two endvertices of e opposite(v, e): the vertex opposite of v on e areAdjacent(v, w): true iff v and w are adjacent replace(v, x): replace element at vertex v with x replace(e, x): replace element at edge e with x Update methods insertVertex(o): insert a vertex storing element o insertEdge(v, w, o): insert an edge (v,w) storing element o removeVertex(v): remove vertex v (and its incident edges) removeEdge(e): remove edge e Iterable collection methods incidentEdges(v): edges incident to v vertices(): all vertices in the graph edges(): all edges in the graph Main Methods of the Graph ADT Graphs

  3. Adjacency Matrix Structure a v b • Edge list structure • Augmented vertex objects • Integer key (index) associated with vertex • 2D-array adjacency array • Reference to edge object for adjacent vertices • Null for non nonadjacent vertices • The “old fashioned” version just has 0 for no edge and 1 for edge u w 2 w 0 u 1 v b a Graphs

  4. Adjacency List Structure a v b • Incidence sequence for each vertex • sequence of references to edge objects of incident edges • Augmented edge objects • references to associated positions in incidence sequences of end vertices u w w u v b a Graphs

  5. Performance Graphs

  6. Applications of BFS and DFS CSE 2011 Winter 2011

  7. Some Applications of BFS and DFS • BFS • To find the shortest path from a vertex s to a vertex v in an unweighted graph • To find the length of such a path • To find out if a graph contains cycles • To construct a BSF tree/forest from a graph • DFS • To find a path from a vertex s to a vertex v. • To find the length of such a path. • To construct a DSF tree/forest from a graph.

  8. Testing for Cycles

  9. Testing for Cycles • Method isCyclic(v) returns true if a directed graph(with only one component) contains a cycle, and returns false otherwise. else return true; return false;

  10. Finding Cycles • To output the cycle just detected, use info in prev[ ]. • NOTE: The code above applies only to directed graphs. • Homework: Explain why that code does not work for undirected graphs.

  11. Finding Cycles in Undirected Graphs • To detect/find cycles in an undirected graph, we need to classify the edges into 3 categories during program execution: • unvisited edge: never visited. • discovery edge: visited for the very first time. • cross edge: edge that forms a cycle. • Code fragment 13.10, p. 623. • When the BFS algorithm terminates, the discovery edges form a spanning tree. • If there exists a cross edge, the undirected graph contains a cycle.

  12. BFS Algorithm (in textbook) • The algorithm uses a mechanism for setting and getting “labels” of vertices and edges AlgorithmBFS(G, s) L0new empty sequence L0.insertLast(s) setLabel(s, VISITED) i  0 while Li.isEmpty() Li +1new empty sequence for all v  Li.elements() for all e  G.incidentEdges(v) ifgetLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.insertLast(w) else setLabel(e, CROSS) i  i +1 AlgorithmBFS(G) Inputgraph G Outputlabeling of the edges and partition of the vertices of G for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() ifgetLabel(v) = UNEXPLORED BFS(G, v) Breadth-First Search

  13. L0 A L1 B C D E F Example unexplored vertex A visited vertex A unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search

  14. L0 L0 A A L1 L1 B C D B C D L2 E F E F L0 L0 A A L1 L1 B C D B C D L2 L2 E F E F Example (2) Breadth-First Search

  15. L0 L0 A A L1 L1 B C D B C D L2 L2 E F E F Example (3) L0 A L1 B C D L2 E F Breadth-First Search

  16. Computing Spanning Trees

  17. Trees • Tree: a connected graph without cycles. • Given a connected graph, remove the cycles  a tree. • The paths found by BFS(s) form a rooted tree (called a spanning tree), with the starting vertex as the root of the tree. BFS tree for vertex s = 2 What would a level-order traversal of the tree tell you?

  18. Computing a BFS Tree • Use BFS on a vertex BFS( v ) with array prev[ ] • The paths from source s to the other vertices form a tree

  19. Computing Spanning Forests

  20. Computing a BFS Forest • A forest is a set of trees. • A connected graph gives a tree (which is itself a forest). • A connected component also gives us a tree. • A graph with k components gives a forest of k trees.

  21. Example A graph with 3 components P Q N L R O M s D E C A F B K G H

  22. Example of a Forest We removed the cycles from the previous graph. P Q N L R O M s D E C A forest with 3 trees A F B K G H

  23. Computing a BFS Forest • Use BFS method on a graph BFSearch( G ), which calls BFS( v ) • Use BFS( v ) with array prev[ ]. • The paths originating from v form a tree. • BFSearch( G ) examines all the components to compute all the trees in the forest.

  24. Applications of DFS • Is there a path from source s to a vertex v? • Is an undirected graph connected? • Is a directed graph strongly connected? • To output the contents (e.g., the vertices) of a graph • To find the connected components of a graph • To find out if a graph contains cycles and report cycles. • To construct a DSF tree/forest from a graph

  25. Finding Cycles Using DFS • Similar to using BFS. • For undirected graphs, classify the edges into 3 categories during program execution: unvisited edge, discovery edge, and back (cross) edge. • Code Fragment 13.1, p. 613. • If there exists a back edge, the undirected graph contains a cycle.

  26. DFS Algorithm • The algorithm uses a mechanism for setting and getting “labels” of vertices and edges AlgorithmDFS(G, v) Inputgraph G and a start vertex v of G Outputlabeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e  G.incidentEdges(v) ifgetLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK) AlgorithmDFS(G) Inputgraph G Outputlabeling of the edges of G as discovery edges and back edges for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() ifgetLabel(v) = UNEXPLORED DFS(G, v) Depth-First Search

  27. A B D E C A A B D E B D E C C Example unexplored vertex A visited vertex A unexplored edge discovery edge back edge Depth-First Search

  28. A A A B D E B D E B D E C C C A B D E C Example (cont.) Depth-First Search

  29. DFS Tree Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls: • when we visit a neighbor w of v, we add w as child of v • whenever DFS returns from a vertex v, we climb up in the tree from v to its parent

  30. Applications – DFS vs. BFS • What can BFS do and DFS can’t? • Finding shortest paths (in unweighted graphs) • What can DFS do and BFS can’t? • Finding out if a connected undirected graph is biconnected • A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph. • Application in computer networks: ensuring that a network is still connected when a router/link fails.

  31. A graph that is not biconnected

  32. L0 A L1 B C D L2 E F DFS vs. BFS A B C D E F DFS BFS

  33. Final Exam • Final Exam • Sunday, April 17, 10:00-13:00 • Materials: • All lectures notes and corresponding sections in the textbook from the beginning to today’s lecture • Homework questions • Assignments 1 to 5

More Related