Lecture 12.2
Lecture 12.2
depthFirstSearch(v)
{
Label vertex v as reached.
for (each unreached vertex u
adjacenct from v)
depthFirstSearch(u);
}
Depth-First Search Example
4
5
9
1
stack
Label vertex 1 and do a depth first search
from either 2 or 4. 6
7
Suppose that vertex 2 is selected.
Depth-First Search Example
2
3
8
1
4
5
9
stack
6
7
4
5
5
12
9
stack
6
7
4
5
59
12
9
stack
6
7
4
5
9
8
9
5
2
1
stack
6
7
4
5
9
9
5
2
1
stack
6
7
4
5
9
6
9
5
2
1
stack
6
7
4
5
9
4
6
9
5
2
1
stack
6
77
4
5
967
125
9
stack
6
7
6
9
5
2
1
4
5 stack
9
6
7
59
12
4
5 stack
9
6
7
4
5
9
5
2
1
stack
6
7
Return to 5.
Depth-First Search Example
2
3
8
1
4
5
9
5
2
1
stack
6
7
Do a dfs(3).
Depth-First Search Example
2
3
8
1
4
5
5
12
9
stack
6
7
2
3
8
1
4
5
9
2
1
stack
6
7
Return to 2.
Depth-First Search Example
2
3
8
1
4
5
9
1
stack
6
7
Return to 1
Depth-First Search Example
2
3
8
1
4
5
9
stack
6
7
OUTPUT:
2
3
8
1
4
5
9
6
7
Depth-First Search
Explore “deeper” in the graph whenever possible - (Follows LIFO mechanism)
Edges are explored/visited out of the most recently discovered vertex v that still has
unexplored/unvisited edges.
When all of v’s edges have been explored, backtrack to the vertex from which v was
discovered.
computes 2 timestamps: (discovered) and (finished)
builds one or more depth-first tree(s) (depth-first forest)
u v w
Undiscovered
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
discovered
starting
Discover time/ Finish time
the
traversal
with
node u
DFS Example: Classification of Edges
u v w
Undiscovered 1/
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
discovered
u v w
Undiscovered 1/ 2/
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
u v w
Undiscovered 1/ 2/
Discovered,
On Process
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
u v w
Undiscovered 1/ 2/
Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
u v w
Undiscovered 1/ 2/
Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
u v w
Undiscovered 1/ 2/
Discovered,
On Process
4/
4/5 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
u v w
Undiscovered 1/ 2/
Discovered,
On Process
4/5
4/ 3/
3/6
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
u v w
Undiscovered 1/ 2/7
2/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
u v w
Undiscovered 1/8
1/ 2/7
2/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/12
9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
Applications of Depth First Search
1. https://en.wikipedia.org/wiki/Data_structure
2. https://visualgo.net/en/dfsbfs?slide=1