Presentation is loading. Please wait.

Presentation is loading. Please wait.

Properties and Applications of Depth-First Search Trees and Forests

Similar presentations


Presentation on theme: "Properties and Applications of Depth-First Search Trees and Forests"— Presentation transcript:

1 Properties and Applications of Depth-First Search Trees and Forests

2 DFS Trees and Forests 1 3 5 2 4 6 6 5 2 4 3 1 DFS Tree DFS Trees
Convention: Roots in decreasing order; other nodes in increasing order Convention: increasing order 1 3 5 2 4 6 6 5 3 1 Can choose any order, so when there is a choice, choose smallest. When starting w/1, there are two choices, 3,5. Choose the smallest, 3. DFS Tree DFS Trees Forest Note: O(m) to create (check each edge once)

3 Edge Classification 3 2 5 4 1 6 Add all edges  but make them dashed if a marked node is encountered. 1 3 5 6 6 5 3 cross 1 backward cross cross forward cross backward cross backward cross 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. backward cross

4 Observations about Edge Classification
Requires O(m) to create  each edge considered only once Only go between trees in a forest with cross edges If we go left to right in building the DFS tree, then all cross edges go from right to left. (For suppose not, then the edge must be a tree edge.) b a c In depth first search tree higher & lower – skipping at least one w/solid lines Must not have been visited! All visited nodes are to the left (cross), above or same (backward), or below (forward).

5 Postorder Numbers Given a DFS tree, do a postorder traversal of the tree edges and number the nodes as they are visited. 1 3 5 6 (6) 1 6 5 3 (5) (1) (6) (5) (4) (4) (2) (3) Order of number determines if forward or backward arc void dfs(int node) { notes(nodes) boolean TestAcyclic() dfsForest(); for (int u=0; j<MAX; u++) { List p = nodes[u].succ; while (; != null) (2) (3) (1) Can be added as the tree is built, so O(m) (assuming m >> n)

6 Observations about Postorder Numbers
For each edge xy, where P(x) and P(y) are the postorder numbers for x and y, if xy, then for a tree edge, P(x) > P(y) for a forward edge, P(x) > P(y) for a backward edge, P(x)  P(y) for a cross edge, P(x) > P(y) There are many interesting applications of all these ideas  all of which take O(m) time, assuming m >> n. different from the others Left to Right Always directing back

7 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(n2) in the worst case for a complete graph), which beats O(n3) in Warshall’s algorithm, i.e. compute matrix and see if there is a 1 on the diagonal. Proof (sketch) (if, i.e. If a DFS-tree of G has a backward edge, G is cyclic.) Let x  y be the backward edge. Then <y, …, x, y> is a cycle. (only if, i.e. If G is cyclic, a DFS-tree of G has a backward edge.) Let the cycle be <n1, n2, …, nk, n1>. If k=1, <n1, n1> is a backward edge. If k>1, then there is a backward edge. For suppose not, then P(n1)>P(n2)>…>P(nk)>P(n1) and thus P(n1)>P(n1)  a contradiction.

8 (for acyclic directed graphs)
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 xRy. 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.)

9 Topological Sort Example
b c f d e Topological Sort Example Alphabetical: a b c f d e (1) (2) (3) (4) (5) (6) b f d c e a1 Reverse Alphabetical: Notes: (1) All edges go to the right. (2) The nodes are correctly and totally ordered. (3) More than one ordering is possible. f b (6) (5) d c (1) (4) b f c a e d1 e a (2) (3)

10 Topological Sort: Observations
The algorithm is correct. Proof (sketch): The tail of x  y must precede the head because P(x) > P(y), for if not then P(x)  P(y) and we have a backward edge  a contradiction since the graph is acyclic. The algorithm takes O(m) time to build the DFS forest and push the elements onto the stack and takes O(n) time to pop the elements. Thus, the algorithm runs in O(m) time assuming m >> n. Why not do regular sort this way? O(m) is O(n2) in the worst case. We can sort in O(nlogn) time. Could we do topological sort using a regular sort? If not, why not? Some nodes not comparable  can let them be equal. If we only have the edges, some nodes that should be comparable through transitivity are not comparable  can do transitive closure to make comparable. If so, should we? (transitive closure: O(n3)  Warshall’s alg.)

11 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. Proof: clear. O(m), (O(n2) in the worst case). Beats Warshall’s O(n3), 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(n3) in the worst case. Which is better depends on whether the graph is sparse: DFS reachability, O(nm), beats Warshall’s, O(n3); or dense: in which case, Warshall’s is cheaper to setup and run.

12 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 exercise) We can observe the proof – not worth the time to formally prove.


Download ppt "Properties and Applications of Depth-First Search Trees and Forests"

Similar presentations


Ads by Google