Download presentation
Presentation is loading. Please wait.
Published byWinfred Webster Modified over 8 years ago
1
CS 8833 Algorithms Algorithms Dynamic Programming
2
CS 8833 Algorithms Dynamic Programming l General approach – combine solutions to subproblems to get the solution to an problem l Unlike Divide and Conquer in that –subproblems are dependent rather than independent –bottom-up approach –save values of subproblems in a table and use more than one time
3
CS 8833 Algorithms Usual Approach l Start with the smallest, simplest subproblems l Combine “appropriate” subproblem solutions to get a solution to the bigger problem l If you have a D&C algorithm that does a lot of duplicate computation, you can often find a DP solution that is more efficient
4
CS 8833 Algorithms A Simple Example l Calculating binomial coefficients l n choose k is the number of different combinations of n things taken k at a time l These are also the coefficients of the binomial expansion (x+y) n
5
CS 8833 Algorithms Two Definitions
6
CS 8833 Algorithms Algorithm for Recursive Definition function C(n,k) if k = 0 or k = n then return 1 else return C(n-1, k-1) + C(n-1, k)
7
CS 8833 Algorithms C(5,3) C(4,2)C(4,3) C(3,1)C(3,2) C(3,3)
8
CS 8833 Algorithms Complexity of D & C Algorithm Time complexity is (n!) l But we did a lot of duplicate computation l Dynamic programming approach –Store solutions to sub-problems in a table –Bottom-up approach
9
CS 8833 Algorithms n k 012345012345 0 1 2 3 n k
10
CS 8833 Algorithms Analysis of DP Version l Time complexity O(n k) l Storage requirements –full table O(n k) –better solution
11
CS 8833 Algorithms Typical Problem l Dynamic Programming is often used for optimization problems that satisfy the principle of optimality l Principle of optimality –In an optimal sequence of decisions or choices, each subsequence must be optimal
12
CS 8833 Algorithms Optimization Problems l Problem has many possible solutions l Each solution has a value l Goal is to find the solution with the optimal value
13
CS 8833 Algorithms Steps in DP Solutions to Optimization Problems 1 Characterize the structure of an optimal solution 2 Recursively define the value of an optimal solution 3 Compute the value of an optimal solution in a bottom-up manner 4 Construct an optimal solution from computed information
14
CS 8833 Algorithms Matrix Chain Multiplication A 1 A 2 A 3 A n l Matrix multiplication is associative l All ways that a sequence can be parenthesized give the same answer l But some are much less expensive to compute
15
CS 8833 Algorithms Matrix Chain Multiplication Problem Given a chain of n matrices, where i = 1, 2,..., n and matrix A i has dimension p i-1 p i, fully parenthesize the product A 1 A 2 A n in a way that minimizes the number of scalar multiplications
16
CS 8833 Algorithms Example Matrix Dimensions A 13 x 5 B5 X 89 C89 X 3 D3 X 34 M = A B C D13 x 34
17
CS 8833 Algorithms ParenthesizationScalar multiplications 1 ((A B) C) D10,582 2 (A B) (C D)54,201 3 (A (B C)) D 2, 856 4 A ((B C) D) 4, 055 5 A (B (C D))26,418 1 13 x 5 x 89 to get (A B) 13 x 89 result 13 x 89 x 3 to get ((AB)C) 13 x 3 result 13 x 3 x 34 to get (((AB)C)D) 13 x 34
18
CS 8833 Algorithms Number of Parenthesizations
19
CS 8833 Algorithms T(n) ways to parenthesize n 12345 10 15 T(n)1125144,8622,674,440
20
CS 8833 Algorithms Steps in DP Solutions to Optimization Problems 1 Characterize the structure of an optimal solution 2 Recursively define the value of an optimal solution 3 Compute the value of an optimal solution in a bottom-up manner 4 Construct an optimal solution from computed information
21
CS 8833 Algorithms Step 1 l Show that the principle of optimality applies –An optimal solution to the problem contains within it optimal solutions to sub-problems –Let A i..j be the optimal way to parenthesize A i A i+1...A j –Suppose the optimal solution has the first split at position k A 1..k A k+1..n –Each of these sub-problems must be optimally parenthesized
22
CS 8833 Algorithms Step 2 l Define value of the optimal solution recursively in terms of optimal solutions to sub-problems. l Consider the problem A i..j l Let m[i,j] be the minimum number of scalar multiplications needed to compute matrix A i..j l The cost of the cheapest way to compute A 1..n is m[1,n]
23
CS 8833 Algorithms Step 2 continued l Define m[i,j] If i = j the chain has one matrix and the cost m[i,i] = 0 for all i n If i < j Assume the optimal split is at position k where i k < j m[i,j] = m[i,k] + m[k+1,j] + p i-1 p k p j to compute A i..k and A k+1..j
24
CS 8833 Algorithms Step 2 continued l Problem: we don’t know what the value for k is, but there are only j-i possibilities
25
CS 8833 Algorithms Step 3 l Compute optimal cost by using a bottom-up approach l Example: 4 matrix chain Matrix Dimensions A 1 100 x 1 A 2 1 x 100 A 3 100 x 1 A 4 1 x 100 l p o p 1 p 2 p 3 p 4 100 1 100 1 100
26
CS 8833 Algorithms i j 12341234 1 2 3 4 p o p 1 p 2 p 3 p 4 100 1 100 1 100 m i j 12341234 1 2 3 4 s
27
CS 8833 Algorithms MATRIX-CHAIN-ORDER(p) 1 n length[p] - 1 2 for i 1 to n 3 do m[i,i] 0 4 for l 2 to n 5 do for i 1 to n - l + 1 6 do j i + l - 1 7 m[i,j] 8 for k i to j - 1 9 do q m[i,k]+m[k+1,j]+ p i-1 p k p j 10 if q < m[i,j] 11 then m[i,j] = q 12 s[i,j] = k 13 return m and s
28
CS 8833 Algorithms Time and Space Complexity l Time complexity –Triply nested loops –O(n 3 ) l Space complexity –n x n two dimensional table –O(n 2 )
29
CS 8833 Algorithms Step 4: Constructing the Optimal Solution l Matrix-Chain-Order determines the optimal number of scalar multiplications l Does not directly compute the product l Step 4 of the dynamic programming paradigm is to construct an optimal solution from computed information. l The s matrix contains the optimal split for every level
30
CS 8833 Algorithms MATRIX-CHAIN-MULTIPLY(A,s,i,j) 1 if j > i 2 then X MATRIX-CHAIN-MULTIPLY(A,s,i,s[i,j]) 3 Y MATRIX-CHAIN-MULTIPLY(A,s,s[i,j]+1,j) 4 return MATRIX-MULTIPLY(X,Y) 5 else return A i Initial call MATRIX-CHAIN-MULTIPLY(A,s,1,n) where A =
31
CS 8833 Algorithms Algorithms Dynamic Programming Continued
32
CS 8833 Algorithms Comments on Dynamic Programming l When the problem can be reduced to several “overlapping” sub-problems. l All possible sub-problems are computed l Computation is done by maintaining a large matrix l Usually has large space requirement l Running times are usually at least quadratic
33
CS 8833 Algorithms Memoization l Variation on dynamic programming l Idea–memoize the natural but inefficient recursive algorithm l As each sub-problem is solved, store values in table l Initialize table with values that indicate if the value has been computed
34
CS 8833 Algorithms MEMOIZED-MATRIX-CHAIN(p) 1 n length(p) - 1 2 for i 1 to n 3 do for j i to n 4 do m[i,j] 5 return LOOKUP-CHAIN(p,1,n)
35
CS 8833 Algorithms LOOKUP-CHAIN(p,i,j) 1 if m[i,j] 2 then return m[i,j] 3 if i = j 4 then m[i,j] 5 else for k i to j - 1 6 do q LOOKUP-CHAIN(p,i,k) + LOOKUP-CHAIN(p,k+1,j) + p i-1 p k p j 7 if q < m[i,j] 8 then m[i,j] q 9 return m[i,j]
36
CS 8833 Algorithms Space and Time Requirements of Memoized Version Running time (n 3 ) Storage (n 2 )
37
CS 8833 Algorithms Longest Common Subsequence l Definition 1: Subsequence Given a sequence X = then another sequence Z = is a subsequence of X if there exists a strictly increasing sequence of indices of x such that for all j = 1,2,...k we have x i j = z j
38
CS 8833 Algorithms Example X = Z = Z is a subsequence of X with index sequence
39
CS 8833 Algorithms More Definitions l Definition 2: Common subsequence – Given 2 sequences X and Y, we say Z is a common subsequence of X and Y if Z is a subsequence of X and a subsequence of Y l Definition 3: Longest common subsequence problem – Given X = and Y = find a maximum length common subsequence of X and Y
40
CS 8833 Algorithms Example X = Y =
41
CS 8833 Algorithms Brute Force Algorithm 1 for every subsequence of X 2 Is there subsequence in Y? 3 If yes, is it longer than the longest subsequence found so far? Complexity?
42
CS 8833 Algorithms Yet More Definitions l Definition 4: Prefix of a subsequence If X =, the ith prefix of X for i = 0,1,...,m is X i = l Example – if X = then X 4 = and X 0 = <>
43
CS 8833 Algorithms Optimal Substructure l Theorem 16.1 Optimal Substructure of LCS Let X = and Y = be sequences and let Z = be any LCS of X and Y 1. if x m = y n then z k = x m = y n and z k-1 is an LCS of x m-1 and y n-1 2. if x m y n and z k x m Z is an LCS of X m-1 and Y 3. if x m y n and z k y n Z is an LCS of X m and Y n-1
44
CS 8833 Algorithms Sub-problem structure l Case 1 – if x m = y n then there is one sub-problem to solve find a LCS of X m-1 and Y n-1 and append x m l Case 2 – if x m y n then there are two sub-problems » find an LCS of X m and Y n-1 » find an LCS of X m-1 and Y n » pick the longer of the two
45
CS 8833 Algorithms Cost of Optimal Solution l Cost is length of the common subsequence l We want to pick the longest one l Let c[i,j] be the length of an LCS of the sequences X i and Y j l Base case is an empty subsequence-- then c[i,j] = 0 because there is no LCS
46
CS 8833 Algorithms The Recurrence
47
CS 8833 Algorithms Dynamic Programming Solution l Table c[0..m, 0..n] stores the length of an LCS of X i and Y j c[i,j] l Table b[1..m, 1..n] stores pointers to optimal sub-problem solutions
48
CS 8833 Algorithms C B R F T S Q 0 1 2 3 4 5 6 7 0 A 1 B 2 D 3 F 4 M 5 Q 6 Matrix C Y X
49
CS 8833 Algorithms C B R F T S Q 1 2 3 4 5 6 7 A 1 B 2 D 3 F 4 M 5 Q 6 Matrix B Y X j i
50
CS 8833 Algorithms LCS-LENGTH(X,Y) 1 m length[X] 2 n length[Y] 3 for i to m 4 do c[i,0] 5 for j to n 6 do c[0,j] 7 for i to m 8 do for j to n 9do if x i = y j then c[i,j] c[i-1,j-1] + 1 b[i,j] ” ” 12 else if c[i-1,j] c[i,j-1] 13 then c[i,j] c[i-1,j] 14 b[i,j] ” ” 15else c[i,j] c[i,j-1] 16 b[i,j] ” ” 17 return c and b
51
CS 8833 Algorithms PRINT-LCS (b,X,i,j) 1if i = 0 or j = 0 2 then return 3 if b[i,j] = ” ” 4 then PRINT-LCS(b,X,i-1,j-1) 5 print x i 6 else if b[i,j] ” ” 7 then PRINT-LCS(b,X,i-1,j) 8 else PRINT-LCS(b,X,i,j-1)
52
CS 8833 Algorithms Code Complexity l Time complexity l Space complexity l Improved space complexity?
53
CS 8833 Algorithms Optimal Polygon Triangulation l Turns out to be very similar to matrix chain multiplication l Mapping one problem to another kind of problem for which you already have a good solution is a useful concept
54
CS 8833 Algorithms Lots of definitions l A polygon is a piecewise-linear, closed curve in the plane. l The “pieces” are called sides. l A point joining two consecutive sides is called a vertex. l The set of points in the plane enclosed by a simple polygon forms the interior of the polygon. l the set of point ons the polygon forms its boundary l the set of points surrounding the polygon form its exterior
55
CS 8833 Algorithms Convex Polygons l A simple polygon is convex if given any two points on its boundary or in its interior, all points on the line segment drawn between them are contained in the polygon’s boundary or interior.
56
CS 8833 Algorithms Labeling a convex polygon P = v 0 = v n v1v1 v2v2 v3v3 v4v4 v5v5
57
CS 8833 Algorithms Chords v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5
58
CS 8833 Algorithms Triangulation l A triangulation of a polygon is a set T of chords of the polygon that divide the polygon into disjoint triangles. – No chords intersect – The set of chords T is maximal; every chord not in T intersects some chord in T l Every triangulation of an n-vertex convex polygon – has n-3 chords – divides polygon into n-2 triangles
59
CS 8833 Algorithms Optimal (polygon) triangulation problem l Given a convex polygon P = and a weight function w defined on triangles formed by sides and chords of P. – Find a triangulation that minimizes the sum of the weights of the triangles in the triangulation – One possible weight function is to minimize the sum of the lengths of the sides
60
CS 8833 Algorithms Correspondance of Parenthesization l We will show how to view both parenthesization and triangulation as parse trees and show the correspondence between the two views. l The result will be that a slight modification of the matrix chain problem can be used to solve the triangulation problem.
61
CS 8833 Algorithms ((A 1 (A 2 A 3 )(A 4 (A 5 A 6 ))) A 1 (A 2 A 3 ) A 4 (A 5 A 6 ) A1A1 A2A3A2A3 A2A2 A3A3 A4A4 A5A6A5A6 A5A5 A6A6
62
CS 8833 Algorithms v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6
63
v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 ((A 1 (A 2 A 3 )(A 4 (A 5 A 6 )))
64
CS 8833 Algorithms A 1 A 2 A 3 A 4 A 5 A 6 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 6 matrices 7 dimensions 6 sides 7 vertices p 0 p 1 p 2 p 3 p 4 p 5 p 6
65
CS 8833 Algorithms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.