Sort::Topological - Topological Sort
use Sort::Topological qw(toposort); my @result = toposort($item_direct_sub, @items);
Sort::Topological does a topological sort of an acyclical directed graph.
my %children = ( 'a' => [ 'b', 'c' ], 'c' => [ 'x' ], 'b' => [ 'x' ], 'x' => [ 'y' ], 'y' => [ 'z' ], 'z' => [ ], ); sub children { @{$children{$_[0]} || []}; } my @unsorted = ( 'z', 'a', 'x', 'c', 'b', 'y' ); my @sorted = toposort(\&children, \@unsorted);
In the above example %children is the graph, &children($x) returns a list of targets of the directed graph from $x.
%children
&children($x)
$x
@sorted is sorted such that:
@sorted
for any $x in @sorted:
i.e.: 'y' is not reachable by 'z', 'x' is not reachable by 'y' or 'z', and so on.
Does not handle cyclical graphs.
If you find this to be useful please contact the author. This is alpha software; all APIs, semantics and behavors are subject to change.
This section describes the external interface of this module.
Version 0.01, $Revision: 1.2 $.
Kurt A. Stephens <ks.perl@kurtstephens.com>
Copyright (c) 2001, 2002, Kurt A. Stephens and ION, INC.
>.
To install Data::Match, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Match
CPAN shell
perl -MCPAN -e shell install Data::Match
For more information on module installation, please visit the detailed CPAN module installation guide.