Discord

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 SS and look for EE. We want to know whether EE is reachable and, if every edge counts as one move, how few moves are needed. Routes through A,CA,C and through B,DB,D both reach EE. 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 EE.

Follow breadth-first search from S through a small unweighted graph. The queue visits vertices in distance layers and discovers E in three edges.

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 O(∣V∣+∣E∣)O(|V|+|E|) 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 SS–AA, SS–BB, AA–CC, BB–DD, CC–EE, and DD–EE. What is the minimum number of edges from SS to EE?

  1. Explore layers. From SS at distance 00, discover A,BA,B at 11, then C,DC,D at 22.
  2. Reach the target. From CC or DD, discover EE at distance 33.
  3. Give a route. SS–AA–CC–EE uses three edges. No two-edge route exists because EE does not appear in the first two BFS layers.

If CC is processed before DD, it becomes EE's parent; the alternative route through DD is equally short.

Application 2: explore dependencies

Directed edges P→QP\to Q, P→RP\to R, Q→SQ\to S, and R→TR\to T mean a task points to tasks depending on it. Starting at PP and choosing QQ before RR, what order can DFS visit?

  1. Follow one branch. Visit P,Q,SP,Q,S.
  2. Backtrack. SS has no outgoing edge; return to PP and visit R,TR,T.
  3. Read the visit order. One DFS visit order is P,Q,S,R,TP,Q,S,R,T. 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

SourceTitleWhy read it
MIT OpenCourseWareSearching II: Breadth-First Search and Depth-First SearchStudy 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