Algorithms and Data Structures · Senior · Question 27 of 75

How do you reduce the memory of an LCS or edit distance DP from O(n × m) to O(m)?

Explanation

Cell (i, j) needs only (i-1, j), (i, j-1) and (i-1, j-1). Two rows (or one with a saved diagonal) are enough for the length. Reconstructing the actual alignment then needs extra work, for example Hirschberg's algorithm.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 27

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz