Shortest Paths
Dijkstra with lazy deletion for non-negative weights, 0-1 BFS for weights 0 and 1, Bellman-Ford for negative edges and Floyd-Warshall for all pairs on small graphs.
The problem
Each edge now has a weight (a length, a cost, a time). The length of a path is the sum of its edge weights. The shortest path problem asks for the smallest total from a source node to every other node.
Plain BFS counts edges, so it fails here: a path with 2 heavy edges can be longer than a path with 5 light ones.
Which tool to use depends on the weights:
| Situation | Tool | Time |
|---|---|---|
| All weights equal | BFS | |
| Weights only 0 or 1 | 0-1 BFS | |
| Weights | Dijkstra | |
| Negative weights possible | Bellman-Ford | |
| All pairs, up to a few hundred | Floyd-Warshall |
1 of 9