π Unique Paths - Formal Proof
Why
dp[i][j] = dp[i-1][j] + dp[i][j-1]
is a mathematical certainty, not just a pattern
π Unique Paths Series:
- The Discovery Journey - How humans naturally discover the solution
- Plain English Proof - Accessible logical reasoning
- π Formal Proof (You are here) - Mathematical rigor and certainty
- When Reality Gets Messy - Adapting to obstacles
For any cell (i,j)
where i > 0
and j > 0
:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
This is not just an observationβit's a mathematical certainty that can be proven rigorously.
π― Setting Up The Proofβ
First, we need to be precise about what we're claiming. Let me define our terms carefully, the way you would in a formal proof.
Definition 1 (The Grid)β
Let G be an
m Γ n
grid where cells are indexed by coordinates(i,j)
with0 β€ i < m
and0 β€ j < n
.
- Cell
(0,0)
is the top-left corner (start)- Cell
(m-1, n-1)
is the bottom-right corner (end)
Definition 2 (Valid Path)β
A valid path P from
(0,0)
to(i,j)
is a sequence of cells(cβ, cβ, cβ, ..., cβ)
where:
cβ = (0,0)
(starts at origin)cβ = (i,j)
(ends at target)- For each consecutive pair of cells, we move either:
- Exactly one step right (increasing j by 1), OR
- Exactly one step down (increasing i by 1)
Definition 3 (dp function)β
Let
dp[i][j]
denote the number of distinct valid paths from(0,0)
to(i,j)
.
π The Proof (by Partition Argument)β
For any cell (i,j)
where i > 0
and j > 0
, the number of paths to (i,j)
equals the sum of paths to (i-1,j)
and paths to (i,j-1)
.
Formally: dp[i][j] = dp[i-1][j] + dp[i][j-1]
Proof:β
Let S be the set of all valid paths from (0,0)
to (i,j)
.
We need to count the cardinality of S, which is what dp[i][j]
represents.
Step 1: Partition the set Sβ
I will partition S into two disjoint subsets based on the final move of each path:
-
Let A =
{paths in S whose final move is DOWN}
- The second-to-last cell is
(i-1, j)
- The second-to-last cell is
-
Let B =
{paths in S whose final move is RIGHT}
- The second-to-last cell is
(i, j-1)
- The second-to-last cell is
Cell (i,j) can be reached from:
(i-1,j)
β
[i,j] β (i,j-1)
Set A: paths ending with β
Set B: paths ending with β
Step 2: Prove A and B partition Sβ
To show A and B partition S, I must prove two things:
Claim 2a: A β© B = β (the sets are disjoint)β
Suppose for contradiction that there exists a path P that belongs to both A and B.
This would mean P's final move is simultaneously:
- DOWN (coming from
i-1,j
) - RIGHT (coming from
i,j-1
)
But a path's final move is uniquely determined by its second-to-last cell.
Since (i-1,j) β (i,j-1)
when both i > 0
and j > 0
, we have a contradiction.
Therefore A and B are disjoint. β
Claim 2b: A βͺ B = S (every path belongs to one of the sets)β
Consider any path P in S.
Since P ends at (i,j)
and starts at (0,0)
where i > 0
and j > 0
, the path must have made at least one move.
Let the second-to-last cell in P be denoted as (i',j')
.
By the definition of valid moves, we must have either:
Case 1: (i',j') = (i-1,j)
- The final move was DOWN by one row
- Therefore
P β A
Case 2: (i',j') = (i,j-1)
- The final move was RIGHT by one column
- Therefore
P β B
These are the only two ways to reach (i,j)
from an adjacent cell.
Therefore every path in S belongs to either A or B. β
Since A and B are disjoint and their union is S, we have successfully partitioned S.
Step 3: Count the elements in Aβ
Consider any path P in A. This path:
- Goes from
(0,0)
to(i,j)
- Its final move is from
(i-1,j)
to(i,j)
Key Insight: If we remove this final move, we get a path from (0,0)
to (i-1,j)
.
Conversely: Any path from (0,0)
to (i-1,j)
can be extended by one DOWN move to create a path in A.
This creates a bijection (one-to-one correspondence) between:
- The set A (paths to
(i,j)
ending with DOWN) - The set of all paths from
(0,0)
to(i-1,j)
Since bijections preserve cardinality, we have:
|A| = number of paths from (0,0) to (i-1,j) = dp[i-1][j]
Step 4: Count the elements in Bβ
By identical reasoning, removing the final RIGHT move from any path in B gives us a bijection between:
- The set B (paths to
(i,j)
ending with RIGHT) - The set of all paths from
(0,0)
to(i,j-1)
Therefore:
|B| = number of paths from (0,0) to (i,j-1) = dp[i][j-1]
Step 5: Apply the Addition Principleβ
Since A and B partition S (they are disjoint and their union equals S), by the fundamental counting principle:
|S| = |A| + |B|
Substituting our results from Steps 3 and 4:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
This completes the proof. β
π‘ Why This Proof Matters (The Deeper Understanding)β
Now that you've seen the formal proof, let me explain why each step was necessary and what mathematical principles we relied on.
The Partition Principleβ
If you can partition a set into disjoint subsets, the total count is the sum of the counts of each subset.
This is sometimes called the Addition Principle or Rule of Sum.
Formally: If S = Aβ βͺ Aβ βͺ ... βͺ Aβ
and the sets Aα΅’
are pairwise disjoint, then:
|S| = |Aβ| + |Aβ| + ... + |Aβ|
We used the two-set case: S = A βͺ B
where A β© B = β
.
Why this matters: We're not just guessing that addition works. We're using a proven theorem from set theory. This is why we can be certain that counting paths from two sources and adding them gives us the total.
The Bijection Principleβ
In Step 3 and Step 4, I established bijections between sets. This is a powerful counting technique.
If there exists a bijection f: X β Y
, then |X| = |Y|
.
In our case:
- X = paths to
(i,j)
whose final move is DOWN - Y = paths to
(i-1,j)
- The bijection is: "remove the final DOWN move" (with inverse "add a DOWN move")
Why this matters: This proves we're not overcounting or undercounting. Every path to (i-1,j)
corresponds to exactly one path in A, and vice versa. There's a perfect one-to-one matching.
The Uniqueness of the Final Moveβ
This might seem obvious, but it's crucial: every path has exactly one final move, and that move is uniquely determined.
This is what made our partition possible. If a path could somehow arrive at (i,j)
from multiple cells simultaneously, or if there were other ways to reach (i,j)
besides from (i-1,j)
or (i,j-1)
, our proof would fail.
The constraint that we can only move RIGHT or DOWN is what guarantees this uniqueness.
π§± Base Cases (Completing the Inductive Structure)β
You might notice our proof assumes i > 0
and j > 0
. What about the edges of the grid?
Base Case 1: dp[0][0] = 1β
There is exactly one path from (0,0)
to (0,0)
: the empty path that makes no moves.
This is our starting point, and it makes intuitive sense. You're already at the destination.
Base Case 2: For all j > 0, dp[0][j] = 1β
When i = 0
, we're in the first row.
To reach (0,j)
, we cannot move DOWN (we're already at the top).
Therefore, every path must consist entirely of RIGHT moves. There is exactly one such path:
(0,0) β (0,1) β (0,2) β ... β (0,j)
Base Case 3: For all i > 0, dp[i][0] = 1β
By symmetrical reasoning, when j = 0
, we're in the first column.
Every path must consist entirely of DOWN moves, and there is exactly one such path.
Now, with these base cases established, our recurrence relation gives us the ability to compute dp[i][j]
for any cell in the grid through a process that resembles mathematical induction.
π² An Alternative Proof (Combinatorial Interpretation)β
Let me show you another way to prove the same result, which gives additional insight.
Observationβ
Any path from (0,0)
to (i,j)
consists of:
- Exactly i DOWN moves
- Exactly j RIGHT moves
- Total of (i+j) moves
Combinatorial Questionβ
In how many ways can we arrange i identical D's and j identical R's?
This is a classic combinatorics problem. We're choosing i positions out of (i+j) total positions to place the D's.
The answer is the binomial coefficient:
dp[i][j] = C(i+j, i) = (i+j)! / (i! Γ j!)
Pascal's Identityβ
There's a well-known identity for binomial coefficients called Pascal's Identity:
C(n, k) = C(n-1, k-1) + C(n-1, k)
Applying this with n = i+j
and k = i
:
C(i+j, i) = C(i+j-1, i-1) + C(i+j-1, i)
Notice that:
C(i+j-1, i-1)
represents choosing(i-1)
positions out of(i+j-1)
total β corresponds to a path to(i-1,j)
C(i+j-1, i)
corresponds to a path to(i,j-1)
Therefore:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
This is the same recurrence relation, proven through a completely different approach using combinatorial identities!
π€ Why Are There Two Different Proofs?β
This is an important meta-mathematical point. The fact that we can prove the same result in two fundamentally different ways tells us something deep.
First proof (partition argument):
- Works directly with the paths themselves
- Reasons about the structure of the problem
- Every path must come from somewhere
- We can classify paths by where they come from
- First principles approach
Second proof (combinatorial counting):
- Transforms the problem into a different domain (arranging symbols)
- Uses known theorems about binomial coefficients
- More elegant but relies on prior knowledge
- Top-down approach using established results
Both are valid. Both give insight.
The first proof is more "first principles" because it builds directly from the definition of paths.
The second proof is more elegant but relies on prior knowledge of Pascal's Identity.
Seeing multiple proofs of the same statement is valuable because each proof illuminates different aspects of why something is true.
π§ What Makes This Proof "Obvious" (In Hindsight)β
After working through the formal proof, you might think "well, of course it works that way."
But notice what you needed to see it:
- Understanding that sets can be partitioned
- Knowing that disjoint sets' cardinalities add
- Recognizing when two sets have the same size (bijection)
- Seeing that every path has a unique final move
These are not trivial insights! They're the result of mathematical training.
When someone says "it's obvious that dp[i][j] = dp[i-1][j] + dp[i][j-1]
," what they really mean is:
"Once you understand partitioning, bijections, and the addition principle, the conclusion follows necessarily."
The proof makes the intuition rigorous. It transforms a pattern you observed into a logical certainty.
π§ͺ Test Your Understandingβ
Question 1: Breaking the Partitionβ
In our partition proof, we relied on the fact that A and B are disjoint.
Can you construct a scenario where, if we allowed diagonal moves, A and B would no longer partition the set S properly?
What would go wrong?
Click for answer
If we allowed diagonal moves, we'd need to add a third set:
- C = paths whose final move is DIAGONAL (from
i-1, j-1
)
Now we'd need to partition into three disjoint sets: A, B, and C.
The formula would become:
dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1]
The original proof structure still works! We just have three subsets instead of two.
The key is that the partition principle generalizes: as long as the sets are disjoint and cover all cases, we can add their cardinalities.
Question 2: The Inverse Functionβ
The bijection in Step 3 maps paths in A to paths to (i-1,j)
by "removing the final move."
What is the inverse function, and why is it important that the inverse exists?
Click for answer
The inverse function: g: paths to (i-1,j) β A
Defined as: "Take a path P to (i-1,j)
and append one DOWN move to reach (i,j)
"
Why it matters:
- For a function to be a bijection, it must be both injective (one-to-one) and surjective (onto)
- Having an inverse proves it's a bijection
- This guarantees every path in A corresponds to exactly one path to
(i-1,j)
- No paths are double-counted or missed
Without the inverse, we couldn't be sure the mapping was one-to-one!
Question 3: Pascal's Identity Proofβ
In the combinatorial proof, we used Pascal's Identity.
Can you prove Pascal's Identity itself using a partition argument similar to what we did for paths?
Hint: Think about choosing k items from n items. Partition based on whether a specific item is chosen or not.
Click for answer
Claim: C(n, k) = C(n-1, k-1) + C(n-1, k)
Proof by Partition:
Let S be the set of all ways to choose k items from n items.
Pick a specific item, call it x.
Partition S based on whether x is chosen:
- A = selections that include x
- B = selections that exclude x
Count A: If x is included, we need to choose k-1
more items from the remaining n-1
items.
|A| = C(n-1, k-1)
Count B: If x is excluded, we choose all k
items from the remaining n-1
items.
|B| = C(n-1, k)
Since A and B partition S:
C(n, k) = |A| + |B| = C(n-1, k-1) + C(n-1, k)
This is the exact same proof structure we used for paths! β
Question 4: Modifying the Rulesβ
If we modified the problem so that you could move RIGHT, DOWN, or stay in place (but you must eventually reach the end), would our recurrence relation still hold?
Why or why not? What would need to change in the proof?
Click for answer
No, the recurrence would NOT hold in its current form.
Why it breaks:
- If we can "stay in place," paths are no longer finite in length
- You could stay at
(i,j)
forever without reaching the end - The number of paths becomes infinite
What would need to change:
If we add the constraint "you cannot stay in the same place twice in a row," then:
- We'd need a 3D state:
dp[i][j][last_move]
- Where
last_move
can be: RIGHT, DOWN, or STAY - The recurrence becomes:
dp[i][j]["STAY"] = dp[i][j]["RIGHT"] + dp[i][j]["DOWN"]
dp[i][j]["RIGHT"] = dp[i][j-1]["RIGHT"] + dp[i][j-1]["DOWN"] + dp[i][j-1]["STAY"]
dp[i][j]["DOWN"] = dp[i-1][j]["RIGHT"] + dp[i-1][j]["DOWN"] + dp[i-1][j]["STAY"]
The partition principle still applies, but we partition based on both the previous cell and the previous move type!
π― Key Takeawaysβ
-
The Recurrence is Provable:
- Not just an observation
- Follows from set theory and counting principles
-
Partition + Bijection = Counting:
- Partition the set by the final move
- Use bijections to count each partition
- Add the counts (addition principle)
-
Multiple Proof Approaches:
- Direct (partition argument)
- Combinatorial (binomial coefficients)
- Both lead to the same truth
-
Prerequisites Matter:
- Understanding partitions, bijections, and addition principle
- These aren't "obvious"βthey're learned foundations
-
Uniqueness of Final Move:
- Critical to the partition working
- Guaranteed by the movement constraints
π Connection to Other Problemsβ
After understanding this proof:
- Minimum Path Sum - Same proof structure, different operation (min instead of +)
- Unique Paths II - Add guard condition for obstacles, proof structure survives
- Binomial Coefficients - Direct combinatorial connection via Pascal's Identity
- Edit Distance - 2D DP with similar partition logic (3 operations instead of 2 moves)
The proof technique generalizes far beyond this specific problem!
Remember: A proof doesn't just show that something is trueβit reveals why it must be true. Understanding the proof gives you the power to adapt the solution when constraints change.