perlmeditation
Limbic~Region
All,
<br />
A few weeks ago, [TedPride] posted an interesting [id://543621|puzzle]. [kvale] provided a hint by linking to [wp://Patience Sorting] that no one apparently used. [bart] mentioned yesterday he would like to see an implementation of both the sorting algorithm as well as the way to divine the longest increasing subsequence from the result.
<h4>Patience Sorting</h4>
<p>
Each new card is placed on the left most pile where the top card in that pile is greater than the new card. If no pile meets this criteria, the new card is placed in a new pile to the right of the existing piles. When all cards are exhausted, the number of piles defines the length of the longest increasing subsequence. It is that simple. You only need to add 1 more thing to retrieve the sequence from the piles. <b><update></b> As [bart] points out below, this may be all that is required to find the longest increasing subsequence but the sort still isn't finished. I have added a merge sort to his reply to demonstrate how that is possible.<b></update></b>
</p>
<h4>Longest Increasing Subsequence</h4>
<p>
After each card is placed, a <i>note</i> is taken of the top card in the previous pile. If it is the left most pile no note is needed. When all the cards are exhausted, starting in the right most pile we can work backwards finding the cards that make up the longest increasing subsequence. The reverse of this is the sequence is what is desired.
</p>
<p>
The following is a straight forward implementation. I took advantage of the fact that you know the length of the sequence before actually finding it to alleviate the need for [doc://reverse] as I fill the array.
</p>
<CODE>
#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'shuffle';
use constant VAL => 0;
use constant PREV => 1;
my @list = shuffle(1..20);
print "@list\n";
print join " ", Long_Inc_Sub(@list);
sub Long_Inc_Sub {
my @list = @_;
my (@pile, @seq);
NUM:
for my $num (@list) {
for (0 .. $#pile) {
if ($num < $pile[$_][-1][VAL]) {
my $prev = $_ ? $#{$pile[$_ - 1]} : undef;
push @{$pile[$_]}, [$num, $prev];
next NUM;
}
}
my $prev = @pile ? $#{$pile[-1]} : undef;
push @pile, [[$num, $prev]];
}
my ($prev, $len) = ($#{$pile[-1]}, scalar @pile);
while ($len--) {
$seq[$len] = $pile[$len][$prev][VAL];
$prev = $pile[$len][$prev][PREV];
}
return @seq;
}
</CODE>
<h4>Things To Consider</h4>
<p>
Do the algorithms still work if the original list sorted in ascending order is non-continguous (! 1..N)? Do the algorithms still work if the original list contains duplicates? Are there ways to maintain the same speed and decrease the memory consumption? Would there be a benefit in replacing the linear card placement search with a binary one or would the overhead outweigh the benefit. If the answer is "it depends", is there a way to know ahead of time to dispatch the appropriate method?
</p>
<p>
These and other interesting things to ponder are all left as an exercise for the reader.
</p>
<div class="pmsig"><div class="pmsig-180961">
<p>
Cheers - [Limbic~Region|L~R]
</p>
</div></div>