Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Dynamic Programming.  Dividing a problem into subproblems  Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped.

Similar presentations


Presentation on theme: " Dynamic Programming.  Dividing a problem into subproblems  Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped."— Presentation transcript:

1  Dynamic Programming

2  Dividing a problem into subproblems  Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped - Divide and conquer : subproblems are independent  Used for finding optimized solutions

3 Assembly-Line Scheduling exits enters Assembly Line 1 Assembly Line 2 n = 6 station 1station 2 station 3 station 4station 5station 6

4 Assembly-Line Scheduling exits 3 2 enters 2 4 Assembly Line 1 Assembly Line 2

5 Assembly-Line Scheduling exits 3 2 enters 2 4 Assembly Line 1 Assembly Line 2

6 Assembly-Line Scheduling exits 3 2 enters Assembly Line 1 Assembly Line 2

7 Assembly-Line Scheduling exits 3 2 enters Assembly Line 1 Assembly Line 2

8 Assembly-Line Scheduling exits enters Assembly Line 1 Assembly Line 2 station 1station 2 station 3 station 4station 5station 6 number of cases = 2 n

9 Assembly-Line Scheduling exits enters Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T

10 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T = min( T 1,6 + 3, T 2,6 + 2) T 1,6 = min( T 1,5 + 4, T 2, ) T 2,6 = min( T 1, , T 2,5 + 7) Divide and Conquer?

11 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T = min( T 1,6 + 3, T 2,6 + 2) T 1,6 = min( T 1,5 + 4, T 2, ) T 2,6 = min( T 1, , T 2,5 + 7) Divide and Conquer?

12 Assembly-Line Scheduling enters 2 4 Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 2,1 T 2,2 T 2,3 T 1,1 = 9 T 2,1 =

13 Assembly-Line Scheduling enters 2 4 Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 2,1 T 2,2 T 2,3 T 1,1 = 9 T 2,1 = 12 T 1,2 = min(T 1,1 + 9, T 2, )

14 Assembly-Line Scheduling enters 2 4 Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 2,1 T 2,2 T 2,3 T 1,1 = 9 T 2,1 = 12 T 1,2 = 18 T 2,2 = min(T 1, , T 2,1 + 5)

15 Assembly-Line Scheduling exits 3 2 enters 2 4 Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T

16 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T = T 1,6 + 3 ? or T 2,6 + 2 ? T 1,6 + 3 = T T 2,6 + 2 > T

17 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T = T 1,6 + 3 ? or T 2,6 + 2 ? T 1,6 + 3 = T T 2,6 + 2 > T

18 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T 1,6 = T 1,5 + 4 ? or T 2, ? T 1,5 + 4 > T 1,6 T 2, = T 1,6

19 Assembly-Line Scheduling exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T 1,6 = T 1,5 + 4 ? or T 2, ? T 1,5 + 4 > T 1,6 T 2, = T 1,6

20 Assembly-Line Scheduling exits 3 2 enters 2 4 Assembly Line 1 Assembly Line 2 T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T

21 Assembly-Line Scheduling exits T 1,5 T 1,6 T 2,5 T 2,6 T T = min( T 1,6 + 3, T 2,6 + 2) 1. Analyze the problem

22 Assembly-Line Scheduling exits T 1,5 T 1,6 T 2,5 T 2,6 T T = min( T 1,6 + 3, T 2,6 + 2) T 1,6 = min( T 1,5 + 4, T 2, ) T 2,6 = min( T 1, , T 2,5 + 7) 1. Analyze the problem 2. Find a recursive solution.

23 Assembly-Line Scheduling T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T Analyze the problem. 2. Find a recursive solution. 3. Compute the fastest time.

24 Assembly-Line Scheduling T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T Analyze the problem. 2. Find a recursive solution. 3. Compute the fastest time. 4. Construct the fastest way.

25 Assembly-Line Scheduling T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6 T Analyze the problem. 2. Find a recursive solution. 3. Compute the fastest time. 4. Construct the fastest way. This algorithm takes time. Brute force :

26 When can we use Dynamic Programming?  Optimal substructures  Overlapping subproblems

27 Overlapping Subprograms exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T T 2,5 T 2,6 T 1,6 T 1,5 T 2,5 T 1,5 T 2,4 T 1,4 T 2,4 T 1,4 T 2,4 T 1,4 T 2,4 T 1,4

28 Overlapping Subprograms exits 3 2 T 1,5 T 1,6 T 2,5 T 2,6 T T T 2,5 T 2,6 T 1,6 T 1,5 T 2,5 T 1,5 T 2,4 T 1,4 T 2,4 T 1,4 T 2,4 T 1,4 T 2,4 T 1,4

29 Optimal Substructures  Problems should be divided into subproblems.  Optimal solution : from optimal subproblem solutions One part of the optimal solution to the problem should be an optimal solution to the subproblem solutions to the subproblems should be independent

30 Optimal Substructures 1. Shortest Path Problem 2. Longest Simple Path Problem

31 Optimal Substructures 1. Shortest Path Problem 2. Longest Simple Path Problem

32 Memoization  Ordinary - Bottom-up strategy  Memoization - Top-down strategy

33 Memoization Bottom Up Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1,6 T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,6

