寻找最长链表最有效的递增方法

2024-09-28 17:06:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一些信号分析,其中的一部分是寻找最长的子序列

我有如下字典:

sequenceDict = {
    0: [168, 360, 470],
    1: [279, 361, 471, 633, 729, 817],
    2: [32, 168, 170, 350, 634, 730, 818],
    3: [33, 155, 171, 363, 635, 731, 765, 819],
    4: [352, 364, 732, 766, 822],
    5: [157, 173, 353, 577, 637, 733, 823, 969],
    6: [158, 174, 578, 638, 706, 734, 824],
    7: [159, 175, 579, 707, 735],
    8: [160, 464, 640, 708, 826],
    9: [173, 709, 757, 827],
    10: [174, 540, 642, 666, 710],
    11: [253, 667, 711],
    12: [254, 304, 668],
    13: [181, 255, 831],
    14: [256, 340, 646, 832],
    16: [184, 416], 
    17: [417], 
    18: [418], 
    19: [875], 
    20: [876], 
    23: [217], 
    24: [168, 218, 880], 
    25: [219, 765, 881], 
    26: [220, 766], 
    27: [221], 
    28: [768], 
    29: [3, 769], 
    30: [344, 476, 706]}

这些基本上总是另一个数组的排序索引,我想通过从每个键中顺序地只选取一个数字(键2紧跟在键1之后,依此类推)来找到最长的递增序列(就像longest increasing subsequence),例如, 从键0和键1,[360361]是一个序列,[470471]是另一个序列。 我称之为递增序列,因为这些数字应该严格地增加1。在

我已经研究过patience sorting等等,但是由于这个问题稍有不同,而且还有一个序列树,除了从这个dict生成所有可能的序列,然后运行patience sort之外,有没有已知的python实现,或者其他有效的方法来完成这个任务?在


Tags: longest字典信号排序顺序序列数字数组
2条回答

我只想实施一个“暴力”解决方案。。。在

  1. 保留“当前序列”列表,最初为空
  2. 对于每个键,检查当前序列是否可以扩展一步。增加序列更新时也是目前为止最好的解决方案。在
  3. 对于未用于扩展序列的任何数字,请启动长度为1的新序列

Python提供set这可能是一个合理的选择。。。这是一个示例实现:

best = None
current_sequences = set()
last_key = None
for key in sorted(sequenceDict.keys()):
    data = set(sequenceDict[key])
    new_sequences = set()
    if last_key == key-1:
        # no gap in key value, may be some sequence got extended
        for val, count in current_sequences:
            if val+1 in data:
                # found a continuation, keep this sequence
                new_sequences.add((val+1, count+1))
                data.remove(val+1)
                if best is None or count+1 > best[0]:
                    # we've got a new champion
                    best = count+1, val+1, key
    # add new sequences starting here
    for v in data:
        new_sequences.add((v, 1))
        if best is None:
            best = 1, v, key
    current_sequences = new_sequences
    last_key = key

一个棘手的部分是,如果键中有一个间隙,那么您就不能扩展序列,这就是last_key的用途。在

复杂性应该是O(input_size × average_number_of_sequences)。我只是一种直觉,但我想你不能再比这低了。我很想用value - key将一个常量与每个序列关联起来。。。然而,这不会检测到“间隙”(即键1中的值100和键3中的值102,但在键2中没有101的。在

对于输入的问题,解决方案是(7, 735, 7),意思是一个7元素序列,在键7处以值735结尾。在

与@6502的解相比,这个解不仅保持了最佳解,而且还跟踪了每一个递增的子序列,如果这更有帮助的话。

这个想法类似于滑动窗口方法。从第一个列表开始,更新currentHotItems和{}字典,然后查看第二个列表并再次更新字典,等等

# fill missing indexes in the dictionary:
for i in range(min(sequenceDict), max(sequenceDict)):
    if i not in sequenceDict:
        sequenceDict[i] = []

# get only lists, ordered:
sortedItems = map(lambda x:x[1], sorted(sequenceDict.items(), key=lambda x:x[0]))    
globalHotItems = {} # (value, startIndex): length
currentHotItems = {} # value: length

for i in range(len(sortedItems)):
    updatedHotItems = {} # updated value: length
    for item in sortedItems[i]:
        if (item - 1) in currentHotItems:
            updatedHotItems[item] = currentHotItems[item-1] + 1
        else:
            updatedHotItems[item] = 1

    deadSet = set(currentHotItems.keys()) - \
            set(updatedHotItems.keys() + [key - 1 for key in updatedHotItems.keys()])

    for item in deadSet:
        globalHotItems[ (item-currentHotItems[item]+1, i-currentHotItems[item]) ] = currentHotItems[item]

    currentHotItems = updatedHotItems

print sorted(globalHotItems.items(), key=lambda x:x[1])[-1]

globalHotItems是包含结果的字典。键是(value,startIndex),value是长度。在

例如,globalHotItems中的最后4项:

^{pr2}$

是:

[((157, 5), 4), ((217, 23), 5), ((706, 6), 6), ((729, 1), 7)]

这意味着最好的解决方案是长度7,从index=1列表中开始为729。最好的第二个解是长度6,从index=6列表开始,如706,等等

复杂性:

我认为复杂性应该是:O(input_size × average_number_of_sequences)

相关问题 更多 >