Analysis of Algorithms CS 477/677

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Dynamic Programming.
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
1 Longest Common Subsequence (LCS) Problem: Given sequences x[1..m] and y[1..n], find a longest common subsequence of both. Example: x=ABCBDAB and y=BDCABA,
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.
Analysis of Algorithms CS 477/677
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming UNC Chapel Hill Z. Guo.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 17.
CS 8833 Algorithms Algorithms Dynamic Programming.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
COSC 3101A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
1 Chapter 6-1 Dynamic Programming Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy algorithms: CSC317
Dynamic Programming Typically applied to optimization problems
Dynamic Programming (DP)
David Meredith Dynamic programming David Meredith
Dynamic Programming (DP)
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
Least common subsequence:
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
CS200: Algorithm Analysis
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
CS38 Introduction to Algorithms
Dynamic Programming Comp 122, Fall 2004.
CSCE 411 Design and Analysis of Algorithms
Prepared by Chen & Po-Chuan 2016/03/29
Lecture 33 CSE 331 Nov 18, 2016.
Chapter 6 Dynamic Programming
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Chapter 6 Dynamic Programming
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
CS 3343: Analysis of Algorithms
Advanced Algorithms Analysis and Design
Dynamic Programming.
CS6045: Advanced Algorithms
Longest Common Subsequence
Analysis of Algorithms CS 477/677
Data Structure and Algorithms
Chapter 15: Dynamic Programming II
Dynamic Programming Comp 122, Fall 2004.
Lecture 8. Paradigm #6 Dynamic Programming
Introduction to Algorithms: Dynamic Programming
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Lecture 36 CSE 331 Nov 28, 2011.
Longest Common Subsequence
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
Algorithms and Data Structures Lecture X
Longest common subsequence (LCS)
Matrix Chain Multiplication
Longest Common Subsequence
Dynamic Programming.
Longest Common Subsequence
Analysis of Algorithms CS 477/677
Week 8 - Friday CS322.
Presentation transcript:

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18

Dynamic Programming Used for optimization problems Find a solution with the optimal value (minimum or maximum) A set of choices must be made to get an optimal solution There may be many solutions that return the optimal value: we want to find one of them CS 477/677 - Lecture 18

Elements of Dynamic Programming Optimal Substructure An optimal solution to a problem contains within it an optimal solution to subproblems Optimal solution to the entire problem is build in a bottom-up manner from optimal solutions to subproblems Overlapping Subproblems If a recursive algorithm revisits the same subproblems again and again ⇒ the problem has overlapping subproblems CS 477/677 - Lecture 18

Optimal Substructure - Examples Assembly line Fastest way of going through a station j contains: the fastest way of going through station j-1 on either line Matrix multiplication Optimal parenthesization of Ai ∙ Ai+1 ∙∙∙ Aj that splits the product between Ak and Ak+1 contains: an optimal solution to the problem of parenthesizing Ai..k an optimal solution to the problem of parenthesizing Ak+1..j CS 477/677 - Lecture 18

Parameters of Optimal Substructure How many subproblems are used in an optimal solution for the original problem Assembly line: Matrix multiplication: How many choices we have in determining which subproblems to use in an optimal solution One subproblem (the line that gives best time) Two subproblems (subproducts Ai..k, Ak+1..j) Two choices (line 1 or line 2) j - i choices for k (splitting the product) CS 477/677 - Lecture 18

Parameters of Optimal Substructure Intuitively, the running time of a dynamic programming algorithm depends on two factors: Number of subproblems overall How many choices we examine for each subproblem Assembly line Θ(n) subproblems (n stations) 2 choices for each subproblem Matrix multiplication: Θ(n2) subproblems (1 ≤ i ≤ j ≤ n) At most n-1 choices Θ(n) overall Θ(n3) overall CS 477/677 - Lecture 18

Longest Common Subsequence Given two sequences X = ⟨x1, x2, …, xm⟩ Y = ⟨y1, y2, …, yn⟩ find a maximum length common subsequence (LCS) of X and Y E.g.: X = ⟨A, B, C, B, D, A, B⟩ Subsequence of X: A subset of elements in the sequence taken in order (but not necessarily consecutive) ⟨A, B, D⟩, ⟨B, C, D, B⟩, etc. CS 477/677 - Lecture 18 7

Example X = ⟨A, B, C, B, D, A, B⟩ X = ⟨A, B, C, B, D, A, B⟩ Y = ⟨B, D, C, A, B, A⟩ Y = ⟨B, D, C, A, B, A⟩ ⟨B, C, B, A⟩ and ⟨B, D, A, B⟩ are longest common subsequences of X and Y (length = 4) ⟨B, C, A⟩ , however is not a LCS of X and Y CS 477/677 - Lecture 18 8

