Skip to content

Visualizing DP State Space: From Confusion to Clarity 🎨

Drawing pictures is the SECRET WEAPON for understanding DP! Here's how to do it effectively.

🎯 Why Visualization Works

  • Makes abstract concepts concrete
  • Reveals patterns and dependencies
  • Helps debug wrong recurrences
  • Shows optimal substructure visually
  • Makes base cases obvious

📊 Visualization Techniques by DP Type

1️⃣ 1D DP: The Timeline/Array View

Example: Fibonacci Numbers

Problem: F(n) = F(n-1) + F(n-2)

Visual representation:
Index:  0   1   2   3   4   5   6
Value: [0] [1] [1] [2] [3] [5] [8]
        ↑   ↑   ↑   ↑   ↑   ↑   ↑
     base base  │   │   │   │   │
                └─┬─┘   │   │   │
                  │     │   │   │
                  └───┬─┘   │   │
                      │     │   │
                      └───┬─┘   │
                          │     │
                          └───┬─┘
                              8

Arrows show dependencies: each cell depends on two previous cells

Example: House Robber

Houses: [2, 7, 9, 3, 1]
dp[i] = max money robbing houses 0 to i

Visual Decision Tree at each house:
       dp[2] = max(dp[1], dp[0] + 9)
              /                    \
         don't rob              rob house 2
         house 2                    |
            |                  dp[0] + 9
         dp[1] = 7              = 2 + 9 = 11

                              dp[2] = 11

Index:  0   1   2   3   4
Houses:[2] [7] [9] [3] [1]
DP:    [2] [7][11][11][12]
        ↑   ↑   ↑   ↑   ↑
       take skip take skip take

2️⃣ 2D DP: The Grid/Table View

Example: Unique Paths (Robot in Grid)

3x7 grid, robot goes from top-left to bottom-right

Grid with DP values (number of ways to reach each cell):
    0   1   2   3   4   5   6
  ┌───┬───┬───┬───┬───┬───┬───┐
0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │
  ├───┼───┼───┼───┼───┼───┼───┤
1 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │
  ├───┼───┼───┼───┼───┼───┼───┤
2 │ 1 │ 3 │ 6 │10 │15 │21 │28 │
  └───┴───┴───┴───┴───┴───┴───┘

Arrows show movement (only right → and down ↓):
Each cell = sum of cell above + cell to the left

dp[i][j] = dp[i-1][j] + dp[i][j-1]
            ↑ from above  ↑ from left

Example: Longest Common Subsequence

s1 = "ABCD"
s2 = "ACBD"

LCS Table (dp[i][j] = LCS length of s1[0..i-1] and s2[0..j-1]):

       ""  A   C   B   D
    ┌─────┬───┬───┬───┬───┐
""  │  0  │ 0 │ 0 │ 0 │ 0 │
    ├─────┼───┼───┼───┼───┤
A   │  0  │ 1 │ 1 │ 1 │ 1 │ ← A matches A: take diagonal + 1
    ├─────┼───┼───┼───┼───┤
B   │  0  │ 1 │ 1 │ 2 │ 2 │ ← B matches B: take diagonal + 1
    ├─────┼───┼───┼───┼───┤
C   │  0  │ 1 │ 2 │ 2 │ 2 │ ← C matches C: take diagonal + 1
    ├─────┼───┼───┼───┼───┤
D   │  0  │ 1 │ 2 │ 2 │ 3 │ ← D matches D: take diagonal + 1
    └─────┴───┴───┴───┴───┘

Decision pattern:
- If chars match: ↖ (diagonal) + 1
- If no match: max(↑ up, ← left)

3️⃣ State Machine DP: The State Diagram

Example: Stock Buy/Sell with Cooldown

States: Hold, Sold, Rest

State Diagram:
┌─────────┐    buy     ┌─────────┐
│  Rest   │ ────────→  │  Hold   │
│ (cash)  │            │(1 stock)│
└─────────┘            └─────────┘
     ↑                      │
     │ cooldown             │ sell
     │                      ↓
