🧮 Dynamic Programming Pathfinder

Learn DP through interactive vector-based pathfinding

šŸ“š What is Dynamic Programming?

Dynamic Programming (DP) is an optimization technique that solves complex problems by breaking them into simpler subproblems and storing their solutions.

Key Principle: Solve each subproblem only once, then store the result for future use.

The Pathfinding Problem

Given a grid with costs on each cell, find the minimum cost path from the top-left to bottom-right corner. You can only move right or down.

Why DP?

Without DP, we'd need to check all possible paths (exponential time). With DP, we compute the optimal solution in O(nƗm) time by building on previous solutions.

// Without DP: O(2^(n+m)) paths to check // With DP: O(n Ɨ m) cells to compute

šŸŽÆ Example Grid

Numbers show the cost of passing through each cell

Start: (0, 0) → Top-left cell
End: (3, 3) → Bottom-right cell
Moves: → (1,0) or ↓ (0,1)

šŸ“ Vector Calculations

In pathfinding, we represent positions and movements as 2D vectors.

Position Vector

A position (row, col) represents coordinates on our grid.

position = [row, col] // Example: [2, 1] means row 2, column 1

Movement Vectors

We define movement as vector addition:

right = [0, 1] // Move one column right down = [1, 0] // Move one row down new_pos = current_pos + movement // [2,1] + [0,1] = [2,2] (moved right) // [2,1] + [1,0] = [3,1] (moved down)

Vector Magnitude (Cost)

The "cost" to reach a cell is the sum of all cell values along the path.

cost(path) = Σ grid[pos] for pos in path

šŸ”¢ Vector Demo

Current: [0, 0]
Movement: [0, 0]
Result: [0, 0]

🧠 The DP Formula

For each cell, the minimum cost to reach it is:

dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) // Where: // dp[i][j] = min cost to reach cell (i,j) // grid[i][j] = cost of cell (i,j) // dp[i-1][j] = cost from above (if exists) // dp[i][j-1] = cost from left (if exists)

Base Cases

// First cell dp[0][0] = grid[0][0] // First row (can only come from left) dp[0][j] = dp[0][j-1] + grid[0][j] // First column (can only come from above) dp[i][0] = dp[i-1][0] + grid[i][0]

Vector Form

For position p = [i, j]: left_neighbor = p + [-1, 0] = [i-1, j] top_neighbor = p + [0, -1] = [i, j-1] dp[p] = grid[p] + min(dp[left], dp[top])

šŸ“Š DP Table Construction

Watch how the DP table is built step by step:

šŸŽ® Interactive Pathfinder

Click cells to toggle obstacles, then run the DP algorithm to find the optimal path.

200ms

šŸ“ˆ Results

Min Cost: —
Path Length: —
Cells Visited: —

Calculation Log

Click "Find Path" to start...

šŸŽÆ Test Your Knowledge

Question 1

What is the time complexity of the DP pathfinding solution for an nƗm grid?

A) O(2^n)
B) O(n Ɨ m)
C) O(n²)
D) O(n log n)

Question 2

In the DP formula dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]), what does dp[i-1][j] represent?

A) Cost from the cell to the right
B) Cost from the cell above
C) Cost from the cell below
D) Cost of the current cell

Question 3

Given movement vector right = [0, 1] and position [2, 3], what is the new position after moving right?

A) [3, 3]
B) [2, 4]
C) [2, 2]
D) [1, 3]

šŸ“Š Your Score

0/3

Answer all questions to see your score!

Key Takeaways:
  • DP breaks problems into overlapping subproblems
  • Store results to avoid redundant calculations
  • Vectors simplify position and movement math
  • Build solutions from base cases upward