Trevor Brown DC 2338, Office hour M3-4pm

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J/SMA5503
Advertisements

Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Overview What is Dynamic Programming? A Sequence of 4 Steps
Dynamic Programming.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
1 Theory I Algorithm Design and Analysis (11 - Edit distance and approximate string matching) Prof. Dr. Th. Ottmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
Algorithm Paradigms High Level Approach To solving a Class of Problems.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CS 8833 Algorithms Algorithms Dynamic Programming.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
6/13/20161 Greedy A Comparison. 6/13/20162 Greedy Solves an optimization problem: the solution is “best” in some sense. Greedy Strategy: –At each decision.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Greedy algorithms: CSC317
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Design & Analysis of Algorithm Dynamic Programming
Algorithmics - Lecture 11
CS 3343: Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
Lecture 5 Dynamic Programming
CS200: Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Dynamic Programming General Idea
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
ICS 353: Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
Longest Common Subsequence
Lecture 8. Paradigm #6 Dynamic Programming
Ch. 15: Dynamic Programming Ming-Te Chi
Dynamic Programming.
Dynamic Programming-- Longest Common Subsequence
Dynamic Programming General Idea
Introduction to Algorithms: Dynamic Programming
Dynamic Programming.
Trevor Brown CS 341: Algorithms Trevor Brown
Trevor Brown DC 2338, Office hour M3-4pm
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Dynamic Programming II DP over Intervals
Algorithms and Data Structures Lecture X
Analysis of Algorithms CS 477/677
Longest Common Subsequence
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Dynamic Programming.
Presentation transcript:

Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm CS 341: Algorithms Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm

This time DP: longest common subsequence (partially covered) Memoization VS dynamic programming DP: minimum length triangulation

Problem: Longest Common Subsequence (LCS)

Examples X=aaaaa Y=bbbbb Z=LCS(X,Y)=? Z=𝜖 (empty sequence) X=abcde Y=bcd Z=LCS(X,Y)=? Z=bcd X=abcde Y=labef Z=LCS(X,Y)=? Z=abe

Thinking about subproblems Entire problem: # characters in LCS(X,Y) How to reduce problem size? Reduce size of X or Y. Define X’ and Y’ as follows 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 Note to self: replace generic X, Y with actual strings you can grab onto… 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1

Consider an optimal solution Z Can we express Z in terms of X’ and Y’ instead of X and Y? By definition, Z = LCS(X,Y) 𝒁= 𝑧 1 𝑧 2 … 𝑧 ℓ−1 𝑧 ℓ 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1

Consider an optimal solution Z Can we express Z in terms of X’ and Y’ instead of X and Y? By definition, Z = LCS(X,Y) Suppose 𝒛 ℓ matches both 𝑥 𝑚 and 𝑦 𝑛 𝒁= 𝑧 1 𝑧 2 … 𝑧 ℓ−1 𝑧 ℓ Then 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) + 𝑧 ℓ 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 Consumed by being matched with yn 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 Consumed by being matched with xm 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1

Consider an optimal solution Z Can we express Z in terms of X’ and Y’ instead of X and Y? By definition, Z = LCS(X,Y) Suppose 𝒛 ℓ matches only 𝒙 𝒎 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ) 𝒁= 𝑧 1 𝑧 2 … 𝑧 ℓ−1 𝑧 ℓ Then 𝑍=𝐿𝐶𝑆(𝑋, 𝑌 ′ ) 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 Maybe still needed by Z 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 (Might be matched with something in Y’) 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 Not needed by Z Remove to shrink problem size!

Consider an optimal solution Z Can we express Z in terms of X’ and Y’ instead of X and Y? By definition, Z = LCS(X,Y) Suppose 𝒛 ℓ matches only 𝒚 𝒏 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ) 𝒁= 𝑧 1 𝑧 2 … 𝑧 ℓ−1 𝑧 ℓ Then 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝑌) 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 Not needed by Z 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 Maybe still needed by Z

Consider an optimal solution Z Can we express Z in terms of X’ and Y’ instead of X and Y? By definition, Z = LCS(X,Y) Suppose 𝑧 ℓ matches neither. 𝒁= 𝑧 1 𝑧 2 … 𝑧 ℓ−1 𝑧 ℓ Take 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) 𝑿= 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 𝑥 𝑚 𝑿 ′ = 𝑥 1 𝑥 2 𝑥 3 𝑥 4 … 𝑥 𝑚−1 Note that 𝒙 𝒎 ≠ 𝒚 𝒏 , or else we could improve 𝑍 by adding them! Not needed by Z 𝒀= 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 𝑦 𝑛 𝒀 ′ = 𝑦 1 𝑦 2 𝑦 3 𝑦 4 … 𝑦 𝑛−1 Not needed by Z

