The most efficient way to find the longest incremental subsequence in a list of lists

I am doing signal analysis, part of which is to search for the longest subsequence

I have a dictionary like the following:

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]}

These are, in fact, always sorted indices of another array, I would like to find the longest incremental sequence (as the longest growing subsequence ) by selecting only one number from each key sequentially (key 2 comes immediately after key 1, etc.), for example, from the keys 0 and 1, [360, 361] - one sequence, and [470, 471] - another. I call this incremental sequence, as these numbers must strictly increase by 1.

, .., , , - python , dict, ?

+4
2

@6502, , , .

. , currentHotItems globalHotItems, ..

# 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 - , . (, startIndex), - .

, 4 globalHotItems:

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

:

[((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)

+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 ... "" (.. 100 1 102 3, 101 2).

(7, 735, 7) 7 , 735 7.

+4

Source: https://habr.com/ru/post/1606175/


All Articles