1 / 34

Graph Traversal

Graph Traversal. Chapter 9 Used lecture slides of Skiena. Graphs. Types of Graphs. Types of Graphs. Data Structures for Graphs. Data Structures for Graphs. Data Structures for Graphs.

jponce
Télécharger la présentation

Graph Traversal

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 Traversal Chapter 9 Used lecture slides of Skiena

  2. Graphs

  3. Types of Graphs

  4. Types of Graphs

  5. Data Structures for Graphs

  6. Data Structures for Graphs

  7. Data Structures for Graphs • Table of Edges - An even simpler data structure is just to maintain an array or linked list of edges. This is not as flexible as the other data structures at answering “who is adjacent to vertex x”?

  8. Directed and Undirected Edges

  9. Breadth-First Traversal

  10. Discovered vs. Processed Vertices

  11. Initializing Search

  12. BFS Implementation

  13. Exploiting Traversal

  14. Finding Paths

  15. Finding Paths

  16. Depth-First Search (DFS)

  17. DFS Implementation

  18. Connected Components

  19. Connected Components

  20. Depth-First Search (DFS)

  21. Previsit and Postvisit Orderings – Each pre and post visit is an ordered event Account for all edges – Tree edges and Back edges

  22. Previsit and Postvisit Orderings – Each pre and post visit is an ordered event

  23. Previsit and Postvisit Orders • DFS yields a forest (disjoint trees) when there are disjoint components in the graph • Can use pre/post visit numbers to detect properties of graphs • Account for all edges: Tree edges (solid) and back edges (dashed) • Back edges detect cycles • Properties still there even if explored in a different order (i.e. start with F, then J)

  24. Depth-First Search in Directed Graphs • Tree edges and back edges (2 below) the same as before • Added terminology for DFS Tree of a directed graph • Forward edges lead from a node to a nonchild descendant (2 below) • Cross edges lead to neither descendant or ancestor, lead to a node which has already had its postvisit (2 below)

  25. Back Edge Detection • Ancestor/descendant relations, as well as edge types can be read off directly from pre and post numbers of the DFS tree – just check nodes connected by each edge • Initial node of edge is u and final node is v • Tree/forward reads pre(u) < pre(v) < post(v) < post(u)

  26. Topological Sorting

  27. Longest Path in a DAG

  28. Topological Sorting Algorithm

  29. Strongly Connected Components • Two nodes u and v in a directed graph are connected if there is a path from u to v and a path from v to u • The disjoint subsets of a directed graph which are connected are called strongly connected components • How could we make the entire graph strongly connected?

  30. Meta-Graphs • Every directed graph is a DAG of its strongly connected components This meta-graph decomposition will be very useful in many applications (e.g. a high level flow of the task with subtasks abstracted)

  31. Example G • Create GR by reversing graph G • Do DFS on GR • Repeat until graph G is empty: • Pick node with highest remaining post score (from DFS tree on GR), and explore starting from that node in G • That will discover one sink SCC in G • Prune all nodes in that SCC from G and GR • Complexity? • Create GR • Post-ordered list –add nodes as do dfs(GR) • Do DFS with V reverse ordered from PO list • visited flag = removed GR

  32. Discovering Separating Vertices with DFS • a is a separating vertex of G if and only if either • a is the root of the DFS tree and has more than one child, or • a is not the root, and there exists a child s of a such that there is no back edge between any descendent of s (including s) and a proper ancestor of a. DFS tree with pre-ordering a:1 a b b:2 c:3 c d d:4 e:5 e

  33. a is a separating vertex of G if and only if either • a is the root of the DFS tree and has more than one child, or • a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. DFS tree with pre-ordering and low numbering a:1,1 a b b:2,2 c:3,1 f c d f:6,6 d:4,1 u is a sep. vertex iff there is any child v of u such thatlow(v) ≥ pre(u) e:5,1 e

  34. Example with pre and low a a,1,1 b,2,1 g,3,1 f low numbering g b e c,4,3 d,5,3 h,6,4 e,7,1 f,8,1 c u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u) d h

More Related