┌─────────┐            ┌─────────┐
│  Sold   │ ←────────  │  Hold   │
│ (cash)  │   sell     │(1 stock)│
└─────────┘            └─────────┘

DP Table:
Day    Hold    Sold    Rest
 0      -p[0]    0       0
 1    max(-p[0], -p[1])  Hold[0]+p[1]  max(0, Sold[0])
 2    max(Hold[1], Rest[1]-p[2])  Hold[1]+p[2]  max(Sold[1], Rest[1])
...

🎨 Drawing Techniques

Technique 1: Dependency Arrows

Purpose: Show which previous states current state depends on

For dp[i][j], draw arrows from cells it depends on:

Coin Change (1D):
dp[5] depends on dp[5-coin] for each coin

[0][1][2][3][4][5]
              ↑ ↑ ↑
              │ │ └─ dp[5-coin3]
              │ └─── dp[5-coin2]
              └───── dp[5-coin1]

Edit Distance (2D):
dp[i][j] depends on three neighbors

      j-1  j
   ┌─────┬─────┐
i-1│     │  ↓  │
   ├─────┼─────┤
i  │   → │ X   │  ← dp[i][j] calculated from three arrows
   └─────┴─────┘

Technique 2: Fill Order Visualization

Purpose: Show the order in which to fill DP table

2D DP Fill Order (row by row):
┌───┬───┬───┬───┐
│ 1 │ 2 │ 3 │ 4 │ ← Fill row 0 first
├───┼───┼───┼───┤
│ 5 │ 6 │ 7 │ 8 │ ← Then row 1
├───┼───┼───┼───┤
│ 9 │10 │11 │12 │ ← Finally row 2
└───┴───┴───┴───┘

Alternative: Diagonal Fill (for some problems)
┌───┬───┬───┬───┐
│ 1 │ 2 │ 4 │ 7 │
├───┼───┼───┼───┤
│   │ 3 │ 5 │ 8 │
├───┼───┼───┼───┤
│   │   │ 6 │ 9 │
├───┼───┼───┼───┤
│   │   │   │10 │
└───┴───┴───┴───┘

Technique 3: Decision Trees

Purpose: Show choices at each step

House Robber Decision Tree:
                  Start

              ┌─────┴─────┐
              │           │
           Rob H0      Skip H0
          (get 2)      (get 0)
              │           │
        ┌─────┴─────┐ ┌───┴───┐
        │           │ │       │
    Skip H1     Rob H1   Rob H1  Skip H1
    (total 2)   (invalid) (7)   (0)
        │           │     │       │
      ...         ...   ...     ...

Technique 4: State Space as Coordinate System

Purpose: Visualize 2D problems as navigation in plane

Edit Distance: Transform "CAT" → "DOG"
Y-axis: position in "DOG"
X-axis: position in "CAT"

    ""  C   A   T
   ┌───┬───┬───┬───┐
"" │ 0 │ 1 │ 2 │ 3 │ ← Delete all from "CAT"
   ├───┼───┼───┼───┤
D  │ 1 │ ? │ ? │ ? │
   ├───┼───┼───┼───┤
O  │ 2 │ ? │ ? │ ? │
   ├───┼───┼───┼───┤
G  │ 3 │ ? │ ? │ ? │
   └───┴───┴───┴───┘
   ↑ Insert all of "DOG"

Movement types:
→ : Delete from source
↓ : Insert into source
↘ : Replace/Match

🛠️ Practical Drawing Steps

Step 1: Set Up Axes and Labels

- X-axis: first parameter (usually i or string1)
- Y-axis: second parameter (usually j or string2)
- Label what each cell represents
- Mark base cases clearly

Step 2: Fill Base Cases First

- Usually first row and/or first column
- These should be obvious/given
- Mark them with different color/style

