330 likes | 346 Views
Use adjacency lists to implement the DFS algorithm on an undirected graph. Follow the steps of discovery and marking vertices.
E N D
Undirected Depth First Search A H I B C G Adjacency Lists A: F C B GB: AC: AD: F EE: G F DF: A E D:G: E A:H: I:I: H: D E F
Undirected Depth First Search A H I B C G D E F newly discovered F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I A alreadymarked B C G D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search E E newly discovered A H I B C G D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search G G newly discovered A H I B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I visit(G) (G, E) (G, A) E alreadymarked B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I visit(G) (G, E) (G, A) A alreadymarked B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I Finished G visit(G) (G, E) (G, A) B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I F alreadymarked B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search D newly discovered D A H I B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I visit(D) (D, F) (D, E) F alreadymarked B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I visit(D) (D, F) (D, E) E alreadymarked B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I Finished D visit(D) (D, F) (D, E) B C G visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G Finished E visit(E) (E, G) (E, F) (E, D) D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I D alreadymarked B C G D E visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E Finished F visit(F) (F, A) (F, E) (F, D) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search C newly discovered C A H I B C G D E F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I A alreadymarked B C G D E visit(C) (C, A) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E Finished C visit(C) (C, A) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search B newly discovered B A H I B C G D E F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I A alreadymarked B C G D E visit(B) (B, A) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E Finished B visit(B) (B, A) F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I G alreadyfinished B C G D E F visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E F Finished A visit(A) (A, F) (A, C) (A, B) (A, G) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E F Undiscovered Marked Active Finished Stack
Undirected Depth First Search I I newly discovered A H I B C G D E F visit(H) (H, I) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I H alreadymarked B C G D E visit(I) (I, H) F visit(H) (H, I) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E Finished I visit(I) (I, H) F visit(H) (H, I) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E F FinishedH visit(H) (H, I) Undiscovered Marked Active Finished Stack
Undirected Depth First Search A H I B C G D E F Undiscovered Marked Active Finished Stack
BFS layers and tree Layer 0 A H Layer 3 B C G D E Layer 1 Layer 2 F Layer i: All nodes at distance i from A
DFS tree A H B C G D E F
Recap BFS tree: A A H H B B C C G G DFS tree: D D E E F F