0% found this document useful (0 votes)
114 views

Dynamic Programming For Blackjack

The document discusses dynamic programming and provides examples of how to apply it to problems. It focuses on text justification, which involves splitting a text into lines while minimizing ugliness. The document defines the subproblems as the badness of using words i through j for each line and notes that dynamic programming can help solve the problem of minimizing the sum of cubed badness values over all lines.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Dynamic Programming For Blackjack

The document discusses dynamic programming and provides examples of how to apply it to problems. It focuses on text justification, which involves splitting a text into lines while minimizing ugliness. The document defines the subproblems as the badness of using words i through j for each line and notes that dynamic programming can help solve the problem of minimizing the sum of cubed badness values over all lines.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Introduction Radboud University Nijmegen

Dynamic programming II

Alexandra Silva
[email protected]
http://www.cs.ru.nl/~alexandra

Institute for Computing and Information Sciences


Radboud University Nijmegen

7th October 2014

Alexandra 7th October 2014 Lesson 4 1 / 19


Introduction Radboud University Nijmegen
Recap

Message of last week


• Simple, powerful technique.
• Particularly suited for optimisation problems (max, min, etc).
• Two examples: Fibonacci and Shortest paths (Bellman-Ford).
• This week: DP in 5 steps; three more examples (text
justification, black jack, invariant sets).

Alexandra 7th October 2014 Lesson 4 2 / 19


Introduction Radboud University Nijmegen
Dynamic programming

DP = careful brute force

DP = guessing + memoize + recursion

DP = shortest path in a DAG

Total time = number of sub-problems × time per sub-problem

Cool thing: recursion is for free (constant time)

Alexandra 7th October 2014 Lesson 4 3 / 19


Introduction Radboud University Nijmegen
Dynamic programming in 5 steps

• Steps are not necessarily sequential.


• How to express a DP, how to invent one, how to explain one.

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.

Alexandra 7th October 2014 Lesson 4 4 / 19


Introduction Radboud University Nijmegen
Last week’s examples
Fibonnacci Shortest Path
1 f1 , . . . , fn δk (s, v ) for v ∈ V , 0 ≤ k ≤ |V |
n.o subp n |V |2

Alexandra 7th October 2014 Lesson 4 5 / 19


Introduction Radboud University Nijmegen
Last week’s examples
Fibonnacci Shortest Path
1 f1 , . . . , fn δk (s, v ) for v ∈ V , 0 ≤ k ≤ |V |
n.o subp n |V |2

2 nothing edge into v


n.o choices - in-degree(v) +1

Alexandra 7th October 2014 Lesson 4 5 / 19


Introduction Radboud University Nijmegen
Last week’s examples
Fibonnacci Shortest Path
1 f1 , . . . , fn δk (s, v ) for v ∈ V , 0 ≤ k ≤ |V |
n.o subp n |V |2

2 nothing edge into v


n.o choices - in-degree(v) +1
3 Fk = Fk−1 + Fk−2 δk (s, v ) = min{δk−1 (s, u) + w (u, v )

| (u, v ) ∈ E }
time per subp constant in-degree(v) +1

Alexandra 7th October 2014 Lesson 4 5 / 19


Introduction Radboud University Nijmegen
Last week’s examples
Fibonnacci Shortest Path
1 f1 , . . . , fn δk (s, v ) for v ∈ V , 0 ≤ k ≤ |V |
n.o subp n |V |2

2 nothing edge into v


