Capstone: Meridian's Router
In short: no single shortest-path algorithm is right for every network — each one trades correctness for speed differently. A router should classify the network it's handed (no cycles? any negative costs? all costs equal?) and run the cheapest algorithm that is still correct, checking from cheapest to most expensive so a fast, safe answer is never skipped in favour of a slow, safe one. Kahn's algorithm answers "any cycles?" for free, as a side effect of trying to find a topological order.

You now have five ways to find a cheapest route, and each is the right answer to a different question:
| The network | The algorithm | Cost | Lesson |
|---|---|---|---|
| no cycles (time only moves forward) | topological sweep | O(V + E) | 1 |
| cycles and negative costs | Bellman-Ford, with a loop check | O(V × E) | 2 |
| every pair, small and dense | Floyd–Warshall | O(V³) | 3 |
| one query, known goal, a map | A* | far fewer expansions | 4 |
| one long query, known goal | search from both ends | about 2·b^(d/2) | 5 |
and the two you started with: BFS when every road costs the same, and Dijkstra when costs are non-negative. That's the whole shortest-path toolkit, and the skill that remains is choosing. Pick too weak a tool and you get a wrong answer (Dijkstra on rebates). Pick too strong a tool and you're slow (Bellman-Ford on a street map).
This capstone builds the piece that chooses: route_query(roads, start, goal), the
single entry point to Meridian's router. It looks at the network it has been handed,
picks the cheapest algorithm that is still correct for it, runs it, and reports
which one it used.
The decision, in order
The checks run from cheapest to most expensive algorithm:
- No cycles at all? Use the topological sweep. It's the fastest correct algorithm there is, and credits don't bother it, so this check comes first even if some costs are negative.
- Any negative cost? Then (with cycles present) only Bellman-Ford is correct, and it must check for money-making loops.
- Every cost the same? BFS order is cheapest order, and the cost is hops × that weight.
- Otherwise costs are non-negative and mixed: Dijkstra.
Kahn's algorithm answers the first question for free: if it emits every stop, there is no cycle. Watch the router classify the night timetable, then the same network with one return road added:
If Kahn's algorithm emits every stop there is no cycle and the sweep is safe; one return road leaves every stop waiting, and the router must fall back.
Each badge counts the roads into a stop. Only the depot waits on nothing. 4 nodes, 4 edges, directed. Frontier: depot. depot: 0, mill: 1, quay: 1, harbour: 2.
The set of costs answers the other two:
The order of the checks is a policy, and it matters:
roads = [["depot", "mill", -1], ["mill", "quay", -1], ["quay", "harbour", -1]]
out = {"depot": ["mill"], "mill": ["quay"], "quay": ["harbour"], "harbour": []}
waiting = {"depot": 0, "mill": 1, "quay": 1, "harbour": 1}
ready, emitted = ["depot"], 0
while ready:
n = ready.pop()
emitted += 1
for nxt in out[n]:
waiting[nxt] -= 1
if waiting[nxt] == 0:
ready.append(nxt)
costs = {c for _, _, c in roads}
if emitted == len(out):
print("dag")
elif min(costs) < 0:
print("bellman-ford")
else:
print("bfs" if len(costs) == 1 else "dijkstra")Reversing the first two checks would still give correct answers, only slower. Other reorderings are not safe: checking "all costs equal" before "any negative" would send a network where every road costs −1 to BFS, which would happily report a cost when the true answer is "no cheapest route exists".
roads = [["depot", "mill", -1], ["mill", "depot", -1], ["mill", "harbour", -1]]
costs = {c for _, _, c in roads}
if len(costs) == 1:
print("bfs")
elif min(costs) < 0:
print("bellman-ford")Step through the set logic that the last two checks rest on: