csc 8301: lecture 9 dynamic programming csc 8301- design...

13
CSC 8301: Lecture 9 Dynamic Programming 1 CSC 8301- Design and Analysis of Algorithms Lecture 10 Dynamic Programming 2 Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems “Programming” here means “planning” Main idea of the classical DP: - set up a recurrence relating a solution to a given problem to solutions to its smaller (overlapping) subproblems - record solutions to the subproblems in a table so that each subproblem is solved only once

Upload: others

Post on 08-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

1

CSC 8301- Design and Analysis of Algorithms

Lecture 10

Dynamic Programming

2

Dynamic Programming

Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems

•  “Programming” here means “planning” •  Main idea of the classical DP:

- set up a recurrence relating a solution to a given problem to solutions to its smaller (overlapping) subproblems - record solutions to the subproblems in a table so that each subproblem is solved only once

Page 2: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

2

3

Example: Fibonacci numbers

Recall definition of Fibonacci numbers: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2)

4

Example: Fibonacci numbers

Recall definition of Fibonacci numbers: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2)

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

F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4)

... Time Complexity:

Page 3: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

3

5

Example: Fibonacci numbers (cont.)

Computing the nth Fibonacci number using bottom-up iteration and recording results:

F(0) = 0 F(1) = 1 F(2) =

Time Complexity:

0 1

7

Example 2: Coin-row problem There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up. E.g. 5, 1, 2, 10, 6, 2. What is the best selection?

Page 4: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

4

8

DP solution to the coin-row problem Let F(n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence for F(n), we partition all the allowed coin selections into two groups:

those without last coin – the max amount is ? those with the last coin -- the max amount is ?

9

DP solution to the coin-row problem Let F(n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence for F(n), we partition all the allowed coin selections into two groups:

those without last coin – the max amount is ? those with the last coin -- the max amount is ? Thus we have the following recurrence

F(n) = max{cn + F(n-2), F(n-1)} for n > 1,

F(0) = 0, F(1)=c₁.

Page 5: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

5

10

DP solution to the coin-row problem (cont.)

index 0 1 2 3 4 5 6

coins -- 5 1 2 10 6 2

F( )

F(n) = max{cn + F(n-2), F(n-1)} for n > 1,

F(0) = 0, F(1)=c₁.

Max amount: Coins of optimal solution: Time efficiency: Space efficiency:

Note: All smaller instances were solved.

Example 3: Coin-collecting by robot Several coins are placed in cells of an n×m board. A robot, located in the upper left cell of the board, needs to collect as many of the coins as possible and bring them to the bottom right cell. On each step, the robot can move either one cell to the right or one cell down from its current location. 1 2 3 4 5 6

1

2

3

4

5

Assume cij = 1 if there is a coin in cell (i,j), and 0 otherwise

Page 6: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

6

Example 3: Coin-collecting by robot

1 2 3 4 5 6

1

2

3

4

5

Assume cij = 1 if there is a coin in cell (i,j), and 0 otherwise. Compute F(i,j): ❂  largest #coins the robot can bring to cell (i,j) in row i and column j The recurrence:

14

Solution to the coin-collecting problem Let F(i,j) be the largest number of coins the robot can collect and bring to cell (i,j) in the ith row and jth column. The largest number of coins that can be brought to cell (i,j): from the left neighbor ? from the neighbor above? The recurrence: F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n.

Page 7: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

7

15

Solution to the coin-collecting problem (cont.)

F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n.

1 2 3 4 5 6

1

2

3

4

5

16

Other Examples of DP Algorithms

•  Computing binomial coefficients

•  Some instances of difficult discrete optimization problems:

- travelling salesman -  knapsack problem

•  Finding the longest common subsequence in two sequences

•  Warshall’s algorithm for transitive closure Floyd’s algorithms for all-pairs shortest paths

Page 8: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

8

17

Knapsack Problem

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

18

Knapsack Problem

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

Consider instance defined by first i items and capacity j (j≤W). Let V[i,j] be optimal value of such instance. Recurrence:

Page 9: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

9

19

Knapsack Problem

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

Consider instance defined by first i items and capacity j (j≤W). Let V[i,j] be optimal value of such instance. Then

max {V[i-1, j], vi + V[i-1, j- wi]} if j- wi ≥ 0 V[i,j] = V[i-1, j] if j- wi < 0

Initial conditions: V[0,j] = 0 and V[i,0] = 0

20

Knapsack Problem 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

w1 = 2, v1= 12 1

w2 = 1, v2= 10 2

w3 = 3, v3= 20 3

w4 = 2, v4= 15 4 ?

Page 10: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

10

Paths and Distances in Graphs: Warshall and Floyd

22

Warshall’s Algorithm for Transitive Closure

Computes a boolean matrix indicating which pairs of vertices are connected by nontrivial paths Example:

3

4 2

1

0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0

3

4 2

1

0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0

Page 11: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

11

24

Warshall’s Algorithm Note: a path exists from vertex i to vertex j, iff

•  there is an edge from i to j; or •  there is a path from i to j going through vertex 1; or •  there is a path from i to j going through vertex 1 and/or 2; or •  ... •  there is a path from i to j going through any of the vertices

3

4 2

1 3

4 2

1 3

4 2

1 3

4 2

1

0 0 1 0 R(0) = 1 0 0 1 0 0 0 0 0 1 0 0

R(1) =

3

4 2

1

R(2) =

R(3) =

R(4) =

26

Warshall’s Algorithm (cont.)

In the kth stage, 1≤k≤n, determine if a path exists from vertex i to vertex j that uses as intermediate only vertices among 1,…,k

R(k)[i,j] =

i

j

k

kth stage

{

Page 12: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

12

28

Warshall’s Algorithm (cont.)

The rules for computing elements of R(k) from R(k-1) : ❂  all 1’s in R(k-1) remain 1’s in R(k) ❂  0 in row i and column j is replaced by 1 iff

•  there is 1 in same row i and column k in R(k-1) and •  there is 1 in same column j and row k in R(k-1)

Example:

0 0 1 0 R(0) = 1 0 0 1 0 0 0 0 0 1 0 0

Time complexity?

R(1) =

R(2) =

R(3) =

29

Floyd’s Algorithm: All Pairs Shortest Paths

In a weighted graph, find shortest paths between every pair of vertices

Same idea: construct solution through series of matrices D(0), D(1), …, D(n), using expanding subsets of the vertices as intermediaries.

D(k)[i,j] =

i

j

k kth stage

Page 13: CSC 8301: Lecture 9 Dynamic Programming CSC 8301- Design ...mdamian/Past/csc8301fa16/notes/Lec10DP… · CSC 8301: Lecture 9 Dynamic Programming 4 8 DP solution to the coin-row problem

CSC 8301: Lecture 9 Dynamic Programming

13

30

Floyd’s Algorithm (cont.)

Example: 3

4 2

1 4

1 6

1

5

3

D(0) =

D(1) =

D(2) =

D(3) =

D(4) =

Works only if there is no cycle of negative length.

31

Homework

Reading: Chapter 8

Exercises 8.1: 5, 8, 10a 8.2: 1 8.4: 1, 3, 7, 9

Next time: Greedy algorithms (Chapter 9)