34 Memoization T T 2,5 T 2,6 T 1,6 T 1,5 T 2,4 T 1,4 … … Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,

35 Memoization T 2,2 T 1,3 T 1,2 T 2,1 T 1,1 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,

36 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, T 2,2 T 1,3 T 1,2 T 2,1 T 1,1

37 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, T 2,2 T 1,3 T 1,2 T 2,1 T 1,1 T 2,1 T 1,1

38 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, T 2,3 T 1,4 T 1,3 T 2,2 T 1,2 T 2,2 T 1,2 T 2,1 T 1,1 T 2,1 T 1,1

39 Memoization T T 2,5 T 2,6 T 1,6 T 1,5 T 2,4 T 1,4 … … T 2,4 T 1,4 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, T 2,3 T 1,3

40 Memoization T T 2,5 T 2,6 T 1,6 T 1,5 T 2,4 T 1,4 … … … … T 2,4 T 1,4 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,

41 Memoization T T 2,5 T 2,6 T 1,6 T 1,5 T 2,4 T 1,4 … … … … T 2,4 T 1,4 T 2,5 T 1,5 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2,

42 Memoization T T 2,5 T 2,6 T 1,6 T 1,5 T 2,4 T 1,4 … … … … T 2,4 T 1,4 T 2,5 T 1,5 Memoization T 1,1 T 1,2 T 1,3 T 1,4 T 1,5 T 1, T 2,1 T 2,2 T 2,3 T 2,4 T 2,5 T 2, = 38

43 Why Memoization?  Ordinary Dynamic Programming (Bottom-Up strategy) - when all subproblems need to be solved  Memoization - when some subproblems do not need to be solved

44 Longest Common Subsequence S1 = ACCGGTCGTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA  DNA sequences : composed of four components – {A, C, G, T}  How similar are they?

45 Longest Common Subsequence S1 = ACCGGTCGTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA Longest Common Sequence : GTCGTCGGAAGCCGGCCGAA

46 Longest Common Subsequence X =, Y = X i = for i = 1 to m Y j = for j = 1 to n Z i,j = for i = 1 to m, j = 1 to n Longest common subsequence of X i and Y j

47 Longest Common Subsequence X =, Y = X i = Y j = Z i,j = (Longest common subsequence) 1. Analyze the problem. Z i-1,j = or when x i used as z k otherwise Z i,j-1 = or

48 Longest Common Subsequence X =, Y = X i = Y j = Z i,j = (Longest common subsequence) 1. Analyze the problem. If x i = y j then z k = x i or z k = y j / // Z i-1,j = or when x i used as z k otherwise

49 Longest Common Subsequence X =, Y = X i = Y j = Z i,j = (Longest common subsequence) 1. Analyze the problem. If x i = y j then z k = x i or z k = y j / // Z i,j = Z i-1,j or Z i,j-1

50 Longest Common Subsequence X =, Y = X i = Y j = Z i,j = (Longest common subsequence) 1. Analyze the problem. If x i = y j then / Z i,j = Z i-1,j or Z i,j-1 If x i = y j then If x i = y j is not used as z k thenZ i,j = Z i-1,j or Z i,j-1 If x i = y j is used as z k thenZ i,j = Z i-1,j-1 + z k Optimal

51 Longest Common Subsequence X =, Y = X i = Y j = Z i,j = (Longest common subsequence) 1. Analyze the problem. If x i = y j then / Z i,j = Z i-1,j or Z i,j-1 If x i = y j thenZ i,j = Z i-1,j-1 + z k c i,j = max(c i-1,j, c i,j-1 ) c i,j = c i-1,j-1 + 1

52 Longest Common Subsequence If x i = y j then / c i,j = max(c i-1,j, c i,j-1 ) c i,j = c i-1,j Find a recursive solution. If i = 0 or j = 0 then c i,j = 0 Else

53 Longest Common Subsequence 3. Compute the fastest time. C 1,1 C 1,2... C 1,n C 2,1 C 2,2... C 2,n C m,1 C m,2... C m,n If x i = y j then / c i,j = max(c i-1,j, c i,j-1 ) c i,j = c i-1,j-1 + 1

54 Longest Common Subsequence 4. Construct the common subsequence. C 1,1 C 1,2... C 1,n C 2,1 C 2,2... C 2,n C m,1 C m,2... C m,n

55 Longest Common Subsequence C i-2,j-2 C i-2,j-1 C i-2,j C i-1,j-2 C i-1,j-1 C i-1,j C i,j-2 C i,j-1 C i,j 4. Construct the common subsequence. If x i = y j then / c i,j = max(c i-1,j, c i,j-1 ) c i,j = c i-1,j move to c i-1,j-1 Else move to c i-1,j If c i,j = c i-1,j then move to c i,j-1 If c i,j = c i,j-1 then If x i = y j then one element of The longest common subsequence

56 Dynamic Programming 1. Analyze the problem. 2. Find a recursive solution. 3. Compute the fastest time/cost. 4. Construct the fastest path.  Dividing a problem into subproblems.  For optimization problems.  Optimal substructures / overlapping subproblems  Process of dynamic programmings


Download ppt " Dynamic Programming.  Dividing a problem into subproblems  Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped."

Similar presentations


Ads by Google