Presentation is loading. Please wait.

Presentation is loading. Please wait.

D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING Informatics Department Parahyangan Catholic University.

Similar presentations


Presentation on theme: "D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING Informatics Department Parahyangan Catholic University."— Presentation transcript:

1 D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING Informatics Department Parahyangan Catholic University

2 I NTRODUCTION We have seen some algorithm design principles, such as divide-and-conquer, brute force, and greedy Brute force is widely applicable, but inefficient Divide-and-conquer and greedy are fast, but only applicable on very specific problems. Dynamic Programming is somewhere in between them, while still providing polynomial time complexity, it is widely applicable.

3 D YNAMIC P ROGRAMMING (DP) Similar to divide-and-conquer, DP solves problem by combining the solutions of its sub-problems. The term “programming” here refers to a tabular method DP solves a sub-problem, then saves its solution in a table

4 F INDING N - TH FIBONACCI NUMBER 0n=0 F(n) = 1n=1 F(n-1) + F(n-2)n>1 FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) Recursive solution :

5 F INDING N - TH FIBONACCI NUMBER F(3)F(2) F(1)F(2)F(0)F(1) F(0)F(1) F(2) F(0)F(1) F(5) F(4) F(3) Recursive solution :

6 F INDING N - TH FIBONACCI NUMBER Memoization : Maintain a table to store sub-problem’s solution // Initially : Arr[0] = 0 // Arr[1] = 1 // Arr[2..n] = -1 FIBONACCI (n) if (Arr[n] != -1) return Arr[n] else Arr[n]=FIBONACCI(n-1) + FIBONACCI(n-2) return Arr[n] // Initially : Arr[0] = 0 // Arr[1] = 1 // Arr[2..n] = -1 FIBONACCI (n) if (Arr[n] != -1) return Arr[n] else Arr[n]=FIBONACCI(n-1) + FIBONACCI(n-2) return Arr[n] solution with memoization:

7 F INDING N - TH FIBONACCI NUMBER F(3)F(2) F(1)F(2) F(0)F(1) F(5) F(4) F(3) solution with memoization: n012345 F(n)01 1235 =1 =2

8 F INDING N - TH FIBONACCI NUMBER Bottom-up solution : Use the natural ordering of sub-problems, solve them one-by-one starting from the “smallest” one FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] Bottom-up solution:

9 T IME COMPLEXITY FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) Recursive solution : Every instance has ≤ 2 recursive calls F(n-2)F(n-3)F(n-4)F(n-3) F(n) F(n-1) F(n-2) Height = n Therefore, time complexity is O(2 n )

10 T IME COMPLEXITY FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] Bottom-up solution: There is a loop that iterates ≤ n times, each doing a constant amount of work. So the time complexity is O(n)

11 R OD C UTTING P ROBLEM Serling Enterprises buys long steel rods and cuts them into shorter rods, which it then sells. Each cut is free, however different rod length sells for different price. The management of Sterling Enterprises wants to know the best way to cut up the rods. We assume that we know : length 12345678910 price 15891017 202430 Example : n=4 (n-1) possible cut locations  2 n-1 ways of cutting

12 R OD C UTTING P ROBLEM Example : n=4 length 12345678910 price 15891017 202430 9 =9 18 55 =10 18 =9 115 =7 115 115 1111 =4 BEST

13 R OD C UTTING P ROBLEM Consider a rod of length n, and we cut a rod of length i Then we left with a rod of length n-i Naturally, we want to optimize the selling price of the remaining rod ni1

14 R OD C UTTING P ROBLEM ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best Recursive solution :

15 R OD C UTTING P ROBLEM Recursive solution : RC(4) RC(3) RC(2) RC(1) RC(0)RC(1)RC(0) RC(1)RC(0) RC(2) RC(1) RC(0) Same problem as recursive Fibonacci

16 R OD C UTTING P ROBLEM // Initially : Arr[0] = 0 // Arr[1..n] = -∞ ROD-CUTTING (n) if Arr[n] ≥ 0 return Arr[n] else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) Arr[n] = best return best // Initially : Arr[0] = 0 // Arr[1..n] = -∞ ROD-CUTTING (n) if Arr[n] ≥ 0 return Arr[n] else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) Arr[n] = best return best Solution with memoization: Exercise Write a bottom-up solution for Rod Cutting problem !