Brute-Force Solution For every subsequence of X, check whether it’s a subsequence of Y There are 2m subsequences of X to check Each subsequence takes Θ(n) time to check scan Y for first letter, from there scan for second, and so on Running time: Θ(n2m) CS 477/677 - Lecture 18

1. Making the choice ✕ ✕ X = ⟨A, B, D, E⟩ Y = ⟨Z, B, E⟩ Choice: include one element into the common sequence (E) and solve the resulting subproblem X = ⟨A, B, D, G⟩ Y = ⟨Z, B, D⟩ Choice: exclude an element from a string and solve the resulting subproblem ✕ X = ⟨A, B, D, G⟩ Y = ⟨Z, B, D⟩ ✕ CS 477/677 - Lecture 18

Notations Given a sequence X = ⟨x1, x2, …, xm⟩ we define the i-th prefix of X, for i = 0, 1, 2, …, m Xi = ⟨x1, x2, …, xi⟩ c[i, j] = the length of a LCS of the sequences Xi = ⟨x1, x2, …, xi⟩ and Yj = ⟨y1, y2, …, yj⟩ CS 477/677 - Lecture 18

2. A Recursive Solution Case 1: xi = yj e.g.: Xi = ⟨A, B, D, E⟩ Yj = ⟨Z, B, E⟩ Append xi = yj to the LCS of Xi-1 and Yj-1 Must find a LCS of Xi-1 and Yj-1 ⇒ optimal solution to a problem includes optimal solutions to subproblems c[i, j] = c[i - 1, j - 1] + 1 CS 477/677 - Lecture 18

2. A Recursive Solution Case 2: xi ≠ yj e.g.: Xi = ⟨A, B, D, G⟩ Yj = ⟨Z, B, D⟩ Must solve two problems find a LCS of Xi-1 and Yj: Xi-1 = ⟨A, B, D ⟩ and Yj = ⟨Z, B, D⟩ find a LCS of Xi and Yj-1: Xi = ⟨A, B, D, G ⟩ and Yj = ⟨Z, B ⟩ Optimal solution to a problem includes optimal solutions to subproblems c[i, j] = max { c[i - 1, j], c[i, j-1] } CS 477/677 - Lecture 18

3. Computing the Length of the LCS 0 if i = 0 or j = 0 c[i, j] = c[i-1, j-1] + 1 if xi = yj max(c[i, j-1], c[i-1, j]) if xi ≠ yj 1 2 n yj: y1 y2 yn Xi: 1 x1 first 2 x2 second i m xm j CS 477/677 - Lecture 18

4. Additional Information A matrix b[i, j]: For a subproblem [i, j] it tells us what choice was made to obtain the optimal value If xi = yj b[i, j] = “ ” Else, if c[i - 1, j] ≥ c[i, j-1] b[i, j] = “ ↑ ” else b[i, j] = “ ←” 0 if i,j = 0 c[i, j] = c[i-1, j-1] + 1 if xi = yj max(c[i, j-1], c[i-1, j]) if xi ≠ yj 1 2 3 n b & c: yj: A C D F xi 1 A 2 B c[i,j-1] c[i-1,j] i 3 C m D j CS 477/677 - Lecture 18

LCS-LENGTH(X, Y, m, n) Running time: Θ(mn) for i ← 1 to m do c[i, 0] ← 0 for j ← 0 to n do c[0, j] ← 0 do for j ← 1 to n do if xi = yj then c[i, j] ← c[i - 1, j - 1] + 1 b[i, j ] ← “ ” else if c[i - 1, j] ≥ c[i, j - 1] then c[i, j] ← c[i - 1, j] b[i, j] ← “↑” else c[i, j] ← c[i, j - 1] b[i, j] ← “←” return c and b The length of the LCS is zero if one of the sequences is empty Case 1: xi = yj Case 2: xi ≠ yj Running time: Θ(mn) CS 477/677 - Lecture 18

Example X = ⟨A, B, C, B, D, A, B⟩ Y = ⟨B, D, C, A, B, A⟩ If xi = yj 0 if i = 0 or j = 0 c[i, j] = c[i-1, j-1] + 1 if xi = yj max(c[i, j-1], c[i-1, j]) if xi≠ yj X = ⟨A, B, C, B, D, A, B⟩ Y = ⟨B, D, C, A, B, A⟩ 1 2 3 4 5 6 If xi = yj b[i, j] = “ ” Else if c[i - 1, j] ≥ c[i, j-1] b[i, j] = “ ↑ ” else b[i, j] = “ ← ” yj B D C A B A xi 1 A ↑ ↑ ↑ 1 1 ←1 2 B 1 ↑ 1 2 ←1 ←1 ←2 3 C ↑ 1 2 ←2 4 B 1 ↑ 2 3 ←3 5 D ↑ 1 2 3 6 A ↑ 1 2 3 4 7 B 1 ↑ 2 3 4 CS 477/677 - Lecture 18

