Depth First Search in Graphs
This lesson will teach you how to write a recursive code for depth-first search in graphs.
We'll cover the following...
What is Depth First Search?
Depth First Search is a method used to traverse and search all nodes in a graph. The algorithm allows us to determine if two nodes, node a and node b, have a path between them. This process starts from the root node and then traverses all through that branch until it reaches the leaf, the last node with no other children, and then backtracks. This continues until all nodes have been traversed. The illustration below explains the process of DFS
in a directed graph.
Implementing the Code
The code below shows how to implement this process using recursion. First, let’s examine the code, and then we will move on to its explanation.
You must modify the edges by using addEdge
, and the number of vertices nVertices
to create your own graph, g
...