Professional Documents
Culture Documents
Chapter 3 Dynamic Programming: Divide-and-Conquer
Chapter 3 Dynamic Programming: Divide-and-Conquer
Chapter 3 Dynamic Programming: Divide-and-Conquer
Divide-and-Conquer
top-down approach
divide an instance of a problem into
smaller instances
both smaller instances are unrelated
e.g., in mergesort, instances are sorted
independently
not suitable for related smaller instances
e.g., Fibonacci F(n) = F(n-1)+F(n-2)
Computing F(4) and F(3) both require F(2)
KPShih@csie.tku.edu.tw 2
Dynamic Programming
bottom up approach
divide an instance into smaller instances
solve small instance first, store it, look it up later
not recomputing
using array (table) to store solution
e.g., Fibonacci F[n] = F[n-1]+F[n-2]
steps:
1. Establish a recursive property to a problem
2. Solve an instance of the problem in a bottom-
up fashion; solving smaller instance first.
KPShih@csie.tku.edu.tw 3
n 1 n 1
n , 0k n
k 1 k
k 1, k 0 or k n
KPShih@csie.tku.edu.tw 5
KPShih@csie.tku.edu.tw 6
3.1 The Binomial Coefficient
KPShih@csie.tku.edu.tw 7
KPShih@csie.tku.edu.tw 8
3.1 The Binomial Coefficient
KPShih@csie.tku.edu.tw 9
i= 0 1 2 k k+1 n
1 + 2 + 3+....+ k + (k+1) + (k+1) + ....+ (k+1)
KPShih@csie.tku.edu.tw 11
KPShih@csie.tku.edu.tw 13
KPShih@csie.tku.edu.tw 15
KPShih@csie.tku.edu.tw 16
3.2 Floyd’s Algorithm for Shortest Paths
KPShih@csie.tku.edu.tw 17
KPShih@csie.tku.edu.tw 18
3.2 Floyd’s Algorithm for Shortest Paths
compute D(k)[i][j] by using dynamic
programming
D(0) =W
D(n) =D
1. Establish a recursive property
compute D(k) from D(k-1)
2. Solve in a bottom-up fashion
D(0), D(1), D(2), ..., D(n)
W D
KPShih@csie.tku.edu.tw 19
KPShih@csie.tku.edu.tw 20
3.2 Floyd’s Algorithm for Shortest Paths
D(k)[i][j] = min(D(k-1)[i][j], Case 1
D(k-1)[i][k]+ D(k-1)[k][j])
Case 2
Step2:
create the sequence of arrays for
computing D(n) from D(0)
e.g., Example 3.3 pp. 106
KPShih@csie.tku.edu.tw 21
KPShih@csie.tku.edu.tw 22
3.2 Floyd’s Algorithm for Shortest Paths
Every-case time complexity of Algorithm
3.3, pp.107
Algorithm 3.4 (Floyd‘s algorithm for
shortest paths 2) for output a shortest
path
e.g., Fig. 3.5 pp.108
Algorithm 3.5 (Print Shortest Path)
e.g., in Fig. 3.5 if q=5, r=3
path [v5, v1, v4, v3]
KPShih@csie.tku.edu.tw 23
KPShih@csie.tku.edu.tw 24
3.2 Floyd’s Algorithm for Shortest Paths
KPShih@csie.tku.edu.tw 25
KPShih@csie.tku.edu.tw 26
3.3 Dynamic Programming
and Optimization Problems
KPShih@csie.tku.edu.tw 28
3.3 Dynamic Programming and
Optimization Problems
Principle of Optimality:
KPShih@csie.tku.edu.tw 29
KPShih@csie.tku.edu.tw 31
KPShih@csie.tku.edu.tw 32
3.4 Chained Matrix Multiplication
how to determine the optimal order in
A1 × A2 × ... × An ?
consider all possible order and take the
minimum (exponential-time)
tn: the number of different order in A1
× A2 × ... × An
A1×(A2×A3×...×An) → tn-1
(A1×A2×A3×...)×An → tn-1
tn ≥ tn-1+tn-1=2tn-1 ; t2 = 1
tn ≥2n-2
KPShih@csie.tku.edu.tw 33
KPShih@csie.tku.edu.tw 34
3.4 Chained Matrix Multiplication
Let d0 = # of rows in A1
dk = # of columns in Ak, 1<=k<=n
e.g., Figure 3.7
A1 × A2 × A3 × A4
d0xd1 d1xd2 d2xd3 d3xd4
Example 3.5 pp. 109
KPShih@csie.tku.edu.tw 35
KPShih@csie.tku.edu.tw 36
3.4 Chained Matrix Multiplication
consider multiplying six matrices (i.e.,
computing M[1][6])
1. A1×(A2×A3×A4×A5×A6)
2. (A1×A2)(A3×A4×A5×A6)
..............................
If (A1×A2)(A3×A4×A5×A6) is optimal,
both (A1×A2) and (A3×A4×A5×A6) are
optimal
M[1][6] = M[1][2]+M[3][6] + d0d2d6
M[1][6] = M[1][k]+M[k+1][6] +
d0dkd6
KPShih@csie.tku.edu.tw 37
M i i 0
KPShih@csie.tku.edu.tw 38
3.4 Chained Matrix Multiplication
how to compute array M[i][j] in steps ?
M[i][j] is computed all entries on row i
but left it and all entries in column j but
beneath it.
compute entries in the diagonal 0, 1,...,j
See Example 3.6 pp. 115
See Fig. 3.8 pp. 116
KPShih@csie.tku.edu.tw 39
KPShih@csie.tku.edu.tw 40
KPShih@csie.tku.edu.tw 41
KPShih@csie.tku.edu.tw 42
3.4 Chained Matrix Multiplication
See Fig. 3.9, pp. 119
KPShih@csie.tku.edu.tw 43
KPShih@csie.tku.edu.tw 45
KPShih@csie.tku.edu.tw 46
3.6 Traveling Salesperson Problem
See Example 3.11, pp. 133
Algorithm 3.11
Every-Case Time and Space Complexity
of Algorithm 3.11, pp. 135
KPShih@csie.tku.edu.tw 47
KPShih@csie.tku.edu.tw 48
The End