Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Patience Sorting To Find Longest Increasing Subsequence

by TedPride (Priest)
on May 03, 2006 at 17:34 UTC ( [id://547221]=note: print w/replies, xml ) Need Help??


in reply to Patience Sorting To Find Longest Increasing Subsequence

use strict; use warnings; my (@arr, @sequence, @result, $p, $node); @arr = 1..20; randomize(\@arr); print "@arr\n"; for (@arr) { $p = simplefind(\@sequence, $_); $node = {'key' => $_}; if (!$p) { $sequence[1] = $node; } else { $node->{'last'} = $sequence[$p]; $sequence[$p+1] = $node; } } $node = $sequence[-1]; while ($node) { push @result, $node->{'key'}; $node = $node->{'last'}; } print join ' ', reverse @result; sub simplefind { my ($p, $key, $i) = @_; for ($i = 1; $i <= $#$p; $i++) { last if $key < $p->[$i]{'key'}; } return $i-1; } sub randomize { my ($p, $size, $swap, $key); $p = $_[0]; $size = $#$p; for (0..($size-1)) { $swap = int rand($size - $_ + 1) + $_; $key = $p->[$_]; $p->[$_] = $p->[$swap]; $p->[$swap] = $key; } }
You build an array of sequences..

1-sequence
2-sequence
3-sequence
etc.

Where each n-sequence is the n-sequence with the smallest possible end item. By the nature of the structure, the end items will always be listed in increasing order - the end item for the 2-sequence will always be less than or equal to the end item for the 3-sequence.

Now, for each new item, just find the sequence with the largest end item that is smaller than or equal to the current item. Link your current item to this sequence and link the slot for the next largest sequence to your current item. Rinse and repeat until all items are processed.

For instance, given 3, 5, 2, 1, 7:

3: (3)
5: (3, 3-5))
2: (2, 3-5))
1: (1, 3-5))
7: (1, 3-5, 3-5-7)

The only really hard part is coming up with a binary find for the largest item that's smaller than or equal to the current item. I know it's possible, but the exact algorithm eludes me. I'm currently using simplefind() for test purposes - an O(n) algorithm rather than the proper O(lg n). Fixing this will make the entire algorithm O(n lg n). (EDIT: Actually, it's O(n lg s), where s is the longest sequence, but worst-case is s = n)

(You may also notice, by the way, that the entire sequence is reconstructed from data stored along the way. It's also possible to store just the the subscript of the end item in each n-length sequence, then do a linear find backwards from that point, recreating the sequence as you go from all the items smaller than or equal to the current part of the sequence.)

Replies are listed 'Best First'.
Re^2: Patience Sorting To Find Longest Increasing Subsequence
by Limbic~Region (Chancellor) on May 04, 2006 at 12:40 UTC
    TedPride,
    Per our /msg conversation, here is a version of my implementation that uses a binary search:
    sub Long_Inc_Sub { my @list = @_; my (@pile, @seq); for my $num (@list) { if (@pile) { if ($num < $pile[-1][-1][VAL]) { my ($beg, $end) = (0, $#pile); while ($beg <= $end) { my $mid = int(($beg + $end) / 2); if ($num < $pile[$mid][-1][VAL]) { $end = $mid - 1; } else { $beg = $mid + 1; } } my $prev = $beg ? $#{$pile[$beg - 1]} : undef; push @{$pile[$beg]}, [$num, $prev]; } else { push @pile, [[$num, $#{$pile[-1]}]]; } } else { push @pile, [[$num, undef]]; } } my ($prev, $len) = ($#{$pile[-1]}, scalar @pile); while ($len--) { $seq[$len] = $pile[$len][$prev][VAL]; $prev = $pile[$len][$prev][PREV]; } return @seq; }
    It does not currently handle exact matches since there should not be any duplicates in the list (1..N). I did leave this open as a question to ponder and it should be fairly trivial to adapt if you decide it is safe to do so ;-)

    Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://547221]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-19 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found