Graphs
Depth-first search vs Dijkstra's algorithm
Depth-first search and Dijkstra's algorithm both explore a graph, in different ways. Depth-first search: go as deep as possible first, with recursion or a stack. Dijkstra's algorithm: greedy: settle the closest unfinished node first (a simple list as the queue).
At a glance
| Property | Depth-first search | Dijkstra's algorithm |
|---|---|---|
| Technique | Go as deep as possible first, with recursion or a stack | Greedy: settle the closest unfinished node first (a simple list as the queue) |
| Best case time | O(V + E) | O(V²) |
| Average case time | O(V + E) | O(V²) |
| Worst case time | O(V + E) | O(V²) |
| Extra space | O(V) | O(V) |
| Frontier (what holds the nodes to visit) | Call stack: last in, first out | Priority queue: here a linear scan for the smallest distance |
| Reads edge weights | No | Yes |
| Path guarantee | None: it finds a path, not a short one | Lowest total weight from the start |
When to choose each
Depth-first search: when to choose it
Choose depth-first search when you need to know whether something is reachable, how the graph is connected, or whether it contains a cycle — questions about shape, not cost. DFS reads no weights at all, keeps no distances, and in this implementation makes zero writes; its whole state is the recursion stack. If the weights on your edges are irrelevant to the question, carrying them through a priority queue is pure overhead, and DFS is the smaller, more obviously correct program.
Dijkstra's algorithm: when to choose it
Choose Dijkstra's algorithm the moment cost matters. It is not a tuned depth-first search: it is a different discipline, settling nodes in order of distance from the start so that a node is only finished once no cheaper route to it can exist. DFS offers no such property, and the first path it finds to a node can be arbitrarily bad. Dijkstra needs non-negative weights to be correct — a negative edge could make an already-settled node cheaper, and this algorithm never revisits a settled node — and it needs a priority queue, which here is a linear scan and so gives O(V²) rather than O(V + E).
The same inputs, counted
These numbers come from running both real implementations: edge checks: one for every neighbour the algorithm looks at, on the three graphs from the lessons. Writes are not counted here: depth-first search and Dijkstra's algorithm record very different amounts of state per node, so a combined total would compare two different things.
| Input | Depth-first search | Dijkstra's algorithm |
|---|---|---|
| Road map | 14 | 14 |
| Ring with shortcuts | 16 | 16 |
| Disconnected | 6 | 12 |
What the numbers show
On the two connected graphs the edge traffic is identical: 14 checks for DFS and 14 for Dijkstra on the road map, 16 and 16 on the ring. Both algorithms consider every edge from both endpoints. The entire difference in cost is what they do at each one — DFS asks "have I visited this node" and records nothing, Dijkstra compares a candidate distance against the stored one and writes a new distance when it is better: 8 writes on the road map and 7 on the ring, against 0 for DFS throughout.
The gap in cost opens on the disconnected graph: 6 edge checks for DFS, 12 for Dijkstra. DFS, started at A, only ever enters A's component and stops; this Dijkstra starts with all six nodes in its priority queue, so it goes on to dequeue D, E and F at distance ∞ and relax their edges before the queue empties. Paying twice the edge traffic to confirm what DFS established by not looking is a real tradeoff, and on a graph of many small components it would be the deciding one.
The answers are not comparable, which is the point: on the road map DFS returns the visiting order A, B, C, E, F, D, and Dijkstra returns the distances 0, 5, 2, 6, 7, 9. DFS's order reaches D last after descending five edges, even though D sits two edges and 8 units of weight from the start. If you read a DFS traversal as a route, you get a path; you do not get a cheap one, and nothing in the algorithm was ever trying to give you one.