Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 15.

Similar presentations


Presentation on theme: "Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 15."— Presentation transcript:

1 Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 15

2 2/ Dynamic Programming (DP) In math and CS, dynamic programming is a method of solving problems exhibiting the properties of overlapping subproblems and optimal substructure that takes much less time than naive methods. The term was originally used in the 1940s by Richard Bellman to describe the process of solving problems where one needs to find the best decisions one after another. By 1953, he had refined this to the modern meaning The word "programming" in "dynamic programming" has no particular connection to computer programming at all, and instead comes from the term “mathematical programming", a synonym for optimization. A large class of programmimg algorithms that are based on breaking a large problem down (if possible) into incremental steps so that, at any given stage, optimal solutions are known sub-problems.

3 3/ Example1 Automobile factory with two assemly lines S 1, S 2 Each line has n stations  S 1,1, S 1,2,…, S 1,n  S 2,1, S 2,2,…, S 2,n  Entry times e 1, and e 2  Exit times x 1, and x 2 Transfer from one station to another has no cost but between lines the cost is t ij

4 4/ Problem Given all these costs, what stations should be chosen from line 1 and line 2 for fastest (cheapest) way through factory?  Try all possibilities and choose the cheapest!  Line 1 has n stations hence providing 2 n possibilities  n=3, {Ǿ,1,2,3,{1,2},{1,3,},{2,3},{1,2,3}}  When n is large this approach is infeasible.

5 5/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6 6/ Structure of an optimal solution Think about fastest way from entry thru S 1,j  J=1, straightforward, just compute how long it takes to get thru S 1,1  j>=2, we have two choices of how to get to S 1,j Thru S 1,j-1, then directly to S 1,j Thru S 2,j-1, then transfer over to S 1,j Key observation: We must have taken the fastest way from entry thru S 1,j-1 in this solution. If there were a faster way thru S 1,j-1 we would use it instead to come up with a faster way thru S 1,j Now, suppose that a fastest way is thru S 2,j-1. Again a fastest way thru S 2,j-1 is taken.

7 7/ … Generally: An optimal solution to a problem contains within it an optimal solution to subproblems (fastest way thru S 1,j-1 or S 2,j-1 ) This is optimal substructure Use optimal substructure to construct optimal solution to problem from optimal solutions to subproblems  Fastest way thru S 1,j-1 then directly to S 1,j, or  Fastest way thru S 2,j-1, then transfer from line 2 to 1, then thru S 1,j

8 8/ … Symmetrically,  Fastest way thru S 2,j-1 then directly to S 2,j, or  Fastest way thru S 1,j-1, then transfer from line 1 to 2, then thru S 2,j Therefore, to solve problems of finding a fastest way thru S 1,j, and S 2,j solve subproblems of finding a fastest way thru S 1,j-1, and S 2,j-1.

9 9/ Recursive solution Let f i [j]= fastest time to get thru S i,j, i = 1,2, and j=1,2,…,n. Goal: Fastest time to get all the way through = f *.  f * = min (f 1 [n]+x 1, f 2 [n]+x 2 ) f 1 [1] = e 1 + a 1,1 f 2 [1] = e 2 + a 2,1  For j = 2,3,…,n f 1 [j] = min (f 1 [j-1]+a 1,j, f 2 [j-1]+t 2,j-1 + a 1,j ) f 2 [j] = min (f 2 [j-1]+ a 2,j, f 1 [j-1]+t 1,j-1 + a 2,j )  f i [j] gives the values of optimal solutions to subproblems

10 10/ Construction of an optimal solution Let L i [j] = line# (1 or 2) whose station j-1 is used in fastest way thru S i,j, defined for i=1,2 and j=2,3,…,n In other words S Li[j],j-1 precedes S i,j. L * = line# whose station n is used. See figure next.

11 11/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12 12/ Problem! How efficient is the computation of f i [j]? Running time is exponential! Let r i (j) be the number of references made to f i [j]. From f * = min (f 1 [n]+x 1, f 2 [n]+x 2 ), we have  r 1 (n) = r 2 (n) = 1 From (j=2,3,…,n)  f 1 [j] = min (f 1 [j-1]+a 1,j, f 2 [j-1]+t 2,j-1 + a 1,j )  f 2 [j] = min (f 2 [j-1]+ a 2,j, f 1 [j-1]+t 1,j-1 + a 2,j ) r 1 (j) = r 2 (j) = r 1 (j+1)+r 2 (j+1), for j=1,2,…,n-1

13 13/ Claim! r i (j) = 2 n-j Proof: Induction on j down form n Basis: True for j=n, 2 n-n =1= r 1 (n) Inductive step: Assume r i (j+1)= 2 n-(j+1) r i (j) = r 1 (j+1)+r 2 (j+1) = 2 n-(j+1) +2 n-(j+1) = 2 n-(j+1)+1 = 2 n-j Therefore, f 1 [1] alone is referenced 2 n-1 times Computation is not efficient!

14 14/ How to compute f then! Observe that for j ≥ 2, each value of f i [j] depends only on the values of f 1 [j-1] and f 2 [j-1]. By computing the f i [j] values in order of increasing staiton numbers j, left to right as in Figure 15.2(b)- we can compute the fastest way thru the factory, and the time it takes, in Θ(n) time.

15 15/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

16 16/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

17 17/ Output of PRINT-STATIONS Line 1, station 6 Line 2, station 5 Line 2, station 4 Line 1, station 3 Line 2, station 2 Line 1, station 1

18 18/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

19 19/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

20 20/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

21 21/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

22 22/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

23 23/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

24 24/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

25 25/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

26 26/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

27 27/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

28 28/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

29 29/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

30 30/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

31 31/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

32 32/ Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.


Download ppt "Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 15."

Similar presentations


Ads by Google