Q 1 Write and Explain Depth Fi
Q 1 Write and Explain Depth Fi
Output: 1 2 0 3 4
Explanation: The source vertex s is 1. We visit it first, then we visit
an adjacent. There are two adjacent 1, 0 and 2. We can pick any
of the two (
Start at 1: Mark as visited. Output: 1
Move to 2: Mark as visited. Output: 2
Move to 0: Mark as visited. Output: 0 (backtrack to 2)
Move to 3: Mark as visited. Output: 3 (backtrack to 2)
Move to 4: Mark as visited. Output: 4 (backtrack to 1)
Output: 0 2 4 3 1
Explanation: DFS Steps:
Start at 0: Mark as visited. Output: 0
Move to 2: Mark as visited. Output: 2
Move to 4: Mark as visited. Output: 4 (backtrack to 2, then
backtrack to 0)
Move to 3: Mark as visited. Output: 3 (backtrack to 0)
Move to 1: Mark as visited. Output: 1
Note that there can be multiple DFS traversals of a graph
according to the order in which we pick adjacent vertices. Here
we pick vertices as per the insertion order.