Dynamic Programming For Blackjack
Dynamic Programming For Blackjack
Dynamic programming II
Alexandra Silva
[email protected]
http://www.cs.ru.nl/~alexandra
1 Define sub-problems.
2 Guess (part of solution).
3 Relate sub-problem solutions (recurrence).
4 Build an algorithm: memoization (or bottom up build the DP
table).
5 Solve original problem.
| (u, v ) ∈ E }
time per subp constant in-degree(v) +1
| (u, v ) ∈ E }
time per subp constant in-degree(v) +1
4 for k = 1, . . . , n for k = 0, 1, . . . , |V |
for v ∈ V
topological DAG
| (u, v ) ∈ E }
time per subp constant in-degree(v) +1
4 for k = 1, . . . , n for k = 0, 1, . . . , |V |
for v ∈ V
topological DAG
• Greedy algorithm: make the first line very good but might
misbalance all the rest.
• Overall goal: minimise sum of badness of all lines.
• Sum of cubes: looks hard. . .
• Greedy algorithm: make the first line very good but might
misbalance all the rest.
• Overall goal: minimise sum of badness of all lines.
• Sum of cubes: looks hard. . . DP helps! You don’t need to
think ;-)
• Try all partitions: it could fit on one line, maybe two (but
then where do I split?)
• For every word: start a new line or not?
• There are n words: 2n different splittings.
• Try all partitions: it could fit on one line, maybe two (but
then where do I split?)
• For every word: start a new line or not?
• There are n words: 2n different splittings.
• But where do the lines begin?
• Try all partitions: it could fit on one line, maybe two (but
then where do I split?)
• For every word: start a new line or not?
• There are n words: 2n different splittings.
• But where do the lines begin?
• What can I (first) guess when given the huge string of words?
• Try all partitions: it could fit on one line, maybe two (but
then where do I split?)
• For every word: start a new line or not?
• There are n words: 2n different splittings.
• But where do the lines begin?
• What can I (first) guess when given the huge string of words?
• Guess how long first line is (=where the second line begins).
4 Topological order:
• This does not return how to pack the words! Just gives you
the minimum badness.
• Similar to shortest path.
• This does not return how to pack the words! Just gives you
the minimum badness.
• Similar to shortest path.
• Parent pointer: remember which guess was best.
parent[i] = argmin(. . .) = j value
To reconstruct where the lines break:
0 → parent[0] → parent[parent[0]] → . . .
• This does not return how to pack the words! Just gives you
the minimum badness.
• Similar to shortest path.
• Parent pointer: remember which guess was best.
parent[i] = argmin(. . .) = j value
To reconstruct where the lines break:
0 → parent[0] → parent[parent[0]] → . . .
• Total automatic technique (like memoisation).
{1, 2, 0}
Returns: 2
There are only 2 invariants sets: {} and {0, 1, 2}.
{0, 0, 1, 2}
Returns: 5
The invariant sets are: {}, {0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}.
{0, 1, 2, 3, 4, 5}
Returns: 64; Every set is invariant when f(x)=x for all x.
• Dealer+player.
• One card of the dealer is hidden.
• Player decides where to stander hit.
j = i+4+hits+hitsdealer }
j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order:
j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order: for i in reversed(range(n))
j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order: for i in reversed(range(n))
Total time: O(n3 )
j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order: for i in reversed(range(n))
Total time: O(n3 )
5 Solution: BJ(0)
Alexandra 7th October 2014 Lesson 4 18 / 19
Introduction Radboud University Nijmegen
Blackjack
Recurrence (detailed):