2. Introduction
The World Series is a seven-game series that terminates as soon as
either team wins four games.
The World Series problem allowed us to see the power of dynamic
programming to compute a result iteratively from very simple rules,
filling a table of partial results that leads to a complete answer.
3. THE WORLD SERIES PROBLEM
Consider a tournament in which two teams A and B compete in no more than
2n -1 games, with the winner being the first team to win n times. We assume
that there are no tied games, that the results of each match are independent,
and that there is a constant chance p that team A will win and hence a constant
probability q = 1 -p that team B would win for every given match.
4. Implementation
Given that in a single contest between team A and team B, the
probability team A wins is p, (and the probability that team B wins is
1-p, so we are precluding ties), what is the probability in i+j games
played, that team A will win i games and team B will win j games. Let
this value be denoted by the function p(i,j).
5. Implementation
An alternative way to solve the problem involves dynamic
programming. In order to obtain the dynamic programming
solution, we must first develop a recursive formula for the function
p(i,j). In order for team A to have won i games and team B to have
won j games, before the last game, either A won i and B won j-1 OR
A won i-1 and B won j. Here is a recursive formula that captures that
reasoning:
p(i, j) = p(i, j - 1)*(1 - p) + p(i -1, j)* p
6. Now, instead of actually using recursion in our code to figure this out, we can
convert our algorithm into dynamic programming by creating a two-dimensional
array that stores the answers to all necessary recursive calls.
7. Algorithm (Code)
public static int WS(int i, int j, double p)
{
double[][] prob = new double[i+1][j+1];
prob[0][0] = 1;
// Fill in probabilities for team A sweeping. for (int Awin=1; Awin<=i; Awin++) prob[Awin][0] =
p*prob[Awin-1][0];
// Probabilities for team B sweeping for (int Bwin=1; Bwin<=j; Bwin++) prob[0][Bwin] = (1-
p)*prob[0][Bwin-1];
// Here's where we fill in the table, using the recursive
// formula.
for (int Awin=1; Awin<=i; Awin++)
for (int Bwin=1; Bwin<=j; Bwin++)
prob[Awin][Bwin] = p*prob[Awin-1][Bwin] + (1-p)*prob[Awin][Bwin-1];
// Here's our answer. return prob[Awin][Bwin]; }
8. The World Series
Example of Dynamic Programming
The teams A and B play in not more than 2n- 1 games. assume
that
There are no tied games
the probability of winning by team A is p
the team B wins with q = 1 – p.
P(i, j) is the probability that the team A will win if it gets i more
victories and the team B will win if it gets j more victories.
9. The World Series…
For example, before the first game off the series the probability that
team A will be the overall winner is P(n, n): both teams still need n
victories.
If team A has already won all the matches it needs, then it is of
course certain that they will win the series:
P(0, i) = 1, 1 ≤ i ≤ n.
Similarly, P(i, 0) = 0, 1 ≤ i ≤n.
Finally, since team A wins any given match with probability p and
loses it with probability q,P(i, j) = ?
10. The World Series…
P(i; j)=pP(i - 1; j) + qP(i; j-1); i≥1;j≥1
function P(i; j)
if i=0 then return 1
else if j = 0 then return 0
else return pP(i - 1; j) + qP(i; j-1)
Let T(k) be the time required to compute P(i; j) where k = i + j T(1) =
c
T(k) ≤2T(k; 1)+d; k > 1
11. The World Series…
T(k) ≤ 4T(k-2) + 2d+d; k > 2
...
≤2k-¹T(1) +(2k-2 +2k-3+...+2+ 1)d
= 2k-1c +(2k-1-1)d
= 2k(c/2+d/2) – d
→ T(k) € 0(2)
12.
13. The World Series…
Function P(i, j)
if i=0 then return 1
else if j = 0 then return 0
else return pP(i-1, j) + qP(i, j - 1)
14.
15. Example
Dodgers and Yankees are playing the World Series in which either team needs to win n games
first. (number of maximum game=2n-1)
- Suppose that each team has a 50% chance of winning any particular game. - Let P(i, j) be the
probability that if Dodgers needs i games to win, and Yankees needs j games, Dodgers will
eventually win the Series.
- Ex: P(2, 3) = 11/16
- Compute P(i, j) (0 <= i, j <= n) for an arbitrary n.
P(0,1)=1(Win), Yankees W
P(0,2)=1(Win), Yankees WW
…
P(1,0)=0(Lose), Yankees L
P(2,0)=0(Lose), Yankees LL
P(2,2)