Depth First Search in Graphs

This lesson will teach you how to write a recursive code for depth-first search in graphs.

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.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.