Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.
|
|
- Jonathan Short
- 6 years ago
- Views:
Transcription
1 Dynamic Programming
2 Deliverables Dynamic Programming basics Binomial Coefficients Weighted Interval Scheduling Matrix Multiplication /1 Knapsack Longest Common Subsequence 6/12/212 6:56 PM gdeepak.com 2
3 Dynamic Programming Nothing to do with dynamic and nothing to do with programming. Actually the name should be bottom up approach without repeated calculations 6/12/212 6:56 PM gdeepak.com 3
4 Dynamic Programming paradigm Difference between Dynamic Programming and Divide and Conquer is that the sub problems in D&C are disjoint and distinct but in DP they are overlapping. Dynamic programming always gives a optimal solution as compared to the greedy paradigm which fails in some cases. Recursively solve sub problems, starting from the trivial case, and save their solutions in memory. In the end we get solution of the whole problem Optimal Substructure of the problems: The solution to the problem must be a composition of sub problem solutions 6/12/212 6:56 PM gdeepak.com 4
5 Dynamic Programming paradigm Divide and Conquer is top-down while DP is bottom up. It works when the number of substructures are polynomial in size of the input. Recursion uses a stack while DP uses a queue whenever required. In many cases we start with the base case and upper level sub problems are generated after doing some work on lower levels. 6/12/212 6:56 PM gdeepak.com 5
6 Pascal triangle or Binomial Coefficients 6/12/212 6:56 PM gdeepak.com 6
7 Pascal triangle or Binomial Coefficients 6/12/212 6:56 PM gdeepak.com 7
8 Pascal triangle or Binomial Coefficients 6/12/212 6:56 PM gdeepak.com 8
9 Binomial Coefficients n where n is the row of Pascal triangle m and m is the column of Pascal triangle. = n 1 m + n 1 m 1 Recursive Algorithm B(n, m) { If m= or m=n then set 1 else ret B(n-1,m) + B(n-1,m-1) } 6/12/212 6:56 PM gdeepak.com 9
10 Analysis Recurrence is T(size) = T(size-1)+T(size-2) This looks like Fibonacci equation. Nth Fibonacci number is an exponential, so we will end up in getting exponential number of calculations in doing recursive algorithm. If you want to calculate B(6,3) it will calculate B(3,2) 3 times, B(3,1) 3 times, B(2,1) 5 times, B(1,1) 6 times and B(1,) 6 times It can be done with an iterative function of factorials in linear time. 6/12/212 6:56 PM gdeepak.com 1
11 Weighted Interval Scheduling Greedy algorithm works if all weights are 1 and consider jobs in ascending order of finish time and add job to subset if it is compatible with previously chosen jobs. Greedy algorithm fails if arbitrary weights are allowed weight = 999 b weight = 1 a 6/12/212 6:56 PM gdeepak.com 11
12 Weighted Interval Scheduling Label jobs by finishing time: f 1 f 2... f n.p(j) = largest index i < j such that job i is compatible with j. p(8) = 5, p(7) = 3, p(2) = Time 6/12/212 6:56 PM gdeepak.com 12
13 Dynamic Programming: Key Technique OPT(j) = value of optimal solution to the problem consisting of job requests 1, 2,..., j. Case 1: OPT selects job j. can't use incompatible jobs {p(j) + 1, p(j) + 2,..., j - 1 } must include optimal solution to problem consisting of remaining compatible jobs 1, 2,..., p(j) Case 2: OPT does not select job j. must include optimal solution to problem consisting of remaining compatible jobs 1, 2,..., j-1 if j OPT( j) max v j OPT( p( j)), OPT( j 1) otherwise 6/12/212 6:56 PM gdeepak.com 13
14 WIS Brute Force Input n,s 1 s n,f 1 f n,v 1 v n Sort jobs by finish times so that f 1 f 2 f n Compute p(1),p(2) p(n) Compute-opt(j) { if j= then return else return max(v j +Compute-opt(p(j)),Compute-opt(j-1)) } 6/12/212 6:56 PM gdeepak.com 14
15 Example of Weighted Interval Scheduling 6/12/212 6:56 PM gdeepak.com 15
16 Exponential Growth 6/12/212 6:56 PM gdeepak.com 16
17 Worst Case Instance 6/12/212 6:56 PM gdeepak.com 17
18 WIS Algorithm Input n,s 1 s n,f 1 f n,v 1 v n Sort jobs by finish times so that f 1 f 2 f n Compute p(1),p(2) p(n) Memo-WIS(j) { if j= then return else if M[j] is not empty then return M[j] else M[j]= max(v j + Memo-WIS(p(j)), Memo-WIS(j-1)) return M[j] } 6/12/212 6:56 PM gdeepak.com 18
19 WIS Improved Input n,s 1 s n,f 1 f n,v 1 v n Sort jobs by finish times so that f 1 f 2 f n Compute p(1),p(2) p(n) iterative-wis(j) { M[]= for j=1,2,,n M[j]= max(v j + M(p(j)),M[j-1]) } 6/12/212 6:56 PM gdeepak.com 19
20 Example with Iterations 6/12/212 6:56 PM gdeepak.com 2
21 Complexity Analysis Running Time in O(nlogn), If we exclude sorting for finishing time then the remaining complexity is only O(n) 6/12/212 6:56 PM gdeepak.com 21
22 Multiplying two matrices For multiplication two matrices A and B number of columns of A must equal the number of rows of B. If A is a p q matrix and B is a q r matrix, the resulting matrix C is a p r matrix. The time to compute C is dominated by the number of scalar multiplications which is pqr Compute A=A *A 1 * *A n-1 A i is d i d i+1 Problem: How to parenthesize? Example B is 3 1 C is 1 5 D is 5 5 (B*C)*D takes = 1575 ops B*(C*D) takes = 4 ops 6/12/212 6:56 PM gdeepak.com 22
23 Matrix Multiplication Want to multiply matrices A x B x C x D x E We could parenthesize many ways (A x (B x (C x (D x E)))) ((((A x B) x C) x D) x E) Each different way has different number of multiplications A x B x C x D x E Calculate in advance the cost (multiplies) AB, BC, CD, DE Use those to find the cheapest way to form ABC, BCD, CDE and then ABCD BCDE From that derive best way to form ABCDE 6/12/212 6:56 PM gdeepak.com 23
24 Example Let matrices are 1x4,4x5,5x3,3x6,6x7,7x1 6/12/212 6:56 PM gdeepak.com 24
25 Example 6/12/212 6:56 PM gdeepak.com 25
26 Time complexity In the th Row or base row No of calucations - In the 1 st Row 1*n In the 2 nd Row- 2*n-1 In the 3 rd Row- 3* n-2 : In the nth Row n*1 Adding All the above n k=1 k(n k + 1) It comes out to be Ө(n 3 ) and the constant is 1/6 6/12/212 6:56 PM gdeepak.com 26
27 Keeping track of the solution Need to keep track of the Split point. For this we need to go backward from the final solution. Getting the actual sequence of solution is always recursive. For Example (M1M2) *(M3M4M5) ((M1)(M2)) * ((M3) (M4M5)) ((M1)(M2)) * ((M3) ((M4)(M5))) 6/12/212 6:56 PM gdeepak.com 27
28 /1 knapsack: Recursive Formulation S k : Set of items numbered 1 to k. Define B[k,w] = best selection from S k with weight exactly equal to w this does have sub problem optimality: B[ k, w] max{ B[ k B[ k 1, w] 1, w], B[ k 1, w w k ] b } k if w k else w 6/12/212 6:56 PM gdeepak.com 28
29 Running Time Running time: O(nW). Not a polynomial-time algorithm if W is large. This is a pseudo-polynomial time algorithm Algorithm 1Knapsack(S, W): Input: set S of items w/ benefit b i and weight w i ; max. weight W Output: benefit of best subset with weight at most W for w to W do B[w] for i = 1 to n B[i,] = for k 1 to n do for w W downto w k do if B[w-w k ]+b k > B[w] then B[w] B[w-w k ]+b k 6/12/212 6:56 PM gdeepak.com 29
30 Initial Setting of the /1 knapsack Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:7 PM gdeepak.com 3
31 Considering Item Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:8 PM gdeepak.com 31
32 Considering Item Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:9 PM gdeepak.com 32
33 Considering Item Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:9 PM gdeepak.com 33
34 Considering Item Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:1 PM gdeepak.com 34
35 Considering Item Item:W, P : 4,2 2: 2,3 3 : 2,6 4 : 6,25 5 :2,8 W=9 6/12/212 7:1 PM gdeepak.com 35
36 Time Complexity Complexity is Ө(nM) where M is the size of the knapsack. It can be written as Ө(n2 s ) where s is the size of the input. It is not polynomial but exponential. 6/12/212 6:56 PM gdeepak.com 36
37 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B ABA? 6/12/212 6:56 PM gdeepak.com 37
38 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B ABA 6/12/212 6:56 PM gdeepak.com 38
39 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B ACA? 6/12/212 6:56 PM gdeepak.com 39
40 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B ACA 6/12/212 6:56 PM gdeepak.com 4
41 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B DCA? 6/12/212 6:56 PM gdeepak.com 41
42 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B DCA 6/12/212 6:56 PM gdeepak.com 42
43 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B AADAA? 6/12/212 6:56 PM gdeepak.com 43
44 Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2,, i k ) where 1 i 1 < i 2 < < i k n X = A B A C D A B A B AADAA 6/12/212 6:56 PM gdeepak.com 44
45 LCS problem Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y Given two sequences X = x 1, x 2,, x n and Y = y 1, y 2,, y n, What is the longest common subsequence? X = A B C B D A B Y = B D C A B A 6/12/212 6:56 PM gdeepak.com 45
46 LCS problem Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y Given two sequences X = x 1, x 2,, x n and Y = y 1, y 2,, y n, What is the longest common subsequence? X = A B C B D A B Y = B D C A B A 6/12/212 6:56 PM gdeepak.com 46
47 Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A 6/12/212 6:56 PM gdeepak.com 47
48 Step 1: Define the problem with respect to subproblems X = A B C B D A? Y = B D C A B? Is the last character part of the LCS? 6/12/212 6:56 PM gdeepak.com 48
49 Step 1: Define the problem with respect to subproblems X = A B C B D A? Y = B D C A B? Two cases: either the characters are the same or they re different 6/12/212 6:56 PM gdeepak.com 49
50 Step 1: Define the problem with respect to subproblems X = A B C B D A A LCS Y = B D C A B A The characters are part of the LCS LCS If they re the same ( X, Y) LCS( X1... n 1, Y1... m 1) x n 6/12/212 6:56 PM gdeepak.com 5
51 Step 1: Define the problem with respect to subproblems X = A B C B D A B LCS Y = B D C A B A If they re different LCS( X, Y) LCS( X 1... n 1, Y) 6/12/212 6:56 PM gdeepak.com 51
52 Step 1: Define the problem with respect to subproblems X = A B C B D A B LCS Y = B D C A B A If they re different LCS( X, Y) LCS( X, Y1... m 1) 6/12/212 6:56 PM gdeepak.com 52
53 Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A X = A B C B D A B? Y = B D C A B A If they re different 6/12/212 6:56 PM gdeepak.com 53
54 Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A LCS( X, Y) 1 LCS( X1... n 1, Y1... m 1) max( LCS( X1... n 1, Y), LCS( X, Y 1... m 1 ) if x n y m otherwise 6/12/212 6:57 PM gdeepak.com 54
55 Step 2: Build the solution from the bottom up LCS( X, Y) 1 LCS( X1... n 1, Y1... m 1) max( LCS( X1... n 1, Y), LCS( X, Y 1... m 1 ) if x n y m otherwise What types of subproblem solutions do we need to store? LCS(X 1 j, Y 1 k ) two different indices 6/12/212 6:57 PM gdeepak.com 55
56 Step 2: Build the solution from the bottom up LCS( X, Y) 1 LCS( X1... n 1, Y1... m 1) max( LCS( X1... n 1, Y), LCS( X, Y 1... m 1 ) if x n y m otherwise What types of subproblem solutions do we need to store? LCS(X 1 j, Y 1 k ) LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j 6/12/212 6:57 PM gdeepak.com 56
57 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i j y j B D C A B A x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B 6/12/212 6:57 PM gdeepak.com 57
58 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A 6/12/212 6:57 PM gdeepak.com 58
59 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A? LCS(A, B) 6/12/212 6:57 PM gdeepak.com 59
60 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A 6/12/212 6:57 PM gdeepak.com 6
61 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A? LCS(A, BDCA) 6/12/212 6:57 PM gdeepak.com 61
62 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A 1 LCS(A, BDCA) 6/12/212 6:57 PM gdeepak.com 62
63 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A ? LCS(ABCB, BDCAB) 6/12/212 6:57 PM gdeepak.com 63
64 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A LCS(ABCB, BDCAB) 6/12/212 6:57 PM gdeepak.com 64
65 LCS[ i, j] 1 LCS[ i max( LCS[ i 1, 1, j 1] j], LCS[ i, j 1] if x i y otherwise j i x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B j y j B D C A B A Where s the final answer? 6/12/212 6:57 PM gdeepak.com 65
66 Questions, Comments and Suggestions 6/12/212 6:57 PM gdeepak.com 66
67 Question 1 Any Dynamic Programming algorithm with n sub problems will run in O(n) time 6/12/212 6:57 PM gdeepak.com 67
68 Question 2 Memoization is the basis for a top-down alternative to the usual bottom-up version of dynamic programming. 6/12/212 6:57 PM gdeepak.com 68
69 Question 3 The Floyd-Warshall algorithm solves the all-pairs shortestpaths problem using dynamic programming. 6/12/212 6:57 PM gdeepak.com 69
CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017
CMSC 45 CMSC 45: Lecture Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct, Reading: Section. in KT. Dynamic Programming: In this lecture we begin our coverage of an important algorithm design
More informationUnit-5 Dynamic Programming 2016
5 Dynamic programming Overview, Applications - shortest path in graph, matrix multiplication, travelling salesman problem, Fibonacci Series. 20% 12 Origin: Richard Bellman, 1957 Programming referred to
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 16 Dynamic Programming (plus FFT Recap) Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Discrete Fourier Transform
More informationmemoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming
Dynamic Programming 1 Introduction to Dynamic Programming weighted interval scheduling the design of a recursive solution memoizing the recursion 2 Principles of Dynamic Programming memoization or iteration
More informationCMSC 451: Dynamic Programming
CMSC 41: Dynamic Programming Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Sections 6.1&6.2 of Algorithm Design by Kleinberg & Tardos. Dynamic Programming
More informationDynamic Programming. Outline and Reading. Computing Fibonacci
Dynamic Programming Dynamic Programming version 1.2 1 Outline and Reading Matrix Chain-Product ( 5.3.1) The General Technique ( 5.3.2) -1 Knapsac Problem ( 5.3.3) Dynamic Programming version 1.2 2 Computing
More informationCS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department
CS473-Algorithms I Lecture 1 Dynamic Programming 1 Introduction An algorithm design paradigm like divide-and-conquer Programming : A tabular method (not writing computer code) Divide-and-Conquer (DAC):
More informationOutline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)
Outline CS8 Introduction to Algorithms Lecture 9 April 9, 0 Divide and Conquer design paradigm matrix multiplication Dynamic programming design paradigm Fibonacci numbers weighted interval scheduling knapsack
More informationAlgorithmic Paradigms
Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into two or more sub -problems, solve each sub-problem
More informationData Structures and Algorithms Week 8
Data Structures and Algorithms Week 8 Dynamic programming Fibonacci numbers Optimization problems Matrix multiplication optimization Principles of dynamic programming Longest Common Subsequence Algorithm
More informationWe ve done. Now. Next
We ve done Matroid Theory Task scheduling problem (another matroid example) Dijkstra s algorithm (another greedy example) Dynamic Programming Now Matrix Chain Multiplication Longest Common Subsequence
More informationPresentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 25 Dynamic Programming Terrible Fibonacci Computation Fibonacci sequence: f = f(n) 2
More informationIntroduction to Algorithms
Introduction to Algorithms Dynamic Programming Well known algorithm design techniques: Brute-Force (iterative) ti algorithms Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic
More informationAlgorithm Design Techniques part I
Algorithm Design Techniques part I Divide-and-Conquer. Dynamic Programming DSA - lecture 8 - T.U.Cluj-Napoca - M. Joldos 1 Some Algorithm Design Techniques Top-Down Algorithms: Divide-and-Conquer Bottom-Up
More informationIntroduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16
600.463 Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16 11.1 Introduction Dynamic programming can be very confusing until you ve used it a
More informationChapter 6. Dynamic Programming. Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
Chapter 6 Dynamic Programming Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 Think recursively (this week)!!! Divide & conquer and Dynamic programming
More informationDynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16
Dynamic Programming Introduction, Weighted Interval Scheduling, Knapsack Tyler Moore CSE, SMU, Dallas, TX Lecture /6 Greedy. Build up a solution incrementally, myopically optimizing some local criterion.
More informationDynamic Programming II
June 9, 214 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity? DP: Longest common
More informationSo far... Finished looking at lower bounds and linear sorts.
So far... Finished looking at lower bounds and linear sorts. Next: Memoization -- Optimization problems - Dynamic programming A scheduling problem Matrix multiplication optimization Longest Common Subsequence
More informationThe Knapsack Problem an Introduction to Dynamic Programming. Slides based on Kevin Wayne / Pearson-Addison Wesley
The Knapsack Problem an Introduction to Dynamic Programming Slides based on Kevin Wayne / Pearson-Addison Wesley Different Problem Solving Approaches Greedy Algorithms Build up solutions in small steps
More informationWe augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real
14.3 Interval trees We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real numbers ], with Interval ]represents the set Open and half-open intervals
More informationIntroduction to Algorithms
Introduction to Algorithms 6.046J/18.401J LECTURE 12 Dynamic programming Longest common subsequence Optimal substructure Overlapping subproblems Prof. Charles E. Leiserson Dynamic programming Design technique,
More informationDynamic Programming (Part #2)
Dynamic Programming (Part #) Introduction to Algorithms MIT Press (Chapter 5) Matrix-Chain Multiplication Problem: given a sequence A, A,, A n, compute the product: A A A n Matrix compatibility: C = A
More informationLecture 8. Dynamic Programming
Lecture 8. Dynamic Programming T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018
More informationDynamic Programming Group Exercises
Name: Name: Name: Dynamic Programming Group Exercises Adapted from material by Cole Frederick Please work the following problems in groups of 2 or 3. Use additional paper as needed, and staple the sheets
More informationIN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech
IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech License CC BY-NC-SA 2.0 http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Outline Previously on IN101 Python s anatomy Functions,
More informationCS 231: Algorithmic Problem Solving
CS 231: Algorithmic Problem Solving Naomi Nishimura Module 5 Date of this version: June 14, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides
More informationAlgorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications
lgorithmic Paradigms reed. Build up a solution incrementally, only optimizing some local criterion. hapter Dynamic Programming Divide-and-conquer. Break up a problem into two sub-problems, solve each sub-problem
More informationModule 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm
Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm This module 27 focuses on introducing dynamic programming design strategy and applying it to problems like chained matrix
More information12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares
12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares Optimal substructure Dynamic programming is typically applied to optimization problems. An optimal solution to the original
More informationCS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya
CS60020: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) Given two
More informationLecture 16: Introduction to Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY
Lecture 16: Introduction to Dynamic Programming Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem of the Day
More informationDynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)
Matrix Chain-Products (not in book) is a general algorithm design paradigm. Rather than give the general structure, let us first give a motivating example: Matrix Chain-Products Review: Matrix Multiplication.
More informationDynamic Programming part 2
Dynamic Programming part 2 Week 7 Objectives More dynamic programming examples - Matrix Multiplication Parenthesis - Longest Common Subsequence Subproblem Optimal structure Defining the dynamic recurrence
More informationECE608 - Chapter 15 answers
¼ À ÈÌ Ê ½ ÈÊÇ Ä ÅË ½µ ½ º¾¹¾ ¾µ ½ º¾¹ µ ½ º¾¹ µ ½ º ¹½ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹ ½¼µ ½ º ¹ ½½µ ½ ¹ ½ ECE608 - Chapter 15 answers (1) CLR 15.2-2 MATRIX CHAIN MULTIPLY(A, s, i, j) 1. if
More informationChapter 3 Dynamic programming
Chapter 3 Dynamic programming 1 Dynamic programming also solve a problem by combining the solutions to subproblems. But dynamic programming considers the situation that some subproblems will be called
More informationCLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION
CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION 1. What is performance measurement? 2. What is an algorithm? 3. How the algorithm is good? 4. What are the
More informationSankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment
Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment
More informationCMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk
CMPS 00 Fall 04 Dynamic Programming Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 9/30/4 CMPS 00 Intro. to Algorithms Dynamic programming Algorithm design technique
More informationDynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms
Entwurf und Analyse von Algorithmen Dynamic Programming Overview Introduction Example 1 When and how to apply this method Example 2 Final remarks Introduction: when recursion is inefficient Example: Calculation
More informationLecture 22: Dynamic Programming
Lecture 22: Dynamic Programming COSC242: Algorithms and Data Structures Brendan McCane Department of Computer Science, University of Otago Dynamic programming The iterative and memoised algorithms for
More informationElements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples
Elements of Dynamic Programming COSC 3A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken from Monica Nicolescu,
More information10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left
13.2 Rotations MAT-72006 AA+DS, Fall 2013 24-Oct-13 368 LEFT-ROTATE(, ) 1. // set 2. // s left subtree s right subtree 3. if 4. 5. // link s parent to 6. if == 7. 8. elseif == 9. 10. else 11. // put x
More informationAnalysis of Algorithms. Unit 4 - Analysis of well known Algorithms
Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms
More information1 Non greedy algorithms (which we should have covered
1 Non greedy algorithms (which we should have covered earlier) 1.1 Floyd Warshall algorithm This algorithm solves the all-pairs shortest paths problem, which is a problem where we want to find the shortest
More informationGreedy algorithms is another useful way for solving optimization problems.
Greedy Algorithms Greedy algorithms is another useful way for solving optimization problems. Optimization Problems For the given input, we are seeking solutions that must satisfy certain conditions. These
More informationDynamic Programming. Lecture Overview Introduction
Lecture 12 Dynamic Programming 12.1 Overview Dynamic Programming is a powerful technique that allows one to solve many different types of problems in time O(n 2 ) or O(n 3 ) for which a naive approach
More informationChapter 6. Dynamic Programming
Chapter 6 Dynamic Programming We began our study of algorithmic techniques with greedy algorithms, which in some sense form the most natural approach to algorithm design. Faced with a new computational
More informationHomework3: Dynamic Programming - Answers
Most Exercises are from your textbook: Homework3: Dynamic Programming - Answers 1. For the Rod Cutting problem (covered in lecture) modify the given top-down memoized algorithm (includes two procedures)
More informationSubsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico
Subsequence Definition CS 461, Lecture 8 Jared Saia University of New Mexico Assume given sequence X = x 1, x 2,..., x m Let Z = z 1, z 2,..., z l Then Z is a subsequence of X if there exists a strictly
More informationComputer Sciences Department 1
1 Advanced Design and Analysis Techniques (15.1, 15.2, 15.3, 15.4 and 15.5) 3 Objectives Problem Formulation Examples The Basic Problem Principle of optimality Important techniques: dynamic programming
More informationLecture 13: Chain Matrix Multiplication
Lecture 3: Chain Matrix Multiplication CLRS Section 5.2 Revised April 7, 2003 Outline of this Lecture Recalling matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm
More informationContents. 1 Introduction. 2 Searching and Traversal Techniques. Preface... (vii) Acknowledgements... (ix)
Contents Preface... (vii) Acknowledgements... (ix) 1 Introduction 1.1 Algorithm 1 1.2 Life Cycle of Design and Analysis of Algorithm 2 1.3 Pseudo-Code for Expressing Algorithms 5 1.4 Recursive Algorithms
More informationChapter 6. Dynamic Programming
Chapter 6 Dynamic Programming CS 573: Algorithms, Fall 203 September 2, 203 6. Maximum Weighted Independent Set in Trees 6..0. Maximum Weight Independent Set Problem Input Graph G = (V, E) and weights
More informationAlgorithms for Data Science
Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Thursday, October 1, 2015 Outline 1 Recap 2 Shortest paths in graphs with non-negative edge weights (Dijkstra
More informationCSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2
CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 11: Dynamic Programming, Part 2 Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: continue with DP (Knapsack, All-Pairs SPs, )
More informationComputer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Dynamic Programming
Computer Science 385 Design and Analysis of Algorithms Siena College Spring 29 Topic Notes: Dynamic Programming We next consider dynamic programming, a technique for designing algorithms to solve problems
More informationAlgorithms: COMP3121/3821/9101/9801
NEW SOUTH WALES Algorithms: COMP3121/3821/9101/9801 Aleks Ignjatović School of Computer Science and Engineering University of New South Wales TOPIC 5: DYNAMIC PROGRAMMING COMP3121/3821/9101/9801 1 / 38
More informationDepartment of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I
MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency
More informationCSC 505, Spring 2005 Week 6 Lectures page 1 of 9
CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 Objectives: learn general strategies for problems about order statistics learn how to find the median (or k-th largest) in linear average-case number of
More informationLecture 6: Combinatorics Steven Skiena. skiena
Lecture 6: Combinatorics Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Learning to Count Combinatorics problems are
More informationL.J. Institute of Engineering & Technology Semester: VIII (2016)
Subject Name: Design & Analysis of Algorithm Subject Code:1810 Faculties: Mitesh Thakkar Sr. UNIT-1 Basics of Algorithms and Mathematics No 1 What is an algorithm? What do you mean by correct algorithm?
More informationDesign and Analysis of Algorithms
Design and Analysis of Algorithms CSE 5311 Lecture 16 Greedy algorithms Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Overview A greedy
More informationEfficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms
Efficient Sequential Algorithms, Comp309 Part 1: Algorithmic Paradigms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press
More informationF(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)
Algorithms Dana Shapira Lesson #4: Dynamic programming Fibonacci Series F()= F()= F(n)=F(n-)+F(n-) Write a Divide and Conquer Algorithm! What is its running time? Binomial Coefficients n! n = ( )! n! Recursive
More informationResources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)
Orders of Growth of Processes Today s topics Resources used by a program to solve a problem of size n Time Space Define order of growth Visualizing resources utilization using our model of evaluation Relating
More informationUnit 4: Dynamic Programming
Unit 4: Dynamic Programming Course contents: Assembly-line scheduling Matrix-chain multiplication Longest common subsequence Optimal binary search trees Applications: Cell flipping, rod cutting, optimal
More informationCS141: Intermediate Data Structures and Algorithms Greedy Algorithms
CS141: Intermediate Data Structures and Algorithms Greedy Algorithms Amr Magdy Activity Selection Problem Given a set of activities S = {a 1, a 2,, a n } where each activity i has a start time s i and
More informationSpecial Topics on Algorithms Fall 2017 Dynamic Programming. Vangelis Markakis, Ioannis Milis and George Zois
Special Topics on Algorithms Fall 2017 Dynamic Programming Vangelis Markakis, Ioannis Milis and George Zois Basic Algorithmic Techniques Content Dynamic Programming Introduc
More informationCS 170 DISCUSSION 8 DYNAMIC PROGRAMMING. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17
CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17 DYNAMIC PROGRAMMING Recursive problems uses the subproblem(s) solve the current one. Dynamic
More information6. Algorithm Design Techniques
6. Algorithm Design Techniques 6. Algorithm Design Techniques 6.1 Greedy algorithms 6.2 Divide and conquer 6.3 Dynamic Programming 6.4 Randomized Algorithms 6.5 Backtracking Algorithms Malek Mouhoub, CS340
More informationPartha Sarathi Manal
MA 515: Introduction to Algorithms & MA353 : Design and Analysis of Algorithms [3-0-0-6] Lecture 29 http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.
More informationDynamic Programming. Applications. Applications. Applications. Algorithm Design 6.1, 6.2, 6.3
Set of weighted intervals with start and finishing times Goal: find maimum weight subset of non-overlapping intervals Dnamic Programming Algorithm Design.,.,. j j j j8 Given n points in the plane find
More informationUnit-2 Divide and conquer 2016
2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of
More informationDesign and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶
Design and Analysis of Algorithms 演算法設計與分析 Lecture 7 April 15, 2015 洪國寶 1 Course information (5/5) Grading (Tentative) Homework 25% (You may collaborate when solving the homework, however when writing
More informationCSC 373 Lecture # 3 Instructor: Milad Eftekhar
Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred
More information1. Basic idea: Use smaller instances of the problem to find the solution of larger instances
Chapter 8. Dynamic Programming CmSc Intro to Algorithms. Basic idea: Use smaller instances of the problem to find the solution of larger instances Example : Fibonacci numbers F = F = F n = F n- + F n-
More informationDynamic Programming 1
Dynamic Programming 1 Jie Wang University of Massachusetts Lowell Department of Computer Science 1 I thank Prof. Zachary Kissel of Merrimack College for sharing his lecture notes with me; some of the examples
More informationCS2223 Algorithms B Term 2013 Exam 3 Solutions
CS2223 Algorithms B Term 2013 Exam 3 Solutions Dec. 17, 2013 By Prof. Carolina Ruiz Dept. of Computer Science WPI PROBLEM I: Dynamic Programming (40 points) Consider the problem of calculating the binomial
More informationEND-TERM EXAMINATION
(Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt
More informationDynamic Programmming: Activity Selection
Dynamic Programmming: Activity Selection Select the maximum number of non-overlapping activities from a set of n activities A = {a 1,, a n } (sorted by finish times). Identify easier subproblems to solve.
More informationShai Simonson, ADU Page 1 2/20/2001
Shai Simonson, ADU Page 1 2/20/2001 ArsDigita University Month 5: Algorithms - Professor Shai Simonson Lectures Second Two weeks In this half of the course we discuss two topics. The first is techniques
More informationCMSC351 - Fall 2014, Homework #4
CMSC351 - Fall 2014, Homework #4 Due: November 14th at the start of class PRINT Name: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,
More informationLongest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017
CS161 Lecture 13 Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017 1 Overview Last lecture, we talked about dynamic programming
More informationData Structures and Algorithms (CSCI 340)
University of Wisconsin Parkside Fall Semester 2008 Department of Computer Science Prof. Dr. F. Seutter Data Structures and Algorithms (CSCI 340) Homework Assignments The numbering of the problems refers
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 16 Dynamic Programming Least Common Subsequence Saving space Adam Smith Least Common Subsequence A.k.a. sequence alignment edit distance Longest Common Subsequence
More informationA BRIEF INTRODUCTION TO DYNAMIC PROGRAMMING (DP) by Amarnath Kasibhatla Nanocad Lab University of California, Los Angeles 04/21/2010
A BRIEF INTRODUCTION TO DYNAMIC PROGRAMMING (DP) by Amarnath Kasibhatla Nanocad Lab University of California, Los Angeles 04/21/2010 Overview What is DP? Characteristics of DP Formulation Examples Disadvantages
More informationDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Module 1 OBJECTIVE: Algorithms play the central role in both the science and the practice of computing. There are compelling reasons to study algorithms.
More informationAlgorithms: Dynamic Programming
Algorithms: Dynamic Programming Amotz Bar-Noy CUNY Spring 2012 Amotz Bar-Noy (CUNY) Dynamic Programming Spring 2012 1 / 58 Dynamic Programming General Strategy: Solve recursively the problem top-down based
More informationAnalysis of Algorithms Prof. Karen Daniels
UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization Problems Greedy Algorithms Algorithmic Paradigm Context
More informationDynamic Programming Matrix-chain Multiplication
1 / 32 Dynamic Programming Matrix-chain Multiplication CS 584: Algorithm Design and Analysis Daniel Leblanc 1 1 Senior Adjunct Instructor Portland State University Maseeh College of Engineering and Computer
More informationLECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS
Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq
More informationChain Matrix Multiplication
Chain Matrix Multiplication Version of November 5, 2014 Version of November 5, 2014 Chain Matrix Multiplication 1 / 27 Outline Outline Review of matrix multiplication. The chain matrix multiplication problem.
More informationCSE 417 Dynamic Programming (pt 4) Sub-problems on Trees
CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees Reminders > HW4 is due today > HW5 will be posted shortly Dynamic Programming Review > Apply the steps... 1. Describe solution in terms of solution
More informationDeliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort
More Sorting Deliverables Quick Sort Randomized Quick Sort Median Order statistics Heap Sort External Merge Sort Copyright @ gdeepak.com 2 Quick Sort Divide and conquer algorithm which relies on a partition
More informationCS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin)
CS 7 - lgorithms Dynamic programming and memoization (ased on slides by Luebke, Lim, Wenbin) When to Use Dynamic Programming Usually used to solve Optimization problems Usually the problem can be formulated
More information15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015
15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015 Dynamic Programming is a powerful technique that allows one to solve many different types
More informationLectures 12 and 13 Dynamic programming: weighted interval scheduling
Lectures 12 and 13 Dynamic programming: weighted interval scheduling COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Lectures 12-13: Dynamic Programming 1 Overview Last week: Graph
More informationECE608 - Chapter 16 answers
¼ À ÈÌ Ê ½ ÈÊÇ Ä ÅË ½µ ½ º½¹ ¾µ ½ º½¹ µ ½ º¾¹½ µ ½ º¾¹¾ µ ½ º¾¹ µ ½ º ¹ µ ½ º ¹ µ ½ ¹½ ½ ECE68 - Chapter 6 answers () CLR 6.-4 Let S be the set of n activities. The obvious solution of using Greedy-Activity-
More information1 Dynamic Programming
CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems
More information