Skip to content
C++ project #20Advanced

Route Planner (Graph Algorithms)

Model a road network as a weighted graph and answer real questions with Dijkstra, BFS, connected components and Kruskal's minimum spanning tree.

Estimated time
~8 hours to build
Files
7 files · 499 lines
Technologies
C++17, <queue>, <map>, <set>, <optional>, Header/source split

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

Build a planner that lets users add roads between places with distances, close roads or whole places, and ask for the shortest route, the route with the fewest legs, the distance to every reachable place, the connected groups of places and the minimum spanning tree. It must reject self-loops and non-positive distances and report when no route exists.

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.

TextArchitecture diagram
Tumakuru --70-- Bengaluru --40-- Hosur --50-- Krishnagiri
| | \ | \
115 145 210 100 260
| | \ | \
Hassan --120-- Mysuru Vellore --------140------ Chennai
|
125
|
Ooty Goa --30-- Panaji

Folder structure

TextProject tree
route-planner/
├── README.md
├── Makefile
├── src/graph.cpp
├── src/graph.h
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/graph.h
The Graph interface and the Path result 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: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
> Road Bengaluru - Mysuru (145 km)
> Road Bengaluru - Tumakuru (70 km)
> Road Bengaluru - Hosur (40 km)
> Road Hosur - Krishnagiri (50 km)
> Road Krishnagiri - Vellore (100 km)
> Road Bengaluru - Vellore (210 km)
> Road Vellore - Chennai (140 km)
> Road Krishnagiri - Chennai (260 km)
> Road Mysuru - Ooty (125 km)
> Road Tumakuru - Hassan (115 km)
> Road Mysuru - Hassan (120 km)
> Road Goa - Panaji (30 km)
> Usage: road <A> <B> <km> (two different places, km > 0)
> Bengaluru Hosur(40) Mysuru(145) Tumakuru(70) Vellore(210)
Chennai Krishnagiri(260) Vellore(140)
Goa Panaji(30)
Hassan Mysuru(120) Tumakuru(115)
Hosur Bengaluru(40) Krishnagiri(50)
Krishnagiri Chennai(260) Hosur(50) Vellore(100)
Mysuru Bengaluru(145) Hassan(120) Ooty(125)
Ooty Mysuru(125)
Panaji Goa(30)
Tumakuru Bengaluru(70) Hassan(115)
Vellore Bengaluru(210) Chennai(140) Krishnagiri(100)
> Shortest: Bengaluru -> Hosur -> Krishnagiri -> Vellore -> Chennai (330 km, 4 legs)
> Fewest stops: Bengaluru -> Vellore -> Chennai (350 km, 2 legs)
> Shortest: Tumakuru -> Bengaluru -> Mysuru -> Ooty (340 km, 3 legs)
> Chennai 330 km
Hassan 185 km
Hosur 40 km
Krishnagiri 90 km
Mysuru 145 km
Ooty 270 km
Tumakuru 70 km
Vellore 190 km
> group 1: Bengaluru Chennai Hassan Hosur Krishnagiri Mysuru Ooty Tumakuru Vellore
group 2: Goa Panaji
> Goa - Panaji 30 km
Bengaluru - Hosur 40 km
Hosur - Krishnagiri 50 km
Bengaluru - Tumakuru 70 km
Krishnagiri - Vellore 100 km
Hassan - Tumakuru 115 km
Hassan - Mysuru 120 km
Mysuru - Ooty 125 km
Chennai - Vellore 140 km
minimum road length to keep everything connected: 790 km
> All roads to Krishnagiri closed
> Shortest: Bengaluru -> Vellore -> Chennai (350 km, 2 legs)
> Closed road Bengaluru - Vellore
> No route from Bengaluru to Chennai
> No route from Bengaluru to Goa
> group 1: Bengaluru Hassan Hosur Mysuru Ooty Tumakuru
group 2: Chennai Vellore
group 3: Goa Panaji
group 4: Krishnagiri
>
Input
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.txt with tests/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

  1. 1Find the second-shortest route.
  2. 2Compute all-pairs shortest paths with Floyd–Warshall and compare.
  3. 3Find bridges: roads whose closure disconnects the network.

Built and tested it yourself? Mark it to update your dashboard.