DSA Notes
Build a route-finding application using graph data structures, Dijkstra\
Project Overview
Every time you open Google Maps, a graph algorithm finds your route. In this project, we model a city as a weighted graph and implement two pathfinding algorithms: Dijkstra (guaranteed optimal) and A* (faster with heuristics). By the end, you will have a working route finder that finds shortest paths between any two locations.
Modeling a City as a Graph
A city map translates naturally to a graph:
- Nodes = intersections or landmarks
- Edges = roads connecting them
- Weights = distance (or travel time)
Building a Sample City
city = CityGraph()
# Add locations with (x, y) positions
city.add_location("Home", 0, 0)
city.add_location("School", 3, 4)
city.add_location("Park", 1, 3)
city.add_location("Mall", 5, 2)
city.add_location("Hospital", 4, 5)
city.add_location("Library", 2, 1)
city.add_location("Station", 6, 0)
# Add roads with distances
city.add_road("Home", "Park", 3.2)
city.add_road("Home", "Library", 2.2)
city.add_road("Park", "School", 2.2)
city.add_road("Library", "Mall", 3.2)
city.add_road("School", "Hospital", 1.4)
city.add_road("Mall", "Station", 2.2)
city.add_road("Mall", "Hospital", 3.2)
city.add_road("Library", "Park", 2.2)Dijkstra's Algorithm: Guaranteed Shortest Path
Dijkstra's algorithm finds the shortest path from a source to all other nodes. It works by always processing the unvisited node with the smallest known distance — a greedy approach that is provably optimal for non-negative weights.
A* Algorithm: Faster with Heuristics
A* improves on Dijkstra by using a heuristic — an estimate of remaining distance to the goal. It explores the most promising nodes first, often finding the answer much faster.
The key formula: f(n) = g(n) + h(n) where:
- g(n) = actual distance from start to n (same as Dijkstra)
- h(n) = heuristic estimate from n to goal
- f(n) = total estimated cost through n
Dijkstra vs A*: When to Use Which
| Feature | Dijkstra | A* |
|---|---|---|
| Optimality | Always optimal | Optimal if heuristic is admissible |
| Nodes explored | More (explores uniformly) | Fewer (directed toward goal) |
| Speed | Slower for point-to-point | Faster for point-to-point |
| Memory | O(V) | O(V) |
| Heuristic needed | No | Yes |
| Best for | All-pairs shortest path | Single source-destination |
Rule of thumb: Use Dijkstra when you need distances to ALL nodes. Use A* when you need the shortest path to ONE specific destination.
Adding Traffic and Dynamic Weights
Real route finders account for traffic. We can modify edge weights based on time of day:
def get_travel_time(self, from_loc, to_loc, time_of_day):
"""Get travel time considering traffic."""
base_time = self.get_base_distance(from_loc, to_loc)
# Rush hour multiplier (7-9 AM, 5-7 PM)
if 7 <= time_of_day <= 9 or 17 <= time_of_day <= 19:
traffic_multiplier = 2.0 # Double travel time
elif 10 <= time_of_day <= 16:
traffic_multiplier = 1.2 # Slight traffic
else:
traffic_multiplier = 1.0 # No traffic
return base_time * traffic_multiplierComplexity Analysis
| Algorithm | Time | Space |
|---|---|---|
| Dijkstra (binary heap) | O((V + E) log V) | O(V) |
| A* (binary heap) | O((V + E) log V) worst, much better in practice | O(V) |
| BFS (unweighted) | O(V + E) | O(V) |
Key Takeaways
- Graph modeling is the first step: Identify what nodes and edges represent in your domain
- Dijkstra guarantees optimality for non-negative weights — use it when you need correctness
- **A* trades a heuristic for speed** — with a good heuristic, it explores far fewer nodes
- Adjacency lists are the right representation for sparse road networks
- Priority queues are the engine that makes these algorithms efficient
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Route Finder Using Graphs.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, projects, route, finder
Related Data Structures & Algorithms Topics