Let 𝑋 𝑖 =( 𝑥 1 ,…, 𝑥 𝑖 ), 𝑌 𝑗 = 𝑦 1 ,…, 𝑦 𝑗 and 𝒄 𝒊,𝒋 = 𝑳𝑪𝑺 𝑿 𝒊 , 𝒀 𝒋 Four cases Case 𝒛 ℓ matches both (so 𝑥 𝑚 = 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) + 𝒛 ℓ Case 𝒛 ℓ matches only 𝒙 𝒎 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆(𝑿, 𝑌 ′ ) Case 𝒛 ℓ matches only 𝒚 𝒏 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝒀) Case 𝒛 ℓ matches neither (recall 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝑌′) We don’t know 𝑧 ℓ ! How to identify case 1 vs 2-4? (If 𝑥 𝑚 = 𝑦 𝑛 ) How to differentiate between cases 2-4 without knowing 𝑧 ℓ ? Try all 3 possibilities in the recurrence and maximize length! Let 𝑋 𝑖 =( 𝑥 1 ,…, 𝑥 𝑖 ), 𝑌 𝑗 = 𝑦 1 ,…, 𝑦 𝑗 and 𝒄 𝒊,𝒋 = 𝑳𝑪𝑺 𝑿 𝒊 , 𝒀 𝒋 In-class exercise: derive the recurrence for 𝑐 𝑖,𝑗 (part 1) and give pseudocode to solve the problem (part 2)

In-class exercise Part 1: derive 𝑐[𝑖,𝑗] Case 𝒛 ℓ matches both (so 𝑥 𝑚 = 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) + 𝒛 ℓ Case 𝒛 ℓ matches only 𝒙 𝒎 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆(𝑿, 𝑌 ′ ) Case 𝒛 ℓ matches only 𝒚 𝒏 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝒀) Case 𝒛 ℓ matches neither (recall 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝑌′) Let 𝑋 𝑖 =( 𝑥 1 ,…, 𝑥 𝑖 ), 𝑌 𝑗 = 𝑦 1 ,…, 𝑦 𝑗 and 𝒄 𝒊,𝒋 = 𝑳𝑪𝑺 𝑿 𝒊 , 𝒀 𝒋 𝑐 𝑖,𝑗 = ??? if 𝑖=0 or 𝑗=0 ??? if 𝑖,𝑗≥1 and 𝑥 𝑖 = 𝑦 𝑗 ??? if 𝑖,𝑗≥1 and 𝑥 𝑖 ≠ 𝑦 𝑗

In-class exercise Part 1: derive 𝑐[𝑖,𝑗] Case 𝒛 ℓ matches both (so 𝑥 𝑚 = 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) + 𝒛 ℓ Case 𝒛 ℓ matches only 𝒙 𝒎 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆(𝑿, 𝑌 ′ ) Case 𝒛 ℓ matches only 𝒚 𝒏 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝒀) Case 𝒛 ℓ matches neither (recall 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝑌′) Let 𝑋 𝑖 =( 𝑥 1 ,…, 𝑥 𝑖 ), 𝑌 𝑗 = 𝑦 1 ,…, 𝑦 𝑗 and 𝒄 𝒊,𝒋 = 𝑳𝑪𝑺 𝑿 𝒊 , 𝒀 𝒋 𝑐 𝑖,𝑗 = 0 if 𝑖=0 or 𝑗=0 𝑐 𝑖−1,𝑗−1 +1 if 𝑖,𝑗≥1 and 𝑥 𝑖 = 𝑦 𝑗 max⁡{𝑐 𝑖,𝑗−1 ,𝑐 𝑖−1,𝑗 ,𝑐[𝑖−1,𝑗−1]} if 𝑖,𝑗≥1 and 𝑥 𝑖 ≠ 𝑦 𝑗 Can simplify! Observe that 𝑐 𝑖−1,𝑗−1 ≤𝑐 𝑖,𝑗−1 , because the former only has a subset of the input to the latter! Therefore, it can’t be the max

In-class exercise Part 1: derive 𝑐[𝑖,𝑗] Case 𝒛 ℓ matches both (so 𝑥 𝑚 = 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ , 𝑌 ′ ) + 𝒛 ℓ Case 𝒛 ℓ matches only 𝒙 𝒎 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆(𝑿, 𝑌 ′ ) Case 𝒛 ℓ matches only 𝒚 𝒏 (so 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝒀) Case 𝒛 ℓ matches neither (recall 𝑥 𝑚 ≠ 𝑦 𝑛 ): 𝑍=𝐿𝐶𝑆( 𝑋 ′ ,𝑌′) Let 𝑋 𝑖 =( 𝑥 1 ,…, 𝑥 𝑖 ), 𝑌 𝑗 = 𝑦 1 ,…, 𝑦 𝑗 and 𝒄 𝒊,𝒋 = 𝑳𝑪𝑺 𝑿 𝒊 , 𝒀 𝒋 𝑐 𝑖,𝑗 = 0 if 𝑖=0 or 𝑗=0 𝑐 𝑖−1,𝑗−1 +1 if 𝑖,𝑗≥1 and 𝑥 𝑖 = 𝑦 𝑗 max⁡{𝑐 𝑖,𝑗−1 ,𝑐 𝑖−1,𝑗 } if 𝑖,𝑗≥1 and 𝑥 𝑖 ≠ 𝑦 𝑗

