1 / 50

CSE 421

This summary provides an overview of the last lecture on Depth First Search (DFS) algorithm, including its implementation using a stack and its applications in finding shortest paths, determining connected components, and testing bipartiteness. The lecture also introduces the concept of non-tree edges and their significance in DFS.

stephanieg
Download Presentation

CSE 421

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 421 Depth First Search Yin Tat Lee

  2. Summary of last lecture • BFS() implemented using queue. • Edges into then-undiscovered vertices define a tree – the “Breadth First spanning tree” of • Level in the tree are exactly all vertices s.t., the shortest path (in ) from the root to is of length • All nontree edges join vertices on the same or adjacent levels of the tree • Applications: • Shortest Path • Connected component • Test bipartiteness / 2-coloring

  3. Preview of this lecture • Depth First Search • 1 property: non-tree edge is vertical instead of horizontal • 1 application: topological sort

  4. Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far you can Naturally implemented using recursive calls or a stack

  5. DFS(s) – Recursive version Initialization: mark all vertices undiscovered DFS() • Mark discovered • for each edge • if ( is undiscovered) • Mark discovered • DFS() • Mark fully-discovered

  6. Non-Tree Edges in DFS BFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way. All non-tree edges join a vertex and one of its descendents/ancestors in the DFS tree

  7. Color code: undiscovered discovered fully-explored DFS(A) A,1 Suppose edge lists at each vertex are sorted alphabetically Call Stack (Edge list): A (B,J) B J C G H K L D F I M st[] = {1} E

  8. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J C G H K L D F I M st[] = {1,2} E

  9. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 G H K L D F I M st[] = {1,2,3} E

  10. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4} E

  11. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4,5} E,5

  12. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 G H K L D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5

  13. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5

  14. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5

  15. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 H K L G,7 D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5

  16. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 H K L G,7 D,4 F,6 I M st[] = {1,2,3,4,5} E,5

  17. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 H K L G,7 D,4 F,6 I M st[] = {1,2,3,4} E,5

  18. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 H K L G,7 D,4 F,6 I M st[] = {1,2,3} E,5

  19. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 H,8 K L G,7 D,4 F,6 I M st[] = {1,2,3,8} E,5

  20. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) I (H) B,2 J C,3 H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8,9} E,5

  21. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8} E,5

  22. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8, 10} E,5

  23. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 H,8 K,11 L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8,10,11} E,5

  24. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M st[] = {1,2,3,8,10,11,12} E,5

  25. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) M (L) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12,13} E,5

  26. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12} E,5

  27. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11} E,5

  28. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5

  29. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5

  30. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8} E,5

  31. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3} E,5

  32. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2} E,5

  33. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2} E,5

  34. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1} E,5

  35. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1} E,5

  36. Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) TA-DA!! B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {} E,5

  37. DFS(A) Edge code: Tree edge Back edge A,1 B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 E,5

  38. A,1 Edge code: Tree edge Back edge No Cross Edges! DFS(A) B,2 C,3 H,8 D,4 J,10 E,5 I,9 K,11 F,6 L,12 G,7 M,13

  39. Properties of (undirected) DFS Like BFS(): • DFS() visits iff there is a path in G from to • So, we can use DFS to find connected components • Edges into then-undiscovered vertices define a tree – the "depth first spanning tree" of G Unlike the BFS tree: • The DF spanning tree isn't minimum depth • Its levels don't reflect min distance from the root • Non-tree edges never join vertices on the same or adjacent levels

  40. Non-Tree Edges in DFS Lemma: For every edge , if is not in DFS tree, then one of or is an ancestor of the other in the tree. Proof: Suppose that is visited first. Therefore DFS() was called before DFS() Since is not in DFS tree, was visited when the edge was examined during DFS() Therefore was visited during the call to DFS() so is a descendant of .

  41. DAGs and Topological Ordering

  42. Precedence Constraints In a directed graph, an edge means task must occur before task . Applications • Course prerequisite: course must be taken before • Compilation: must compile module before • Computing overflow: output of job is part of input to job • Manufacturing or assembly: sand it before paint it

  43. Directed Acyclic Graphs (DAG) Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as so that for every edge we have . 2 3 6 5 4 1 2 3 4 5 6 7 7 1 a topological ordering of that DAG–all edges left-to-right a DAG

  44. DAGs: A Sufficient Condition Lemma: If has a topological order, then is a DAG. Proof. (by contradiction) Suppose that has a topological order and that also has a directed cycle . Let be the lowest-indexed node in , and let be the node just before ; thus is an (directed) edge. By our choice of , we have . On the other hand, since is an edge and is a topological order, we must have , a contradiction the directed cycle C 1 j n i the supposed topological order: 1,2,…,n

  45. DAGs: A Sufficient Condition G is a DAG G has a topological order ?

  46. Every DAG has a source node Lemma: If is a DAG, then has a node with no incoming edges (i.e., a source). Proof. (by contradiction) Suppose that is a DAG and it has no source Pick any node , and begin following edges backward from . Since has at least one incoming edge we can walk backward to . Then, since has at least one incoming edge , we can walk backward to . Repeat until we visit a node, say w, twice. Let C be the sequence of nodes encountered between successive visits to w. C is a cycle. The proof is similar to “tree has edges”. w x u v w x u v C

  47. DAG => Topological Order Lemma: If is a DAG, then has a topological order Proof. (by induction on n) Base case: true if . Hypothesis: Every DAG with vertices has a topological ordering. Inductive Step: Given DAG with nodes, find a source node . is a DAG, since deleting cannot create cycles. By hypothesis, has a topological ordering. Place first in topological ordering; then append nodes of • in topological order. This is valid since has no incoming edges. Reminder: Always remove vertices/edges to use IH

  48. A Characterization of DAGs G is a DAG G has a topological order

  49. Topological Order Algorithm 1: Example 2 3 6 5 4 7 1

  50. Topological Order Algorithm 1: Example 2 3 6 5 4 1 2 3 4 5 6 7 7 1 Topological order: 1, 2, 3, 4, 5, 6, 7

More Related