Algorithm-MLCS
view release on metacpan or search on metacpan
lib/Algorithm/MLCS.pm view on Meta::CPAN
use vars qw/ $VERSION @ISA @EXPORT /;
require Exporter;
@ISA = qw/ Exporter /;
@EXPORT = qw/ lcs /;
$VERSION = '1.02';
# Gets arrayref of sequences (arrayrefs) and return LCS array in list context
# or length of LCS in scalar context
sub lcs {
my ( @seq, @lcs ) = map { _build_seq($_) } _get_dict( $_[0] );
while ( @seq && !( grep { !@$_ } @seq ) ) {
my %dict = ( %{ $seq[0][0] } );
for my $s ( @seq[ 1 .. $#seq ] ) {
%dict = map {
$_ => $dict{$_} > $s->[0]{$_}
? $s->[0]{$_} : $dict{$_}
lib/Algorithm/MLCS.pm view on Meta::CPAN
use Algorithm::MLCS;
my @seqs = (
[ qw/a b c d f g h j q z/ ],
[ qw/a b c d f g h j q z/ ],
[ qw/a b c x f h j q z/ ],
[ qw/a b c f g j q z/ ],
);
my @lcs = lcs( \@seqs );
my $lcs_length = lcs( \@seqs );
print Dumper( \@lcs );
=head1 ABSTRACT
Finding the longest common subsequence (LCS) for the general case of an arbitrary
number of input sequences is an NP-hard problem. Algorithm::MLCS implements a fast
heuristic algorithm that addresses the general case of multiple sequences.
It is able to extract common subsequence that is close to the optimal ones.
=head1 METHODS
=head2 lcs ( \@seqs )
Finds a Longest Common Subsequence of multiple sequences given by @seqs arrayref.
Each element of @seqs is arrayref that represents the one of multiple sequences
(e.g. [ ['a', 'b', 'c'], ['a', 'c', 'd', 'e'], ... ]). In list context it returns
LCS array, in scalar - the length of LCS.
=head1 SEE ALSO
Algorithm::LCS
=head1 AUTHOR
Slava Moiseev, C<< <slava.moiseev at yahoo.com> >>
=head1 LICENSE AND COPYRIGHT
( run in 0.577 second using v1.01-cache-2.11-cpan-65fba6d93b7 )