INAIO Guide Stage 1 12Graph Theory and Algorithms
12.3 Graph traversal and search
Search from a starting vertex with BFS or DFS to find reachable vertices and routes.
After representing a network as a graph, we face a practical problem: starting at one vertex, which other vertices can we reach, and how can we find a route to a target? Branches and cycles can make trying every possible route by hand slow or repetitive. We need a rule for choosing which vertex to examine next and a record of what we have already seen. This systematic exploration is a graph traversal; when we use it to find a target or answer a route question, it is also a search.
There are two basic ways to organize the exploration: breadth-first search (BFS) and depth-first search (DFS). One works outward from the start in distance layers; the other follows a branch before returning to try another. Both can find reachable vertices. Their order differs, and that difference decides which one to use for a shortest route in an unweighted graph.
This is intermediate algorithms. Review graph representations and paths. MIT OpenCourseWare's BFS and DFS lecture gives the algorithmic version.
A basic idea: reach a target without guessing routes
In the undirected graph below, start at and look for . We want to know whether is reachable and, if every edge counts as one move, how few moves are needed. Routes through and through both reach . A search should discover these possibilities without repeatedly wandering around a cycle or forgetting a branch. The animation follows the breadth-first choice so we can see how it finds a three-edge route.
Use Next to follow the queue, newly found vertices, and the first shortest route to .
A quick tour of BFS and DFS
Breadth-first search (BFS) explores in layers: first the start, then vertices one edge away, then two edges away, and so on. Put the start in a first-in, first-out queue and mark it visited. Repeatedly remove the front vertex; for each unvisited neighbour, mark it, record its parent and distance, and add it to the back. Marking on insertion prevents the same neighbour from entering twice. When BFS first discovers a vertex in an unweighted graph, it has found a route with the fewest edges. Follow parent pointers backwards to recover that route.
Depth-first search (DFS) follows one branch as far as it can, then backtracks to try an unexplored branch. Use a last-in, first-out stack or recursion, and mark each visited vertex so a cycle does not make the search repeat forever. DFS is useful for exploring connected components, finding cycles, and ordering dependencies. Its first route to a target need not be shortest.
| Method | Next vertex comes from | Typical reason to use it |
|---|---|---|
| BFS | Front of a queue | Distance in number of edges; shortest unweighted route |
| DFS | Top of a stack or recursive call | Deep exploration; components and cycles |
Both methods should follow edge direction in a directed graph. With an adjacency list and a visited set, each vertex and edge is examined only a bounded number of times, giving work. BFS's shortest-route claim does not apply to unequal edge weights; a longer route in edges may be cheaper in distance or cost.
Application 1: shortest route in an unweighted network
Let the undirected edges be –, –, –, –, –, and –. What is the minimum number of edges from to ?
- Explore layers. From at distance , discover at , then at .
- Reach the target. From or , discover at distance .
- Give a route. ––– uses three edges. No two-edge route exists because does not appear in the first two BFS layers.
If is processed before , it becomes 's parent; the alternative route through is equally short.
Application 2: explore dependencies
Directed edges , , , and mean a task points to tasks depending on it. Starting at and choosing before , what order can DFS visit?
- Follow one branch. Visit .
- Backtrack. has no outgoing edge; return to and visit .
- Read the visit order. One DFS visit order is . A different neighbour order could change it, but the same reachable tasks would be found.
DFS gives a systematic exploration. A valid task schedule requires an additional dependency-ordering step.
Resources
| Source | Title | Why read it |
|---|---|---|
| MIT OpenCourseWare | Searching II: Breadth-First Search and Depth-First Search | Study the queue, stack, and visited-set search methods. |
Finished this module? Mark it done.
Comments
Stuck on something, spotted a mistake, or found a resource that helped you? Tell us below.
Loading comments