Page 76 - Data Structures Interactive Book
P. 76
This program demonstrates BFS starting from vertex 0. The traversal visits vertices in
increasing levels of distance from the source, ensuring that closer vertices are processed first.
8.3.2 Depth-First Search (DFS)
Depth-First Search explores as far as possible along each branch before backtracking.
Starting from a source vertex, DFS moves to one of its neighbors, then to a neighbor of that
neighbor, and so on, until it reaches a vertex with no unvisited neighbors. At that point, it
backtracks and continues exploring other paths. DFS can be implemented using recursion or
an explicit stack.
DFS is useful for detecting cycles, checking connectivity, and performing tasks such as
topological sorting in directed acyclic graphs. It is also used in solving mazes and puzzles
where exploring deep paths is necessary.
76

