Académique Documents
Professionnel Documents
Culture Documents
Briana B. Morrison
With thanks to Dr. Hung
Topics
What
is Dynamic Programming
Binomial Coefficient
Floyds Algorithm
Chained Matrix Multiplication
Optimal Binary Search Tree
Traveling Salesperson
2
a top-down approach.
Many smaller instances are computed more
than once.
Dynamic
Dynamic Programming
Dynamic Programming
Dynamic Programming
Define
results
Access container versus recomputing results
Fibonacci
Dynamic Programming
Fibonacci
numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 24
Recurrence
?
9
for n 2
+
f(n-3)
f(n-2)
f(n-3)
f(n-4)
10
11
f(n-2) = f(n-3)+f(n-4)
f(n-1) = f(n-2)+f(n-3)
f(n) = f(n-1) + f(n-2)
12
f ib ( 4 )
f ib ( 3 )
f ib ( 2 )
f ib ( 1 )
f ib ( 2 )
f ib ( 1 )
f ib ( 1 )
f ib ( 1 )
f ib ( 2 )
f ib ( 0 )
f ib ( 1 )
f ib ( 0 )
f ib ( 0 )
13
f ib ( 4 )
f ib ( 3 )
5
f ib ( 3 )
f ib ( 2 )
f ib ( 1 )
f ib ( 2 )
4
f ib ( 2 )
f ib ( 1 )
f ib ( 1 )
f ib ( 0 )
f ib ( 1 )
f ib ( 0 )
3
f ib ( 1 )
f ib ( 0 )
14
fibDyn
N = 20
21,891
39
N = 40
331,160,281
79
15
Select
19
Principle of optimality
20
Binomial Coefficient
Binomial coefficient:
n
n!
k!( n k )!
k
for 0 k n
22
Binomial formula:
n 1
n 1
C
C
k 1
k
n
C
k
1
0k n
n
n
k 0 or k n (C or C )
0
n
23
24
Bottom-Up
Recursive
B[i] [j] =
property:
B[i 1] [j 1] + B[i 1][j] 0 < j < i
1
j = 0 or j = i
25
Pascals Triangle
0
3 1
6 4
i
n
3 4
1
B[i-1][j-1]+ B[i-1][j]
B[i][j]
26
Binomial Coefficient
k-1
...
k
n-1
n 1
k 1
n 1
k
n
C
k
27
Binomial Coefficient
ALGORITHM Binomial(n,k)
//Computes C(n, k) by the dynamic programming algorithm
//Input: A pair of nonnegative integers n k 0
//Output: The value of C(n ,k)
for i 0 to n do
for j 0 to min (i ,k) do
if j = 0 or j = k
C [i , j] 1
else C [i , j] C[i-1, j-1] + C[i-1, j]
return C [n, k]
k
i 1
A( n, k ) 1
i 1 j 1
i 1
i K 1
1 (i 1) k
i k 1 j 1
( k 1)k
k (n k ) (nk )
2
28
29
Shortest Path
Example
1
10 11
D - Floyds algorithm
31
Meanings
Floyds Algorithm
2
d
1
Computing D
D(0)
=W
Now compute D(1)
Then D(2)
Etc.
34
For k 1 to n do
For i 1 to n do
For j 1 to n do
W[i, j] min{W[i,j], W{i, k] + W[k, j]}
Return W
Efficiency = ?
(n)
35
2
0
3
0
2
1
2
4
0
3
0
36
Visualizations
http://www.ifors.ms.unimelb.edu.au/tutorial/path/#list
http://www1.math.luc.edu/~dhardy/java/alg/floyd.html
http://students.ceid.upatras.gr/%7Epapagel/project/kef5_7_2.htm
37
Strategy:
Breaking a problem into sub-problem
A1A2...Ak, Ak+1Ak+2An
Recursively define the value of an optimal solution
m[i,j] = 0 if i = j
m[i,j]= min{i<=k<j} (m[i,k]+m[k+1,j]+p i-1pkpj)
38
Example
Suppose
39
Example
B
(2 x 30)
C
(30 x 12)
D
(12 x 8)
Algorithm
int minmult (int n, const ind d[], index P[ ] [ ])
{
index i, j, k, diagonal;
int M[1..n][1..n];
for (i = 1; i <= n; i++)
M[i][i] = 0;
for (diagonal = 1; diagonal <= n-1; diagonal++)
for (i = 1; i <= n-diagonal; i++)
{
j = i + diagonal;
M[i] [j] = minimum(M[i][k] + M[k+1][j] + d[i-1]*d[k]*d[j]);
// minimun (i <= k <= j-1)
P[i] [j] = a value of k that gave the minimum;
}
return M[1][n];
}
41
tree
Minimum depth, balanced (if all keys have
same probability of being the search key)
What if probability is not all the same?
Multiply probability of accessing that key by
number of links to get to that key
42
Example
If p1 = 0.7
Key3
p2 = 0.2
p3 = 0.1
key2
(n3) Efficiency
key1
43
Traveling Salesperson
The
Why study?
Efficiency
The
Later
Well
47
Animations
http://www.pcug.org.au/~dakin/tsp.htm
http://www.ing.unlp.edu.ar/cetad/mos/TSPBI
B_home.html
48
Chapter Summary
Dynamic programming is similar to divide-andconquer.
Dynamic programming is a bottom-up approach.
Dynamic programming stores the results (small
instances) in the table and reuses it instead of
recomputing it.
Two steps in development of a dynamic
programming algorithm:
Establish a recursive property
Solve an instance of the problem in a bottom-up 49
50
Rules of Sudoku
Place a number (1-9) in each blank cell.
Each row (nine lines from left to right), column (also
nine lines from top to bottom) and 3x3 block bounded
by bold line (nine blocks) contains number from 1
through 9.
51
this:
http://www.ccs.neu.edu/jpt/sudoku/
52