Is this Longest Common Subsequence Correct?

强颜欢笑 提交于 2019-12-05 20:43:52

Use this correct, proven but inefficient implementation of the algorithm to check against your results - it's the standard recursive solution, it doesn't use dynamic programming:

def lis(nums):
    def max_length(i):
        if i == -1:
            return 0
        maxLen, curLen = 0, 0
        for j in xrange(i-1, -1, -1):
            if nums[j] < nums[i]:
                curLen = max_length(j)
                if curLen > maxLen:
                    maxLen = curLen
        return 1 + maxLen
    if not nums:
        return 0
    return max(max_length(x) for x in xrange(len(nums)))

Check to see if your_lis(nums) == my_lis(nums) for as many different-sized input lists with numbers as possible, they should be equal. At some point, for long lists my implementation will be far slower than yours.

As a further comparison point, here's my own optimized dynamic programming solution. It runs in O(n log k) time and O(n) space, returning the actual longest increasing subsequences it finds along the way:

def an_lis(nums):
    table, lis = lis_table(nums), []
    for i in xrange(len(table)):
        lis.append(nums[table[i]])
    return lis

def lis_table(nums):
    if not nums:
        return []
    table, preds = [0], [0] * len(nums)
    for i in xrange(1, len(nums)):
        if nums[table[-1]] < nums[i]:
            preds[i] = table[-1]
            table.append(i)
            continue
        minIdx, maxIdx = 0, len(table)-1
        while minIdx < maxIdx:
            mid = (minIdx + maxIdx) / 2
            if nums[table[mid]] < nums[i]:
                minIdx = mid + 1
            else:
                maxIdx = mid
        if nums[i] < nums[table[minIdx]]:
            if minIdx > 0:
                preds[i] = table[minIdx-1]
            table[minIdx] = i
    current, i = table[-1], len(table)
    while i:
        i -= 1
        table[i], current = current, preds[current]
    return table

I implement dynamic programming algorithms fairly often.

I have found that the best way to check for correctness is to write a brute-force version of the algorithm and compare the output with the dynamic programming implementation on small examples.

If the output of the two versions agree, then you have reasonable confidence of correctness.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!