Upcoming SlideShare
×

# 03 dp

422 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
422
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
21
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 03 dp

1. 1. Dynamic Programming Problems Dynamic Programming League of Programmers ACA, IIT Kanpur October 22, 2012League of Programmers Dynamic Programming
2. 2. Dynamic Programming ProblemsOutline 1 Dynamic Programming 2 Problems League of Programmers Dynamic Programming
3. 3. Dynamic Programming ProblemsDynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesnt necessarily mean that every optimal solution to a subproblem will contribute to the main solution. League of Programmers Dynamic Programming
4. 4. Dynamic Programming ProblemsDynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesnt necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. League of Programmers Dynamic Programming
5. 5. Dynamic Programming ProblemsDynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesnt necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. For greedy algorithms (bottom up), we can always choose the "right" subproblem by a greedy choice. League of Programmers Dynamic Programming
6. 6. Dynamic Programming ProblemsDynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesnt necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. For greedy algorithms (bottom up), we can always choose the "right" subproblem by a greedy choice. In dynamic programming, we solve many subproblems and store the results: not all of them will contribute to solving the larger problem. Because of optimal substructure, we can be sure that at least some of the subproblems will be useful League of Programmers Dynamic Programming
7. 7. Dynamic Programming ProblemsDynamic Programming Steps to solve a DP problem League of Programmers Dynamic Programming
8. 8. Dynamic Programming ProblemsDynamic Programming Steps to solve a DP problem 1 Dene subproblems League of Programmers Dynamic Programming
9. 9. Dynamic Programming ProblemsDynamic Programming Steps to solve a DP problem 1 Dene subproblems 2 Write down the recurrence that relates subproblems League of Programmers Dynamic Programming
10. 10. Dynamic Programming ProblemsDynamic Programming Steps to solve a DP problem 1 Dene subproblems 2 Write down the recurrence that relates subproblems 3 Recognize and solve the base cases League of Programmers Dynamic Programming
11. 11. Dynamic Programming ProblemsFibonacci Sequence Naive Recursive Function int Fib(int n){ if(n==1 || n==2) return 1; return Fib(n-1)+Fib(n-2) } League of Programmers Dynamic Programming
12. 12. Dynamic Programming ProblemsFibonacci Sequence DP Solution O(n) Fib[1] = Fib[2] = 1; for(i=3;iN;i++) Fib[i] = Fib[i-1]+Fib[i-2] League of Programmers Dynamic Programming
13. 13. Dynamic Programming ProblemsProblem Problem 2 Given n, nd the number of dierent ways to write n as the sum of 1, 3, 4 Example: for n = 5, the answer is 6 5 = 1+1+1+1+1 = 1+1+3 = 1+3+1 = 3+1+1 = 1+4 League of Programmers Dynamic Programming
14. 14. Dynamic Programming ProblemsProblem Dene Subproblems League of Programmers Dynamic Programming
15. 15. Dynamic Programming ProblemsProblem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 League of Programmers Dynamic Programming
16. 16. Dynamic Programming ProblemsProblem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 League of Programmers Dynamic Programming
17. 17. Dynamic Programming ProblemsProblem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 Solve the base cases D =1 0 Dn = 0 for all negative n League of Programmers Dynamic Programming
18. 18. Dynamic Programming ProblemsProblem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 Solve the base cases D =1 0 Dn = 0 for all negative n Alternatively, can set: D = D = D = 1, D = 2 0 1 2 3 League of Programmers Dynamic Programming
19. 19. Dynamic Programming ProblemsProblem Implementation D[0]=D[1]=D[2]=1; D[3]=2; for(i=4;i=n;i++) D[i]=D[i-1]+D[i-3]+D[i-4]; League of Programmers Dynamic Programming
20. 20. Dynamic Programming ProblemsLCS Problem 3 Given two strings x and y, nd the longest common subsequence (LCS) and print its length. Example: x: ABCBDAB y: BDCABC BCAB is the longest subsequence found in both sequences, so the answer is 4 League of Programmers Dynamic Programming
21. 21. Dynamic Programming ProblemsLCS Analysis League of Programmers Dynamic Programming
22. 22. Dynamic Programming ProblemsLCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). League of Programmers Dynamic Programming
23. 23. Dynamic Programming ProblemsLCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? League of Programmers Dynamic Programming
24. 24. Dynamic Programming ProblemsLCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, cant tell what initial division or greedy choice to make. League of Programmers Dynamic Programming
25. 25. Dynamic Programming ProblemsLCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, cant tell what initial division or greedy choice to make. Thus, none of the approaches we have learned so far work here!!! League of Programmers Dynamic Programming
26. 26. Dynamic Programming ProblemsLCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, cant tell what initial division or greedy choice to make. Thus, none of the approaches we have learned so far work here!!! Intuition A LCS of two sequences has as a prex a LCS of prexes of the sequences. So, We concentrate on LCS for smaller problems, i.e simply removes the last (common) element. League of Programmers Dynamic Programming
27. 27. Dynamic Programming ProblemsLCS Dene Subproblems League of Programmers Dynamic Programming
28. 28. Dynamic Programming ProblemsLCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j League of Programmers Dynamic Programming
29. 29. Dynamic Programming ProblemsLCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence League of Programmers Dynamic Programming
30. 30. Dynamic Programming ProblemsLCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 League of Programmers Dynamic Programming
31. 31. Dynamic Programming ProblemsLCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y 1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 Either xi or yj does not contribute to the LCS, so one can be dropped. Otherwise, Di ,j = max {Di −1,j , Di ,j −1 } League of Programmers Dynamic Programming
32. 32. Dynamic Programming ProblemsLCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 Either xi or yj does not contribute to the LCS, so one can be dropped. Otherwise, Di ,j = max {Di −1,j , Di ,j −1 } Find and solve the base cases: Di , = D ,j = 0 0 0 League of Programmers Dynamic Programming
33. 33. Dynamic Programming ProblemsLCS Implementation for(i=0;i=n;i++) D[i][0]=0; for(j=0;j=m;j++) D[0][j]=0; for(i=1;i=n;i++) { for(j=1;j=m;j++) { if(x[i]==y[j]) D[i][j]=D[i-1][j-1]+1; else D[i][j]=max(D[i-1][j],D[i][j-1]); } } League of Programmers Dynamic Programming
34. 34. Dynamic Programming ProblemsLCS Recovering the LCS Modify the algorithm to also build a matrix D[1 . . . n; 1 . . . m], recording how the solutions to subproblems were arrived at. League of Programmers Dynamic Programming
35. 35. Dynamic Programming ProblemsLongest Non Decresing Subsequence Problem 4 Given an array [1 , 2 , 5, 2, 8, 6, 3, 6, 9, 7]. Find a subsequence which is non decreasing and of maximum length. 1-5-8-9 forms a non decreasing subsequence So does 1-2-2-6-6-7 but it is longer League of Programmers Dynamic Programming
36. 36. Dynamic Programming ProblemsLNDS Subproblem Length of LNDS ending at i th location League of Programmers Dynamic Programming
37. 37. Dynamic Programming ProblemsLNDS Subproblem Length of LNDS ending at i th location Implementation for(i=0;i100;i++) { max=0; for(j=0;ji;j++) { if(A[i]=A[j] L[j]max) max = L[j]; } L[i] = max+1; } League of Programmers Dynamic Programming
38. 38. Dynamic Programming ProblemsProblem Problem 5 Given a tree, color nodes black as many as possible without coloring two adjacent nodes. League of Programmers Dynamic Programming
39. 39. Dynamic Programming ProblemsProblem Dene Subproblems League of Programmers Dynamic Programming
40. 40. Dynamic Programming ProblemsProblem Dene Subproblems we arbitrarily decide the root node r League of Programmers Dynamic Programming
41. 41. Dynamic Programming ProblemsProblem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black League of Programmers Dynamic Programming
42. 42. Dynamic Programming ProblemsProblem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black Wv : the optimal solution for a subtree having v as the root, where we dont color v League of Programmers Dynamic Programming
43. 43. Dynamic Programming ProblemsProblem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black Wv : the optimal solution for a subtree having v as the root, where we dont color v The answer is max{Br ,Wr } League of Programmers Dynamic Programming
44. 44. Dynamic Programming ProblemsProblem Find The Recurrence Observation League of Programmers Dynamic Programming
45. 45. Dynamic Programming ProblemsProblem Find The Recurrence Observation Once vs color is determined, its subtrees can be solved independently League of Programmers Dynamic Programming
46. 46. Dynamic Programming ProblemsProblem Find The Recurrence Observation Once vs color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu League of Programmers Dynamic Programming
47. 47. Dynamic Programming ProblemsProblem Find The Recurrence Observation Once vs color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu If v is not colored, its children can have any color Wv =1+ u ∈child (v ) Bu League of Programmers Dynamic Programming
48. 48. Dynamic Programming ProblemsProblem Find The Recurrence Observation Once vs color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu If v is not colored, its children can have any color Wv =1+ u ∈child (v ) Bu Base cases: leaf nodes League of Programmers Dynamic Programming
49. 49. Dynamic Programming ProblemsOutline 1 Dynamic Programming 2 Problems League of Programmers Dynamic Programming
50. 50. Dynamic Programming ProblemsProblems Added on the contest on VOC http://ahmed-aly.com/voc/ Contest ID: 2616 Name: ACA, IITK LOP 03 Author: pnkjjindal Links: 1 http://spoj.pl/problems/ACTIV 2 http://www.spoj.pl/problems/COINS 3 http://spoj.pl/problems/IOIPALIN 4 http://spoj.pl/problems/ADFRUITS 5 http://www.spoj.pl/problems/RENT 6 http://www.spoj.pl/problems/M3TILE 7 http://www.spoj.pl/problems/IOPC1203 8 http://www.spoj.pl/problems/NGON League of Programmers Dynamic Programming