Follow these steps for the depth-first traversal of a graph:
- Push the first vertex of the graph into the stack. You can choose any vertex of the graph as the starting vertex.
- Then, repeat the following steps 3 to 7 until the stack is empty.
- Pop the vertex from the stack and call it by any name, say, v.
- Mark the popped vertex as visited. This marking is done so that this vertex should not be traversed again.
- Display the marked vertex.
- Find out the adjacency vertices of the v vertex, and then perform step 7 on each of them.
- If any of the adjacency vertices of v are not marked, mark them as visited and push them on to the stack.
- Exit.
The program for the depth-first traversal of a graph is as follows:
//depthfirsttrav.c#include ...