如果我理解正确,您想计算图中每个节点附近的节点数。我认为这可以满足您的要求。
我已经修改了我的代码,因为您已经解释了从节点到自身的向量应该被忽略。
use v5.10;
use warnings;
my %routes;
while (<DATA>) {
my @nodes = /\d+/g;
$routes{$_} //= {} for @nodes;
while (@nodes >= 2) {
my ($from, $to) = @nodes;
$routes{$from}{$to}++ unless $from == $to;
shift @nodes;
}
}
for my $key (sort { $a <=> $b } keys %routes) {
my $val = $routes{$key};
printf "%d - neighbourhood size %d",
$key,
scalar keys %$val;
printf " (%s)", join ', ', keys %$val if %$val;
print "\n";
}
__DATA__
12 45 64
12 45 76
12 37 39 87
12 67 90
12 39 60
1 4 3
1 2 2
2 6 7
输出
1 - neighbourhood size 2 (4, 2)
2 - neighbourhood size 1 (6)
3 - neighbourhood size 0
4 - neighbourhood size 1 (3)
6 - neighbourhood size 1 (7)
7 - neighbourhood size 0
12 - neighbourhood size 4 (67, 39, 37, 45)
37 - neighbourhood size 1 (39)
39 - neighbourhood size 2 (60, 87)
45 - neighbourhood size 2 (64, 76)
60 - neighbourhood size 0
64 - neighbourhood size 0
67 - neighbourhood size 1 (90)
76 - neighbourhood size 0
87 - neighbourhood size 0
90 - neighbourhood size 0