Whatever-First Search
Explore the concept of Whatever-First Search as a flexible graph traversal method that encompasses depth-first search. Understand how it marks all reachable vertices from a starting point and builds a spanning tree through recursive or iterative approaches. This lesson helps you grasp its working, implementation, and time complexity in undirected graphs.
We'll cover the following...
Depth-first search
So far, we have only discussed local operations on graphs; arguably, the most fundamental global question we can ask about graphs is about its reachability. Given a graph and a vertex in , the reachability question asks which vertices are reachable from ; that is, for which vertices is there a path from to ? For now, let’s consider only undirected graphs; we’ll consider directed graphs briefly at the end of this section. For undirected graphs, the vertices reachable from are precisely the vertices in the same component as .
Perhaps the most natural reachability algorithm—at least for people who are used to thinking recursively—is depth-first search (DFS). This algorithm can be written either recursively or iteratively. It’s exactly the same algorithm either way; the only difference is that we can actually see the “recursion” stack in the non-recursive version.
Algorithm
...