4. Constructing a LCS Start at b[m, n] and follow the arrows When we encounter a “ “ in b[i, j] ⇒ xi = yj is an element of the LCS 1 2 3 4 5 6 yj B D C A B A xi 1 A ↑ ↑ ↑ 1 1 ←1 2 B 1 ↑ 1 2 ←1 ←1 ←2 3 C ↑ 1 2 ←2 4 B 1 ↑ 2 3 ←3 5 D ↑ 1 2 3 6 A ↑ 1 2 3 4 7 B 1 ↑ 2 3 4 CS 477/677 - Lecture 18

PRINT-LCS(b, X, i, j) if i = 0 or j = 0 then return if b[i, j] = “ ” then PRINT-LCS(b, X, i - 1, j - 1) print xi else if b[i, j] = “↑” then PRINT-LCS(b, X, i - 1, j) else PRINT-LCS(b, X, i, j - 1) Initial call: PRINT-LCS(b, X, length[X], length[Y]) Running time: Θ(m + n) CS 477/677 - Lecture 18

Improving the Code What can we say about how each entry c[i, j] is computed? It depends only on c[i -1, j - 1], c[i - 1, j], and c[i, j - 1] Eliminate table b and compute in O(1) which of the three values was used to compute c[i, j] We save Θ(mn) space from table b However, we do not asymptotically decrease the auxiliary space requirements: still need table c CS 477/677 - Lecture 18

Improving the Code If we only need the length of the LCS LCS-LENGTH works only on two rows of c at a time The row being computed and the previous row We can reduce the asymptotic space requirements by storing only these two rows CS 477/677 - Lecture 18

Weighted Interval Scheduling Job j starts at sj, finishes at fj, and has weight or value vj Two jobs are compatible if they don't overlap Goal: find maximum weight subset of mutually compatible jobs a b c d e f g h Time 1 2 3 CS 477/677 - Lecture 18 4 5 6 7 8 9 10 11

Weighted Interval Scheduling Label jobs by finishing time: f1 ≤ f2 ≤ . . . ≤ fn Def. p(j) = largest index i < j such that job i is compatible with j Ex: p(8) = 5, p(7) = 3, p(2) = 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 Time 1 2 3 4 5 6 7 8 9 10 11 CS 477/677 - Lecture 18

1. Making the Choice 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 optimal substructure CS 477/677 - Lecture 18

2. A Recursive Solution 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) OPT(j) = vj + OPT(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 OPT(i) = OPT(j-1) CS 477/677 - Lecture 18

Top-Down Recursive Algorithm WRONG! Input: n, s1,…,sn , f1,…,fn , v1,…,vn Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn Compute p(1), p(2), …, p(n) Compute-Opt(j) { if (j = 0) return 0 else return max(vj + Compute-Opt(p(j)), Compute-Opt(j-1)) } CS 477/677 - Lecture 18

3. Compute the Optimal Value Compute values in increasing order of j Input: n, s1,…,sn , f1,…,fn , v1,…,vn Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn Compute p(1), p(2), …, p(n) Iterative-Compute-Opt { M[0] = 0 for j = 1 to n M[j] = max(vj + M[p(j)], M[j-1]) } CS 477/677 - Lecture 18

Memoized Version Store results of each sub-problem; lookup as needed Input: n, s1,…,sn , f1,…,fn , v1,…,vn Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn. Compute p(1), p(2), …, p(n) for j = 1 to n M[j] = empty M[j] = 0 M-Compute-Opt(j) { if (M[j] is empty) M[j] = max(vj + M-Compute-Opt(p(j)), M-Compute-Opt(j-1)) return M[j] } global array CS 477/677 - Lecture 18

4. Finding the Optimal Solution Two options Store additional information: at each time step store either j or p(j) – value that gave the optimal solution Recursively find the solution by iterating through array M Find-Solution(j) { if (j = 0) output nothing else if (vj + M[p(j)] > M[j-1]) print j Find-Solution(p(j)) else Find-Solution(j-1) } CS 477/677 - Lecture 18

An Example CS 477/677 - Lecture 18

Readings Chapters 14, 15 CS 477/677 - Lecture 18