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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is just a simple implementation of Patience Sorting, finding the LIS using backrefs, and doing the final merge phase to produce a sorted list. I didnt bother with anything special like binsearch or what not.

use strict; use warnings; use List::Util qw(shuffle); use Text::Wrap qw(wrap); sub patience { my ($deck)=@_; my @piles; CARD: foreach my $card (@$deck) { foreach my $pid ( 0..$#piles ) { if ( $card < $piles[$pid][-1][0] ) { push @{$piles[$pid]}, [ $card, $pid ? $piles[$pid-1][-1] : undef ]; next CARD; } } push @piles,[ [ $card, @piles ? $piles[-1][-1] : undef ] ]; } return \@piles } sub lis { my ($piles)= @_; my $iter= $piles->[-1][-1]; my @lis; while ($iter) { push @lis, $iter->[0]; $iter= $iter->[1]; } @lis=reverse @lis; return \@lis } sub inorder { my ($piles)= @_; my @inorder=(pop(@{$piles->[0]})->[0]); while (@$piles) { my $min=0; foreach my $pid (0..$#$piles) { if (@{$piles->[$pid]} && $piles->[$pid][-1][0] < $piles->[$min][-1][0] ){ $min=$pid; } } push @inorder,(pop(@{$piles->[$min]})->[0]); shift @$piles while @$piles && !@{$piles->[0]}; pop @$piles while @$piles && !@{$piles->[-1]}; } return \@inorder } sub print_ary { my ($head,$list)=@_; print wrap($head,"",join ", ",@$list),"\n---\n"; } my @deck= shuffle(1..52); my $piles=patience(\@deck); my $lis=lis($piles); my $inorder=inorder($piles); print_ary("DECK : ",\@deck); print_ary("LIS : ",$lis); print_ary("INORDER: ",$inorder);

Ive been trying to put together a vEB tree implementation to play with, but so far the documentation on it that I've found hasnt been as great as it looks at first glance. Notably ive been going a little barmy working out how you update the "max" property correctly when deleting the maximum element in a tree. Probably i just tried to work on it too long and if i rereview the documentation again it will clarify itself...


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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2018-05-21 23:36 GMT
    Find Nodes?
      Voting Booth?