BFS DFS Applications of DFS Trees and Forests
BFS, DFS, Applications of DFS Trees and Forests, Stronglyconnected Components
Breadth-First Search (BFS)
Mark start node and enqueue While queue is not empty Dequeue N For each neighbor X of N If X is not marked Mark X and enqueue BFS Algorithm Marked a b c a c b d N a a a b c c d Queue a b bc c d - d Search order: a b c d Undirected edges: each edge twice = O(n+2 m) = O(m) if m>>n
Depth-First Search (DFS)
DFS (start node) Proc DFS (N) Mark N For each neighbor X of N If X is not marked DFS (X) DFS Algorithm Marked N a a a b b c c d c b a c b c d d Search order: a b c d DFS a b c d = O(n+2 m) = O(m) if m>>n
BFS vs. DFS Marked a b c e d f N a a b b c c e d f Queue a b bc bce ce ced ed edf df f - a e b c f BFS Order: a b c e d f d
BFS vs. DFS Marked N a a a b b c c f c b d b a e a b c f d e DFS a a e b c d b c f f d e DFS Order: a b c f d e (BFS Order: a b c e d f)
BFS & DFS with Directed Graphs a b c e j a f k d g l h b i m BFS a, b, c, d, e, f, g, h, i, j, k, l, m, n Same as before, by chance c e n j f k d g l h i m DFS a, b, e, j, f, k, l, h, c, g, d, i, m, n Not same as before n
DFS Trees and Forests 3 2 1 5 Convention: increasing order Convention: Roots in decreasing order; other nodes in increasing order 6 4 1 6 3 DFS Tree 2 5 4 3 DFS Trees 2 4 6 1 5 Note: O(m) to create (check each edge once) Forest
Edge Classification 3 2 1 6 5 4 Add all edges but make them dashed if a marked node is encountered. 6 1 forward backward 3 5 backward 2 cross 4 backward 6 cross 5 cross 2 cross 4 cross 1 cross 3 backward Tree edge if y is a child of x in DFS forest. Forward edge if y is a descendent of x, but not a child. Backward edge if y is an ancestor of x or if x = y. Cross edge if y is not x and is neither a descendent nor an ancestor of x.
Postorder Numbers Given a DFS tree, do a postorder traversal of the tree edges and number the nodes as they are visited. 1 (6) 6 3 (5) (2) (5) 5 2 5 (4) 2 (1) (2) 4(3) 1(6) 4 (4) 3 (3) 6(1) • Can be added as the tree is built, so O(m) (assuming m >> n)
Observations about DFS and Postorder Numbers There are many interesting applications of all these ideas all of which take O(m) time, assuming m >> n.
Cycles A directed graph G is cyclic iff a DFS-tree of G has a backward edge This implies cycle detection can be done in O(m) (O(n 2) in the worst case for a complete graph), which beats O(n 3) in Warshall’s algorithm, i. e. compute matrix and see if there is a 1 on the diagonal.
Topological Sorting (for acyclic directed graphs) Recall that partial orders and Hasse diagrams are acyclic directed graphs. A topological sort of a partial ordering R is an imposed total ordering over the elements such that x precedes y if x. Ry. Algorithm: List the nodes in reverse postorder (i. e. push nodes on a stack whenever we assign a postorder number; then pop all of them. )
e a Topological Sort Example b c Alphabetical: a b d f (6) (1) c f (3) e d (2) Reverse Alphabetical: b f (5) d (1) (6) (5) b 6 f 5 d 4 c 3 e 2 a 1 (4) Notes: (1) All edges go to the right. (2) The nodes are correctly and totally ordered. (3) More than one ordering is possible. c (4) e (2) b 6 a (3) f 5 c 4 a 3 e 2 d 1
Reachability To test if we can reach y from x, build a DFS tree starting at x; y is reachable iff y is in the tree rooted at x. O(m), (O(n 2) in the worst case). Beats Warshall’s O(n 3), but Warshall’s tests reachability for all pairs. Can start the DFS reachability algorithm at every node, but then the algorithm is O(nm), which is O(n 3) in the worst case. Which is better depends on whether the graph is sparse: DFS reachability, O(nm), beats Warshall’s, O(n 3); dense: Warshall’s is cheaper to setup and run.
Strongly-connected Components Definition: Maximal set of vertices such that every vertex in the set can reach every other vertex in the set through some path that never leaves the set The SCCs in a graph partition the vertices into disjoint sets
SCC Algorithm 1. Create the reverse graph (all the edges are reversed) 2. Compute the post-order traversal number for each vertex using the reversed graph (the order of leaving the vertices in the depth-search) 3. Following the post-order traversal numbers on vertices from the last vertex to the first vertex, start a DFS on the original graph, and any vertex visited in that search belongs to the SCC. Repeat until all the vertices are visited.
SCC Example (1) 1. Create the reverse graph (all the edges are reversed) e a b c f b d e a c f d
SCC Example (2) 2. Compute the post-order traversal number for each vertex using the reversed graph (the order of leaving the vertices in the depth-search) e a b c f d Starting with a f: b: c: a: d: e: 0 1 2 3 4 5
SCC Example (3) 3. Following the post-order traversal numbers on vertices from the last vertex to the first vertex, start a DFS on the original graph, and any vertex visited in that search belongs to the SCC. Repeat until all the vertices are visited. e a b c f d Start with e: {e} Start with d: {d} Start with a: {a, b, c, f}
Connected Components for Undirected Graphs Replace each edge with two directed edges and then run the DFS forest algorithm. O(m) + O(m) = O(m) We can prove: x and y are in the same connected component of G iff x and y are in the same DFS tree. (left as an exercise).
- Slides: 22