Download presentation
Presentation is loading. Please wait.
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.