Week 14
Week 14
CENG 303
Design and Analysis
of Algorithms
Introduction
𝐹1 = 𝐹2 = 1
• ቊ
𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2
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
Memoized DP Algorithm
𝐹𝑛
𝐹𝑛−1
𝐹𝑛−2
𝐹𝑛
𝐹𝑛−1
𝐹𝑛−2
𝐹𝑛
𝐹𝑛−1
𝐹𝑛−2
𝐹𝑛
𝐹𝑛−1
𝐹𝑛−2
• memorize (remember)
• re-use solutions to subproblems
• total time = the number of subproblems X time for per subproblem
• We do not need to count memoized recursions
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]
Bottom-up DP Algorithm
1.define subproblems
2.guess (part of solution)
3.relate subproblem solutions
4.recurse & memoize
5.solve original problem
Fibonacci Numbers
1.define subproblems
fib(k) for 𝑘 = 1, … , 𝑛 | number of subproblems is 𝑛
• Guessing
s v
Shortest Paths
• Guessing
• What is the last edge? Maybe (𝑢, 𝑣)?
• To find best guess, try all edges going 𝑣 and use best
s u v
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
Shortest Paths
𝛿 𝑠, 𝑣 ∀𝑣
• recursive formulation
𝛿 𝑠, 𝑣 = min 𝛿 𝑠, 𝑢 + 𝑤 𝑢, 𝑣 𝑢, 𝑣 𝜖𝐸}
the base case: 𝛿 𝑠, 𝑠 = 0
s u v
Shortest Paths
The drawback:
Takes infinite time if the graph is cyclic
𝛿 𝑠, 𝑣
a
𝛿 𝑠, 𝑎
s v
𝛿 𝑠, 𝑏
b
𝛿 𝑠, 𝑠 𝛿 𝑠, 𝑣
Shortest Paths
• How to solve?
• Convert cyclic graph to acyclic graph
time
Shortest Paths
time
Shortest Paths
4. topological order
• 𝑇𝐽(𝑛) = 0
• 𝑖 = 𝑛, 𝑛 − 1, 𝑛 − 2, … , 0
• total time = Θ(𝑛2 )
5. Original problem: DP(0)
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
• 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.
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
BlackJack
1. subproblems: suffix 𝑐𝑖
• number of subproblems: |𝑛
2. guess: how many hits?
• number of choices |≤ 𝑛
3. recurrence
• Blackjack(i) = max (outcome ∈ {−1,0,1} | 𝑂(𝑛)
+ Blackjack(j)
for # hits in range (0,n) | 𝑂(𝑛)
• 𝑗 = 𝑖 + 4 + #hits + #𝑑𝑒𝑎𝑙𝑒𝑟 hits
• time per subproblem O(𝑛2 )
BlackJack
4. topological order:
for i in reversed(range(n))
4. Solution:
Blackjack(o)
BlackJack
outcomes
1
1
0
-1
valid plays