Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trevor Brown DC 2338, Office hour M3-4pm

Similar presentations


Presentation on theme: "Trevor Brown DC 2338, Office hour M3-4pm"β€” Presentation transcript:

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

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

3 Problem: Longest Common Subsequence (LCS)

4 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

5 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

6 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

7 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

8 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!

9 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

10 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

11 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)

12 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 π‘₯ 𝑖 β‰  𝑦 𝑗

13 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

14 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 π‘₯ 𝑖 β‰  𝑦 𝑗

15 Suppose 𝑿= gdvegta and 𝒀= gvcekst
𝒄 π’Š,𝒋 = 𝟎 π’Šπ’‡ π’Š=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 π’Šβˆ’πŸ,π’‹βˆ’πŸ +𝟏 𝐒𝐟 π’Š,𝒋β‰₯𝟏 𝒂𝒏𝒅 𝒙 π’Š = π’š 𝒋 𝐦𝐚𝐱⁑{𝒄 π’Š,π’‹βˆ’πŸ ,𝒄 π’Šβˆ’πŸ,𝒋 } 𝐒𝐟 π’Š,𝒋β‰₯𝟏 𝒂𝒏𝒅 𝒙 π’Š β‰  π’š 𝒋 Suppose 𝑿= gdvegta and 𝒀= gvcekst Question 1 Q2 Q3 … Q6 … Q4 Q7 Q5 … …

16 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: ???

17 Computing the LCS (not its length)
𝒋 π’Š 𝑐[𝑖,𝑗] 𝒄 π’Š,𝒋 = 𝟎 π’Šπ’‡ π’Š=𝟎 𝒐𝒓 𝒋=𝟎 𝒄 π’Šβˆ’πŸ,π’‹βˆ’πŸ +𝟏 𝐒𝐟 π’Š,𝒋β‰₯𝟏 𝒂𝒏𝒅 𝒙 π’Š = π’š 𝒋 𝐦𝐚𝐱⁑{𝒄 π’Š,π’‹βˆ’πŸ ,𝒄 π’Šβˆ’πŸ,𝒋 } 𝐒𝐟 π’Š,𝒋β‰₯𝟏 𝒂𝒏𝒅 𝒙 π’Š β‰  π’š 𝒋

18 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

19 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

20 Following predecessors to compute the LCS
Complexity of this trace-back: Space? Time? Recall: 𝑋 =π‘š, π‘Œ =𝑛 space: O(nm) time: O(n+m)

21 Memoization: an alternative to DP

22 Example: using memorization to compute Fibonacci numbers efficiently

23 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!

24 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]

25 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 )

26 Problem decomposition

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

28 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?


Download ppt "Trevor Brown DC 2338, Office hour M3-4pm"

Similar presentations


Ads by Google