Depth-First-Search Trees DFS Forest Will DFS always reach all the nodes in a graph? No 1. The graph may not be connected. 2. Some nodes may not be reachable from the starting node. How can you make DFS reach all the nodes? dfsForest() for each node x clear the visit mark on node x for each node x if node x is not marked dfs(x) DFS Trees Why is this function called dfsForest? Because each initial call to 'dfs' forms a tree. How is a DFS Tree formed? 1. The initial call to 'dfs' is the root of the tree. 2. When dfs(x) calls dfs(y), y becomes a child of x. Draw the DFS Forest for the directed graph starting with node a. (graph3.pdf) V = { a, b, c, d, e, f } E = { (a,b), (a,d), (b,d), (b,e), (c,b), (c,f), (e,a), (e,d), (f,b), (f,e) } a---b---c | X | \ | d---e---f Classwork You may work with a partner. Draw the DFS Forest for the directed graph starting with node a. (When there is a choice of nodes to visit, choose the one that is first in alphabetic order.) (graph2.pdf) V = { a, b, c, d, e } E = { (b,a), (b,c), (b,d), (b,e), (c,b), (d,c), (d,e), (e,a) } a---b---c \ / \ / e---d Types of Edges What are the types of edges in a DFS Forest? 1. Tree edges: x->y where dfs(x) calls dfs(y) 2. Forward edges: x->y where y is proper descendant of x but x->y is not a tree edge 3. Backward edges: x->y where y is ancestor of x 4. Cross edges: x->y where y is not ancestor nor descendant Label each edge in the DFS forest with its type. Classwork You may work with a partner. Label each edge in the DFS forest with its type. Postorder Numbers How do you number the nodes in a DFS Tree in Postorder? Assign the next number to node x just before dfs(x) returns. Label each node in the DFS forest with its postorder number. (Start with number 1.) Classwork You may work with a partner. Label each node in the DFS forest with its postorder number. (Start with number 1.) How do Postorder Numbers identify the types of edges in the forest? 1. tree-edge or forward-edge (x->y) x > y (y is a descendant of x) 2. cross-edge (x->y) x > y (y is to the left of x) 3. backward-edge (x->y) x <= y (y is an ancestor of x) Finding Cycles How do you use DFS to find cycles in a graph? 1. Run 'dfsForest' and assign postorder numbers. 2. Compare postorder numbers to find backward edges. What's the code for finding cycles? isCyclic() run dfsForest() for each edge x->y (for each node and for each adjacent node) if x.postorder <= y.postorder return true end if end for return false end isCyclic What's the Big-Oh for the running time of isCyclic? 1. dfsForest is O(n+e) 2. the loop repeats once for each edge 3. isCyclic is O(n+e) Topological Sorting What's Topological Sorting? 1. Order the nodes so all edges go from left to right. 2. The graph must be a partial order. (no cycles) How do you use DFS to do a Topological Sort? 1. Run 'dfsForest' and assign postorder numbers. 2. List the nodes in reverse postorder. What's the Big-Oh for the running time of Topological Sort? Topological Sort is O(n+e) Give a topological order for the directed graph. (Assume dfsForest has already been run and postorder numbers have been assigned as given.) How can you modify the graph to allow the sort to succeed? (remove backward edge e->a) nodes: a b c d e f numbers: 4 3 6 1 2 5 Classwork You may work with a partner. Give a topological order for the directed graph. (Assume dfsForest has already been run and postorder numbers have been assigned as given.) How can you modify the graph to allow the sort to succeed? (remove backward edge c->b) nodes: a b c d e numbers: 1 5 2 4 3