Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28
Implementing DFS using a Stack DFS ( s ) head ← 1 , stack [1] ← s 1 mark all vertices as “unexplored” 2 while head ≥ 1 3 v ← stack [ head ] , head ← head − 1 4 if v is unexplored then 5 mark v as “explored” 6 for all neighbours u of v 7 if u is not explored then 8 head ← head + 1 , stack [ head ] = u 9 Running time: O ( n + m ) . 15/28
Example of DFS using Stack head 1 7 2 3 1 4 5 8 6 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 4 5 8 6 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 4 5 8 6 1 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 3 2 4 5 8 6 1 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 4 5 8 6 1 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 4 5 8 6 1 2 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 5 4 3 4 5 8 6 1 2 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 5 4 4 5 8 6 1 2 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 5 4 4 5 8 6 1 2 3 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 3 5 4 8 7 5 4 5 8 6 1 2 3 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 v 4 5 8 6 1 2 3 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 v 4 5 8 6 1 2 3 5 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 6 4 v 4 5 8 6 1 2 3 5 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 v 3 5 4 8 7 6 4 5 8 6 1 2 3 5 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 v 3 5 4 8 7 6 4 5 8 6 1 2 3 5 4 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 4 5 8 v 6 1 2 3 5 4 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 4 5 8 v 6 1 2 3 5 4 6 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 3 5 4 8 4 5 8 6 1 2 3 5 4 6 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 3 5 4 8 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28
Example of DFS using Stack v head 1 7 2 3 3 5 4 8 8 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 v 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 8 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 5 4 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 v 3 5 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28
Example of DFS using Stack head 1 7 2 3 3 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28
Example of DFS using Stack head 1 7 v 2 3 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28
Implementing DFS using Recurrsion DFS( s ) mark all vertices as “unexplored” 1 recursive-DFS( s ) 2 recursive-DFS ( v ) if v is explored then return 1 mark v as “explored” 2 for all neighbours u of v 3 recursive-DFS( u ) 4 17/28
Outline Graphs 1 Connectivity and Graph Traversal 2 Testing Bipartiteness Topological Ordering 3 18/28
Testing Bipartiteness: Applications of BFS Def. A graph G = ( V, E ) is a bipartite graph if there is a partition of V into two sets L and R such that for every edge ( u, v ) ∈ E , we have either u ∈ L, v ∈ R or v ∈ L, u ∈ R . 19/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · Report “not a bipartite graph” if contradiction was found 20/28
Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · Report “not a bipartite graph” if contradiction was found If G contains multiple connected components, repeat above algorithm for each component 20/28
Test Bipartiteness 21/28
Test Bipartiteness 21/28
Test Bipartiteness 21/28
Test Bipartiteness 21/28
Test Bipartiteness 21/28
Test Bipartiteness 21/28
Recommend
More recommend