Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How would you write a memoized, iterative version of the longest common subsequence algorithm? A memoized, recursive version looks as follows: def lcsLena (X,Y): gamma
How would you write a "memoized", iterative version of the longest common subsequence algorithm?
A memoized, recursive version looks as follows:
def lcsLena (X,Y): gamma make Table (len (X)+1 len (Y)+1) #cache for length values, with extra 0th row & column. return c(X,Y,len (x),len (Y) ,gamma) def c cache) if cache Ci] [j None if i 0 or j 0: cache [i] Cj] 0 elif xli-1] YDj-1]: We need to offset both i and j, because of the extra values cache [i] Cj] c (x,Y,i-1,j-1, cache 1 else a c cache) b c(x,Y,i, j 1, cache) if a b: cache [i] [1] a else cache [i] Cj] b return cache Llen(X)] Clen (Y)]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started