Step 3: Show One Cell Calculation in Detail

Pick a middle cell and show exactly how it's calculated:
- Draw arrows from dependencies
- Write out the formula
- Show the actual numbers

Step 4: Highlight the Path to Answer

- Mark the final answer cell
- If needed, trace back the optimal path
- Show how subproblems combine

🎯 Specific Examples

Interleaving String Visualization

s1 = "aab", s2 = "axy", s3 = "aaxaby"

DP Table: dp[i][j] = can form s3[0..i+j-1] with s1[0..i-1] and s2[0..j-1]

       ""  a   x   y
    ┌─────┬───┬───┬───┐
""  │  T  │ T │ F │ F │ ← s3="", "a", "ax", "axy"
    ├─────┼───┼───┼───┤
a   │  T  │ T │ T │ F │ ← s3="a", "aa", "aax", "aaxy"
    ├─────┼───┼───┼───┤
a   │  T  │ T │ T │ T │ ← s3="aa", "aaa", "aaax", "aaaxy"
    ├─────┼───┼───┼───┤
b   │  F  │ F │ F │ T │ ← s3="aab", "aaab", "aaaxb", "aaxyb"
    └─────┴───┴───┴───┘

T = True, F = False
Answer = dp[3][3] = T

Key insight: Each cell depends on:
- dp[i-1][j] if s1[i-1] == s3[i+j-1] (take from s1)
- dp[i][j-1] if s2[j-1] == s3[i+j-1] (take from s2)

Coin Change Visualization

Coins = [1, 3, 4], Amount = 6

DP Array: dp[i] = min coins to make amount i

Amount:  0   1   2   3   4   5   6
dp:     [0] [1] [2] [1] [1] [2] [2]

Calculation trace for dp[6]:
- Use coin 1: dp[6-1] + 1 = dp[5] + 1 = 2 + 1 = 3
- Use coin 3: dp[6-3] + 1 = dp[3] + 1 = 1 + 1 = 2  ← minimum
- Use coin 4: dp[6-4] + 1 = dp[2] + 1 = 2 + 1 = 3

Visual dependency:
dp[6] ← dp[5] (use coin 1)
      ← dp[3] (use coin 3) ★ optimal
      ← dp[2] (use coin 4)

💡 Pro Tips for Effective Visualization

1. Use Different Colors/Symbols

  • Base cases: Green/★
  • Impossible states: Red/X
  • Optimal path: Bold/highlighted
  • Current calculation: Blue/○

2. Start Small

  • Use 2x2 or 3x3 grids first
  • Understand pattern before scaling up
  • Hand-trace 2-3 examples

3. Digital Tools

  • Paper + pencil: Best for learning
  • Excel/Google Sheets: Great for DP tables
  • Graphviz: For decision trees
  • Whiteboard: For collaboration

4. Common Mistakes to Avoid

  • ❌ Drawing too large initially
  • ❌ Not labeling axes clearly
  • ❌ Skipping base cases
  • ❌ Not showing dependencies

5. Practice Exercise

Try visualizing these problems:

  1. Climbing Stairs: 1D array with arrows
  2. Unique Paths II: 2D grid with obstacles
  3. Word Break: Decision tree or 1D array

🎨 Template for Any DP Visualization

1. Problem: [Write the problem statement]

2. State Definition:
   dp[...] = [Be very specific]

3. Dimensions:
   - X-axis: [what it represents]
   - Y-axis: [what it represents] (if 2D)

4. Base Cases:
   [Mark these clearly in your drawing]

5. Recurrence:
   [Show with arrows and formula]

6. Fill Order:
   [Number the cells 1, 2, 3...]

7. Answer Location:
   [Mark where final answer is]

Remember: A good visualization is worth 1000 lines of code! 🚀

The goal is to make the abstract concrete. Once you can see the problem visually, the code becomes much easier to write and debug.

Software Engineer Interview Preparation