Depth-First Search (DFS)

Explores as deep as possible before backtracking using stack/recursion. Basis for topological sort, maze solving, cycle detection. Stack LIFO; recurse to neighbours.

Method

• Push start. • Pop, explore one unvisited neighbour deeply. • Backtrack when dead end. • Repeat.

Limitations

Can get lost in deep/infinite graphs without depth limit. DFS finds reachability and paths, not generally shortest paths. Restart from unvisited vertices to cover disconnected components.

Example

Maze solving: DFS follows one corridor to its end before trying another.

Editorial clarification

DFS finds reachability and paths, not generally shortest paths. Restart from unvisited vertices to cover disconnected components.

Sources

Educational reference only, not professional advice.

All algorithms