Depth First Traversal for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array.
For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Depth First Traversal of the following graph is 2, 0, 1, 3.

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20print%20DFS%20traversal%20from%20a%0A%23%20given%20given%20graph%0Afrom%20collections%20import%20defaultdict%0A%20%0A%23%20This%20class%20represents%20a%20directed%20graph%20using%0A%23%20adjacency%20list%20representation%0Aclass%20Graph%3A%0A%20%0A%20%20%20%20%23%20Constructor%0A%20%20%20%20def%20__init__(self)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20default%20dictionary%20to%20store%20graph%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20defaultdict(list)%0A%20%0A%20%20%20%20%23%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20def%20addEdge(self%2Cu%2Cv)%3A%0A%20%20%20%20%20%20%20%20self.graph%5Bu%5D.append(v)%0A%20%0A%20%20%20%20%23%20A%20function%20used%20by%20DFS%0A%20%20%20%20def%20DFSUtil(self%2Cv%2Cvisited)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20current%20node%20as%20visited%20and%20print%20it%0A%20%20%20%20%20%20%20%20visited%5Bv%5D%3D%20True%0A%20%20%20%20%20%20%20%20print%20v%2C%0A%20%0A%20%20%20%20%20%20%20%20%23%20Recur%20for%20all%20the%20vertices%20adjacent%20to%20this%20vertex%0A%20%20%20%20%20%20%20%20for%20i%20in%20self.graph%5Bv%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.DFSUtil(i%2C%20visited)%0A%20%0A%20%0A%20%20%20%20%23%20The%20function%20to%20do%20DFS%20traversal.%20It%20uses%0A%20%20%20%20%23%20recursive%20DFSUtil()%0A%20%20%20%20def%20DFS(self%2Cv)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%20%5BFalse%5D*(len(self.graph))%0A%20%0A%20%20%20%20%20%20%20%20%23%20Call%20the%20recursive%20helper%20function%20to%20print%0A%20%20%20%20%20%20%20%20%23%20DFS%20traversal%0A%20%20%20%20%20%20%20%20self.DFSUtil(v%2Cvisited)%0A%20%0A%20%0A%23%20Driver%20code%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Ag%20%3D%20Graph()%0Ag.addEdge(0%2C%201)%0Ag.addEdge(0%2C%202)%0Ag.addEdge(1%2C%202)%0Ag.addEdge(2%2C%200)%0Ag.addEdge(2%2C%203)%0Ag.addEdge(3%2C%203)%0A%20%0Aprint%20%22Following%20is%20DFS%20from%20(starting%20from%20vertex%202)%22%0Ag.DFS(2)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Neelam%20Yadav” message=”” highlight=”” provider=”manual”/]

Output:

Following is Depth First Traversal (starting from vertex 2)
2 0 1 3

Note that the above code traverses only the vertices reachable from a given source vertex. All the vertices may not be reachable from a given vertex (example Disconnected graph). To do complete DFS traversal of such graphs, we must call DFSUtil() for every vertex. Also, before calling DFSUtil(), we should check if it is already printed by some other call of DFSUtil(). Following implementation does the complete graph traversal even if the nodes are unreachable.

[ad type=”banner”]

The differences from the above code are highlighted in the below code.

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20print%20DFS%20traversal%20for%20complete%20graph%0Afrom%20collections%20import%20defaultdict%0A%20%0A%23%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%0A%23%20list%20representation%0Aclass%20Graph%3A%0A%20%0A%20%20%20%20%23%20Constructor%0A%20%20%20%20def%20__init__(self)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20default%20dictionary%20to%20store%20graph%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20defaultdict(list)%0A%20%0A%20%20%20%20%23%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20def%20addEdge(self%2Cu%2Cv)%3A%0A%20%20%20%20%20%20%20%20self.graph%5Bu%5D.append(v)%0A%20%0A%20%20%20%20%23%20A%20function%20used%20by%20DFS%0A%20%20%20%20def%20DFSUtil(self%2C%20v%2C%20visited)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20current%20node%20as%20visited%20and%20print%20it%0A%20%20%20%20%20%20%20%20visited%5Bv%5D%3D%20True%0A%20%20%20%20%20%20%20%20print%20v%2C%0A%20%0A%20%20%20%20%20%20%20%20%23%20Recur%20for%20all%20the%20vertices%20adjacent%20to%0A%20%20%20%20%20%20%20%20%23%20this%20vertex%0A%20%20%20%20%20%20%20%20for%20i%20in%20self.graph%5Bv%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.DFSUtil(i%2C%20visited)%0A%20%0A%20%0A%20%20%20%20%23%20The%20function%20to%20do%20DFS%20traversal.%20It%20uses%0A%20%20%20%20%23%20recursive%20DFSUtil()%0A%20%20%20%20def%20DFS(self)%3A%0A%20%20%20%20%20%20%20%20V%20%3D%20len(self.graph)%20%20%23total%20vertices%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%5BFalse%5D*(V)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Call%20the%20recursive%20helper%20function%20to%20print%0A%20%20%20%20%20%20%20%20%23%20DFS%20traversal%20starting%20from%20all%20vertices%20one%0A%20%20%20%20%20%20%20%20%23%20by%20one%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(V)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.DFSUtil(i%2C%20visited)%0A%20%0A%20%0A%23%20Driver%20code%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Ag%20%3D%20Graph()%0Ag.addEdge(0%2C%201)%0Ag.addEdge(0%2C%202)%0Ag.addEdge(1%2C%202)%0Ag.addEdge(2%2C%200)%0Ag.addEdge(2%2C%203)%0Ag.addEdge(3%2C%203)%0A%20%0Aprint%20%22Following%20is%20Depth%20First%20Traversal%22%0Ag.DFS()%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Neelam%20Yadav%0A” message=”” highlight=”” provider=”manual”/]

Output:

Following is Depth First Traversal
0 1 2 3

Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.

[ad type=”banner”]