17 T IME C OMPLEXITY ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best Recursive solution : Every instance has ≤ n recursive calls, and the depth of the recursive tree is O(n). So the time complexity is O(n n ) What is the time complexity for the bottom-up solution ?

18 S HORTEST P ATH IN DAG DAG = Directed Acyclic Graph Example : S A C B D E 2 1 1 12 4 6 3

19 S HORTEST P ATH IN DAG SACBDE S A C B D E 2 1 1 12 46 3 211 12 46 3 Sort using topological sort

20 S HORTEST P ATH IN DAG SACBDE 211 12 46 3 Starting from S, suppose we want to reach node D The only way to reach D is either through B or C dist(D) = MIN(dist(B)+1, dist(C)+3)

21 S HORTEST P ATH IN DAG A similar relation can be written for every node. As we have seen before, it is best to use bottom-up way of computing dist, that is from “left most” node to “right most” node DAG-SHORTEST-PATH () initialize all dist[.] to ∞ dist[S] = 0 for each vertex v except S in left-to-right order do for each edge (u,v) do dist[v] = MIN(dist[v], dist[u] + weight(u,v)) DAG-SHORTEST-PATH () initialize all dist[.] to ∞ dist[S] = 0 for each vertex v except S in left-to-right order do for each edge (u,v) do dist[v] = MIN(dist[v], dist[u] + weight(u,v))

22 D YNAMIC P ROGRAMMING DAG’s shortest path is a very general technique. We model many other problems into DAG problem. Example #1: Fibonacci Example #2 : Rod Cutting 02134 0 021345 01

23 P ROPERTIES OF D YNAMIC P ROGRAMMING Problem can be divided into smaller sub-problems Optimal substructure: an optimal solution to the problem contains within its optimal solutions to sub-problems Overlapping sub-problems: the space of sub-problems is “small”, in the sense that a recursive algorithm for the problem solves the same sub-problems over and over, rather than always generating new sub-problems

24 D YNAMIC P ROGRAMMING V.S. D IVIDE - AND -C ONQUER Divide-and-Conquer Dynamic Programming And so on…

25 L ONGEST I NCREASING S UBSEQUENCE Given a sequence of numbers a 1, a 2, a 3, …, a n. A subsequence is any subset of these numbers taken in order of the form a i1, a i2, a i3 …, a ik, where 1 ≤ i1 < i2 < … < ik ≤ n, and an increasing subsequence is one which the numbers are getting strictly larger. The task is to find the increasing subsequence of greatest length. Example: 5, 2, 8, 6, 3, 6, 9, 7

26 L ONGEST I NCREASING S UBSEQUENCE How do we model this problem into a DAG ? any number a i can precede a j iff a i < a j 58263697 Consider this number, LIS that ends here must be either: subsequence consist of “6” alone LIS that ends at “5” + “6” LIS that ends at “2” + “6” LIS that ends at “”3” + “6”

27 L ONGEST I NCREASING S UBSEQUENCE How do we find the LIS of sequence 5, 2, 8, 6, 3, 6, 9, 7 ? Does the optimal solution always ends at “7” ? // Initially L[1..n] = 1 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that a i < a j do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] return maximum of L[1..n] // Initially L[1..n] = 1 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that a i < a j do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] return maximum of L[1..n] This algorithm only gives the sequence’s length. How do we find the actual sequence ?

28 R ECONSTRUCTING A SOLUTION We can extend the dynamic programming approach to record not only the optimal value for each sub-problem, but also the choice that led to that value // Initially L[1..n] = 1 // Initially prev[1..n] = 0 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that a i < a j do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] prev[j] = i return maximum of L[1..n] and array prev // Initially L[1..n] = 1 // Initially prev[1..n] = 0 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that a i < a j do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] prev[j] = i return maximum of L[1..n] and array prev

29 E XERCISE : Y UCKDONALD ’ S Yuckdonald’s is considering opening a series of restaurants along Quaint Valley Highway (QVH). The n possible locations are along a straight line, and the distance of these locations from the start of QVH are, in miles and in increasing order, m 1, m 2, …, m n. The constraints are as follows: At each location, Yuckdonald’s may open at most one restaurant. The expected profit from opening a restaurant at location i is p i > 0 Any two restaurants should be at least k miles apart, where k is a positive integer


Download ppt "D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING Informatics Department Parahyangan Catholic University."

Similar presentations


Ads by Google