n.o choices - in-degree(v) +1
3 Fk = Fk−1 + Fk−2 δk (s, v ) = min{δk−1 (s, u) + w (u, v )

| (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

Alexandra 7th October 2014 Lesson 4 5 / 19


Introduction Radboud University Nijmegen
Last week’s examples
Fibonnacci Shortest Path
1 f1 , . . . , fn δk (s, v ) for v ∈ V , 0 ≤ k ≤ |V |
n.o subp n |V |2

2 nothing edge into v


n.o choices - in-degree(v) +1
3 Fk = Fk−1 + Fk−2 δk (s, v ) = min{δk−1 (s, u) + w (u, v )

| (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

5 Fn δ|V |−1 (s, v )(no negative weight cycles!)


extra time constant O(|V |)
Alexandra 7th October 2014 Lesson 4 5 / 19
Introduction Radboud University Nijmegen
Text justification

Given some text (a string), split it into good lines.


Rules:
• We can only split between word boundaries.
• Write some text (with spaces in it), new line, more text, etc.
• Justified text (not ugly ;-)).

some words in one line


text and|{z}more text
ugly

Alexandra 7th October 2014 Lesson 4 6 / 19


Introduction Radboud University Nijmegen
Text justification

Given some text (a string), split it into good lines.


Rules:
• We can only split between word boundaries.
• Write some text (with spaces in it), new line, more text, etc.
• Justified text (not ugly ;-)).

some words in one line


text and|{z}more text
ugly

• MS word (older versions): greedy strategy, pack as many


words on the first line, etc.
• LaTeX: DP, what we will see here.

Alexandra 7th October 2014 Lesson 4 6 / 19


Introduction Radboud University Nijmegen
Text justification

• Text: list of words.


• We define badness(i,j) – ugliness measure: how bad it is to
use words i through j as a line.

Alexandra 7th October 2014 Lesson 4 7 / 19


Introduction Radboud University Nijmegen
Text justification

• Text: list of words.


• We define badness(i,j) – ugliness measure: how bad it is to
use words i through j as a line.
• Two options: words fit on a line or they do not fit.

Alexandra 7th October 2014 Lesson 4 7 / 19


Introduction Radboud University Nijmegen
Text justification

• Text: list of words.


• We define badness(i,j) – ugliness measure: how bad it is to
use words i through j as a line.
• Two options: words fit on a line or they do not fit.
• Sum of lengths plus spaces (minimised) larger than width of
page = bad (∞).
• Otherwise = (page width minus total width)3 . (why cubed?)

Alexandra 7th October 2014 Lesson 4 7 / 19


Introduction Radboud University Nijmegen
Text justification

• Text: list of words.


• We define badness(i,j) – ugliness measure: how bad it is to
use words i through j as a line.
• Two options: words fit on a line or they do not fit.
• Sum of lengths plus spaces (minimised) larger than width of
page = bad (∞).
• Otherwise = (page width minus total width)3 . (why cubed?)
• total and page width very close = difference small = cubed
value very small
• total and page width not close = difference large = cubed
value very large = discourages huge spaces

Alexandra 7th October 2014 Lesson 4 7 / 19


Introduction Radboud University Nijmegen
Text justification

• 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. . .

Alexandra 7th October 2014 Lesson 4 8 / 19


Introduction Radboud University Nijmegen
Text justification

• 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 ;-)

Alexandra 7th October 2014 Lesson 4 8 / 19


Introduction Radboud University Nijmegen
Text justification

• Define sub-problems (hard part).


• What is the brute-force strategy?
• Try all possibilities?

Alexandra 7th October 2014 Lesson 4 9 / 19


Introduction Radboud University Nijmegen
Text justification

• Define sub-problems (hard part).


• What is the brute-force strategy?
• Try all possibilities?

• 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.

Alexandra 7th October 2014 Lesson 4 9 / 19


Introduction Radboud University Nijmegen
Text justification

• Define sub-problems (hard part).


• What is the brute-force strategy?
• Try all possibilities?

• 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?

Alexandra 7th October 2014 Lesson 4 9 / 19


Introduction Radboud University Nijmegen
Text justification

• Define sub-problems (hard part).


• What is the brute-force strategy?
• Try all possibilities?

• 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?

Alexandra 7th October 2014 Lesson 4 9 / 19


Introduction Radboud University Nijmegen
Text justification

• Define sub-problems (hard part).


• What is the brute-force strategy?
• Try all possibilities?

• 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).

Alexandra 7th October 2014 Lesson 4 9 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . .

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!
• Originally: all words. After first guess: remaining words.
• Sub-problems: suffixes.
• How many?

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!
• Originally: all words. After first guess: remaining words.
• Sub-problems: suffixes.
• How many?
• Key observation: If we think for every word: in or out then 2n .
But in fact we do that for remaining words.
• First line: 100 words, remaining: n − 100; First line: 2000
words, remaining: n − 2000 → n possibilities.

Alexandra 7th October 2014 Lesson 4 10 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . .

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!
• Originally: all words. After first guess: remaining words.
• Sub-problems: suffixes.
• How many?

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

• Try all words to begin second line.


• After I know where the second line begins. . . I need to know
where the third begins!
• And then the fourth, and then . . .
• Sub-problems: after first guess problem of original type!
• Originally: all words. After first guess: remaining words.
• Sub-problems: suffixes.
• How many?
• Key observation: If we think for every word: in or out then 2n .
But in fact we do that for remaining words.
• First line: 100 words, remaining: n − 100; First line: 2000
words, remaining: n − 2000 → n possibilities.

Alexandra 7th October 2014 Lesson 4 11 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) =

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) = TJ(j) +

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) = TJ(j) + badness(i,j)

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) = TJ(j) + badness(i,j) for j = i + 1 . . . n + 1

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) = min{TJ(j) + badness(i,j) for j = i + 1 . . . n + 1 }
Time per subproblem =

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

1 subproblems : suffixes ; number: n.


Given some words (n − i) how do we justify the text? )
2 guess: where to start the second line; number: ≤ n − i ≈ O(n)
3 recurrrence: TJ(i) = ?
we have done i-1 lines
i some words and so
j some other words
And a lot of more stuff
TJ(i) = min{TJ(j) + badness(i,j) for j = i + 1 . . . n + 1 }
Time per subproblem = O(n).

Alexandra 7th October 2014 Lesson 4 12 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order:

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case:

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time =

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time = O(n2 )
5 Original problem:

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time = O(n2 )
5 Original problem: TJ(0).

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time = O(n2 )
5 Original problem: TJ(0).

• This does not return how to pack the words! Just gives you
the minimum badness.
• Similar to shortest path.

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time = O(n2 )
5 Original problem: TJ(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]] → . . .

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Text justification

4 Topological order: n, n − 1, . . . , 0 (Base case: TJ(n) = 0)


Total time = O(n2 )
5 Original problem: TJ(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).

Alexandra 7th October 2014 Lesson 4 13 / 19


Introduction Radboud University Nijmegen
Invariant Sets (TC628 Round 1 - Division II, Level Three)

Janusz is a young physicist. Recently he has been studying a dynamic


system. There are n elements in the system. We will label them 0
through n − 1. The set of all elements will be denoted E . Janusz models
the changes in his system using a function that operates on elements of
E . You are given this function as a list of integers f with n elements,
each between 0 and n − 1, inclusive. The list f describes Janusz’s
function as follows: For each valid i, if the function is given the input i,
its output will be f [i]. For Janusz, invariant subsets of E have a special
significance. A subset S of the set E is called invariant (with respect to
f ) if it has the following property: for any x ∈ S, f [x] is also in S. For
example, the entire set E is invariant (for any f ). The empty set is
always invariant as well.
Given is the list f , compute and return the number of invariant subsets of
E (including E itself and the empty set).

Alexandra 7th October 2014 Lesson 4 14 / 19


Introduction Radboud University Nijmegen
Invariant Sets
{1, 0, 0, 0}
Returns: 5
The invariant sets are: {}, {0, 1}, {0, 1, 2}, {0, 1, 3}, {0, 1, 2, 3}.

{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.

{12, 10, 0, 4, 0, 6, 3, 8, 9, 14, 1, 5, 6, 12, 5}


Returns: 90
Alexandra 7th October 2014 Lesson 4 15 / 19
Introduction Radboud University Nijmegen
Blackjack

• Dealer+player.
• One card of the dealer is hidden.
• Player decides where to stander hit.

Alexandra 7th October 2014 Lesson 4 16 / 19


Introduction Radboud University Nijmegen
Blackjack

• Perfect-information blackjack (inside man!).


• Knowledge of the whole deck ahead of time.
• At each stage, decide whether to hit or stand.
• Similar strategy to text justification.

Alexandra 7th October 2014 Lesson 4 17 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems:
2 Guess:

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems:
2 Guess: how many hits?

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems:

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices:

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) =

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} +

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j)

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j) | hits = 0, · · · , n

j = i+4+hits+hitsdealer }

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j) | hits = 0, · · · , n

j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order:

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j) | hits = 0, · · · , n

j = i+4+hits+hitsdealer }
Time per sub-problem: O(n2 )
4 Topological order: for i in reversed(range(n))

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j) | hits = 0, · · · , 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 )

Alexandra 7th October 2014 Lesson 4 18 / 19


Introduction Radboud University Nijmegen
Blackjack
Parameters:
• Deck: c0 , c1 , . . . cn−1
• 1 player vs dealer (solution for two players?); 1 euro bet/hand
1 subproblems: suffixes: cards left with
2 Guess: how many hits?
Number of subproblems: n (suffixes! not subsets!).
Number of choices: at most n
3 Recurrence:
BJ(i) = max{outcome ∈ {−1, 0, 1} + BJ(j) | hits = 0, · · · , 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 )
5 Solution: BJ(0)
Alexandra 7th October 2014 Lesson 4 18 / 19
Introduction Radboud University Nijmegen
Blackjack
Recurrence (detailed):

Total time = O(n2 )


Alexandra 7th October 2014 Lesson 4 19 / 19

You might also like