Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.)


In reply to Re: Patience Sorting To Find Longest Increasing Subsequence by TedPride
in thread Patience Sorting To Find Longest Increasing Subsequence by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found