Introduction
Journey planners must weigh more than distance: changing lines takes time. Searching over (station, line) pairs instead of stations lets Dijkstra's algorithm charge a penalty exactly when a journey switches lines, and the resulting path can be presented as legs on each line.
Problem statement
Objectives
- Search over an expanded state space (station, line)
- Implement Dijkstra with a priority queue and stale-entry skipping
- Implement BFS for unweighted shortest paths
- Reconstruct and group paths into journey legs
Features
- Lines defined at run time with validation
- Fastest route with interchange penalties
- Fewest-stops route
- Legs, changes, total time and fares
Concepts demonstrated
- Graphs
- Dijkstra's algorithm
- Breadth-first search
- State-space search
- Path reconstruction
System architecture
MetroNetwork stores an adjacency list of Edge(to, line, minutes). fastest runs Dijkstra over State(station, line): moving along an edge costs its minutes, plus the penalty if the edge's line differs from the state's line. fewestStops is a BFS that prefers staying on the current line. build walks the predecessor map back from the destination, sums minutes and penalties, and groups consecutive steps on the same line into Legs.
Folder structure
Complete source code
src/Main.java
What each file does
- src/MetroNetwork.java
- Lines and edges, Dijkstra over (station, line) states, BFS, interchanges and route building.
- src/Main.java
- The command loop, fares and route printing.
How to run it
Compile
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.
line Purple Whitefield 4 Indiranagar 3 Trinity 2 MGRoad 2 Cubbon 3 Majestic 4 Vijayanagar 5 Kengeri line Green Nagasandra 6 Yeshwanthpur 4 Malleshwaram 3 Majestic 2 Chickpet 3 KRMarket 4 Jayanagar 5 Silk line Yellow MGRoad 3 Richmond 4 Lalbagh 3 Jayanagar 6 BTM 5 Bommasandra line Blue Trinity 2 Ulsoor 3 Yeshwanthpur line Pink Whitefield 4 Indiranagar line Broken Alpha 0 Beta lines interchanges fastest Indiranagar Jayanagar fewest Indiranagar Jayanagar fastest Whitefield Kengeri fastest Nagasandra BTM fewest Silk Whitefield fare Majestic Cubbon fare Kengeri Bommasandra fastest Majestic Majestic fastest Majestic Airport quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Indiranagar → Jayanagar takes 20 minutes via MG Road (5 + 10 + 5 penalty), beating 24 minutes via Majestic.
- Silk → Whitefield needs two changes.
- A line with a 0-minute segment, identical start and end, and the unknown station 'Airport' are rejected.
Common errors
Routes ignore the cost of changing lines
Include the current line in the search state so the penalty can be charged on the edge that switches lines.
Dijkstra returns non-optimal routes
Stop when the destination is polled from the queue, not when it is first discovered, and skip stale queue entries.
Possible improvements
- Load the network from a file
- Add walking transfers between nearby stations
- Show departure times using a timetable
Additional challenges
- 1Find the route with the fewest changes, then the fastest among those.
- 2Close a station and reroute.
- 3Compute the station that is most central (smallest average travel time).
Built and tested it yourself? Mark it to update your dashboard.