Graphs, A* and Dijkstra's Algorithm
1. What Is a Graph?
A graph is an abstract data type consisting of nodes (also called vertices) connected by edges (also called arcs). Unlike a tree, a graph has no root and may contain cycles, and any node may connect to any other.
| Term | Meaning |
|---|---|
| Node / vertex | An item of data in the graph |
| Edge / arc | A connection between two nodes |
| Weight | A value attached to an edge, e.g. distance, time or cost |
| Directed graph | Edges may be travelled in one direction only |
| Undirected graph | Edges may be travelled in either direction |
| Adjacency matrix | A 2D array storing the weight between every pair of nodes |
| Adjacency list | Each node stores a list of only the nodes it connects to |
Why graphs matter for AI
Many problems become searches over a graph: route-finding in a satellite navigation system, moving a character through a game map, planning moves in a puzzle, or modelling a network. AI needs to find a good path through such a graph efficiently, and that is what these two algorithms do.
2. The Example Graph
Both algorithms below are traced on this weighted, undirected graph. We are travelling from A to E.
3. Dijkstra's Algorithm
Dijkstra's algorithm finds the shortest path from a start node to every other node, using only the actual weights of the edges travelled so far.
The method
- Set the distance to the start node to 0, and every other node to infinity.
- Mark all nodes unvisited.
- Select the unvisited node with the smallest distance — call it the current node.
- For each neighbour, calculate distance to current + edge weight. If this is less than the neighbour's recorded distance, update it and record the current node as its predecessor.
- Mark the current node visited.
- Repeat from step 3 until the destination is visited or no unvisited nodes remain.
Traced on the example
| Step | Current | A | B | C | D | E | Visited |
|---|---|---|---|---|---|---|---|
| Init | – | 0 | ∞ | ∞ | ∞ | ∞ | – |
| 1 | A | 0 | 4 | 2 | ∞ | ∞ | A |
| 2 | C (2) | 0 | 4 | 2 | 7 | ∞ | A, C |
| 3 | B (4) | 0 | 4 | 2 | 5 | 7 | A, C, B |
| 4 | D (5) | 0 | 4 | 2 | 5 | 7 | A, C, B, D |
| 5 | E (7) | 0 | 4 | 2 | 5 | 7 | all |
At step 3, D's distance improved from 7 (via C) to 5 (via B), because 4 + 1 = 5 beats 2 + 5 = 7. That replacement is the heart of the algorithm.
4. A* Algorithm
A* improves on Dijkstra by adding an estimate of the distance still remaining. It therefore heads towards the goal instead of expanding evenly in all directions.
A* always expands the unvisited node with the lowest f value.
The heuristic
The heuristic is an informed guess about the remaining distance — for map routing, typically the straight-line distance to the destination. For A* to be guaranteed to find the shortest path, the heuristic must never overestimate the true remaining cost. Such a heuristic is called admissible.
Traced on the example
Using these straight-line estimates to E: h(A)=6, h(B)=3, h(C)=6, h(D)=5, h(E)=0
| Node | g (actual from A) | h (estimate to E) | f = g + h |
|---|---|---|---|
| A | 0 | 6 | 6 |
| C | 2 | 6 | 8 |
| B | 4 | 3 | 7 |
| D | 5 | 5 | 10 |
| E | 7 | 0 | 7 |
From A, both B (f=7) and C (f=8) are options. A* expands B first because its f value is lower — and B leads straight to E with a total of 7. Dijkstra, by contrast, expanded C first because C's actual distance (2) was smallest. A* reached the goal having examined fewer nodes.
5. Comparing the Two
| Dijkstra's algorithm | A* | |
|---|---|---|
| Uses | Actual cost only, g(n) | Actual cost plus heuristic, g(n) + h(n) |
| Search shape | Expands outwards in all directions | Directed towards the goal |
| Nodes examined | More | Fewer, if the heuristic is good |
| Finds | Shortest path to every node | Shortest path to one goal |
| Guarantees shortest path? | Always | Only if the heuristic never overestimates |
6. Exam Focus
Quick self-check
- Define node, edge and weight.
- Give one advantage of an adjacency list over an adjacency matrix.
- State the A* formula and define each term.
- Why must a heuristic never overestimate the remaining distance?
- Under what condition does A* behave identically to Dijkstra's algorithm?
- Trace Dijkstra from C to E on the example graph.