Data Structure (2130702)

BE | Semester-3   Summer-2017 | 05/31/2017

Q5) (b)

Explain Depth First Search operation.

  • Most graph problems involve traversal of a graph. Traversal of a graph means visit each node exactly once.
  • Two commonly used graphs traversal techniques are
    1. Depth First Search (DFS)
    2. Breadth First Search (BFS)

Depth First Search (DFS)

  • It is like preorder traversal of tree.
  • Traversal can start from any vertex vi
  • Vi is visited and then all vertices adjacent to vi are traversed recursively using DFS
Q5-B-DFS
DFS(G,1) is given by
a) Visit(1)
b) DFS(G,2)
 DFS(G,3)
 DFS(G,4)
 DFS(G,5)
DFS traversal of given graph is: 1, 2, 6, 3, 8, 7, 4, 5
 
Since graph can have cycles, we must avoid re-visiting a node. To do this when we visit a vertex V, we marks it visited as visited should not be selected for traversal.