Week 11: Shortest Paths, Transitive Closure & Matrix Powers
3517 words
18 min read
2026-09-12T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
Weighted graphs, relaxation, Dijkstra, Bellman-Ford, negative cycles, DAG longest paths, and adjacency matrix powers. # Mathematics I — Week 11: Shortest Paths, Transitive Closure & Matrix Powers IITM’s Week 11 covers **longest path, transitive closure, matrix multiplication in graph settings, and single-source shor...

Mathematics I — Week 11: Shortest Paths, Transitive Closure & Matrix Powers
IITM’s Week 11 covers longest path, transitive closure, matrix multiplication in graph settings, and single-source shortest paths including Dijkstra’s and Bellman–Ford algorithms.
TL;DR
Week 10 taught how to traverse graphs.
Week 11 asks:
How do we compute richer path information?
The main ideas are:
- shortest path vs longest path
- weighted graphs
- DAG longest path intuition
- reachability
- transitive closure
- adjacency matrices and matrix powers
- single-source shortest path
- Dijkstra’s algorithm
- Bellman–Ford algorithm
- negative edge weights
1. Weighted graphs
A weighted graph assigns a numerical value to each edge.
Example:
textA --5-- B --2-- C \ | 9 1 \ | D
Weights might represent:
- distance
- time
- cost
- probability-related score
- energy
- latency
Formally, an edge may have weight:
2. Path weight
For a path:
its total weight is:
Example:
If:
has weight 4, and:
has weight 7,
then path:
has total weight:
3. Shortest-path problem
Given two vertices s and t:
Find a path from s to t with minimum total weight.
For unweighted graphs, BFS solves this when each edge effectively has equal cost.
For weighted graphs, we need algorithms like:
- Dijkstra
- Bellman–Ford
4. Single-source shortest path
Instead of solving for one target, we often ask:
From one source s, find shortest distances to every reachable vertex.
This is the single-source shortest-path problem, often abbreviated SSSP.
Output:
for every vertex v, where:
is shortest known distance from source s to v.
5. Relaxation — the core idea
Most shortest-path algorithms repeatedly perform relaxation.
Suppose we know:
and have edge:
with weight:
If:
then update:
and often:
This means:
We found a cheaper route to v through u.
6. Example of relaxation
Suppose:
and:
has weight:
Current:
Going through A gives:
Since:
we update:
7. Initial shortest-path setup
For source:
initialize:
For every other vertex:
Why infinity?
Because initially we know no path to those vertices.
Then relax edges to improve estimates.
8. Dijkstra’s algorithm
Dijkstra solves single-source shortest paths when edge weights are nonnegative.
Core idea:
Repeatedly finalize the currently closest unprocessed vertex.
It is a greedy algorithm.
9. Dijkstra intuition
Suppose source is A.
At every step:
- choose unvisited vertex with smallest current distance
- finalize it
- relax all outgoing edges
- repeat
Once a vertex is finalized, its shortest distance is guaranteed correct — provided all edge weights are nonnegative.
10. Dijkstra example
Consider:
textA --4--> B | | 1 1 v v C --2--> D
Source:
Initialize:
From A:
Choose smallest unvisited:
with distance:
Relax:
weight 2:
Now smallest is:
Then:
So final shortest distances:
11. Why Dijkstra works
The greedy step relies on:
If we already selected the vertex with smallest tentative distance, no future path through unprocessed vertices can make it smaller using nonnegative edges.
That guarantee breaks with negative weights.
12. Negative-weight problem
Suppose:
textA --2--> B | ^ 5 | v -10 C -------
Initially Dijkstra may think:
But path:
costs:
So a later negative edge can improve a vertex that seemed already finalized.
That breaks Dijkstra's logic.
13. Dijkstra restriction
You should memorize:
Zero-weight edges are fine.
Negative edges are not.
14. Dijkstra pseudocode intuition
textdist[source] = 0 all others = infinity while unprocessed vertices remain: choose u with smallest dist[u] mark u processed for each edge u -> v: if dist[u] + w(u,v) < dist[v]: update dist[v]
Efficient implementations typically use a priority queue.
15. Dijkstra complexity
With a binary heap / priority queue:
often simplified to:
for connected sparse graphs.
A simpler array/matrix implementation may be:
For exam purposes, context matters.
16. Bellman–Ford algorithm
Bellman–Ford also solves single-source shortest paths.
But unlike Dijkstra, it can handle:
as long as there is no reachable negative-weight cycle that makes shortest paths undefined.
17. Bellman–Ford core idea
Instead of greedily finalizing vertices, Bellman–Ford repeatedly relaxes all edges.
For a graph with:
repeat edge relaxation:
times.
Why?
Any simple path can contain at most:
edges.
18. Bellman–Ford procedure
Initialize:
and all others infinity.
Then:
textrepeat |V|-1 times: for every edge (u,v): relax(u,v)
After that, one additional pass can test for negative cycles.
19. Why ∣V∣−1 iterations?
A shortest simple path cannot repeat a vertex.
If it repeated a vertex, that contains a cycle.
A simple path on ∣V∣ vertices uses at most:
edges.
After the first relaxation round, shortest paths of at most one edge can propagate.
After two rounds, two-edge paths.
Eventually:
edges are enough.
20. Bellman–Ford example
Suppose:
textS --4--> A | | 5 -2 v v B --1--> C
There may be alternative routes.
Bellman–Ford repeatedly examines all edges and improves estimates whenever possible.
The important conceptual point is:
improvements are allowed even if a vertex was already improved before.
Nothing is permanently finalized too early.
21. Negative cycles
A negative cycle is a directed cycle whose total weight is negative.
Example:
weight 2
weight −5
weight 1
Total:
22. Why negative cycles break shortest paths
Suppose a path can reach a negative cycle.
You could traverse that cycle repeatedly:
making path cost arbitrarily small.
So there is no finite "shortest" path.
Mathematically:
in the limiting sense.
23. Detecting negative cycles with Bellman–Ford
After:
full relaxation rounds, perform one more.
If any distance can still decrease, then:
This is a major advantage of Bellman–Ford.
24. Bellman–Ford complexity
Bellman–Ford scans all E edges for roughly V rounds.
Therefore:
This is slower than Dijkstra on ordinary nonnegative graphs.
25. Dijkstra vs Bellman–Ford
| Dijkstra | Bellman–Ford |
|---|---|
| greedy | repeated relaxation |
| nonnegative weights | negative weights allowed |
| faster | slower |
| usually priority queue | scan all edges |
| cannot detect negative cycles properly | can detect reachable negative cycles |
High-value rule:
26. BFS vs Dijkstra
BFS can be viewed as a special shortest-path algorithm where every edge has identical weight:
So:
Unweighted / equal-weight graph
Use BFS.
Nonnegative weighted graph
Use Dijkstra.
Negative weights
Use Bellman–Ford.
This taxonomy is worth memorizing.
27. Shortest-path algorithm decision table
| Graph | Preferred basic algorithm |
|---|---|
| unweighted | BFS |
| weighted, all weights ≥0 | Dijkstra |
| negative weights, no problematic negative cycle | Bellman–Ford |
| DAG | topological-order dynamic programming can be even simpler |
28. Longest-path problem
Now reverse the goal:
Find a path with maximum total weight or maximum number of edges.
This sounds like simply modifying shortest path.
Unfortunately, in general graphs, longest path is much harder.
29. Why arbitrary longest path is difficult
If cycles are allowed, you can potentially keep looping.
Even when restricting to simple paths, the general longest-path problem is computationally difficult.
But on a DAG, the problem becomes manageable.
This is why DAGs from Week 10 matter.
30. Longest path in a DAG
Because a DAG has no cycles, vertices can be arranged in topological order.
Then compute longest distances in that order.
Conceptually:
This resembles shortest-path relaxation, but uses:
instead of:
31. Example DAG
textA → B → D \ ↑ → C ──
Suppose weights:
Possible paths to D:
So longest path from A to D has weight:
32. Longest path as scheduling
A DAG may represent tasks:
textA → B → D \→ C ─→
Suppose edge/node weights represent durations.
The longest dependency chain determines the critical path.
This is important in:
- project scheduling
- build systems
- dependency planning
- workflow optimization
33. Reachability
A vertex:
is reachable from:
if there exists a directed path:
We often want to know reachability for every pair of vertices.
That leads to transitive closure.
34. Transitive closure
The transitive closure of a directed graph adds an edge:
whenever v is reachable from u.
So it converts:
path exists
into:
direct relation in the closure.
35. Example
Original:
textA → B → C
Since:
the transitive closure also contains:
Closure:
textA → B → C \------→
36. Relation-theory connection
Recall Week 1 transitivity:
and:
implies:
Transitive closure means:
Add the minimum required relationships so the relation becomes transitive.
So Week 11 reconnects directly to Week 1.
37. Reachability matrix
For vertices:
define matrix:
where:
This is essentially an adjacency matrix of the transitive closure.
38. Adjacency matrix recap
For graph G, adjacency matrix:
if edge:
exists.
Otherwise:
Matrix multiplication reveals deeper path information.
39. Why A2 is interesting
Consider:
By matrix multiplication:
Each term:
equals 1 exactly when:
and:
Therefore:
counts walks of length:
from i to j.
40. General matrix-power interpretation
More generally:
counts the number of walks of length:
from vertex i to vertex j.
This is a major connection between linear algebra and graph theory.
41. Example
Suppose:
textA → B → C
Adjacency matrix in order A,B,C:
Then:
The 1 in position (A,C) indicates one walk of length 2:
42. Reachability from matrix powers
A path between distinct vertices can have length at most:
if considering a simple path.
So reachability information is related to:
If the (i,j) entry is nonzero somewhere in these powers, then:
is reachable from:
For Boolean reachability, we care about existence, not count.
43. Boolean matrix viewpoint
Instead of ordinary arithmetic:
- multiplication behaves like AND
- addition behaves like OR
Then matrix multiplication can directly encode reachability.
This is sometimes called Boolean matrix multiplication.
Conceptually:
means both required edges exist.
And:
means at least one path exists.
44. Transitive closure via repeated paths
One conceptual formula is:
depending on whether self-reachability is included.
Here:
represents length-0 paths:
The exact convention may vary, so check whether the relation is defined reflexively.
45. Matrix multiplication definition
For:
and:
the product:
has size:
Each entry:
Graph applications rely heavily on this formula.
46. Matrix-dimension rule
If:
is:
and:
is:
then:
exists only when:
Result size:
47. Matrix multiplication is not commutative
In general:
and one product may exist when the reverse doesn't.
This matters because directed graph structure itself is order-sensitive.
48. Path reconstruction in shortest-path algorithms
Distances tell us cost.
To recover the actual path, maintain:
Whenever relaxation improves:
set:
Then trace backward from target.
49. Example
Suppose final parents are:
Then path to D backwards:
Reverse:
50. Triangle inequality intuition for shortest paths
Shortest-path distances satisfy:
If the inequality is violated by our current estimates, relaxation fixes it.
So relaxation is repeatedly enforcing shortest-path consistency.
51. Dijkstra full small example
Consider:
textS --2--> A --3--> C | | 5 1 v v B --1--> C
Source:
Initialize:
others infinity.
From S:
Choose A:
Relax:
So:
Suppose:
has weight 1.
Then:
Update:
Choose B next:
Relax:
weight 1:
So:
Final:
52. Why greedy order mattered
Dijkstra processes:
based on smallest tentative distance, not alphabetical order or graph depth.
This is a common misunderstanding.
Common mistake: Dijkstra with negative edges
Do not.
Even one negative edge can invalidate its finalized-distance assumption.
Use Bellman–Ford or another appropriate method.
Common mistake: Bellman–Ford needs all cycles absent
No.
Bellman–Ford can work with cycles.
The problem is a reachable:
not cycles in general.
Positive or zero-weight cycles are fine for shortest-path existence.
Common mistake: negative edge = negative cycle
These are different.
A graph may contain a negative edge but no negative cycle.
Example:
textA --(-3)--> B → C
No return route means no cycle.
Bellman–Ford can still compute shortest paths.
Common mistake: longest path = negate weights + Dijkstra
Not generally.
Negating weights creates negative edges, which Dijkstra cannot handle.
And arbitrary longest simple paths are fundamentally harder.
On DAGs, use topological structure.
Common mistake: adjacency matrix power counts simple paths
Not necessarily.
counts walks of length k.
Walks may repeat vertices and edges.
That is different from simple paths.
Common mistake: transitive closure stores distance
No.
Transitive closure answers:
It doesn't inherently store:
- shortest distance
- number of paths
- path weight
Those are different graph problems.
Common mistake: shortest path means fewest edges
Only in unweighted/equal-weight graphs.
Weighted shortest path minimizes:
A path with more edges can still be cheaper.
60. Example
Path 1:
one edge of weight:
Path 2:
three edges of weights:
Weighted shortest path is Path 2.
61. Algorithm selection mental map
textNeed shortest path? │ ├── unweighted / equal weights │ └── BFS │ ├── weighted and all weights >= 0 │ └── Dijkstra │ ├── negative weights possible │ └── Bellman–Ford │ └── DAG └── topological-order DP
This is extremely useful for exam reasoning.
62. Week 11 formula/fact sheet
Relaxation
d[v]←min(d[v],d[u]+w(u,v))Dijkstra condition
w(e)≥0Bellman–Ford rounds
∣V∣−1Bellman–Ford complexity
O(VE)Adjacency powers
(Ak)ij=number of length-k walks from i to jDAG longest-path recurrence
d[v]=u→vmax(d[u]+w(u,v))63. Week 11 conceptual map
textWeighted Graphs │ ├── path weight ├── relaxation │ ├── shortest paths │ ├── BFS │ ├── Dijkstra │ └── Bellman–Ford │ ├── longest path │ └── tractable on DAGs │ ├── reachability │ └── transitive closure │ └── adjacency matrix └── A^k → walks of length k
64. How Week 10 and Week 11 connect
Week 10:
Week 11:
and:
So the progression is:
65. What you should actually master
By the end of Week 11, you should be able to:
- Understand weighted graphs.
- Compute total path weight.
- Explain single-source shortest paths.
- Perform a relaxation step.
- Trace basic Dijkstra examples.
- Know why Dijkstra requires nonnegative weights.
- Trace basic Bellman–Ford logic.
- Understand why Bellman–Ford uses ∣V∣−1 rounds.
- Recognize and explain negative-weight cycles.
- Distinguish negative edges from negative cycles.
- Choose between BFS, Dijkstra and Bellman–Ford.
- Understand longest-path reasoning in DAGs.
- Define reachability.
- Define transitive closure.
- Read/write adjacency matrices.
- Multiply small matrices.
- Understand: (Ak)ij as counting length-k walks.
- Distinguish walks from simple paths.
- Understand path reconstruction with parents.
If those are comfortable, Week 11 is under control.
Quick Recall & Flashcards
Test your active recall before proceeding to practice problems. Format:
Question :: Answer.- Relaxation step for edge (u,v) with weight w(u,v) :: If dist[u]+w(u,v)<dist[v]: dist[v]=dist[u]+w(u,v)
- Dijkstra's algorithm requirement :: All edge weights must be nonnegative (w(u,v)≥0)
- Bellman–Ford algorithm number of relaxation rounds :: ∣V∣−1 rounds over all edges
- How Bellman–Ford detects negative-weight cycles :: A negative cycle exists if an edge can still be relaxed in the ∣V∣-th round
- Adjacency matrix power (Ak)ij interpretation :: Number of walks of length exactly k from vertex i to vertex j
- Transitive closure definition :: Reachability matrix indicating if a path of any length exists from i to j
- Algorithm for single-source shortest path in DAG with arbitrary weights :: Topological sort + single relaxation pass in O(∣V∣+∣E∣)
Practice & Self-Check
Attempt these diagnostic questions to verify your operational understanding.
1
Question 1
Which algorithm is suitable for finding single-source shortest paths in a directed graph that contains negative edge weights but no negative cycles?
MCQ
3 Unit Assessment
A. Dijkstra’s Algorithm
B. Bellman–Ford Algorithm
C. Standard unweighted BFS
D. Kruskal’s Algorithm
2
Question 2
Let A be the adjacency matrix of an unweighted graph. The entry (A3)ij represents:
MCQ
3 Unit Assessment
A. The shortest distance between vertex i and vertex j
B. The number of walks of length exactly 3 from vertex i to vertex j
C. The weight of edge (i,j) cubed
D. Whether i and j belong to the same component