Skip to content

Space efficient LCS using Hirschberg's algorithm #3314

@ggyuchive

Description

@ggyuchive

Search before asking

  • I had searched in the issues and found no similar issues.

Motivation

In kvrocks LCS command, it uses Dynamic Programming. When two string S, T and get LCS(S, T),

  • Time Complexity: O(|S||T|)
  • Space Complexity: O(|S||T|)

When |S| and |T| is about 10000, it requires over 400MB.

Solution

We can reduce space complexity to O(|S|+|T|) using Hirschberg's algorithm, also it can recover LCS string.

Relevant paper: A Linear Space Algorithm for Computing Maximal Common Subsequences (Author: D.S. Hirschberg)

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions