Lecture 18

advertisement
Recurrence Relations and
Difference Tables !
CSCI 2824, Fall 2012!
!
!
Assignments
• Game Theory reading has been posted
• Problem Set 3 is due Thursday November 8 (hard copy,
in class).
• Challenge Problem 3 due today!
!
!
!
!!
The Hockey Stick Identity
1!
1
1
1
1
1
1
1
1!
2
3
4
5
6
7
1!
3
6
10
15
21
1!
4
10
20
35
1!
5 1!
15 6 1!
35 21 7 1!
!
C(n,2) = C(n-1, 1) + C(n-2, 1) +… C(1, 1)
The Hockey Stick Identity
1!
1
1
1
1
1
1
1
1!
2
3
4
5
6
7
1!
3
6
10
15
21
1!
4
10
20
35
1!
5 1!
15 6 1!
35 21 7 1!
!
C(n,3) = C(n-1, 2) + C(n-2, 2) +… C(2, 2)!
C(n,m) = C(n-1, m-1) + C(n-2, m-1) + … C(m, m)
A General Difference Table
Define Sequence(n)
IF n = 0
THEN 2
ELSE 3 + Sequence (n – 1)
n
0
1
2
3
4
5
6
S(n)
2
5
8
11
14
17
20
Δ(n)
3
3
3
3
3
3
3
n
0
1
2
3
4
S(n)
6
11
19
30
44
Δ(n)
5
8
11
14
Δ2(n)
3
3
3
Define Sequence (n)
IF n = 0 THEN 6
ELSE Sequence (n-1) + SeqDiff1 (n-1)
Define SeqDiff1 (n)
IF n = 0
THEN 5
ELSE 3 + SeqDiff1 (n -1 )
Suppose the first differences are
constant…
n
0
1
2
3
4
5
6
S(n)
2
3
4
5
6
7
8
Δ(n)
1
1
1
1
1
1
1
S(n) = 2 + n A first-order polynomial
Suppose the second differences
are constant…
n
0
0
1
1
2
2
3
3
4
4
5
5
6
6
S(n)
S(n)
Δ(n)
Δ(n)
2
3
1
2
3
5
1
3
4
8
1
4
5
6
7
8
Δ2(n)
1
1
1
12
1
S(n) = 3 + Σ0 <= i <= n-1 2 + i
= 3 + 2n + (n (n-1))/2
= n2/2 + 3n/2 + 3
A second order polynomial
17
1
23
1
30
1
5
6
7
8
1
1
1
1
Suppose the third differences are
constant…
n
0
1
2
3
4
5
6
S(n)
4
7
12
20
32
49
72
Δ(n)
3
5
8
12
17
23
30
Δ2(n)
2
3
4
5
6
7
8
Δ3(n)
1
1
1
1
1
1
1
S(n) = 4 + Σ0 <= i <= n-1 3 + 2i + C(i,2)
= 4 + 3n + 2n(n-1)/2 + C(n, 3)
= 4 + 3n + 2n(n-1)/2 + n(n-1)(n-2)/6
A third order polynomial
n
0
1
2
3
4
5
6
S(n)
5
9
16
28
48
80
129
Δ(n)
4
7
12
20
32
49
72
Δ2(n)
3
5
8
12
17
23
30
Δ3(n)
2
3
4
5
6
7
8
Δ4(n)
1
1
1
1
1
1
1
S(n) = 5 + Σ0 <= i <= n-1 4 + 3i + 2C(i,2) + C(i,3)
= 5 + 4n + 3n(n-1)/2 + 2C(n, 3) + C(n,4)
= 5 + 4n + 3n(n-1)/2 + 2n(n-1)(n-2)/6 + n(n-1)(n-2)(n-3)/24
A fourth order polynomial
n
0
1
2
3
4
5
6
S(n)
e
e+dn+cC(n,2) +
bC(n,3)+aC(n,
4)
Δ(n)
d
d+cn+bC(n,
2)+aC(n,3)
Δ2(n)
c
c + bn + a(n
(n-1))/2
Δ3(n)
b
b+a or
b+an
b+2a
b+3a
b+4a
b+5a
b+6a
Δ4(n)
a
a
a
a
a
a
a
S(n) = e + Σ0 <= i <= n-1 d + ci + bC(i,2) + aC(i,3)
= e + dn + cn(n-1)/2 + bC(n, 3) + a(n,4)
= e + dn + cn(n-1)/2 + bn(n-1)(n-2)/6 + an(n-1)(n-2)(n-3)/24
So, what’s the moral of all this?
•  If you make a difference table from an nth-order
polynomial, you’ll find that the nth row of the table
(starting the count from a zero-th top row) will be constant.
•  If you want to make a polynomial function starting from a
difference table, you have to specify the 0th column (a, b, c,
d, …) and you have to specify which row you choose to be
constant. (In our previous case, it was the 4th row, starting
our count from 0.)
•  If the nth row is constant, you have just created a
representation for an nth-order polynomial.
Some general recurrence patterns
•  T(n) = aT(n-1) + b
•  Exponential function
•  T(n) = T(n-1) + an
•  Polynomial function (order 2)
•  T(n) = T(n-1) + an2 + b
•  Polynomial function (order 3)
•  T(n) = T(n/2) + a
•  Logarithmic function
Recurrences Without Closed
Forms
•  The partition problem: How many ways are there
of partitioning a number n into k positive
numbers?
•  Let’s try 6 as an example, with 3 whole numbers:
4,1,1
3,2,1 2,2,2
How to think about this problem?
•  Suppose we want to partition the number 12
into 5 positive pieces. (We’ll call this
Partition(12, 5).) •  Let’s first assume that the smallest piece
has size 1. In that case, we need to partition
the remaining 11 into 4 pieces, so we
compute Partition (11, 4). But what if the smallest piece is > 1?
•  In that case, we know that each of our 5 pieces
already has (at least 1) out of our starting 12. So
our job is now to partition the remaining 7 among
5 pieces: Partition(7, 5).
•  In summary:
Partition (12, 5) = Partition(11,4) + Partition(7,5)
And in general:
Partition (n, k) = Partition(n-1, k-1) +
Partition (n-k, k) Let’s express this as a program:
Define Partition (n, k)
IF n < k THEN 0
IF n = k THEN 1
IF k = 1 THEN 1
ELSE Partition (n-1, k-1) + Partition(n-k, k)
Partition (8, 5)
= Partition (7, 4) + Partition (3, 5)
= Partition (6, 3) + Partition (3, 4)
= Partition (5, 2) + Partition (3, 3)
= Partition (4, 1) + Partition (3, 2) + 1
= 1 + Partition(2, 1) + Partition(1, 2) + 1
= 3
1, 1, 1, 1, 4
1, 1, 1, 2, 3
1, 1, 2, 2, 2
The “Change” Problem
•  Suppose you have to provide $3.28 in
change, with only pennies, nickels, dimes,
and quarters. How many possible ways are
there of doing this?
Here’s our recurrence
Change (328, 25) means “Number of ways of making change
when the highest coin is a quarter.”
So Change (328, 25)
= Change (303, 25) we use a quarter… + Change (328, 10) or we don’t!
The base cases…
•  Change (amount, highcoin) = 0 if amount < 0
= 1 if amount = 0
•  Change (amount, highcoin)
= Change (amount, nextcoin) if amount < highcoin
Example: Change (8, 10) = Change (8, 5)
The base cases…
•  Change (amount, 1)
= 1
When you’ve only got pennies, there’s only one
possible recipe for making change in any
amount
Bonnie’s Deal
Here is the deal that we are offering both you
and Clyde:
•  If you confess, and Clyde doesn’t, you’ll go free
and he gets 20 years. On the other hand, if you
don’t confess, and he does, then he goes free, and
you get 20 years.
•  If you both confess, you both get 10 years in
prison.
•  If you both clam up, we’ll convict you both on a
lesser charge (endangering corn), and you both get
1 year. Clyde
Clam up
Clam up
Stool Pigeon
1 year
1 year
20 years
0 years
0 years
20 years
10 years
10 years
Bonnie
Stool Pigeon
Prisoner’s Dilemma: The Formal
Conditions
DC > CC > DD > CD
CC > (DC + CD)/2
Blue
Cooperate
Cooperate
Defect
3
3
0
5
5
0
1
1
Red
Defect
Game Theory: Basic Taxonomy
• 
• 
• 
• 
• 
Zero- vs. non-zero sum
Two- vs. N-person games
Finite vs. infinite number of choices
Iterated vs. non-iterated games
Games of perfect information
Two-Person Zero Sum Games
•  The notion of a dominant choice
•  The solution of a zero-sum game
–  The value of a game
–  The minimax theorem
•  Pure vs. Mixed strategies
Blue
A
A
5
B
4
B
3
Red
1
Worst I
can do
Blue
Red
Worst I
can do
4
5
9
3
8
4
3
7
7
6
8
9
7
2
4
6
Scissors
Paper
Stone
Scissors
0
1
-1
Paper
-1
0
1
1
-1
Stone
0
The Minimax Theorem in Game
Theory
•  Applies to zero-sum, two-person finite games.
•  The minimax theorem says that in such a game, there is a
value V for the game (the same value V for both players).
Given an optimal strategy (possibly a mixed strategy),
each player can be assured (on average) of obtaining at
least V for the game regardless of what the other player
does.
•  What this means, essentially, is that both players can
examine such a matrix and determine beforehand (and
regardless of the other player s plan) what they need to do
to ensure receiving an average of V for the game.
Download