Introduction to Depth First Search

Depth-first search is an algorithm for traversing graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node ) and explores as far as possible along each branch before backtracking.

Steps to understand DFS:
1. Initially stack and visited arrays are empty.
2. For example, Visit 0 and put its adjacent nodes which are not visited yet into the stack.
3.Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put all of its adjacent nodes which are not visited in the stack. 4. Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put all of its adjacent nodes which are not visited (i.e, 3, 4) in the stack. 5. Repeats the steps until stack becomes empty, which means we have visited all the nodes and our DFS traversal ends.