Discrete Mathematics / Further counting, graphs and algorithms
Graph Search and Shortest Paths
Breadth-first search, depth-first search and Dijkstra’s algorithm: how a map application finds your route.
Contents 2 sections
By the end you can
- Trace BFS and DFS on a graph
- Run Dijkstra’s algorithm on a small weighted graph
- Explain why BFS finds fewest-edge paths
Helps to know: Graph Theory
§1Two ways to explore
Breadth-first search explores in rings: all vertices one edge away, then two, and so on, using a queue. The first time it reaches a vertex is by a path with the fewest edges. Depth-first search follows one path as far as it can before backing up, using a stack; it is the tool for detecting cycles and ordering dependencies.
Check yourself
- Q1Which search finds the path with the fewest edges in an unweighted graph?