Introduction
Navigation apps, network routers and game AI all rest on a handful of graph algorithms. This planner implements four of them over a map of towns: the shortest route by distance (Dijkstra), the route with fewest stops (BFS), which places are reachable from each other (components) and the cheapest set of roads that keeps everything connected (minimum spanning tree with union-find).
Problem statement
Objectives
- Represent a graph with nested std::maps (adjacency map)
- Implement Dijkstra with a min-priority queue and stale-entry skipping
- Reconstruct paths from predecessor maps
- Implement union-find with path compression for Kruskal's algorithm
Features
- Shortest distance route with legs and total
- Fewest-stops route with its real length
- Distances to every reachable place
- Connected groups and minimum spanning forest
- Closing roads and places, with routes updating immediately
Concepts demonstrated
- Graphs
- Dijkstra's algorithm
- Breadth-first search
- Depth-first search
- Union-find
- Kruskal's algorithm
System architecture
Graph stores map<place, map<neighbour, km>>, so neighbours are iterated alphabetically and results are deterministic. shortest pushes (distance, place) pairs onto a priority_queue with std::greater<>; an entry whose distance is larger than the best known is stale and skipped, and the target's first pop is optimal. fewest_hops is a BFS with a prev map. components runs an iterative DFS from every unvisited place. minimum_spanning_tree sorts all edges and adds each one whose endpoints are in different union-find sets.
Folder structure
Complete source code
Makefile
What each file does
- src/graph.h
- The
Graphinterface and thePathresult type. - src/graph.cpp
- Road editing, Dijkstra (single target and all targets), BFS, components and Kruskal with union-find.
- src/main.cpp
- The command interpreter and result formatting.
- Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
road Bengaluru Mysuru 145 road Bengaluru Tumakuru 70 road Bengaluru Hosur 40 road Hosur Krishnagiri 50 road Krishnagiri Vellore 100 road Bengaluru Vellore 210 road Vellore Chennai 140 road Krishnagiri Chennai 260 road Mysuru Ooty 125 road Tumakuru Hassan 115 road Mysuru Hassan 120 road Goa Panaji 30 road Chennai Chennai 5 roads route Bengaluru Chennai hops Bengaluru Chennai route Tumakuru Ooty from Bengaluru groups mst close Krishnagiri route Bengaluru Chennai close-road Bengaluru Vellore route Bengaluru Chennai route Bengaluru Goa groups quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - Bengaluru → Chennai: shortest is via Hosur, Krishnagiri and Vellore (330 km) but the fewest-stops route is via Vellore only (350 km, 2 legs).
- The MST uses 9 roads totalling 790 km across two groups (Goa–Panaji is separate).
- After closing Krishnagiri and the Bengaluru–Vellore road, Chennai becomes unreachable.
Common errors
Dijkstra gives wrong answers
It requires non-negative weights; the program rejects km ≤ 0. Also don't stop at the first time you *push* the target — stop when you *pop* it.
Slow on big graphs
Without the stale-entry check (if (d > dist[u]) continue;) the same node is expanded many times.
Possible improvements
- Load the map from a CSV file
- Add one-way roads (a directed graph)
- Use A* with straight-line distance as the heuristic
Additional challenges
- 1Find the second-shortest route.
- 2Compute all-pairs shortest paths with Floyd–Warshall and compare.
- 3Find bridges: roads whose closure disconnects the network.
Built and tested it yourself? Mark it to update your dashboard.