Graph
您可以使用该模块在 Perl 中构建图形
你需要一个Graph::Directed
对象
这是一个例子
use strict;
use warnings 'all';
use Graph::Directed;
my %HoA = (
"M" => [ "L", "E" ],
"L" => [ "I" ],
"E" => [ "B", "C" ],
"B" => [ "A" ],
"C" => [ "A" ]
);
# Build the graph
#
my $g = Graph::Directed->new;
while ( my ($from, $to) = each %HoA ) {
$g->add_edge($from, $_) for @$to;
}
# Build a table of successors of each vertex
#
my %succ;
for my $v ( $g->vertices ) {
my @succ = $g->all_successors($v);
$succ{$v} = \@succ;
}
# Print the vertices in descending order of successors
#
for my $v ( sort { @{$succ{$b}} <=> @{$succ{$a}} } $g->vertices ) {
printf "%s %d\n", $v, scalar @{$succ{$v}};
}
输出
M 6
E 3
C 1
B 1
L 1
A 0
I 0