You are on page 1of 28

Chapter 8

Dynamic Programming

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

Example: Fibonacci numbers


Recall definition of Fibonacci numbers:

F(n) = F(n-1) + F(n-2)


F(0) = 0
F(1) = 1
Computing the nth Fibonacci number recursively (top-down):
F(n)

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

. . .

F(n-2) F(n-1) F(n)

n
n

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-4

Computing a binomial coefficient by DP


Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-5

Value of C(n,k) can be computed by filling a table:


0 1 2 . . . . k-1
k
0 1
1 1 1
2 1 2 1
.
k 1
1
.
n-1
C(n-1,k-1) C(n-1,k)
n
C(n,k)

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-6

and analysis

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-7

Time efficiency: (nk)

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-8

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-9

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-10

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-11

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-12

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-13

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-14

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-15

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-16

Floyds Algorithm (example)


2

3 6

D(2)

2
=

D(0)

0
2
9
6

0
7

3
5
0
9

1
0

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-18

Floyds Algorithm (pseudocode and analysis)

Time efficiency: (n3)

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-22

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 8

8-23

Example: Knapsack of capacity W = 5


item
weight
value
1
2
$12
2
1
$10
3
3
$20
4
2
$15
capacity j
0 1 2 3 4 5
0 0 0
0 0 0 0
w1 = 2, v1= 12

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

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

You might also like