340 likes | 368 Views
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.
E N D
Graph Traversal Chapter 9 Used lecture slides of Skiena
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”?
Previsit and Postvisit Orderings – Each pre and post visit is an ordered event Account for all edges – Tree edges and Back edges
Previsit and Postvisit Orderings – Each pre and post visit is an ordered event
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)
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)
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)
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?
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)
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
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
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
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