80 likes | 181 Views
CS344: Lecture 16. S. Muthu Muthukrishnan. E. D. C. B. A. Graph Navigation. BFS: DFS: DFS numbering by start time or finish time. tree, back, forward and cross edges. How to do a DFS and identify what each edge type is?
E N D
CS344: Lecture 16 S. Muthu Muthukrishnan
E D C B A Graph Navigation • BFS: • DFS: DFS numbering by start time or finish time. • tree, back, forward and cross edges. • How to do a DFS and identify what each edge type is? • Prove that a directed graph G is acyclic if and only if DFS of G has no back edges. E D C B A
Details • Iterative algorithm for BFS uses a queue, that for DFS uses a stack. • How to use BFS or DFS to test whether a undirected graph is connected? • Both BFS and DFS take time O(|V|+|E|) given graph in the adjacency list format.
DAGs • DAG • Topological ordering of a DAG G is a linear ordering of the vertices of G such that if (u,v) is an edge in G than v appears before u in the linear ordering. This can be thought of as numbering vertices in the topological order, and we will refer to it as topological numbering. • Use DFS to do topological numbering.
Another application • A directed graph G is strongly connected if there is a path from u to v and v to u for all pairs of vertices u,v. • Cute trick to check if G is strongly connected: • Pick some v. • Do DFS(v). If some vertex w is not reachable, print NOT strongly connected. • Reverse edges of G to get G’. • Do DFS(v) in G’. If some vertex w is not reachable, print NOT strongly connected. Else, print YES. • Time: O(|V|+|E|)
Strongly connected graph? Example a G: g c d e b f a g G’: c d e b f
a g c d e b f SC components • How to partition a graph into strongly connected components (SCC)? • SCC: Maximal subgraphs such that each vertex can reach all other vertices in the subgraph { a , c , g } { f , d , e , b }
SC Components Algorithm • Do DFS of G and put vertices in a stack at their finishing times. • Reverse edges of G to get G’. • Do DFS of G’ starting from vertices that get popped off the stack from the first step! Each DFS tree generated will be a SCC. • Question: What is the graph of SCC components of a directed graph G?