Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Dynamic Programming
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by or formulated as recurrences
with overlapping subinstances
Invented by American mathematician Richard Bellman in the
1950s to solve optimization problems and
Programming here means planning
Main idea:
- set up a recurrence relating a solution to a larger instance
to solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-1
F(n-1)
F(n-2)
+
F(n-3)
F(n-2)
F(n-3)
F(n-4)
...
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-2
(cont.)
Computing the nth Fibonacci number using bottom-up iteration and
recording results:
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
0
Efficiency:
- time
- space
. . .
n
n
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-3
Examples of DP algorithms
Computing a binomial coefficient
Warshalls algorithm for transitive closure
Floyds algorithm for all-pairs shortest paths
Some instances of difficult discrete optimization problems:
- traveling salesman
- knapsack
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-4
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-5
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-6
and analysis
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-7
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-8
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-9
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-10
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-11
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-12
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-13
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-14
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-15
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-16
3 6
D(2)
2
=
D(0)
0
2
9
6
0
7
3
5
0
9
1
0
0
2
0
7
D(3)
1
0
0
2
9
6
10
0
7
16
D(1)
3
5
0
9
4
6
1
0
0
2
0
7
D(4)
3
5
0
9
1
0
0
2
7
6
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
10
0
7
16
3
5
0
9
4
6
1
0
8-17
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-18
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-19
Problem
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-20
Given n items of
integer weights: w1 w2 wn
values:
v1 v2 vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
Knapsack Problem by DP
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-21
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-22
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-23
w2 = 1, v2= 10
0 10 12 22 22 22
w3 = 3, v3= 20
0 10 12 22 30 32
w4 = 2, v4= 15 4
?
0 10 15 25 30 37
12
12 12 12
Backtracing
finds the actual
optimal subset,
i.e. solution.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-24
Top-down approach solves only required instances but more than once
Bottom-up approach- solves all the instances exactly once.
Algorithm MFKnapsack( i,j)
//Input: nonnegative integer i indicating the number first I items and j
//indicating knapsacks capacity
//Output: The value of an optimal feasible subset of the first i items.
if V[i,j] < 0
if j < Weights[i]
valueMFKnapsack(i-1,j)
else
valuemax(MFKnapsack(i-1,j),Values[i] + MFKnapsack(i-1,j- Weights[i]))
V[i,j]value
return V[i,j]
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-25
i/j
12
12
12
12
12
22
22
22
32
37
Example
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-26
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8
8-27