ENع
→States and transitions
الدرس 39 · Dynamic programming and backtracking

Defining states and transitions

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

Every dynamic programming solution has two parts. A state is a small question with a stored answer. A transition builds one state's answer from smaller ones. This lesson takes a row of coins (2, 7, 9, 3, 1): take any coins, but never two that sit side by side, and find the largest total. You name the state, write the transition, fill the table in a safe order, and walk back to recover the coins you took. The Software lesson "Dynamic programming" covers memoisation and tables. This lesson is about how to choose what goes in them.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 39 · تفاعلي
01 / 09

Coins, never two neighbours

Five coins in a row: 2, 7, 9, 3, 1. Take any you like, but never two side by side. What is the largest total? Checking every pick works but explodes: 13 legal picks here, and about 1.6 times more for each extra coin.

coins v = [2, 7, 9, 3, 1]
rule: never take two neighbours
subsets of 5 coins: 2^5 = 32
legal picks (no two neighbours): 13
the count for n coins is F(n + 2), a Fibonacci number:
  n = 5, 10, 20, 30  →  13, 144, 17711, 2178309
it grows about 1.618 times per extra coin
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Coins, never two neighbours

    Five coins in a row: 2, 7, 9, 3, 1. Take any you like, but never two side by side. What is the largest total? Checking every pick works but explodes: 13 legal picks here, and about 1.6 times more for each extra coin.

    coins v = [2, 7, 9, 3, 1]
    rule: never take two neighbours
    subsets of 5 coins: 2^5 = 32
    legal picks (no two neighbours): 13
    the count for n coins is F(n + 2), a Fibonacci number:
      n = 5, 10, 20, 30  →  13, 144, 17711, 2178309
    it grows about 1.618 times per extra coin
  2. Name the state

    Dynamic programming starts by naming a state: a small question whose answer we store. Here: best(i) = the largest total using only the first i coins. One cell per i, from best(0) to best(5). The answer we want is best(5).

    state:   best(i) = largest total from coins 1..i
             with no two neighbours taken
    states:  i = 0, 1, ..., 5  (6 cells)
    answer:  best(5)
    
    in Python: best = [0] * (n + 1), coin i is v[i-1]
    MIT 6.006 calls this step S: subproblem definition
  3. Base cases need no choice

    Some states are answered straight away. best(0) = 0: no coins, nothing to take. best(1) = 2: with one coin, take it. These are the base cases; every other cell is built from them.

    lines 3 and 4:
      best[0] = 0          no coins
      best[1] = v[0] = 2   one coin: take it
    
    best = [0, 2, ?, ?, ?, ?]
    MIT 6.006 step B: base cases
  4. The transition: skip or take

    A transition builds one state from smaller ones. At coin i: skip it and keep best(i−1), or take it, which bans coin i−1, giving coin i + best(i−2). Keep the larger. For i = 2: max(2, 7) = 7.

    best(i) = max(best(i−1), v[i−1] + best(i−2))
    
    i = 2, coin 2 = v[1] = 7
      skip = best[1]            = 2
      take = best[0] + v[1] = 0 + 7 = 7
      best[2] = max(2, 7) = 7   (take wins)
    
    best = [0, 2, 7, ?, ?, ?]
    MIT 6.006 step R: relate subproblems
  5. best(3): take wins

    Coin 3 is worth 9. Skip it: best(2) = 7. Take it: 9 + best(1) = 11. Take wins, so best(3) = 11. Each cell reads only two cells to its left, and both are already filled.

    i = 3, coin 3 = v[2] = 9
      skip = best[2]            = 7
      take = best[1] + v[2] = 2 + 9 = 11
      best[3] = max(7, 11) = 11   (take wins)
    
    best = [0, 2, 7, 11, ?, ?]
  6. best(4): skip wins

    Coin 4 is worth 3. Take it: 3 + best(2) = 10. Skip it: best(3) = 11. Skip wins, so best(4) = 11. The state does not remember which coins made 11, and it does not need to.

    i = 4, coin 4 = v[3] = 3
      skip = best[3]            = 11
      take = best[2] + v[3] = 7 + 3 = 10
      best[4] = max(11, 10) = 11   (skip wins)
    
    best = [0, 2, 7, 11, 11, ?]
  7. best(5) is the answer

    Coin 5 is worth 1. Skip: best(4) = 11. Take: 1 + best(3) = 12. So best(5) = 12: the most you can take from all five coins without two neighbours. return best[n] hands it back.

    i = 5, coin 5 = v[4] = 1
      skip = best[4]            = 11
      take = best[3] + v[4] = 11 + 1 = 12
      best[5] = max(11, 12) = 12   (take wins)
    
    best = [0, 2, 7, 11, 11, 12]
    
    pick([2, 7, 9, 3, 1]) returns 12
    MIT 6.006 step O: original problem = best(n)
  8. Order and cost

    Every state needs only smaller states, so filling left to right never reads an empty cell: a topological order. Cost = states × work per transition: 6 cells, one max each. O(n) time, not an exponential search.

    dependencies: best(i) ← best(i−1), best(i−2)
    all point left, so i = 0, 1, 2, ... is a valid order
    
    time  = (number of states) × (work per transition)
          = (n + 1) × O(1) = O(n)     here 6 cells
    space = n + 1 cells (two would do: only i−1, i−2 are read)
    
    MIT 6.006 steps T and T: topological order, time
  9. Walk back to the picks

    The table stores totals, not picks, but we can walk back. best(5) ≠ best(4), so coin 5 was taken: jump to best(3). 11 ≠ 7: coin 3 taken, jump to best(1). Coin 1 taken. 2 + 9 + 1 = 12.

    i, picks = n, []
    while i >= 1:
        if best[i] != best[i-1]:
            picks.append(i); i -= 2
        else:
            i -= 1
    
    visits i = 5, 3, 1; picks coins 5, 3, 1
    values 2 + 9 + 1 = 12

اختبار يتاح بعد الخطوة الأخيرة.