Algorithm::TSort - Perl extension for topological sort
use Algorithm::TSort; # $adj = { 1 => [ 2, 3], 2 => [4], 3 => [4] } ; my (@sorted ) = tsort( Graph( ADJ => $adj ). keys %$adj ); say for @sorted; # -- OR -- # $adj_sub = sub { return unless $adj->{ $_[0] } ; return @{$adj->{$_[0]}}; }; my (@sorted) = tsort( Graph( ADJSUB => $adj_sub ), @nodes_for_sort ); # -- OR -- # $sub_arrayref = sub { $adj->{ $_[0] } }; my (@sorted) = tsort( Graph( ADJSUB_ARRAYREF => $adj_sub ), @nodes_for_sort ); # -- OR -- # $buf = # "1 2 3 # 2 4 # 3 4"; my (@sorted) = tsort( Graph ( SCALAR => $buf )); # -- OR -- # my (@sorted) = tsort( Graph ( IO => \*STDIN) ); print "$_\n" for @sorted; # -- OR -- # Write your own class for graph with 'adj_nodes' method # my $graph = MyGraph->new; # # Initialization ... my (@sorted ) = tsort( $graph, @nodes_for_sort );
Topological sort for varing inputs
Graph, tsort
Sort::Topological, Graph
A. G. Grishaev, <grian@cpan.org>
Copyright (C) 2010-2016 by A. G. Grishaev
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
To install Algorithm::TSort, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Algorithm::TSort
CPAN shell
perl -MCPAN -e shell install Algorithm::TSort
For more information on module installation, please visit the detailed CPAN module installation guide.