line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::Graphs::Reachable::Tiny;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
70635
|
use 5.008;
|
|
2
|
|
|
|
|
22
|
|
4
|
2
|
|
|
2
|
|
24
|
use strict;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
5
|
2
|
|
|
2
|
|
13
|
use warnings;
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
100
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
20
|
use Carp;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
163
|
|
8
|
2
|
|
|
2
|
|
13
|
use Exporter 'import';
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
780
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.07';
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(all_reachable);
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub all_reachable {
|
16
|
36
|
50
|
|
36
|
1
|
2766
|
@_ == 2 or croak("Need exactly two arguments!");
|
17
|
36
|
|
|
|
|
75
|
my ($graph, $nodes) = @_;
|
18
|
36
|
100
|
|
|
|
107
|
my %visited = ref($nodes) eq "ARRAY" ? map {$_ => undef} @{$nodes} : %{$nodes};
|
|
30
|
|
|
|
|
112
|
|
|
18
|
|
|
|
|
46
|
|
|
18
|
|
|
|
|
65
|
|
19
|
36
|
100
|
|
|
|
142
|
return {} unless %visited;
|
20
|
26
|
|
|
|
|
76
|
my @queue = keys(%visited);
|
21
|
26
|
100
|
|
|
|
77
|
if (ref($graph) eq 'HASH') {
|
|
|
50
|
|
|
|
|
|
22
|
10
|
50
|
|
|
|
35
|
return \%visited unless %{$graph};
|
|
10
|
|
|
|
|
23
|
|
23
|
10
|
|
|
|
|
33
|
while (defined(my $v = shift(@queue))) {
|
24
|
62
|
100
|
|
|
|
142
|
if (exists($graph->{$v})) { ## we need this if() to avoid autovivification!
|
25
|
40
|
|
|
|
|
58
|
foreach my $s (keys(%{$graph->{$v}})) {
|
|
40
|
|
|
|
|
90
|
|
26
|
50
|
100
|
|
|
|
107
|
if (!exists($visited{$s})) {
|
27
|
42
|
|
|
|
|
70
|
$visited{$s} = undef;
|
28
|
42
|
|
|
|
|
117
|
push(@queue, $s);
|
29
|
|
|
|
|
|
|
}
|
30
|
|
|
|
|
|
|
}
|
31
|
|
|
|
|
|
|
}
|
32
|
|
|
|
|
|
|
}
|
33
|
|
|
|
|
|
|
}
|
34
|
|
|
|
|
|
|
elsif (ref($graph) eq 'ARRAY') {
|
35
|
16
|
50
|
|
|
|
25
|
return \%visited unless @{$graph};
|
|
16
|
|
|
|
|
39
|
|
36
|
16
|
|
|
|
|
46
|
while (defined(my $v = shift(@queue))) {
|
37
|
124
|
100
|
|
|
|
318
|
if (defined($graph->[$v])) {
|
38
|
92
|
|
|
|
|
138
|
foreach my $s (keys(%{$graph->[$v]})) {
|
|
92
|
|
|
|
|
228
|
|
39
|
100
|
100
|
|
|
|
216
|
if (!exists($visited{$s})) {
|
40
|
84
|
|
|
|
|
147
|
$visited{$s} = undef;
|
41
|
84
|
|
|
|
|
207
|
push(@queue, $s);
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
}
|
44
|
|
|
|
|
|
|
}
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
} else {
|
47
|
0
|
|
|
|
|
0
|
croak("Arg 1 must be an ARRAY or HASH reference");
|
48
|
|
|
|
|
|
|
}
|
49
|
26
|
|
|
|
|
161
|
return \%visited;
|
50
|
|
|
|
|
|
|
}
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; # End of Algorithm::Graphs::Reachable::Tiny
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__
|