Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement space optimized LCS for Length/LengthContext #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions golcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,47 @@ func (lcs *lcs) Length() (length int) {
return length
}

func (lcs *lcs) lengthContext(ctx context.Context) (length int, err error) {
m := len(lcs.left)
n := len(lcs.right)

// allocate storage for one-dimensional array `curr`
prev := 0
curr := make([]int, n+1)

// fill the lookup table in a bottom-up manner
for i := 0; i <= m; i++ {
select { // check in each y to save some time
case <-ctx.Done():
return 0, ctx.Err()
default:
// nop
}
prev = curr[0]
for j := 0; j <= n; j++ {
backup := curr[j]
if i == 0 || j == 0 {
curr[j] = 0
} else if reflect.DeepEqual(lcs.left[i-1], lcs.right[j-1]) {
// if the current character of `X` and `Y` matches
curr[j] = prev + 1
} else {
// otherwise, if the current character of `X` and `Y` don't match
curr[j] = max(curr[j], curr[j-1])
}
prev = backup
}
}
// LCS will be the last entry in the lookup table
return curr[n], nil
}

// Table implements Lcs.LengthContext()
func (lcs *lcs) LengthContext(ctx context.Context) (length int, err error) {
table, err := lcs.TableContext(ctx)
if err != nil {
return 0, err
if len(lcs.right) > len(lcs.left) {
lcs.left, lcs.right = lcs.right, lcs.left
}
return table[len(lcs.left)][len(lcs.right)], nil
return lcs.lengthContext(ctx)
}

// Table implements Lcs.IndexPairs()
Expand Down