Breadth-first and depth-first search
Both methods visit every reachable vertex, but in a different order.
Rules
- BFS: queue, level by level
- DFS: stack or recursion, depth first
- Mark visited vertices
Your turn
Three problems on this topic. Separate lists with commas.
- 1 · BFS & DFS
- 2
- 3
Breadth-first search from A on the undirected graph. Neighbours in alphabetical order. In which order are the vertices visited?
| Edge | Weight |
|---|---|
| A – B | 6 |
| B – C | 1 |
| B – D | 4 |
| B – E | 2 |
| A – E | 3 |
| D – E | 4 |
Reihenfolge (comma-separated)
Worked example
Depth-first search from A on the undirected graph. Neighbours in alphabetical order. In which order are the vertices visited?
| Edge | Weight |
|---|---|
| A – B | 7 |
| B – C | 8 |
| B – D | 6 |
| C – E | 3 |
| A – F | 3 |
| F – G | 6 |
| D – F | 5 |
| B – F | 7 |
Worked solution
- 1A → B → C → E → D → F → G
Typical mistake: Breadth-first uses a queue, depth-first goes as deep as possible first.
Common questions
Which one finds shortest paths?
In unweighted graphs, breadth-first search.
