Week 13 - Dynamic Programming
Week 13 - Dynamic Programming
DESIGN AND
ANALYSIS OF
ALGORITHMS
WEEK 13
Dynamic
Programming
Dynamic Programming (DP)
• Fibonacci numbers
• Shortest path problems
• Text Justification
• Blackjack
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Dynamic Programming (DP)
1. define subproblems
2. guess (part of solution)
3. relate subproblem solutions
4. recurse & memoize
5. solve original problem
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Fibonacci Numbers
𝐹1 = 𝐹2 = 1
• ቊ
𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Fibonacci Numbers
• fib(n) = if n≤2 : f = 1
else f = fib(n-1) + fib(n-2)
return f
• Exponential
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Fibonacci Numbers
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Fibonacci Numbers
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
memo = {}
fib(n)
if n in memo: return memo[n]
if n ≤2 : f=1
else f=fib(n-1) + fib(n-2)
memo[n] = f
return f
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
𝐹𝑛
𝐹𝑛−1 𝐹𝑛−2
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
𝐹𝑛
𝐹𝑛−1 𝐹𝑛−2
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
𝐹𝑛
𝐹𝑛−1 𝐹𝑛−2
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
𝐹𝑛
𝐹𝑛−1 𝐹𝑛−2
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Memoized DP Algorithm
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Dynamic Programming
• memoize (remember)
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Bottom-up DP Algorithm
fib = {}
for k in range(1,n+1)
if k≤2 : f=1
else f=fib[k-1] + fib[k-2]
fib[k] = f
return fib[n]
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Bottom-up DP Algorithm
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Fibonacci Numbers
1. define subproblems
– fib(k) for 𝑘 = 1, … , 𝑛 | number of subproblems is 𝑛
2. guess (part of solution)
– nothing | number of choices is 1
3. relate subproblem solutions
– recurrence | fib(k) = fib(k-1) + fib(k-2)
Θ(1)
4. recurse & memorize
– topological order | for 𝑘 = 1, … , 𝑛
check subprob recurrence is acyclic
5. solve original problem
– fib(n)
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Shortest Paths
• 𝛿 𝑠, 𝑣 ∀𝑣
• recursive formulation
𝛿 𝑠, 𝑣 = min 𝛿 𝑠, 𝑢 + 𝑤 𝑢, 𝑣 𝑢, 𝑣 𝜖𝐸}
s u v
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Shortest Paths
• Guessing
• What is the last edge? Maybe 𝑢, 𝑣 ?
• To find best guess, try all edges going 𝑣 and use best
s u v
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Shortest Paths
• Guessing
• What is the last edge? Maybe 𝑢, 𝑣 ?
• To find best guess, try all edges going 𝑣 and use best
• key: small (polynomial) number of possible guesses per
subproblem — typically this dominates time/subproblem
s u v
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Shortest Paths
• The drawback:
– Takes infinite time if the graph is cyclic
𝛿 𝑠, 𝑣 a
𝛿 𝑠, 𝑎
s v
𝛿 𝑠, 𝑏
𝛿 𝑠, 𝑠 𝛿 𝑠, 𝑣 b
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Shortest Paths
• How to solve?
• Convert cyclic graph to acyclic graph
• We will not cover this!
time
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Text Justification
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Blackjack
• The goal of blackjack is to beat the dealer's hand without going over 21.
• Face cards are worth 10. Aces are worth 1 or 11, whichever makes a better hand.
• Each player starts with two cards, one of the dealer's cards is hidden until the end.
• To 'Hit' is to ask for another card. To 'Stand' is to hold your total and end your turn.
• If you go over 21 you bust, and the dealer wins regardless of the dealer's hand.
• If you are dealt 21 from the start (Ace & 10), you got a blackjack.
• Blackjack usually means you win 1.5 the amount of your bet. Depends on the casino.
• Dealer will hit until his/her cards total 17 or higher.
• Doubling is like a hit, only the bet is doubled, and you only get one more card.
• Split can be done when you have two of the same card - the pair is split into two hands.
• Splitting also doubles the bet, because each new hand is worth the original bet.
• You can only double/split on the first move, or first move of a hand created by a split.
• You cannot play on two aces after they are split.
• You can double on a hand resulting from a split, tripling or quadrupling you bet.
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Blackjack
• Perfect-information Blackjack
• deck = 𝑐0 , 𝑐1 , … , 𝑐𝑛−1
• the sequence of cards in the deck is known
• 1 player vs dealer
• $1 bet per game
• there is no split, no doubling
• maximize the earning
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Blackjack
1. subproblems: suffix 𝑐𝑖
– number of subproblems: |n
2. guess: how many hits?
– number of choices | ≤n
3. recurrence
– Blackjack(i) = max (outcome ∈ {−1,0,1} | O(n)
+ Blackjack(j)
for # hits in range (0,n) | O(n)
– j = i+4+#hits+#dealer hits
– time per subproblem O(𝑛2 )
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Blackjack
4. topological order:
– for i in reversed(range(n)) |Θ(𝑛3 )
5. Solution:
– Blackjack(o)
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi
Blackjack
outcomes
1
1
0
-1
valid plays
Ogr. Gor. Dr. Fadi YILMAZ – FALL 2020 Mühendislik ve Doğa Bilimleri Fakültesi