Suppose 𝑿= gdvegta and 𝒀= gvcekst 𝒄 𝒊,𝒋 = 𝟎 𝒊𝒇 𝒊=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 𝒊−𝟏,𝒋−𝟏 +𝟏 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 = 𝒚 𝒋 𝐦𝐚𝐱⁡{𝒄 𝒊,𝒋−𝟏 ,𝒄 𝒊−𝟏,𝒋 } 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 ≠ 𝒚 𝒋 Suppose 𝑿= gdvegta and 𝒀= gvcekst Question 1 Q2 Q3 … Q6 … Q4 Q7 Q5 … …

Exercise part 2: pseudocode 𝒄 𝒊,𝒋 = 𝟎 𝒊𝒇 𝒊=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 𝒊−𝟏,𝒋−𝟏 +𝟏 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 = 𝒚 𝒋 𝐦𝐚𝐱⁡{𝒄 𝒊,𝒋−𝟏 ,𝒄 𝒊−𝟏,𝒋 } 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 ≠ 𝒚 𝒋 Give pseudocode to compute c[i,j] for all i,j and return the length of LCS(X,Y). Remaining code: Assume c[] already exists. ??? Complexity? Space? Time? Θ 𝑛𝑚 for both Start here: ???

Computing the LCS (not its length) 𝒋 𝒊 𝑐[𝑖,𝑗] 𝒄 𝒊,𝒋 = 𝟎 𝒊𝒇 𝒊=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 𝒊−𝟏,𝒋−𝟏 +𝟏 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 = 𝒚 𝒋 𝐦𝐚𝐱⁡{𝒄 𝒊,𝒋−𝟏 ,𝒄 𝒊−𝟏,𝒋 } 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 ≠ 𝒚 𝒋

Saving the direction to the predecessor subproblem 𝝅 𝒄 𝒊,𝒋 = 𝟎 𝒊𝒇 𝒊=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 𝒊−𝟏,𝒋−𝟏 +𝟏 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 = 𝒚 𝒋 𝐦𝐚𝐱⁡{𝒄 𝒊,𝒋−𝟏 ,𝒄 𝒊−𝟏,𝒋 } 𝐢𝐟 𝒊,𝒋≥𝟏 𝒂𝒏𝒅 𝒙 𝒊 ≠ 𝒚 𝒋 +1 means 𝑥 𝑖 is in the LCS! hidden If there are multiple possible sequences with the same length |LCS(X,Y)| then 𝑥 𝑖 is in some such seqeuence hidden hidden

How to obtain LCS=gvet from this table? Example seq=et Done: seq=gvet this is. seq=t seq=gvet seq=vet this “a” is not in

Following predecessors to compute the LCS Complexity of this trace-back: Space? Time? Recall: 𝑋 =𝑚, 𝑌 =𝑛 space: O(nm) time: O(n+m)

Memoization: an alternative to DP

Example: using memorization to compute Fibonacci numbers efficiently

Comparing with Traditional recursion Done! Memoization reduces this tree to a line with right-hanging leaves. # recursive calls = O(n) instead of ~2n Done! Already done! Done! Already done! Already done! Done! Done! Done! Calls not needed because of memoization Calls not needed because of memoization Calls not needed because of memoization Calls not needed because of memoization Calls not needed because of memoization Calls not needed because of memoization If M[n] is already computed, don’t recurse!

Problem: minimum length triangulation Input: 𝑛 points 𝑞 1 ,…, 𝑞 𝑛 in 2D space that form a convex 𝑛-gon 𝑃 Find: a triangulation of 𝑃 such that the sum of the perimeters of the 𝑛−2 triangles is minimized Output: the sum of the perimeters of the triangles in 𝑃 Input points are sorted in clockwise order around the center of 𝑃 [Example input on blackboard]

How hard is this Problem? How many triangulations are there? Number of triangulations of a convex 𝑛-gon = the 𝒏−𝟐 nd Catalan number This is 𝐶 𝑛−2 = 1 𝑛−1 2𝑛−4 𝑛−2 It can be shown that 𝐶 𝑛−2 ∈Θ( 4 𝑛 / 𝑛−2 3/2 )

Problem decomposition

How to fill in the table? [blackboard] Recurrence relation How to fill in the table? [blackboard]

Next time Graph algorithms Maybe: big-picture overview of the algorithmic design paradigms we’ve seen so far Brute force, divide and conquer, dynamic programming, greedy Pros/cons of each? When to use each?