Maintain a visited [] to keep track of already visited vertices to avoid loops.

Iterate through all the vertices and for each vertex, make a recursive call to all the vertices which can be visited from the source and in recursive call, all these vertices will act a source.

See the code for more understanding.

Time Complexity: O(V+E) V – no of vertices E – no of edges

Code:

Output:

Depth-First Search:

0 4 5 6 1 3 2

__________________________________________________ Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________