Graph.pm

filter: Statementstmt 99.5% 1881 / 1897 (10 uncoverable) Branchbran 84.3% 727 / 862 Conditioncond 78.5% 223 / 284 MC/DCmcdc 64.5% 109 / 169 Subroutinesub 100% 343 / 343 Podpod 100% 247 / 247 totaltotal 93.0% 3530 / 3802 (10 uncoverable) CC 610 SCAR 17.8
CC
610
Cov
93%
random_graph
26.5
longest_path
17.7
shortest_path
17.6
CRAP 5.9
« Graph/TransitiveClosure.pm ↑ Summary Graph/UnionFind.pm »
1 package Graph;
2  
3 84 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 84 ▶BEGIN { warnings->unimport('recursion') if $ENV{GRAPH_ALLOW_RECURSION} }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GRAPH_ALLOW_RECURSION"} 0 84 84
6  
7 20 ▶sub __carp_confess { require Carp; Carp::confess(@_) }
Subroutine __carp_confess: called CC=1 SCAR=0.0
8 84 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 84 if (0) { # SET THIS TO ZERO FOR TESTING AND RELEASES!
10 $SIG{__DIE__ } = \&__carp_confess;
11 $SIG{__WARN__} = \&__carp_confess;
12 }
13 }
14  
15 84 ▶use Graph::AdjacencyMap qw(:flags :fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 84 our $VERSION = '0.9735';
18  
19 84 require 5.006; # Weak references are absolutely required.
20  
21 84 my @GRAPH_PROPS_COPIED = qw(
22 undirected refvertexed countvertexed multivertexed __stringified
23 hyperedged countedged multiedged
24 );
25 84 my $_empty_array = [];
26 1 ▶sub _empty_array () { $_empty_array }
Subroutine _empty_array: called CC=1 SCAR=0.0
27  
28 84 my $can_deep_copy_Storable;
29 25 ▶sub _can_deep_copy_Storable () {
Subroutine _can_deep_copy_Storable: called CC=3 SCAR=11.0
30 25 ▶ return $can_deep_copy_Storable if defined $can_deep_copy_Storable;
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_deep_copy_Storable 21 4 25
31 4 ▶ return $can_deep_copy_Storable = 0 if $] < 5.010; # no :load tag Safe 5.8
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.01" 0 4 4
32 4 eval {
33 4 require Storable;
34 4 require B::Deparse;
35 4 Storable->VERSION(2.05);
36 4 B::Deparse->VERSION(0.61);
37 };
38 4 $can_deep_copy_Storable = !$@;
39 }
40  
41 sub _F () { 0 } # Flags.
42 sub _G () { 1 } # Generation.
43 sub _V () { 2 } # Vertices.
44 sub _E () { 3 } # Edges.
45 sub _A () { 4 } # Attributes.
46 sub _U () { 5 } # Union-Find.
47  
48 my $Inf;
49  
50 84 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=24.7
51 84 ▶ if ($] >= 5.022) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.022) { } 84 0 84
52 84 $Inf = eval '+"Inf"'; # uncoverable statement
53 } else {
54 0 local $SIG{FPE}; # uncoverable statement
55 0 eval { $Inf = exp(999) } || # uncoverable statement
56 0 eval { $Inf = 9**9**9 } || # uncoverable statement
57 0 ▶ eval { $Inf = 1e+999 } || # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } } or {$Inf = "1e+99"} 0 0 0
Condition0%0/6
eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } }
l!l&&r!l&&!r
000
eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } }
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } } or eval { do { $Inf = "Inf" } }
eval { do { $Inf = "Inf" } } eval { do { $Inf = "Inf" } } eval { do { $Inf = "Inf" } }
Truth table0%0/4
A or B or C
A= eval { do { $Inf = "Inf" } }B= eval { do { $Inf = "Inf" } }C= eval { do { $Inf = "Inf" } }
ABCresult
1XX1
01X1
0011
0000
58 { $Inf = 1e+99 }; # uncoverable statement
59 # Close enough for most practical purposes.
60 }
61 }
62  
63 44 ▶sub Infinity () { $Inf }
Subroutine Infinity: called CC=1 SCAR=0.0
Pod documented
64  
65 # Graphs are blessed array references.
66 # - The first element contains the flags.
67 # - The second element is the vertices.
68 # - The third element is the edges.
69 # - The fourth element is the attributes of the whole graph.
70 # The defined flags for Graph are:
71 # - unionfind
72 # The vertices are contained in a "simplemap"
73 # (if no attributes) or in a "map".
74 # The edges are always in a "map".
75 # The defined flags for maps are:
76 # - _COUNT for countedness: more than one instance
77 # expects one for vertices and two for edges
78 # - _UNORD for unordered coordinates (a set): if _UNORD is not set
79 # the coordinates are assumed to be meaningfully ordered
80 # Vertices and edges assume none of these flags.
81  
82 84 ▶use Graph::Attribute array => _A, map => 'graph';
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 645 ▶sub stringify {
Subroutine stringify: called CC=4 SCAR=13.9
Pod documented
85 645 my ($u, $h) = (&is_undirected, &is_hyperedged);
86 645 ▶ my $e = $u ? '=' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$u ? : 328 317 645
87 my @edges = map join($e,
88 3155 ▶ $u ? sort { "$a" cmp "$b" } @$_ :
Branch: 1 branch
BranchTrueFalseTotal
$h ? : 4 1373 1377
89 645 ▶ $h ? map '['.join(",", sort { "$a" cmp "$b" } @$_).']', @$_ :
Branch: 1 branch
BranchTrueFalseTotal
$u ? : 3145 1377 4522
90 @$_), &_edges05;
91 645 my @s = sort @edges;
92 645 push @s, sort { "$a" cmp "$b" } &isolated_vertices;
93 645 join(",", @s);
94 }
95  
96 318 ▶sub eq {
Subroutine eq: called CC=1 SCAR=0.0
Pod documented
97 318 "$_[0]" eq "$_[1]"
98 }
99  
100 280 ▶sub boolify {
Subroutine boolify: called CC=1 SCAR=0.0
Pod documented
101 280 1; # Important for empty graphs: they stringify to "", which is false.
102 }
103  
104 11 ▶sub ne {
Subroutine ne: called CC=1 SCAR=0.0
Pod documented
105 11 "$_[0]" ne "$_[1]"
106 }
107  
108 use overload
109 84 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
110 'bool' => \&boolify,
111 'eq' => \&eq,
112 84 'ne' => \&ne;
113  
114 6864 ▶sub _opt {
Subroutine _opt: called CC=4 SCAR=13.9
115 6864 my ($opt, $flags, %flags) = @_;
116 6864 ▶ while (my ($flag, $FLAG) = each %flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %flags) 20592 6864 27456
117 20592 ▶ $$flags |= $FLAG if delete $opt->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
if delete $opt->{$flag} 1960 18632 20592
118 20592 ▶ $$flags &= ~$FLAG if delete $opt->{"non$flag"};
Branch: 1 branch
BranchTrueFalseTotal
if delete $$opt{"non$flag"} 0 20592 20592
119 }
120 }
121  
122 6 ▶sub _opt_get {
Subroutine _opt_get: called CC=2 SCAR=6.9
123 6 my ($opt, $key, $var) = @_;
124 6 ▶ return if !exists $opt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt->{$key} 5 1 6
125 1 $$var = delete $opt->{$key};
126 }
127  
128 2645 ▶sub _opt_unknown {
Subroutine _opt_unknown: called CC=3 SCAR=11.0
129 2645 my ($opt) = @_;
130 2645 ▶ return unless my @opt = keys %$opt;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@opt) = keys %$opt 2639 6 2645
131 6 __carp_confess sprintf
132 6 ▶ "@{[(caller(1))[3]]}: Unknown option%s: @{[map qq['$_'], sort @opt]}",
Branch: 1 branch
BranchTrueFalseTotal
@opt > 1 ? : 1 5 6
133 @opt > 1 ? 's' : '';
134 }
135  
136 1496 ▶sub _opt_from_existing {
Subroutine _opt_from_existing: called CC=3 SCAR=11.0
137 1496 my ($g) = @_;
138 1496 my %existing;
139 1496 $existing{$_}++ for grep $g->$_, @GRAPH_PROPS_COPIED;
140 1496 ▶ $existing{unionfind}++ if $g->has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if $g->has_union_find 1 1495 1496
141 1496 %existing;
142 }
143  
144 2288 ▶sub _opt_to_vflags {
Subroutine _opt_to_vflags: called CC=1 SCAR=0.0
145 2288 my ($vflags, $opt) = (0, @_);
146 2288 _opt($opt, \$vflags,
147 countvertexed => _COUNT,
148 multivertexed => _MULTI,
149 refvertexed => _REF,
150 refvertexed_stringified => _REFSTR ,
151 __stringified => _STR,
152 );
153 2288 $vflags;
154 }
155  
156 2288 ▶sub _opt_to_eflags {
Subroutine _opt_to_eflags: called CC=2 SCAR=6.9
157 2288 my ($eflags, $opt) = (0, @_);
158 2288 ▶ $opt->{undirected} = !delete $opt->{directed} if exists $opt->{directed};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"directed"} 290 1998 2288
159 2288 _opt($opt, \$eflags,
160 countedged => _COUNT,
161 multiedged => _MULTI,
162 undirected => _UNORD,
163 );
164 2288 ($eflags, delete $opt->{hyperedged});
165 }
166  
167 2288 ▶sub new {
Subroutine new: called CC=16 SCAR=27.8
Pod documented
168 2288 my ($class, @args) = @_;
169 2288 my $gflags = 0;
170 2288 my %opt = _get_options( \@args );
171  
172 2288 ▶ %opt = (_opt_from_existing($class), %opt) # allow overrides
Branch: 1 branch
BranchTrueFalseTotal
if ref $class and $class->isa("Graph") 1496 792 2288
Condition66%2/3
ref $class and $class->isa("Graph")
!ll&&!rl&&r
79201496
MC/DC50%1/2
ref $class and $class->isa("Graph")
ref $class $class->isa("Graph")
Truth table66%2/3
A and B
A= ref $classB= $class->isa("Graph")
ABresult
0X0
100
111
173 if ref $class && $class->isa('Graph');
174  
175 2288 my $vflags = _opt_to_vflags(\%opt);
176 2288 my ($eflags, $is_hyper) = _opt_to_eflags(\%opt);
177  
178 2288 _opt(\%opt, \$gflags,
179 unionfind => _UNIONFIND,
180 );
181  
182 2288 my @V;
183 2288 ▶ if ($opt{vertices}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"vertices"}) 80 2208 2288
184 __carp_confess "Graph: vertices should be an array ref"
185 80 ▶ if ref $opt{vertices} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt{"vertices"} ne "ARRAY" 0 80 80
186 80 @V = @{ delete $opt{vertices} };
187 }
188  
189 2288 my @E;
190 2288 ▶ if ($opt{edges}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"edges"}) 8 2280 2288
191 __carp_confess "Graph: edges should be an array ref of array refs"
192 8 ▶ if ref $opt{edges} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt{"edges"} ne "ARRAY" 0 8 8
193 8 @E = @{ delete $opt{edges} };
194 8 ▶ __carp_confess "Graph: edges should be array refs"
Branch: 1 branch
BranchTrueFalseTotal
if grep((ref $_ ne "ARRAY"), @E) 0 8 8
195 if grep ref $_ ne 'ARRAY', @E;
196 }
197  
198 2288 _opt_unknown(\%opt);
199  
200 2285 ▶ __carp_confess "Graph: both countvertexed and multivertexed"
Branch: 1 branch
BranchTrueFalseTotal
if $vflags & 1 and $vflags & 2 1 2284 2285
Condition100%3/3
$vflags & 1 and $vflags & 2
!ll&&!rl&&r
2268161
MC/DC100%2/2
$vflags & 1 and $vflags & 2
$vflags & 1 $vflags & 2
Truth table100%3/3
A and B
A= $vflags & 1B= $vflags & 2
ABresult
0X0
100
111
201 if ($vflags & _COUNT) && ($vflags & _MULTI);
202  
203 2284 ▶ __carp_confess "Graph: both countedged and multiedged"
Branch: 1 branch
BranchTrueFalseTotal
if $eflags & 1 and $eflags & 2 1 2283 2284
Condition100%3/3
$eflags & 1 and $eflags & 2
!ll&&!rl&&r
2258251
MC/DC100%2/2
$eflags & 1 and $eflags & 2
$eflags & 1 $eflags & 2
Truth table100%3/3
A and B
A= $eflags & 1B= $eflags & 2
ABresult
0X0
100
111
204 if ($eflags & _COUNT) && ($eflags & _MULTI);
205  
206 2283 ▶ my $g = bless [ ], ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
14967870
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
207 2283 $g->[ _F ] = $gflags;
208 2283 $g->[ _G ] = 0;
209 2283 $g->[ _V ] = _make_v($vflags);
210 2283 $g->[ _E ] = _make_e($is_hyper, $eflags);
211 2283 ▶ $g->[ _U ] = do { require Graph::UnionFind; Graph::UnionFind->new }
Branch: 1 branch
BranchTrueFalseTotal
if $gflags & 16 6 2277 2283
212 if $gflags & _UNIONFIND;
213 2283 ▶ $g->add_vertices(@V) if @V;
Branch: 1 branch
BranchTrueFalseTotal
if @V 74 2209 2283
214 2283 ▶ $g->add_edges(@E) if @E;
Branch: 1 branch
BranchTrueFalseTotal
if @E 8 2275 2283
215  
216 2283 return $g;
217 }
218  
219 2283 ▶sub _make_v {
Subroutine _make_v: called CC=2 SCAR=6.9
220 2283 my ($vflags) = @_;
221 2283 ▶ $vflags ? _am_heavy($vflags, 1) : _am_light($vflags, 1);
Branch: 1 branch
BranchTrueFalseTotal
$vflags ? : 135 2148 2283
222 }
223  
224 2283 ▶sub _make_e {
Subroutine _make_e: called CC=4 SCAR=13.9
225 2283 my ($is_hyper, $eflags) = @_;
226 2283 ▶ ($is_hyper or $eflags & ~_UNORD) ?
Branch: 2 branches
BranchTrueFalseTotal
$is_hyper || $eflags & 18446744073709551611 ? : 89 2194 2283
$is_hyper ? : 19 70 89
Condition100%3/3
$is_hyper or $eflags & '18446744073709551611'
l!l&&r!l&&!r
19702194
MC/DC100%2/2
$is_hyper or $eflags & '18446744073709551611'
$is_hyper $eflags & '18446744073709551611'
Truth table100%3/3
A or B
A= $is_hyperB= $eflags & '18446744073709551611'
ABresult
1X1
011
000
227 _am_heavy($eflags, $is_hyper ? 0 : 2) :
228 _am_light($eflags, 2);
229 }
230  
231 4342 ▶sub _am_light {
Subroutine _am_light: called CC=1 SCAR=0.0
232 4342 require Graph::AdjacencyMap::Light;
233 4342 Graph::AdjacencyMap::Light->_new(@_);
234 }
235  
236 224 ▶sub _am_heavy {
Subroutine _am_heavy: called CC=1 SCAR=0.0
237 224 Graph::AdjacencyMap->_new(@_);
238 }
239  
240 3871 ▶sub countvertexed { $_[0]->[ _V ]->_is_COUNT }
Subroutine countvertexed: called CC=1 SCAR=0.0
Pod documented
241 9574 ▶sub multivertexed { $_[0]->[ _V ]->_is_MULTI }
Subroutine multivertexed: called CC=1 SCAR=0.0
Pod documented
242 1516 ▶sub refvertexed { $_[0]->[ _V ]->_is_REF }
Subroutine refvertexed: called CC=1 SCAR=0.0
Pod documented
243 1 ▶sub refvertexed_stringified { $_[0]->[ _V ]->_is_REFSTR }
Subroutine refvertexed_stringified: called CC=1 SCAR=0.0
Pod documented
244 1496 ▶sub __stringified { $_[0]->[ _V ]->_is_STR }
Subroutine __stringified: called CC=1 SCAR=0.0
245  
246 1938 ▶sub countedged { $_[0]->[ _E ]->_is_COUNT }
Subroutine countedged: called CC=1 SCAR=0.0
Pod documented
247 66851 ▶sub multiedged { $_[0]->[ _E ]->_is_MULTI }
Subroutine multiedged: called CC=1 SCAR=0.0
Pod documented
248 173802 ▶sub hyperedged { !$_[0]->[ _E ]->[ _arity ] }
Subroutine hyperedged: called CC=1 SCAR=0.0
Pod documented
249 68802 ▶sub undirected { $_[0]->[ _E ]->_is_UNORD }
Subroutine undirected: called CC=1 SCAR=0.0
Pod documented
250  
251 68479 ▶sub directed { ! $_[0]->[ _E ]->_is_UNORD }
Subroutine directed: called CC=1 SCAR=0.0
Pod documented
252  
253 84 *is_directed = \&directed;
254 84 *is_undirected = \&undirected;
255  
256 84 *is_countvertexed = \&countvertexed;
257 84 *is_multivertexed = \&multivertexed;
258 84 *is_refvertexed = \&refvertexed;
259 84 *is_refvertexed_stringified = \&refvertexed_stringified;
260  
261 84 *is_countedged = \&countedged;
262 84 *is_multiedged = \&multiedged;
263 84 *is_hyperedged = \&hyperedged;
264  
265 44712 ▶sub has_union_find { $_[0]->[ _U ] }
Subroutine has_union_find: called CC=1 SCAR=0.0
Pod documented
266  
267 2122 ▶sub add_vertex {
Subroutine add_vertex: called CC=3 SCAR=11.0
Pod documented
268 2122 ▶ __carp_confess "Graph::add_vertex: use add_vertices for more than one vertex" if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 5 2117 2122
269 2117 ▶ __carp_confess "Graph::add_vertex: undef vertex" if grep !defined, @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), @_) 1 2116 2117
270 2116 goto &add_vertices;
271 }
272  
273 12026 ▶sub has_vertex {
Subroutine has_vertex: called CC=2 SCAR=6.9
Pod documented
274 12026 my $g = $_[0];
275 12026 my $V = $g->[ _V ];
276 12026 ▶ return defined $V->has_path($_[1]) if ($V->[ _f ] & _REF);
Branch: 1 branch
BranchTrueFalseTotal
if $V->[1] & 8 534 11492 12026
277 11492 exists $V->[ _pi ]->{ $_[1] };
278 }
279  
280 3530 ▶sub _vertices05 {
Subroutine _vertices05: called CC=1 SCAR=0.0
281 3530 my $g = $_[0];
282 3530 $g->[ _V ]->paths;
283 }
284  
285 2359 ▶sub vertices {
Subroutine vertices: called CC=5 SCAR=16.1
Pod documented
286 2359 my $g = $_[0];
287 2359 my @v = &_vertices05;
288 2359 ▶ return @v if !(&is_multivertexed || &is_countvertexed);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multivertexed or &is_countvertexed 2346 13 2359
Condition100%3/3
&is_multivertexed or &is_countvertexed
l!l&&r!l&&!r
492346
MC/DC100%2/2
&is_multivertexed or &is_countvertexed
&is_multivertexed &is_countvertexed
Truth table100%3/3
A or B
A= &is_multivertexedB= &is_countvertexed
ABresult
1X1
011
000
289 13 ▶ return map +(($_) x $g->get_vertex_count($_)), @v if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 12 13
290 12 my $V = 0;
291 12 $V += $g->get_vertex_count($_) for @v;
292 12 return $V;
293 }
294  
295 84 *unique_vertices = \&_vertices05;
296  
297 22 ▶sub has_vertices {
Subroutine has_vertices: called CC=1 SCAR=0.0
Pod documented
298 22 my $g = shift;
299 22 scalar $g->[ _V ]->has_any_paths;
300 }
301  
302 35668 ▶sub add_edge {
Subroutine add_edge: called CC=2 SCAR=6.9
Pod documented
303 35668 ▶ &expect_hyperedged, &expect_undirected if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 11 35657 35668
304 35666 $_[0]->add_edges([ @_[1..$#_] ]);
305 }
306  
307 127 ▶sub _vertex_ids_ensure {
Subroutine _vertex_ids_ensure: called CC=1 SCAR=0.0
308 127 push @_, 1;
309 127 goto &_vertex_ids_maybe_ensure;
310 }
311  
312 114 ▶sub _vertex_ids_ensure_multi {
Subroutine _vertex_ids_ensure_multi: called CC=2 SCAR=7.0
313 114 my $id = pop;
314 114 my @i = &_vertex_ids_ensure;
315 114 push @_, $id;
316 114 ▶ @i ? (@i, $id) : ();
Branch: 1 branch
BranchTrueFalseTotal
@i ? : 114 0 114
317 }
318  
319 73334 ▶sub _vertex_ids {
Subroutine _vertex_ids: called CC=1 SCAR=0.0
320 73334 push @_, 0;
321 73334 goto &_vertex_ids_maybe_ensure;
322 }
323  
324 635 ▶sub _vertex_ids_multi {
Subroutine _vertex_ids_multi: called CC=2 SCAR=6.9
325 635 my $id = pop;
326 635 my @i = &_vertex_ids;
327 635 push @_, $id;
328 635 ▶ @i ? (@i, $id) : ();
Branch: 1 branch
BranchTrueFalseTotal
@i ? : 616 19 635
329 }
330  
331 73461 ▶sub _vertex_ids_maybe_ensure {
Subroutine _vertex_ids_maybe_ensure: called CC=8 SCAR=20.8
332 73461 my $ensure = pop;
333 73461 my ($g, @args) = @_;
334 73461 ▶ __carp_confess "Graph: given undefined vertex" if grep !defined, @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), @args) 0 73461 73461
335 73461 my $V = $g->[ _V ];
336 73461 ▶ my $deep = &is_hyperedged && &is_directed;
Condition100%3/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
733466055
MC/DC100%2/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
337 73461 ▶ return $V->get_ids_by_paths(\@args, $ensure, $deep) if ($V->[ _f ] & _REF) or $deep;
Branch: 1 branch
BranchTrueFalseTotal
if $V->[1] & 8 or $deep 344 73117 73461
Condition100%3/3
$V->[1] & 8 or $deep
l!l&&r!l&&!r
2895573117
MC/DC100%2/2
$V->[1] & 8 or $deep
$V->[1] & 8 $deep
Truth table100%3/3
A or B
A= $V->[1] & 8B= $deep
ABresult
1X1
011
000
338 73117 my $pi = $V->[ _pi ];
339 73117 my @non_exist = grep !exists $pi->{ $_ }, @args;
340 73117 ▶ return if !$ensure and @non_exist;
Branch: 1 branch
BranchTrueFalseTotal
if not $ensure and @non_exist 1300 71817 73117
Condition100%3/3
not $ensure and @non_exist
!ll&&!rl&&r
123716941300
MC/DC100%2/2
not $ensure and @non_exist
not $ensure @non_exist
Truth table100%3/3
not(A) and B
A= not $ensureB= @non_exist
ABresult
0X0
100
111
341 71817 ▶ $V->get_ids_by_paths(\@non_exist, 1) if @non_exist;
Branch: 1 branch
BranchTrueFalseTotal
if @non_exist 51 71766 71817
342 71817 @$pi{ @args };
343 }
344  
345 54434 ▶sub has_edge {
Subroutine has_edge: called CC=8 SCAR=20.8
Pod documented
346 54434 my $g = $_[0];
347 54434 my $E = $g->[ _E ];
348 54434 my ($Ef, $Ea) = @$E[ _f, _arity ];
349 54434 ▶ return 0 if $Ea and @_ != $Ea + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Ea and @_ != $Ea + 1 4 54430 54434
Condition100%3/3
$Ea and @_ != $Ea + 1
!ll&&!rl&&r
31543994
MC/DC100%2/2
$Ea and @_ != $Ea + 1
$Ea @_ != $Ea + 1
Truth table100%3/3
A and B
A= $EaB= @_ != $Ea + 1
ABresult
0X0
100
111
350 54430 my $directed = &is_directed;
351 54430 ▶ my $deep = &is_hyperedged && $directed;
Condition100%3/3
&is_hyperedged && $directed
!ll&&!rl&&r
543991417
MC/DC100%2/2
&is_hyperedged && $directed
&is_hyperedged $directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= $directed
ABresult
0X0
100
111
352 54430 ▶ return 0 if (my @i = &_vertex_ids) != @_ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = &_vertex_ids) != @_ - 1 1417 53013 54430
353 53013 ▶ return defined $E->has_path($directed ? \@i : [ map [ sort @$_ ], @i ]) if $deep;
Branch: 2 branches
BranchTrueFalseTotal
if $deep 11 53002 53013
$directed ? : 11 0 11
354 53002 ▶ @i = sort @i if !$directed;
Branch: 1 branch
BranchTrueFalseTotal
unless $directed 43727 9275 53002
355 53002 exists $E->[ _pi ]{ "@i" };
356 }
357  
358 22 ▶sub any_edge {
Subroutine any_edge: called CC=2 SCAR=6.9
Pod documented
359 22 my ($g, @args) = @_;
360 22 my $E = $g->[ _E ];
361 22 my $V = $g->[ _V ];
362 22 ▶ return 0 if (my @i = $V->get_ids_by_paths(\@args)) != @args;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = $V->get_ids_by_paths(\@args)) != @args 6 16 22
363 16 $E->has_successor(@i);
364 }
365  
366 1311 ▶sub _edges05 {
Subroutine _edges05: called CC=3 SCAR=11.0
367 1311 my $g = $_[0];
368 1311 my @e = $g->[ _E ]->paths;
369 1311 ▶ return @e if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 55 1256 1311
370 1256 ▶ $g->[ _V ]->get_paths_by_ids(\@e, &is_hyperedged && &is_directed);
Condition100%3/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
1235156
MC/DC100%2/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
371 }
372  
373 84 *unique_edges = \&_edges05;
374  
375 385 ▶sub edges {
Subroutine edges: called CC=5 SCAR=16.1
Pod documented
376 385 my $g = $_[0];
377 385 my @e = &_edges05;
378 385 ▶ return @e if !(&is_multiedged || &is_countedged);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multiedged or &is_countedged 332 53 385
Condition100%3/3
&is_multiedged or &is_countedged
l!l&&r!l&&!r
4310332
MC/DC100%2/2
&is_multiedged or &is_countedged
&is_multiedged &is_countedged
Truth table100%3/3
A or B
A= &is_multiedgedB= &is_countedged
ABresult
1X1
011
000
379 53 ▶ return map +(($_) x $g->get_edge_count(@$_)), @e if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 39 14 53
380 14 my $E = 0;
381 14 $E += $g->get_edge_count(@$_) for @e;
382 14 return $E;
383 }
384  
385 7 ▶sub has_edges {
Subroutine has_edges: called CC=1 SCAR=0.0
Pod documented
386 7 scalar $_[0]->[ _E ]->has_any_paths;
387 }
388  
389 ###
390 # by_id
391 #
392  
393 45 ▶sub add_vertex_by_id {
Subroutine add_vertex_by_id: called CC=3 SCAR=11.0
Pod documented
394 45 &expect_multivertexed;
395 44 my ($g, $v, $id) = @_;
396 44 my $V = $g->[ _V ];
397 44 ▶ return $g if $V->has_path_by_multi_id( my @args = ($v, $id) );
Branch: 1 branch
BranchTrueFalseTotal
if $V->has_path_by_multi_id(my(@args) = ) 1 43 44
398 43 my ($i) = $V->set_path_by_multi_id( @args );
399 43 ▶ $g->[ _U ]->add($i) if &has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if &has_union_find 0 43 43
400 43 $g->[ _G ]++;
401 43 return $g;
402 }
403  
404 6 ▶sub add_vertex_get_id {
Subroutine add_vertex_get_id: called CC=2 SCAR=7.0
Pod documented
405 6 &expect_multivertexed;
406 6 my ($g, $v) = @_;
407 6 my ($i, $multi_id) = $g->[ _V ]->set_path_by_multi_id( $v, _GEN_ID );
408 6 ▶ $g->[ _U ]->add($i) if &has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if &has_union_find 0 6 6
409 6 $g->[ _G ]++;
410 6 return $multi_id;
411 }
412  
413 177 ▶sub has_vertex_by_id {
Subroutine has_vertex_by_id: called CC=1 SCAR=0.0
Pod documented
414 177 &expect_multivertexed;
415 176 my ($g, $v, $id) = @_;
416 176 $g->[ _V ]->has_path_by_multi_id( $v, $id );
417 }
418  
419 8 ▶sub delete_vertex_by_id {
Subroutine delete_vertex_by_id: called CC=5 SCAR=16.1
Pod documented
420 8 &expect_multivertexed;
421 7 &expect_non_unionfind;
422 7 my ($g, $v, $id) = @_;
423 7 ▶ return $g unless &has_vertex_by_id;
Branch: 1 branch
BranchTrueFalseTotal
unless &has_vertex_by_id 1 6 7
424 6 ▶ if ($g->[ _V ]->get_multi_ids( $v ) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->[2]->get_multi_ids($v) == 1) 3 3 6
425 # only incarnation, zap edges
426 3 my @i = &_vertex_ids_multi;
427 3 pop @i; # the id
428 3 my $E = $g->[ _E ];
429 3 my @edges = $E->paths_from(@i);
430 3 ▶ push @edges, $E->paths_to(@i) if !&is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_undirected 3 0 3
431 3 $E->del_path( $_ ) for @edges;
432 }
433 6 $g->[ _V ]->del_path_by_multi_id( $v, $id );
434 6 $g->[ _G ]++;
435 6 return $g;
436 }
437  
438 105 ▶sub get_multivertex_ids {
Subroutine get_multivertex_ids: called CC=1 SCAR=0.0
Pod documented
439 105 &expect_multivertexed;
440 104 my $g = shift;
441 104 $g->[ _V ]->get_multi_ids( @_ );
442 }
443  
444 115 ▶sub add_edge_by_id {
Subroutine add_edge_by_id: called CC=3 SCAR=11.0
Pod documented
445 115 &expect_multiedged;
446 114 my $g = $_[0];
447 114 my @i = &_vertex_ids_ensure_multi;
448 114 my $id = pop @i;
449 114 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 30 84 114
450 114 $g->[ _E ]->set_path_by_multi_id( \@i, $id );
451 114 $g->[ _G ]++;
452 114 ▶ $g->[ _U ]->union(\@i) if &has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if &has_union_find 2 112 114
453 114 return $g;
454 }
455  
456 13 ▶sub add_edge_get_id {
Subroutine add_edge_get_id: called CC=3 SCAR=11.0
Pod documented
457 13 &expect_multiedged;
458 13 my $g = $_[0];
459 13 my @i = &_vertex_ids_ensure;
460 13 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 6 7 13
461 13 my (undef, $id) = $g->[ _E ]->set_path_by_multi_id( \@i, _GEN_ID );
462 13 $g->[ _G ]++;
463 13 ▶ $g->[ _U ]->union(\@i) if &has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if &has_union_find 0 13 13
464 13 return $id;
465 }
466  
467 326 ▶sub has_edge_by_id {
Subroutine has_edge_by_id: called CC=3 SCAR=11.0
Pod documented
468 326 &expect_multiedged;
469 325 my $g = $_[0];
470 325 my @i = &_vertex_ids_multi;
471 325 ▶ return 0 if @i < @_ - 2;
Branch: 1 branch
BranchTrueFalseTotal
if @i < @_ - 2 19 306 325
472 306 my $id = pop @i;
473 306 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 48 258 306
474 306 $g->[ _E ]->has_path_by_multi_id( \@i, $id );
475 }
476  
477 6 ▶sub delete_edge_by_id {
Subroutine delete_edge_by_id: called CC=4 SCAR=13.9
Pod documented
478 6 &expect_multiedged;
479 5 &expect_non_unionfind;
480 5 my $g = $_[0];
481 5 my $E = $g->[ _E ];
482 5 my @i = &_vertex_ids_multi;
483 5 ▶ return if @i < @_ - 2;
Branch: 1 branch
BranchTrueFalseTotal
if @i < @_ - 2 0 5 5
484 5 my $id = pop @i;
485 5 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 0 5 5
486 5 ▶ return unless $E->has_path_by_multi_id( my @args = (\@i, $id) );
Branch: 1 branch
BranchTrueFalseTotal
unless $E->has_path_by_multi_id(my(@args) = (\@i, $id)) 1 4 5
487 4 $E->del_path_by_multi_id( @args );
488 4 $g->[ _G ]++;
489 4 return $g;
490 }
491  
492 198 ▶sub get_multiedge_ids {
Subroutine get_multiedge_ids: called CC=2 SCAR=7.1
Pod documented
493 198 &expect_multiedged;
494 197 ▶ return unless @_-1 == (my @i = &_vertex_ids);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ - 1 == (my(@i) = &_vertex_ids) 0 197 197
495 197 $_[0]->[ _E ]->get_multi_ids( \@i );
496 }
497  
498 ###
499 # Neighbourhood.
500 #
501  
502 195 ▶sub _edges_at {
Subroutine _edges_at: called CC=3 SCAR=11.0
503 195 ▶ goto &_edges_from if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 80 115 195
504 115 require Set::Object;
505 115 ▶ Set::Object->new(&_edges_from, &_edges_to)->${ wantarray ? \'members' : \'size' };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 114 1 115
506 }
507  
508 1669 ▶sub _edges_from {
Subroutine _edges_from: called CC=3 SCAR=11.0
509 1669 my ($g, @args) = @_;
510 1669 my ($V, $E) = @$g[ _V, _E ];
511 1669 ▶ return if (my @i = $V->get_ids_by_paths(\@args, &is_hyperedged && &is_directed)) != @args;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = $V->get_ids_by_paths(\@args, &is_hyperedged && &is_directed)) != @args 4 1665 1669
Condition100%3/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
166162
MC/DC100%2/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
512 1665 $E->paths_from(@i);
513 }
514  
515 331 ▶sub _edges_to {
Subroutine _edges_to: called CC=4 SCAR=14.0
516 331 ▶ goto &_edges_from if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 0 331 331
517 331 my ($g, @args) = @_;
518 331 my ($V, $E) = @$g[ _V, _E ];
519 331 ▶ return if (my @i = $V->get_ids_by_paths(\@args, &is_hyperedged && &is_directed)) != @args;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = $V->get_ids_by_paths(\@args, &is_hyperedged && &is_directed)) != @args 3 328 331
Condition66%2/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
32902
MC/DC50%1/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table66%2/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
520 328 $E->paths_to(@i);
521 }
522  
523 12 ▶sub edges_at {
Subroutine edges_at: called CC=3 SCAR=11.0
Pod documented
524 12 ▶ goto &_edges_at if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 11 12
525 11 ▶ $_[0]->[ _V ]->get_paths_by_ids([ &_edges_at ], &is_hyperedged && &is_directed);
Condition100%3/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
461
MC/DC100%2/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
526 }
527  
528 1474 ▶sub edges_from {
Subroutine edges_from: called CC=3 SCAR=11.7
Pod documented
529 1474 ▶ goto &_edges_from if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1474 1474
530 1474 ▶ $_[0]->[ _V ]->get_paths_by_ids([ &_edges_from ], &is_hyperedged && &is_directed);
Condition66%2/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
147301
MC/DC50%1/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table66%2/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
531 }
532  
533 246 ▶sub edges_to {
Subroutine edges_to: called CC=4 SCAR=14.2
Pod documented
534 246 ▶ goto &edges_from if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 30 216 246
535 216 ▶ goto &_edges_to if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 216 216
536 216 ▶ $_[0]->[ _V ]->get_paths_by_ids([ &_edges_to ], &is_hyperedged && &is_directed);
Condition66%2/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
21501
MC/DC50%1/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table66%2/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
537 }
538  
539 18483 ▶sub successors {
Subroutine successors: called CC=3 SCAR=11.0
Pod documented
540 18483 my ($g, @args) = @_;
541 18483 my ($V, $E) = @$g[ _V, _E ];
542 18483 ▶ return if (my @i = $V->get_ids_by_paths(\@args)) != @args;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = $V->get_ids_by_paths(\@args)) != @args 6 18477 18483
543 18477 my @v = $E->successors(@i);
544 18477 ▶ return @v if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4063 14414 18477
545 14414 map @$_, $V->get_paths_by_ids([ \@v ]);
546 }
547  
548 4801 ▶sub predecessors {
Subroutine predecessors: called CC=4 SCAR=13.9
Pod documented
549 4801 ▶ goto &successors if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 3430 1371 4801
550 1371 my ($g, @args) = @_;
551 1371 my ($V, $E) = @$g[ _V, _E ];
552 1371 ▶ return if (my @i = $V->get_ids_by_paths(\@args)) != @args;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = $V->get_ids_by_paths(\@args)) != @args 4 1367 1371
553 1367 my @v = $E->predecessors(@i);
554 1367 ▶ return @v if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1175 192 1367
555 192 map @$_, $V->get_paths_by_ids([ \@v ]);
556 }
557  
558 166 ▶sub _cessors_by_radius {
Subroutine _cessors_by_radius: called CC=7 SCAR=19.5
559 166 my ($radius, $method, $self_only_if_loop) = splice @_, -3, 3;
560 166 my ($g, @v) = @_;
561 166 require Set::Object;
562 166 my ($init, $next) = map Set::Object->new(@v), 1..2;
563 166 ▶ my $self = $self_only_if_loop ? Set::Object->new(grep $g->has_edge($_, $_), @v) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self_only_if_loop ? : 80 86 166
564 166 my ($got, $found) = map Set::Object->new, 1..2;
565 166 ▶ while (!defined $radius or $radius-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $radius or $radius-- > 0) 335 42 377
Condition100%3/3
not defined $radius or $radius-- > 0
l!l&&r!l&&!r
2647142
MC/DC100%2/2
not defined $radius or $radius-- > 0
not defined $radius $radius-- > 0
Truth table100%3/3
not(A) or B
A= not defined $radiusB= $radius-- > 0
ABresult
1X1
011
000
566 335 $found->insert($g->$method($next->members));
567 335 $next = $found->difference($got);
568 335 ▶ last if $next->is_null; # Leave if no new found.
Branch: 1 branch
BranchTrueFalseTotal
if $next->is_null 124 211 335
569 211 $got->insert($next->members);
570 211 $found->clear;
571 }
572 166 ▶ $got->remove($init->difference($self)->members) if $self_only_if_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self_only_if_loop 80 86 166
573 166 ▶ $got->${ wantarray ? \'members' : \'size' };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 160 6 166
574 }
575  
576 37 ▶sub all_successors {
Subroutine all_successors: called CC=1 SCAR=0.0
Pod documented
577 37 &expect_directed;
578 37 push @_, undef, 'successors', 0;
579 37 goto &_cessors_by_radius;
580 }
581  
582 9 ▶sub successors_by_radius {
Subroutine successors_by_radius: called CC=1 SCAR=0.0
Pod documented
583 9 &expect_directed;
584 9 push @_, 'successors', 0;
585 9 goto &_cessors_by_radius;
586 }
587  
588 18 ▶sub all_predecessors {
Subroutine all_predecessors: called CC=1 SCAR=0.0
Pod documented
589 18 &expect_directed;
590 18 push @_, undef, 'predecessors', 0;
591 18 goto &_cessors_by_radius;
592 }
593  
594 22 ▶sub predecessors_by_radius {
Subroutine predecessors_by_radius: called CC=1 SCAR=0.0
Pod documented
595 22 &expect_directed;
596 22 push @_, 'predecessors', 0;
597 22 goto &_cessors_by_radius;
598 }
599  
600 26 ▶sub neighbours_by_radius {
Subroutine neighbours_by_radius: called CC=1 SCAR=0.0
Pod documented
601 26 push @_, 'neighbours', 1;
602 26 goto &_cessors_by_radius;
603 }
604 84 *neighbors_by_radius = \&neighbours_by_radius;
605  
606 9606 ▶sub neighbours {
Subroutine neighbours: called CC=3 SCAR=11.0
Pod documented
607 9606 require Set::Object;
608 9606 my $s = Set::Object->new(&successors);
609 9606 ▶ $s->insert(&predecessors) if &is_directed;
Branch: 1 branch
BranchTrueFalseTotal
if &is_directed 82 9524 9606
610 9606 ▶ $s->${ wantarray ? \'members' : \'size' };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9594 12 9606
611 }
612 84 *neighbors = \&neighbours;
613  
614 54 ▶sub all_neighbours {
Subroutine all_neighbours: called CC=1 SCAR=0.0
Pod documented
615 54 push @_, undef, 'neighbours', 1;
616 54 goto &_cessors_by_radius;
617 }
618 84 *all_neighbors = \&all_neighbours;
619  
620 36 ▶sub all_reachable {
Subroutine all_reachable: called CC=2 SCAR=6.9
Pod documented
621 36 ▶ &directed ? goto &all_successors : goto &all_neighbors;
Branch: 1 branch
BranchTrueFalseTotal
&directed ? : 18 18 36
622 }
623  
624 17 ▶sub reachable_by_radius {
Subroutine reachable_by_radius: called CC=2 SCAR=6.9
Pod documented
625 17 ▶ &directed ? goto &successors_by_radius : goto &neighbors_by_radius;
Branch: 1 branch
BranchTrueFalseTotal
&directed ? : 9 8 17
626 }
627  
628 456 ▶sub delete_edge {
Subroutine delete_edge: called CC=5 SCAR=16.1
Pod documented
629 456 &expect_non_unionfind;
630 455 my $g = $_[0];
631 455 ▶ return $g if (my @i = &_vertex_ids) != @_ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = &_vertex_ids) != @_ - 1 5 450 455
632 450 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 12 438 450
633 450 ▶ return $g unless @i and $g->[ _E ]->del_path( \@i );
Branch: 1 branch
BranchTrueFalseTotal
unless @i and $g->[3]->del_path(\@i) 9 441 450
Condition100%3/3
@i and $g->[3]->del_path(\@i)
!ll&&!rl&&r
18441
MC/DC100%2/2
@i and $g->[3]->del_path(\@i)
@i $g->[3]->del_path(\@i)
Truth table100%3/3
A and B
A= @iB= $g->[3]->del_path(\@i)
ABresult
0X0
100
111
634 441 $g->[ _G ]++;
635 441 return $g;
636 }
637  
638 190 ▶sub delete_vertex {
Subroutine delete_vertex: called CC=4 SCAR=13.9
Pod documented
639 190 &expect_non_unionfind;
640 190 my $g = $_[0];
641 190 ▶ return $g if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 1 189 190
642 189 my $V = $g->[ _V ];
643 189 ▶ return $g unless defined $V->has_path($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $V->has_path($_[1]) 6 183 189
644 # TODO: _edges_at is excruciatingly slow (rt.cpan.org 92427)
645 183 my $E = $g->[ _E ];
646 183 $E->del_path( $_ ) for &_edges_at;
647 183 $V->del_path($_[1]);
648 183 $g->[ _G ]++;
649 183 return $g;
650 }
651  
652 60 ▶sub get_vertex_count {
Subroutine get_vertex_count: called CC=1 SCAR=0.0
Pod documented
653 60 my $g = shift;
654 60 $g->[ _V ]->_get_path_count( @_ );
655 }
656  
657 1187 ▶sub get_edge_count {
Subroutine get_edge_count: called CC=3 SCAR=11.0
Pod documented
658 1187 my $g = $_[0];
659 1187 ▶ return 0 if (my @i = &_vertex_ids) != @_ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (my(@i) = &_vertex_ids) != @_ - 1 16 1171 1187
660 1171 ▶ @i = sort @i if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 102 1069 1171
661 1171 $g->[ _E ]->_get_path_count( \@i );
662 }
663  
664 8 ▶sub delete_vertices {
Subroutine delete_vertices: called CC=2 SCAR=6.9
Pod documented
665 8 &expect_non_unionfind;
666 8 my $g = shift;
667 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 8 20
668 12 my $v = shift @_;
669 12 $g->delete_vertex($v);
670 }
671 8 return $g;
672 }
673  
674 8 ▶sub delete_edges {
Subroutine delete_edges: called CC=2 SCAR=6.9
Pod documented
675 8 &expect_non_unionfind;
676 8 my $g = shift;
677 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 8 18
678 10 my ($u, $v) = splice @_, 0, 2;
679 10 $g->delete_edge($u, $v);
680 }
681 8 return $g;
682 }
683  
684 ###
685 # Degrees.
686 #
687  
688 232 ▶sub in_degree {
Subroutine in_degree: called CC=6 SCAR=18.3
Pod documented
689 232 my $g = $_[0];
690 232 ▶ return undef unless @_ > 1 && &has_vertex;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 and &has_vertex 0 232 232
Condition33%1/3
@_ > 1 and &has_vertex
!ll&&!rl&&r
00232
MC/DC0%0/2
@_ > 1 and &has_vertex
@_ > 1 &has_vertex
Truth table33%1/3
A and B
A= @_ > 1B= &has_vertex
ABresult
0X0
100
111
691 232 my $in = 0;
692 232 $in += $g->get_edge_count( @$_ ) for &edges_to;
693 232 ▶ $in++ if &is_undirected and &is_self_loop_vertex;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected and &is_self_loop_vertex 5 227 232
Condition100%3/3
&is_undirected and &is_self_loop_vertex
!ll&&!rl&&r
202255
MC/DC100%2/2
&is_undirected and &is_self_loop_vertex
&is_undirected &is_self_loop_vertex
Truth table100%3/3
A and B
A= &is_undirectedB= &is_self_loop_vertex
ABresult
0X0
100
111
694 232 return $in;
695 }
696  
697 208 ▶sub out_degree {
Subroutine out_degree: called CC=6 SCAR=18.3
Pod documented
698 208 my $g = $_[0];
699 208 ▶ return undef unless @_ > 1 && &has_vertex;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 and &has_vertex 0 208 208
Condition33%1/3
@_ > 1 and &has_vertex
!ll&&!rl&&r
00208
MC/DC0%0/2
@_ > 1 and &has_vertex
@_ > 1 &has_vertex
Truth table33%1/3
A and B
A= @_ > 1B= &has_vertex
ABresult
0X0
100
111
700 208 my $out = 0;
701 208 $out += $g->get_edge_count( @$_ ) for &edges_from;
702 208 ▶ $out++ if &is_undirected and &is_self_loop_vertex;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected and &is_self_loop_vertex 1 207 208
Condition100%3/3
&is_undirected and &is_self_loop_vertex
!ll&&!rl&&r
20251
MC/DC100%2/2
&is_undirected and &is_self_loop_vertex
&is_undirected &is_self_loop_vertex
Truth table100%3/3
A and B
A= &is_undirectedB= &is_self_loop_vertex
ABresult
0X0
100
111
703 208 return $out;
704 }
705  
706 42 ▶sub _total_degree {
Subroutine _total_degree: called CC=4 SCAR=15.2
707 42 ▶ return undef unless @_ > 1 && &has_vertex;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 and &has_vertex 0 42 42
Condition33%1/3
@_ > 1 and &has_vertex
!ll&&!rl&&r
0042
MC/DC0%0/2
@_ > 1 and &has_vertex
@_ > 1 &has_vertex
Truth table33%1/3
A and B
A= @_ > 1B= &has_vertex
ABresult
0X0
100
111
708 42 ▶ &is_undirected ? &in_degree : &in_degree - &out_degree;
Branch: 1 branch
BranchTrueFalseTotal
&is_undirected ? : 24 18 42
709 }
710  
711 38 ▶sub degree {
Subroutine degree: called CC=4 SCAR=13.9
Pod documented
712 38 ▶ goto &_total_degree if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 36 2 38
713 2 ▶ return 0 if &is_directed;
Branch: 1 branch
BranchTrueFalseTotal
if &is_directed 1 1 2
714 1 my $g = $_[0];
715 1 my $total = 0;
716 1 $total += $g->_total_degree( $_ ) for &_vertices05;
717 1 return $total;
718 }
719  
720 84 *vertex_degree = \&degree;
721  
722 36 ▶sub is_sink_vertex {
Subroutine is_sink_vertex: called CC=3 SCAR=11.1
Pod documented
723 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
724 36 ▶ &successors == 0 && &predecessors > 0;
Branch: 1 branch
BranchTrueFalseTotal
&successors == 0 and &predecessors > 0 10 26 36
725 }
726  
727 36 ▶sub is_source_vertex {
Subroutine is_source_vertex: called CC=3 SCAR=11.1
Pod documented
728 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
729 36 ▶ &predecessors == 0 && &successors > 0;
Branch: 1 branch
BranchTrueFalseTotal
&predecessors == 0 and &successors > 0 7 29 36
730 }
731  
732 36 ▶sub is_successorless_vertex {
Subroutine is_successorless_vertex: called CC=2 SCAR=7.2
Pod documented
733 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
734 36 &successors == 0;
735 }
736  
737 36 ▶sub is_predecessorless_vertex {
Subroutine is_predecessorless_vertex: called CC=2 SCAR=7.2
Pod documented
738 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
739 36 &predecessors == 0;
740 }
741  
742 36 ▶sub is_successorful_vertex {
Subroutine is_successorful_vertex: called CC=2 SCAR=7.2
Pod documented
743 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
744 36 &successors > 0;
745 }
746  
747 36 ▶sub is_predecessorful_vertex {
Subroutine is_predecessorful_vertex: called CC=2 SCAR=7.2
Pod documented
748 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
749 36 &predecessors > 0;
750 }
751  
752 4437 ▶sub is_isolated_vertex {
Subroutine is_isolated_vertex: called CC=3 SCAR=11.1
Pod documented
753 4437 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 4437 4437
754 4437 ▶ &predecessors == 0 && &successors == 0;
Branch: 1 branch
BranchTrueFalseTotal
&predecessors == 0 and &successors == 0 459 3978 4437
755 }
756  
757 36 ▶sub is_interior_vertex {
Subroutine is_interior_vertex: called CC=6 SCAR=17.9
Pod documented
758 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
759 36 my $s = &successors;
760 36 ▶ $s-- if my $isl = &is_self_loop_vertex;
Branch: 1 branch
BranchTrueFalseTotal
if my $isl = &is_self_loop_vertex 8 28 36
761 36 ▶ return 0 if $s == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $s == 0 13 23 36
762 23 ▶ return $s > 0 if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 15 8 23
763 8 my $p = &predecessors;
764 8 ▶ $p-- if $isl;
Branch: 1 branch
BranchTrueFalseTotal
if $isl 2 6 8
765 8 $p > 0;
766 }
767  
768 36 ▶sub is_exterior_vertex {
Subroutine is_exterior_vertex: called CC=3 SCAR=11.1
Pod documented
769 36 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 36 36
770 36 ▶ &predecessors == 0 || &successors == 0;
Branch: 1 branch
BranchTrueFalseTotal
&predecessors == 0 or &successors == 0 7 29 36
771 }
772  
773 108 ▶sub is_self_loop_vertex {
Subroutine is_self_loop_vertex: called CC=3 SCAR=11.1
Pod documented
774 108 ▶ return 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 108 108
775 108 ▶ return 1 if grep $_ eq $_[1], &successors; # @todo: multiedges
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ eq $_[1]), &successors) 22 86 108
776 86 return 0;
777 }
778  
779 84 for my $p (qw(
780 is_sink_vertex
781 is_source_vertex
782 is_successorless_vertex
783 is_predecessorless_vertex
784 is_successorful_vertex
785 is_predecessorful_vertex
786 is_isolated_vertex
787 is_interior_vertex
788 is_exterior_vertex
789 is_self_loop_vertex
790 )) {
791 84 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
792 840 (my $m = $p) =~ s/^is_(.*)ex$/${1}ices/;
793 681 ▶ *$m = sub { my $g = $_[0]; grep $g->$p($_), &_vertices05 };
Subroutine __ANON__: called CC=1 SCAR=0.0
794 }
795  
796 ###
797 # Paths and cycles.
798 #
799  
800 140 ▶sub add_path {
Subroutine add_path: called CC=2 SCAR=6.9
Pod documented
801 140 my $g = shift;
802 140 my $u = shift;
803 140 my @edges;
804 140 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 438 140 578
805 438 my $v = shift;
806 438 push @edges, [ $u, $v ];
807 438 $u = $v;
808 }
809 140 $g->add_edges(@edges);
810 140 return $g;
811 }
812  
813 4 ▶sub delete_path {
Subroutine delete_path: called CC=2 SCAR=6.9
Pod documented
814 4 &expect_non_unionfind;
815 4 my $g = shift;
816 4 my $u = shift;
817 4 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 4 14
818 10 my $v = shift;
819 10 $g->delete_edge($u, $v);
820 10 $u = $v;
821 }
822 4 return $g;
823 }
824  
825 20 ▶sub has_path {
Subroutine has_path: called CC=3 SCAR=11.0
Pod documented
826 20 my $g = shift;
827 20 my $u = shift;
828 20 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 43 7 50
829 43 my $v = shift;
830 43 ▶ return 0 unless $g->has_edge($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $g->has_edge($u, $v) 13 30 43
831 30 $u = $v;
832 }
833 7 return $g;
834 }
835  
836 52 ▶sub add_cycle {
Subroutine add_cycle: called CC=1 SCAR=0.0
Pod documented
837 52 push @_, $_[1];
838 52 goto &add_path;
839 }
840  
841 2 ▶sub delete_cycle {
Subroutine delete_cycle: called CC=1 SCAR=0.0
Pod documented
842 2 &expect_non_unionfind;
843 2 push @_, $_[1];
844 2 goto &delete_path;
845 }
846  
847 9 ▶sub has_cycle {
Subroutine has_cycle: called CC=2 SCAR=6.9
Pod documented
848 9 ▶ return 0 if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 8 9
849 8 push @_, $_[1];
850 8 goto &has_path;
851 }
852  
853 84 *has_this_cycle = \&has_cycle;
854  
855 17 ▶sub has_a_cycle {
Subroutine has_a_cycle: called CC=1 SCAR=0.0
Pod documented
856 17 my $g = shift;
857 17 require Graph::Traversal::DFS;
858 17 my $t = Graph::Traversal::DFS->new($g, has_a_cycle => 1, @_);
859 17 $t->dfs;
860 17 return $t->get_state('has_a_cycle');
861 }
862  
863 2 ▶sub find_a_cycle {
Subroutine find_a_cycle: called CC=3 SCAR=11.0
Pod documented
864 2 require Graph::Traversal::DFS;
865 2 my @r = ( back_edge => \&Graph::Traversal::find_a_cycle);
866 2 ▶ push @r,
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 1 1 2
867 down_edge => \&Graph::Traversal::find_a_cycle
868 if &is_undirected;
869 2 my $g = shift;
870 2 my $t = Graph::Traversal::DFS->new($g, @r, @_);
871 2 $t->dfs;
872 2 ▶ $t->has_state('a_cycle') ? @{ $t->get_state('a_cycle') } : ();
Branch: 1 branch
BranchTrueFalseTotal
$t->has_state('a_cycle') ? : 2 0 2
873 }
874  
875 ###
876 # Attributes.
877  
878 84 my @generic_methods = (
879 [ 'set_attribute', 'my (\$attr, \$value) = splice \@_, -2; &$add unless &$has;',
880 '\$_[0]->[ $offset ]->_set_path_attr( \@args, \$attr, \$value );' ],
881 [ 'set_attributes', 'my \$attr = pop; &$add unless &$has;',
882 '\$_[0]->[ $offset ]->_set_path_attrs( \@args, \$attr );', ],
883 [ 'has_attributes', 'return 0 unless &$has;',
884 '\$_[0]->[ $offset ]->_has_path_attrs( \@args );', ],
885 [ 'has_attribute', 'my \$attr = pop; return 0 unless &$has;',
886 '\$_[0]->[ $offset ]->_has_path_attr( \@args, \$attr );', ],
887 [ 'get_attributes', 'return undef unless &$has;',
888 'scalar \$_[0]->[ $offset ]->_get_path_attrs( \@args );', ],
889 [ 'get_attribute', 'my \$attr = pop; return undef unless &$has;',
890 'scalar \$_[0]->[ $offset ]->_get_path_attr( \@args, \$attr );', ],
891 [ 'get_attribute_names', 'return unless &$has;',
892 '\$_[0]->[ $offset ]->_get_path_attr_names( \@args );', ],
893 [ 'get_attribute_values', 'return unless &$has;',
894 '\$_[0]->[ $offset ]->_get_path_attr_values( \@args );', ],
895 [ 'delete_attributes', 'return undef unless &$has;',
896 '\$_[0]->[ $offset ]->_del_path_attrs( \@args );', ],
897 [ 'delete_attribute', 'my \$attr = pop; return undef unless &$has;',
898 '\$_[0]->[ $offset ]->_del_path_attr( \@args, \$attr );', ],
899 );
900 84 my %entity2offset = (vertex => '_V', edge => '_E');
901 84 my %entity2args = (edge => '&_vertex_ids');
902 84 my $template_mid = 'my \@args = @{[ $args || "\@_[1..\$#_]" ]};$munge';
903 84 for my $entity (qw(vertex edge)) {
904 84 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
905 168 my $has_base = 'has_' . $entity;
906 168 my $add_base = 'add_' . $entity;
907 168 my $offset = $entity2offset{$entity};
908 168 for my $t (@generic_methods) {
909 1680 my ($raw, $t1, $t2) = @$t;
910 1680 my ($first, $rest) = ($raw =~ /^(\w+?)_(.+)/);
911 1680 my $is_vertex = $entity eq 'vertex';
912 1680 my $m = join '_', $first, $entity, $rest;
913 1680 ▶ my ($args, $munge, $has, $add) = ($entity2args{$entity}, $is_vertex ? '' : "\n\@args = &is_undirected ? [sort \@args] : [\@args];", $has_base, $add_base);
Branch: 1 branch
BranchTrueFalseTotal
$is_vertex ? : 840 840 1680
914 1680 my $func_text = "qq{sub $m {\n&expect_non_multi$entity;\n$t1\n$template_mid\n$t2\n}}\n"; #warn "$m:\n$func_text\n";
915 1680 my $tv2 = eval $func_text; #warn "$m v2:\n$tv2\n";
916 3 ▶ eval $tv2; die if $@;
Subroutine delete_edge_attribute: called CC=3 SCAR=11.0
Subroutine delete_edge_attributes: called CC=3 SCAR=11.0
Subroutine delete_vertex_attribute: called CC=2 SCAR=7.0
Subroutine delete_vertex_attributes: called CC=2 SCAR=7.0
Subroutine get_edge_attribute: called CC=3 SCAR=11.0
Subroutine get_edge_attribute_names: called CC=3 SCAR=11.0
Subroutine get_edge_attribute_values: called CC=3 SCAR=11.0
Subroutine get_edge_attributes: called CC=3 SCAR=11.0
Subroutine get_vertex_attribute: called CC=2 SCAR=7.0
Subroutine get_vertex_attribute_names: called CC=2 SCAR=7.0
Subroutine get_vertex_attribute_values: called CC=2 SCAR=7.0
Subroutine get_vertex_attributes: called CC=2 SCAR=7.0
Subroutine has_edge_attribute: called CC=3 SCAR=11.0
Subroutine has_edge_attributes: called CC=3 SCAR=11.0
Subroutine has_vertex_attribute: called CC=2 SCAR=7.0
Subroutine has_vertex_attributes: called CC=2 SCAR=7.0
Subroutine set_edge_attribute: called CC=3 SCAR=11.0
Subroutine set_edge_attributes: called CC=3 SCAR=11.0
Subroutine set_vertex_attribute: called CC=2 SCAR=7.0
Subroutine set_vertex_attributes: called CC=2 SCAR=7.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 31 branches
BranchTrueFalseTotal
unless &has_edge 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge 0 2 2
&is_undirected ? : 0 2 2
unless &has_vertex 0 3 3
unless &has_vertex 0 2 2
unless &has_edge 61 11226 11287
&is_undirected ? : 10335 891 11226
unless &has_edge 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge 1 537 538
&is_undirected ? : 15 522 537
unless &has_vertex 7 280 287
unless &has_vertex 0 3 3
unless &has_vertex 0 3 3
unless &has_vertex 1 62 63
unless &has_edge 1 5 6
&is_undirected ? : 0 5 5
unless &has_edge 0 10 10
&is_undirected ? : 0 10 10
unless &has_vertex 1 5 6
unless &has_vertex 0 10 10
unless &has_edge 4588 34 4622
&is_undirected ? : 3937 685 4622
unless &has_edge 2 3 5
&is_undirected ? : 2 3 5
unless &has_vertex 48 10 58
unless &has_vertex 1033 85 1118
if $@ 0 1680 1680
917 1680 $m .= '_by_id';
918 1680 ▶ ($args, $munge, $has, $add) = ($entity2args{$entity} && "$entity2args{$entity}_multi", $is_vertex ? '' : "\n\@args = (&is_undirected ? [sort \@args[0..\$#args-1]] : [\@args[0..\$#args-1]], \$args[-1]);", $has_base.'_by_id', $add_base.'_by_id');
Branch: 1 branch
BranchTrueFalseTotal
$is_vertex ? : 840 840 1680
Condition100%2/2
$entity2args{$entity} && "$entity2args{$entity}_multi"
!ll
840840
MC/DC100%1/1
$entity2args{$entity} && "$entity2args{$entity}_multi"
$entity2args{$entity}
Truth table100%2/2
A
A= $entity2args{$entity}
Aresult
00
11
919 1680 $func_text = "qq{sub $m {\n&expect_multi$entity;\n$t1\n$template_mid\n$t2\n}}\n"; #warn "$m:\n$func_text\n";
920 1680 $tv2 = eval $func_text; #warn "$m v2:\n$tv2\n";
921 3 ▶ eval $tv2; die if $@;
Subroutine delete_edge_attribute_by_id: called CC=3 SCAR=11.0
Subroutine delete_edge_attributes_by_id: called CC=3 SCAR=11.0
Subroutine delete_vertex_attribute_by_id: called CC=2 SCAR=7.0
Subroutine delete_vertex_attributes_by_id: called CC=2 SCAR=7.0
Subroutine get_edge_attribute_by_id: called CC=3 SCAR=11.0
Subroutine get_edge_attribute_names_by_id: called CC=3 SCAR=11.0
Subroutine get_edge_attribute_values_by_id: called CC=3 SCAR=11.0
Subroutine get_edge_attributes_by_id: called CC=3 SCAR=11.0
Subroutine get_vertex_attribute_by_id: called CC=2 SCAR=7.0
Subroutine get_vertex_attribute_names_by_id: called CC=2 SCAR=7.0
Subroutine get_vertex_attribute_values_by_id: called CC=2 SCAR=7.0
Subroutine get_vertex_attributes_by_id: called CC=2 SCAR=7.0
Subroutine has_edge_attribute_by_id: called CC=3 SCAR=11.0
Subroutine has_edge_attributes_by_id: called CC=3 SCAR=11.0
Subroutine has_vertex_attribute_by_id: called CC=2 SCAR=7.0
Subroutine has_vertex_attributes_by_id: called CC=2 SCAR=7.0
Subroutine set_edge_attribute_by_id: called CC=3 SCAR=11.0
Subroutine set_edge_attributes_by_id: called CC=3 SCAR=11.0
Subroutine set_vertex_attribute_by_id: called CC=2 SCAR=7.0
Subroutine set_vertex_attributes_by_id: called CC=2 SCAR=7.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 31 branches
BranchTrueFalseTotal
unless &has_edge_by_id 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge_by_id 0 2 2
&is_undirected ? : 0 2 2
unless &has_vertex_by_id 0 3 3
unless &has_vertex_by_id 0 2 2
unless &has_edge_by_id 0 129 129
&is_undirected ? : 2 127 129
unless &has_edge_by_id 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge_by_id 0 3 3
&is_undirected ? : 0 3 3
unless &has_edge_by_id 0 85 85
&is_undirected ? : 37 48 85
unless &has_vertex_by_id 0 17 17
unless &has_vertex_by_id 0 3 3
unless &has_vertex_by_id 0 3 3
unless &has_vertex_by_id 0 78 78
unless &has_edge_by_id 1 5 6
&is_undirected ? : 0 5 5
unless &has_edge_by_id 0 10 10
&is_undirected ? : 0 10 10
unless &has_vertex_by_id 1 5 6
unless &has_vertex_by_id 0 10 10
unless &has_edge_by_id 34 9 43
&is_undirected ? : 5 38 43
unless &has_edge_by_id 18 1 19
&is_undirected ? : 6 13 19
unless &has_vertex_by_id 3 9 12
unless &has_vertex_by_id 14 1 15
if $@ 0 1680 1680
922 }
923 }
924  
925 5740 ▶sub get_edge_attribute_all {
Subroutine get_edge_attribute_all: called CC=3 SCAR=11.1
Pod documented
926 5740 my ($g, $u, $v, $name) = @_;
927 5740 ▶ die "no attribute name given" if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5740 5740
928 5740 ▶ grep defined(),
Branch: 1 branch
BranchTrueFalseTotal
&is_multiedged ? : 81 5659 5740
929 &is_multiedged ? (map $g->get_edge_attribute_by_id($u, $v, $_, $name),
930 $g->get_multiedge_ids($u, $v))
931 : $g->get_edge_attribute($u, $v, $name);
932 }
933  
934 3524 ▶sub add_vertices {
Subroutine add_vertices: called CC=4 SCAR=13.9
Pod documented
935 3524 my ($g, @v) = @_;
936 3524 ▶ if (&is_multivertexed) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multivertexed) 1 3523 3524
937 1 $g->add_vertex_by_id($_, _GEN_ID) for @v;
938 1 return $g;
939 }
940 3523 my @i = $g->[ _V ]->set_paths(@v);
941 3523 $g->[ _G ]++;
942 3523 ▶ return $g if !&has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
unless &has_union_find 3518 5 3523
943 5 $g->[ _U ]->add(@i);
944 5 $g;
945 }
946  
947 38776 ▶sub add_edges {
Subroutine add_edges: called CC=10 SCAR=23.0
Pod documented
948 38776 my ($g, @args) = @_;
949 38776 my @edges;
950 38776 ▶ while (defined(my $u = shift @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $u = shift @args)) 55686 38775 94461
951 55686 ▶ push @edges, ref $u eq 'ARRAY' ? $u : @args ? [ $u, shift @args ]
Branch: 2 branches
BranchTrueFalseTotal
ref $u eq 'ARRAY' ? : 55580 106 55686
@args ? : 105 1 106
952 : __carp_confess "Graph::add_edges: missing end vertex";
953 }
954 38775 ▶ if (&is_multiedged) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multiedged) 25 38750 38775
955 25 $g->add_edge_by_id(@$_, _GEN_ID) for @edges;
956 25 return $g;
957 }
958 38750 my $uf = &has_union_find;
959 38750 ▶ my $deep = &is_hyperedged && &is_directed;
Condition100%3/3
&is_hyperedged && &is_directed
!ll&&!rl&&r
387231710
MC/DC100%2/2
&is_hyperedged && &is_directed
&is_hyperedged &is_directed
Truth table100%3/3
A && B
A= &is_hyperedgedB= &is_directed
ABresult
0X0
100
111
960 38750 ▶ my @paths = $g->[ _V ]->get_ids_by_paths(\@edges, 1, 1 + ($deep ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$deep ? : 10 38740 38750
961 38750 ▶ @paths = map [ sort @$_ ], @paths if &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
if &is_undirected 30926 7824 38750
962 38750 $g->[ _E ]->set_paths( @paths );
963 38750 ▶ $uf->union(@paths) if $uf;
Branch: 1 branch
BranchTrueFalseTotal
if $uf 15 38735 38750
964 38750 $g->[ _G ]++;
965 38750 return $g;
966 }
967  
968 4 ▶sub add_edges_by_id {
Subroutine add_edges_by_id: called CC=5 SCAR=16.7
Pod documented
969 4 &expect_multiedged;
970 4 my ($g, $id) = (shift, pop);
971 4 my @edges;
972 4 ▶ while (defined(my $u = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $u = shift @_)) 8 4 12
973 8 ▶ push @edges, ref $u eq 'ARRAY' ? $u : @_ ? [ $u, shift @_ ]
Branch: 2 branches
BranchTrueFalseTotal
ref $u eq 'ARRAY' ? : 8 0 8
@_ ? : 0 0 0
974 : __carp_confess "Graph::add_edges: missing end vertex";
975 }
976 4 $g->add_edge_by_id(@$_, $id) for @edges;
977 4 return $g;
978 }
979  
980 24 ▶sub rename_vertex {
Subroutine rename_vertex: called CC=1 SCAR=0.0
Pod documented
981 24 my $g = shift;
982 24 $g->[ _V ]->rename_path(@_);
983 24 return $g;
984 }
985  
986 3 ▶sub rename_vertices {
Subroutine rename_vertices: called CC=2 SCAR=6.9
Pod documented
987 3 my ($g, $code) = @_;
988 3 my %seen;
989 $g->rename_vertex($_, $code->($_))
990 3 for grep !$seen{$_}++, $g->[ _V ]->paths;
991 3 return $g;
992 }
993  
994 3 ▶sub filter_vertices {
Subroutine filter_vertices: called CC=4 SCAR=13.9
Pod documented
995 3 my ($g, $code) = @_;
996 3 my @v = &_vertices05;
997 3 ▶ if (&is_multivertexed) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multivertexed) { } 1 2 3
998 1 for my $v (@v) {
999 7 $g->delete_vertex_by_id($v, $_) for
1000 grep !$code->($g, $v, $_), $g->get_multivertex_ids($v);
1001 }
1002 } else {
1003 2 $g->delete_vertices(grep !$code->($g, $_), @v);
1004 }
1005 3 $g;
1006 }
1007  
1008 3 ▶sub filter_edges {
Subroutine filter_edges: called CC=4 SCAR=13.9
Pod documented
1009 3 my ($g, $code) = @_;
1010 3 my @e = &_edges05;
1011 3 ▶ if (&is_multiedged) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multiedged) { } 1 2 3
1012 1 for my $e (@e) {
1013 6 $g->delete_edge_by_id(@$e, $_) for
1014 grep !$code->($g, @$e, $_), $g->get_multiedge_ids(@$e);
1015 }
1016 } else {
1017 2 $g->delete_edges(map @$_, grep !$code->($g, @$_), @e);
1018 }
1019 3 $g;
1020 }
1021  
1022 22 ▶sub as_hashes {
Subroutine as_hashes: called CC=13 SCAR=25.7
Pod documented
1023 22 my ($g) = @_;
1024 22 my (%v, %e, @e);
1025 22 my ($is_hyper, $is_directed) = (&is_hyperedged, &is_directed);
1026 22 ▶ if (&is_multivertexed) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multivertexed) { } 13 9 22
1027 13 for my $v ($g->unique_vertices) {
1028 59 ▶ $v{$v} = {
Condition50%1/2
$g->get_vertex_attributes_by_id($v, $_) || {}
l!l
600
MC/DC0%0/1
$g->get_vertex_attributes_by_id($v, $_) || {}
$g->get_vertex_attributes_by_id($v, $_)
Truth table50%1/2
A
A= $g->get_vertex_attributes_by_id($v, $_)
Aresult
11
00
1029 map +($_ => $g->get_vertex_attributes_by_id($v, $_) || {}),
1030 $g->get_multivertex_ids($v)
1031 };
1032 }
1033 } else {
1034 9 ▶ %v = map +($_ => $g->get_vertex_attributes($_) || {}), $g->unique_vertices;
Condition100%2/2
$g->get_vertex_attributes($_) || {}
l!l
149
MC/DC100%1/1
$g->get_vertex_attributes($_) || {}
$g->get_vertex_attributes($_)
Truth table100%2/2
A
A= $g->get_vertex_attributes($_)
Aresult
11
00
1035 }
1036 22 my $multi_e = &is_multiedged;
1037 22 for my $e ($g->edges) {
1038 my $edge_attr = {
1039 $multi_e
1040 ? map +($_ => $g->get_edge_attributes_by_id(@$e, $_) || {}),
1041 $g->get_multiedge_ids(@$e)
1042 105 ▶ : %{ $g->get_edge_attributes(@$e)||{} }
Branch: 1 branch
BranchTrueFalseTotal
$multi_e ? : 65 40 105
Condition100%4/4
$g->get_edge_attributes_by_id(@$e, $_) || {}
l!l
641
$g->get_edge_attributes(@$e) || {}
l!l
436
MC/DC100%1/1
$g->get_edge_attributes_by_id(@$e, $_) || {}
$g->get_edge_attributes_by_id(@$e, $_)
MC/DC100%1/1
$g->get_edge_attributes(@$e) || {}
$g->get_edge_attributes(@$e)
Truth table100%2/2
A
A= $g->get_edge_attributes_by_id(@$e, $_)
Aresult
11
00
Truth table100%2/2
A
A= $g->get_edge_attributes(@$e)
Aresult
11
00
1043 };
1044 105 ▶ if ($is_hyper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hyper) { } 12 93 105
1045 12 my %h = (attributes => $edge_attr);
1046 12 ▶ if ($is_directed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_directed) { } 8 4 12
1047 8 @h{qw(predecessors successors)} = @$e;
1048 } else {
1049 4 $h{vertices} = $e;
1050 }
1051 12 push @e, \%h;
1052 } else {
1053 93 $e{ $e->[0] }{ $e->[1] } = $edge_attr;
1054 93 ▶ $e{ $e->[1] }{ $e->[0] } = $edge_attr if !$is_directed;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_directed 39 54 93
1055 }
1056 }
1057 22 ▶ ( \%v, $is_hyper ? \@e : \%e );
Branch: 1 branch
BranchTrueFalseTotal
$is_hyper ? : 4 18 22
1058 }
1059  
1060 3 ▶sub ingest {
Subroutine ingest: called CC=1 SCAR=0.0
Pod documented
1061 3 my ($g, $g2) = @_;
1062 3 _copy_vertices($g2, $g, 1);
1063 3 _copy_edges($g2, $g, 1);
1064 3 $g;
1065 }
1066  
1067 ###
1068 # More constructors.
1069 #
1070  
1071 52 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
1072 52 my ($g, @args) = @_;
1073 52 my $c = $g->new(@args);
1074 52 _copy_vertices($g, $c);
1075 52 _copy_edges($g, $c);
1076 52 return $c;
1077 }
1078  
1079 84 *copy_graph = \&copy;
1080  
1081 24 ▶sub _deep_copy_best {
Subroutine _deep_copy_best: called CC=2 SCAR=7.6
1082 24 ▶ _can_deep_copy_Storable()
Branch: 1 branch
BranchTrueFalseTotal
&_can_deep_copy_Storable() ? : 24 0 24
1083 ? _deep_copy_Storable(@_) : _deep_copy_DataDumper(@_);
1084 }
1085  
1086 25 ▶sub _deep_copy_Storable {
Subroutine _deep_copy_Storable: called CC=1 SCAR=0.0
1087 25 my $g = shift;
1088 25 require Safe; # For deep_copy().
1089 25 my $safe = Safe->new;
1090 25 $safe->permit(qw/:load/);
1091 25 local $Storable::Deparse = 1;
1092 25 ▶ local $Storable::Eval = sub { $safe->reval($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
1093 25 return Storable::thaw(Storable::freeze($g));
1094 }
1095  
1096 1 ▶sub _deep_copy_DataDumper {
Subroutine _deep_copy_DataDumper: called CC=2 SCAR=6.9
1097 1 my $g = shift;
1098 1 require Data::Dumper;
1099 1 my $d = Data::Dumper->new([$g]);
1100 84 ▶ use vars qw($VAR1);
Subroutine BEGIN: called CC=1 SCAR=0.0
1101 1 $d->Purity(1)->Terse(1)->Deepcopy(1);
1102 1 ▶ $d->Deparse(1) if $] >= 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 1 0 1
1103 1 ▶ eval $d->Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1104 }
1105  
1106 17 ▶sub deep_copy {
Subroutine deep_copy: called CC=2 SCAR=6.9
Pod documented
1107 17 local $. = $.;
1108 17 my $g2 = _deep_copy_best(@_);
1109 17 ▶ $g2->[ _V ]->reindex if grep ref, &_vertices05;
Branch: 1 branch
BranchTrueFalseTotal
if grep ref($_), &_vertices05 1 16 17
1110 17 $g2;
1111 }
1112  
1113 84 *deep_copy_graph = \&deep_copy;
1114  
1115 492 ▶sub transpose_edge {
Subroutine transpose_edge: called CC=5 SCAR=16.3
Pod documented
1116 492 my $g = $_[0];
1117 492 ▶ return $g if !&is_directed;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_directed 0 492 492
1118 492 ▶ return undef unless &has_edge;
Branch: 1 branch
BranchTrueFalseTotal
unless &has_edge 0 492 492
1119 492 my $c = &get_edge_count;
1120 492 my $a = &get_edge_attributes;
1121 492 my @e = reverse @_[1..$#_];
1122 492 ▶ &delete_edge unless $g->has_edge( @e );
Branch: 1 branch
BranchTrueFalseTotal
unless $g->has_edge(@e) 399 93 492
1123 492 $g->add_edges(map \@e, 1..$c);
1124 492 ▶ $g->set_edge_attributes(@e, $a) if $a;
Branch: 1 branch
BranchTrueFalseTotal
if $a 0 492 492
1125 492 return $g;
1126 }
1127  
1128 20 ▶sub transpose_graph {
Subroutine transpose_graph: called CC=3 SCAR=11.0
Pod documented
1129 20 my $t = &copy;
1130 20 ▶ return $t if !&directed;
Branch: 1 branch
BranchTrueFalseTotal
unless &directed 3 17 20
1131 17 $t->transpose_edge(@$_) for &_edges05;
1132 17 return $t;
1133 }
1134  
1135 84 *transpose = \&transpose_graph;
1136  
1137 9 ▶sub complete_graph {
Subroutine complete_graph: called CC=3 SCAR=11.0
Pod documented
1138 9 my $directed = &is_directed;
1139 9 my $c = &new;
1140 9 my @v = &_vertices05;
1141 9 my @edges;
1142 9 ▶ for (my $i = $#v; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 20 9 29
1143 20 ▶ push @edges, map +([$v[$i], $v[$_]], $directed ? [$v[$_], $v[$i]] : ()),
Branch: 1 branch
BranchTrueFalseTotal
$directed ? : 12 7 19
1144 0..$i - 1;
1145 }
1146 9 $c->add_edges(@edges);
1147 9 return $c;
1148 }
1149  
1150 3 ▶sub max_cliques {
Subroutine max_cliques: called CC=2 SCAR=6.9
Pod documented
1151 3 my ($g) = @_;
1152 3 &expect_undirected;
1153 3 $g->bron_kerbosch_pivot([], [$g->vertices], [], \ my @cliques);
1154 3 ▶ return wantarray ? @cliques : \@cliques
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
1155 }
1156  
1157 62 ▶sub bron_kerbosch_pivot {
Subroutine bron_kerbosch_pivot: called CC=6 SCAR=17.9
Pod documented
1158 62 my ($g, $r, $p, $x, $max_cliques) = @_;
1159 62 ▶ if (! @$p && ! @$x && @$r) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$p || @$x and @$r) 22 40 62
Condition83%5/6
not @$p || @$x and @$r
!ll&&!rl&&r
40022
@$p || @$x
l!l&&r!l&&!r
31922
MC/DC66%2/3
not @$p || @$x and @$r
@$p @$x @$r
Truth table75%3/4
not(A || B) and C
A= @$pB= @$xC= @$r
ABCresult
1XX0
01X0
0000
0011
1160 22 push @$max_cliques, [@$r];
1161 22 return;
1162 }
1163 40 my $pivot = (@$p, @$x)[0];
1164 40 for my $v (my @p = @$p) {
1165 76 ▶ next if $g->has_edge($pivot, $v);
Branch: 1 branch
BranchTrueFalseTotal
if $g->has_edge($pivot, $v) 17 59 76
1166 $g->bron_kerbosch_pivot(
1167 [@$r, $v],
1168 196 [grep { my $w = $_; grep $_ eq $w, @$p } $g->neighbours($v)],
1169 59 [grep { my $w = $_; grep $_ eq $w, @$x } $g->neighbours($v)],
1170 $max_cliques);
1171 59 @$p = grep $_ ne $v, @$p;
1172 59 push @$x, $v;
1173 }
1174 }
1175  
1176 84 *complement = \&complement_graph;
1177  
1178 5 ▶sub complement_graph {
Subroutine complement_graph: called CC=2 SCAR=6.9
Pod documented
1179 5 my $c = &complete_graph;
1180 5 $c->delete_edge(@$_) for &edges;
1181 5 return $c;
1182 }
1183  
1184 84 *complete = \&complete_graph;
1185  
1186 1222 ▶sub subgraph {
Subroutine subgraph: called CC=11 SCAR=24.1
Pod documented
1187 1222 my ($g, $src, $dst) = @_;
1188 1222 ▶ __carp_confess "Graph::subgraph: need src and dst array references"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $src eq "ARRAY" and !defined($dst) || ref $dst eq "ARRAY" 0 1222 1222
Condition50%3/6
ref $src eq "ARRAY" and !defined($dst) || ref $dst eq "ARRAY"
!ll&&!rl&&r
001222
not defined $dst or ref $dst eq "ARRAY"
l!l&&r!l&&!r
121840
MC/DC0%0/3
ref $src eq "ARRAY" and !defined($dst) || ref $dst eq "ARRAY"
ref $src eq "ARRAY" not defined $dst ref $dst eq "ARRAY"
Truth table50%2/4
A and not(B) or C
A= ref $src eq "ARRAY"B= not defined $dstC= ref $dst eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
1189 unless ref $src eq 'ARRAY' && (!defined($dst) or ref $dst eq 'ARRAY');
1190 1222 require Set::Object;
1191 1222 my $s = $g->new;
1192 1222 my @u = grep $g->has_vertex($_), @$src;
1193 1222 ▶ my $v = Set::Object->new($dst ? grep $g->has_vertex($_), @$dst : @u);
Branch: 1 branch
BranchTrueFalseTotal
$dst ? : 4 1218 1222
1194 1222 ▶ $s->add_vertices(@u, $dst ? $v->members : ());
Branch: 1 branch
BranchTrueFalseTotal
$dst ? : 4 1218 1222
1195 1222 my $directed = &is_directed;
1196 1222 ▶ if ($directed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directed) { } 12 1210 1222
1197 12 $s->add_edges(grep $v->contains($_->[1]), $g->edges_from(@u));
1198 } else {
1199 1210 ▶ my $valid = $dst ? $v + Set::Object->new(@u) : $v;
Branch: 1 branch
BranchTrueFalseTotal
$dst ? : 2 1208 1210
1200 1210 ▶ $s->add_edges(
Condition100%9/9
($v->contains($_->[0]) || $v->contains($_->[1])) && ($valid->contains($_->[0]) && $valid->contains($_->[1]))
!ll&&!rl&&r
557688463
$v->contains($_->[0]) or $v->contains($_->[1])
l!l&&r!l&&!r
1035238795
$valid->contains($_->[0]) and $valid->contains($_->[1])
!ll&&!rl&&r
387618928463
MC/DC100%4/4
($v->contains($_->[0]) || $v->contains($_->[1])) && ($valid->contains($_->[0]) && $valid->contains($_->[1]))
$v->contains($_->[0]) $v->contains($_->[1]) $valid->contains($_->[0]) $valid->contains($_->[1])
Truth table71%5/7
A or B && C and D
A= $v->contains($_->[0])B= $v->contains($_->[1])C= $valid->contains($_->[0])D= $valid->contains($_->[1])
ABCDresult
00XX0
1X0X0
1X100
010X0
01100
1X111
01111
1201 grep +($v->contains($_->[0]) || $v->contains($_->[1])) &&
1202 ($valid->contains($_->[0]) && $valid->contains($_->[1])),
1203 $g->edges_from(@u)
1204 );
1205 }
1206 1222 return $s;
1207 }
1208  
1209 ###
1210 # Transitivity.
1211 #
1212  
1213 4 ▶sub is_transitive {
Subroutine is_transitive: called CC=1 SCAR=0.0
Pod documented
1214 4 my $g = shift;
1215 4 require Graph::TransitiveClosure;
1216 4 Graph::TransitiveClosure::is_transitive($g);
1217 }
1218  
1219 ###
1220 # Weighted vertices.
1221 #
1222  
1223 84 my $defattr = 'weight';
1224  
1225 149 ▶sub _defattr {
Subroutine _defattr: called CC=1 SCAR=0.0
1226 149 return $defattr;
1227 }
1228  
1229 1 ▶sub add_weighted_vertex {
Subroutine add_weighted_vertex: called CC=1 SCAR=0.0
Pod documented
1230 1 &expect_non_multivertexed;
1231 1 push @_, $defattr, pop;
1232 1 goto &set_vertex_attribute;
1233 }
1234  
1235 1 ▶sub add_weighted_vertices {
Subroutine add_weighted_vertices: called CC=2 SCAR=6.9
Pod documented
1236 1 &expect_non_multivertexed;
1237 1 my $g = shift;
1238 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
1239 2 my ($v, $w) = splice @_, 0, 2;
1240 2 $g->set_vertex_attribute($v, $defattr, $w);
1241 }
1242 }
1243  
1244 5 ▶sub get_vertex_weight {
Subroutine get_vertex_weight: called CC=1 SCAR=0.0
Pod documented
1245 5 &expect_non_multivertexed;
1246 5 push @_, $defattr;
1247 5 goto &get_vertex_attribute;
1248 }
1249  
1250 3 ▶sub has_vertex_weight {
Subroutine has_vertex_weight: called CC=1 SCAR=0.0
Pod documented
1251 3 &expect_non_multivertexed;
1252 3 push @_, $defattr;
1253 3 goto &has_vertex_attribute;
1254 }
1255  
1256 1 ▶sub set_vertex_weight {
Subroutine set_vertex_weight: called CC=1 SCAR=0.0
Pod documented
1257 1 &expect_non_multivertexed;
1258 1 push @_, $defattr, pop;
1259 1 goto &set_vertex_attribute;
1260 }
1261  
1262 1 ▶sub delete_vertex_weight {
Subroutine delete_vertex_weight: called CC=1 SCAR=0.0
Pod documented
1263 1 &expect_non_multivertexed;
1264 1 push @_, $defattr;
1265 1 goto &delete_vertex_attribute;
1266 }
1267  
1268 1 ▶sub add_weighted_vertex_by_id {
Subroutine add_weighted_vertex_by_id: called CC=1 SCAR=0.0
Pod documented
1269 1 &expect_multivertexed;
1270 1 push @_, $defattr, pop;
1271 1 goto &set_vertex_attribute_by_id;
1272 }
1273  
1274 1 ▶sub add_weighted_vertices_by_id {
Subroutine add_weighted_vertices_by_id: called CC=2 SCAR=6.9
Pod documented
1275 1 &expect_multivertexed;
1276 1 my $g = shift;
1277 1 my $id = pop;
1278 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
1279 2 my ($v, $w) = splice @_, 0, 2;
1280 2 $g->add_vertex_by_id($v, $id);
1281 2 $g->set_vertex_attribute_by_id($v, $id, $defattr, $w);
1282 }
1283 }
1284  
1285 5 ▶sub get_vertex_weight_by_id {
Subroutine get_vertex_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1286 5 &expect_multivertexed;
1287 5 push @_, $defattr;
1288 5 goto &get_vertex_attribute_by_id;
1289 }
1290  
1291 3 ▶sub has_vertex_weight_by_id {
Subroutine has_vertex_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1292 3 &expect_multivertexed;
1293 3 push @_, $defattr;
1294 3 goto &has_vertex_attribute_by_id;
1295 }
1296  
1297 1 ▶sub set_vertex_weight_by_id {
Subroutine set_vertex_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1298 1 &expect_multivertexed;
1299 1 push @_, $defattr, pop;
1300 1 goto &set_vertex_attribute_by_id;
1301 }
1302  
1303 1 ▶sub delete_vertex_weight_by_id {
Subroutine delete_vertex_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1304 1 &expect_multivertexed;
1305 1 push @_, $defattr;
1306 1 goto &delete_vertex_attribute_by_id;
1307 }
1308  
1309 ###
1310 # Weighted edges.
1311 #
1312  
1313 2588 ▶sub add_weighted_edge {
Subroutine add_weighted_edge: called CC=1 SCAR=0.0
Pod documented
1314 2588 &expect_non_multiedged;
1315 2588 push @_, $defattr, pop;
1316 2588 goto &set_edge_attribute;
1317 }
1318  
1319 3 ▶sub add_weighted_edges {
Subroutine add_weighted_edges: called CC=2 SCAR=6.9
Pod documented
1320 3 &expect_non_multiedged;
1321 3 my $g = shift;
1322 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 3 17
1323 14 my ($u, $v, $w) = splice @_, 0, 3;
1324 14 $g->set_edge_attribute($u, $v, $defattr, $w);
1325 }
1326 }
1327  
1328 1 ▶sub add_weighted_edges_by_id {
Subroutine add_weighted_edges_by_id: called CC=2 SCAR=6.9
Pod documented
1329 1 &expect_multiedged;
1330 1 my $g = shift;
1331 1 my $id = pop;
1332 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
1333 2 my ($u, $v, $w) = splice @_, 0, 3;
1334 2 $g->set_edge_attribute_by_id($u, $v, $id, $defattr, $w);
1335 }
1336 }
1337  
1338 6 ▶sub add_weighted_path {
Subroutine add_weighted_path: called CC=2 SCAR=6.9
Pod documented
1339 6 &expect_non_multiedged;
1340 6 my $g = shift;
1341 6 my $u = shift;
1342 6 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 22 6 28
1343 22 my ($w, $v) = splice @_, 0, 2;
1344 22 $g->set_edge_attribute($u, $v, $defattr, $w);
1345 22 $u = $v;
1346 }
1347 }
1348  
1349 7 ▶sub get_edge_weight {
Subroutine get_edge_weight: called CC=1 SCAR=0.0
Pod documented
1350 7 &expect_non_multiedged;
1351 7 push @_, $defattr;
1352 7 goto &get_edge_attribute;
1353 }
1354  
1355 3 ▶sub has_edge_weight {
Subroutine has_edge_weight: called CC=1 SCAR=0.0
Pod documented
1356 3 &expect_non_multiedged;
1357 3 push @_, $defattr;
1358 3 goto &has_edge_attribute;
1359 }
1360  
1361 3 ▶sub set_edge_weight {
Subroutine set_edge_weight: called CC=1 SCAR=0.0
Pod documented
1362 3 &expect_non_multiedged;
1363 3 push @_, $defattr, pop;
1364 3 goto &set_edge_attribute;
1365 }
1366  
1367 1 ▶sub delete_edge_weight {
Subroutine delete_edge_weight: called CC=1 SCAR=0.0
Pod documented
1368 1 &expect_non_multiedged;
1369 1 push @_, $defattr;
1370 1 goto &delete_edge_attribute;
1371 }
1372  
1373 17 ▶sub add_weighted_edge_by_id {
Subroutine add_weighted_edge_by_id: called CC=1 SCAR=0.0
Pod documented
1374 17 &expect_multiedged;
1375 17 push @_, $defattr, pop;
1376 17 goto &set_edge_attribute_by_id;
1377 }
1378  
1379 4 ▶sub add_path_by_id {
Subroutine add_path_by_id: called CC=2 SCAR=6.9
Pod documented
1380 4 &expect_multiedged;
1381 4 my ($g, $u, $id) = (shift, shift, pop);
1382 4 my @edges;
1383 4 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 4 12
1384 8 my $v = shift;
1385 8 push @edges, [ $u, $v ];
1386 8 $u = $v;
1387 }
1388 4 $g->add_edges_by_id(@edges, $id);
1389 4 return $g;
1390 }
1391  
1392 3 ▶sub add_weighted_path_by_id {
Subroutine add_weighted_path_by_id: called CC=2 SCAR=6.9
Pod documented
1393 3 &expect_multiedged;
1394 3 my ($g, $u, $id) = (shift, shift, pop);
1395 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 3 9
1396 6 my ($w, $v) = splice @_, 0, 2;
1397 6 $g->set_edge_attribute_by_id($u, $v, $id, $defattr, $w);
1398 6 $u = $v;
1399 }
1400 }
1401  
1402 8 ▶sub get_edge_weight_by_id {
Subroutine get_edge_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1403 8 &expect_multiedged;
1404 8 push @_, $defattr;
1405 8 goto &get_edge_attribute_by_id;
1406 }
1407  
1408 3 ▶sub has_edge_weight_by_id {
Subroutine has_edge_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1409 3 &expect_multiedged;
1410 3 push @_, $defattr;
1411 3 goto &has_edge_attribute_by_id;
1412 }
1413  
1414 3 ▶sub set_edge_weight_by_id {
Subroutine set_edge_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1415 3 &expect_multiedged;
1416 3 push @_, $defattr, pop;
1417 3 goto &set_edge_attribute_by_id;
1418 }
1419  
1420 1 ▶sub delete_edge_weight_by_id {
Subroutine delete_edge_weight_by_id: called CC=1 SCAR=0.0
Pod documented
1421 1 &expect_multiedged;
1422 1 push @_, $defattr;
1423 1 goto &delete_edge_attribute_by_id;
1424 }
1425  
1426 ###
1427 # Error helpers.
1428 #
1429  
1430 84 my %expected;
1431 84 @expected{qw(directed undirected acyclic)} = qw(undirected directed cyclic);
1432  
1433 43 ▶sub _expected {
Subroutine _expected: called CC=4 SCAR=13.9
1434 43 my $exp = shift;
1435 43 ▶ my $got = @_ ? shift : $expected{$exp};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 38 43
1436 43 ▶ $got = defined $got ? ", got $got" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $got ? : 30 13 43
1437 43 ▶ if (my @caller2 = caller(2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller2) = caller 2) { } 43 0 43
1438 43 die "$caller2[3]: expected $exp graph$got, at $caller2[1] line $caller2[2].\n";
1439 } else {
1440 0 my @caller1 = caller(1); # uncoverable statement
1441 0 die "$caller1[3]: expected $exp graph$got, at $caller1[1] line $caller1[2].\n"; # uncoverable statement
1442 }
1443 }
1444  
1445 10 ▶sub expect_no_args {
Subroutine expect_no_args: called CC=2 SCAR=7.0
Pod documented
1446 10 my $g = shift;
1447 10 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 0 10
1448 0 my @caller1 = caller(1); # uncoverable statement
1449 0 die "$caller1[3]: expected no arguments, got " . scalar @_ . ", at $caller1[1] line $caller1[2]\n"; # uncoverable statement
1450 }
1451  
1452 1212 ▶sub expect_undirected {
Subroutine expect_undirected: called CC=2 SCAR=6.9
Pod documented
1453 1212 ▶ _expected('undirected') unless &is_undirected;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_undirected 14 1198 1212
1454 }
1455  
1456 219 ▶sub expect_directed {
Subroutine expect_directed: called CC=2 SCAR=6.9
Pod documented
1457 219 ▶ _expected('directed') unless &is_directed;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_directed 10 209 219
1458 }
1459  
1460 3 ▶sub expect_acyclic {
Subroutine expect_acyclic: called CC=2 SCAR=6.9
Pod documented
1461 3 ▶ _expected('acyclic') unless &is_acyclic;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_acyclic 1 2 3
1462 }
1463  
1464 7 ▶sub expect_dag {
Subroutine expect_dag: called CC=4 SCAR=13.9
Pod documented
1465 7 my @got;
1466 7 ▶ push @got, 'undirected' unless &is_directed;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_directed 2 5 7
1467 7 ▶ push @got, 'cyclic' unless &is_acyclic;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_acyclic 3 4 7
1468 7 ▶ _expected('directed acyclic', "@got") if @got;
Branch: 1 branch
BranchTrueFalseTotal
if @got 5 2 7
1469 }
1470  
1471 11 ▶sub expect_hyperedged {
Subroutine expect_hyperedged: called CC=2 SCAR=6.9
Pod documented
1472 11 ▶ _expected('hyperedged') unless &is_hyperedged;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_hyperedged 2 9 11
1473 }
1474  
1475 502 ▶sub expect_multivertexed {
Subroutine expect_multivertexed: called CC=2 SCAR=6.9
Pod documented
1476 502 ▶ _expected('multivertexed') unless &is_multivertexed;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multivertexed 4 498 502
1477 }
1478 84 *expect_multivertex = \&expect_multivertexed;
1479  
1480 1566 ▶sub expect_non_multivertexed {
Subroutine expect_non_multivertexed: called CC=2 SCAR=6.9
Pod documented
1481 1566 ▶ _expected('non-multivertexed') if &is_multivertexed;
Branch: 1 branch
BranchTrueFalseTotal
if &is_multivertexed 1 1565 1566
1482 }
1483 84 *expect_non_multivertex = \&expect_non_multivertexed;
1484  
1485 19091 ▶sub expect_non_multiedged {
Subroutine expect_non_multiedged: called CC=2 SCAR=6.9
Pod documented
1486 19091 ▶ _expected('non-multiedged') if &is_multiedged;
Branch: 1 branch
BranchTrueFalseTotal
if &is_multiedged 1 19090 19091
1487 }
1488 84 *expect_non_multiedge = \&expect_non_multiedged;
1489  
1490 1005 ▶sub expect_multiedged {
Subroutine expect_multiedged: called CC=2 SCAR=6.9
Pod documented
1491 1005 ▶ _expected('multiedged') unless &is_multiedged;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multiedged 4 1001 1005
1492 }
1493 84 *expect_multiedge = \&expect_multiedged;
1494  
1495 680 ▶sub expect_non_unionfind {
Subroutine expect_non_unionfind: called CC=2 SCAR=6.9
Pod documented
1496 680 ▶ _expected('non-unionfind') if &has_union_find;
Branch: 1 branch
BranchTrueFalseTotal
if &has_union_find 1 679 680
1497 }
1498  
1499 2472 ▶sub _get_options {
Subroutine _get_options: called CC=4 SCAR=13.9
1500 2472 my @caller = caller(1);
1501 2472 ▶ unless (@_ == 1 && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 1 and ref $_[0] eq "ARRAY") 3 2469 2472
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
212469
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
1502 3 die "$caller[3]: internal error: should be called with only one array ref argument, at $caller[1] line $caller[2].\n";
1503 }
1504 2469 my @opt = @{ $_[0] };
1505 2469 ▶ unless (@opt % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@opt % 2 == 0) 0 2469 2469
1506 0 die "$caller[3]: expected an options hash, got a non-even number of arguments, at $caller[1] line $caller[2].\n"; # uncoverable statement
1507 }
1508 2469 return @opt;
1509 }
1510  
1511 ###
1512 # Random constructors and accessors.
1513 #
1514  
1515 1 ▶sub __fisher_yates_shuffle (@) {
Subroutine __fisher_yates_shuffle: called CC=2 SCAR=6.9
1516 # From perlfaq4, but modified to be non-modifying.
1517 1 my @a = @_;
1518 1 my $i = @a;
1519 1 ▶ while ($i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 3 1 4
1520 3 my $j = int rand ($i+1);
1521 3 @a[$i,$j] = @a[$j,$i];
1522 }
1523 1 return @a;
1524 }
1525  
1526 84 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.5
1527 sub _shuffle(@);
1528 # Workaround for the Perl bug [perl #32383] where -d:Dprof and
1529 # List::Util::shuffle do not like each other: if any debugging
1530 # (-d) flags are on, fall back to our own Fisher-Yates shuffle.
1531 # The bug was fixed by perl changes #26054 and #26062, which
1532 # went to Perl 5.9.3. If someone tests this with a pre-5.9.3
1533 # bleadperl that calls itself 5.9.3 but doesn't yet have the
1534 # patches, oh, well.
1535 *_shuffle = $^P && $] < 5.009003 ?
1536 84 ▶ \&__fisher_yates_shuffle : do { require List::Util; \&List::Util::shuffle };
Branch: 1 branch
BranchTrueFalseTotal
$^P && $] < 5.009003 ? : 0 84 84
Condition33%1/3
$^P and $] < "5.009003"
!ll&&!rl&&r
0840
MC/DC0%0/2
$^P and $] < "5.009003"
$^P $] < "5.009003"
Truth table33%1/3
A and B
A= $^PB= $] < "5.009003"
ABresult
0X0
100
111
1537 }
1538  
1539 14 ▶sub random_graph {
Subroutine random_graph: called CC=25 SCAR=32.8
Pod documented
1540 14 ▶ my $class = (@_ % 2) == 0 ? 'Graph' : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 3 11 14
1541 14 my %opt = _get_options( \@_ );
1542 __carp_confess "Graph::random_graph: argument 'vertices' missing or undef"
1543 14 ▶ unless defined $opt{vertices};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"vertices"} 2 12 14
1544 __carp_confess "Graph::random_graph: both arguments 'edges' and 'edges_fill' specified"
1545 12 ▶ if exists $opt{edges} && exists $opt{edges_fill};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"edges"} and exists $opt{"edges_fill"} 0 12 12
Condition66%2/3
exists $opt{"edges"} and exists $opt{"edges_fill"}
!ll&&!rl&&r
1020
MC/DC0%0/2
exists $opt{"edges"} and exists $opt{"edges_fill"}
exists $opt{"edges"} exists $opt{"edges_fill"}
Truth table66%2/3
A and B
A= exists $opt{"edges"}B= exists $opt{"edges_fill"}
ABresult
0X0
100
111
1546 12 ▶ srand delete $opt{random_seed} if exists $opt{random_seed};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"random_seed"} 4 8 12
1547 12 my $random_edge = delete $opt{random_edge};
1548 12 my @V;
1549 12 ▶ if (my $ref = ref $opt{vertices}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $opt{'vertices'}) { } 1 11 12
1550 1 ▶ __carp_confess "Graph::random_graph: argument 'vertices' illegal"
Branch: 1 branch
BranchTrueFalseTotal
if $ref ne "ARRAY" 0 1 1
1551 if $ref ne 'ARRAY';
1552 1 @V = @{ $opt{vertices} };
1553 } else {
1554 11 @V = 0..($opt{vertices} - 1);
1555 }
1556 12 delete $opt{vertices};
1557 12 my $V = @V;
1558 12 my $C = $V * ($V - 1) / 2;
1559 12 ▶ my $E = exists $opt{edges_fill} ? $opt{edges_fill} * $C : $opt{edges};
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'edges_fill'} ? : 1 11 12
1560 12 delete @opt{qw(edges edges_fill)};
1561 12 my $g = $class->new(%opt);
1562 12 $g->add_vertices(@V);
1563 12 ▶ return $g if $V < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $V < 2 0 12 12
1564 12 ▶ $C *= 2 if $g->directed;
Branch: 1 branch
BranchTrueFalseTotal
if $g->directed 11 1 12
1565 12 ▶ $E = $C / 2 unless defined $E;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $E 9 3 12
1566 12 $E = int($E + 0.5);
1567 12 my $p = $E / $C;
1568 12 ▶ $random_edge = sub { $p } unless defined $random_edge;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $random_edge 11 1 12
1569 12 ▶ __carp_confess "Graph::random_graph: needs to be countedged or multiedged ($E > $C)"
Branch: 1 branch
BranchTrueFalseTotal
if $p > 1 and not $g->countedged || $g->multiedged 0 12 12
Condition16%1/6
$p > 1 and not $g->countedged || $g->multiedged
!ll&&!rl&&r
1200
$g->countedged || $g->multiedged
l!l&&r!l&&!r
000
MC/DC0%0/3
$p > 1 and not $g->countedged || $g->multiedged
$p > 1 $g->countedged $g->multiedged
Truth table25%1/4
A and not(B || C)
A= $p > 1B= $g->countedgedC= $g->multiedged
ABCresult
0XX0
11X0
1010
1001
1570 if $p > 1.0 && !($g->countedged || $g->multiedged);
1571 # Shuffle the vertex lists so that the pairs at
1572 # the beginning of the lists are not more likely.
1573 12 my @V1 = _shuffle @V;
1574 12 my @V2 = _shuffle @V;
1575 LOOP:
1576 12 ▶ while ($E) {
Branch: 1 branch
BranchTrueFalseTotal
if ($E) 21 0 21
1577 21 for my $v1 (@V1) {
1578 325 for my $v2 (@V2) {
1579 14193 ▶ next if $v1 eq $v2; # TODO: allow self-loops?
Branch: 1 branch
BranchTrueFalseTotal
if $v1 eq $v2 320 13873 14193
1580 13873 my $q = $random_edge->($g, $v1, $v2, $p);
1581 13873 ▶ if ($q && ($q == 1 || rand() <= $q) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($q and $q == 1 || rand() <= $q and not $g->has_edge($v1, $v2)) 6027 7846 13873
Condition88%8/9
$q and $q == 1 || rand() <= $q and not $g->has_edge($v1, $v2)
!ll&&!rl&&r
7804426027
$q and $q == 1 || rand() <= $q
!ll&&!rl&&r
28075246069
$q == 1 or rand() <= $q
l!l&&r!l&&!r
060697524
MC/DC75%3/4
$q and $q == 1 || rand() <= $q and not $g->has_edge($v1, $v2)
$q $q == 1 rand() <= $q not $g->has_edge($v1, $v2)
Truth table66%4/6
A and B or C and not(D)
A= $qB= $q == 1C= rand() <= $qD= not $g->has_edge($v1, $v2)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
1582 !$g->has_edge($v1, $v2)) {
1583 6027 $g->add_edge($v1, $v2);
1584 6027 $E--;
1585 6027 ▶ last LOOP unless $E;
Branch: 1 branch
BranchTrueFalseTotal
unless $E 12 6015 6027
1586 }
1587 }
1588 }
1589 }
1590 12 $g;
1591 }
1592  
1593 114 ▶sub random_vertex {
Subroutine random_vertex: called CC=1 SCAR=0.0
Pod documented
1594 114 my @V = &_vertices05;
1595 114 @V[rand @V];
1596 }
1597  
1598 31 ▶sub random_edge {
Subroutine random_edge: called CC=1 SCAR=0.0
Pod documented
1599 31 my @E = &_edges05;
1600 31 @E[rand @E];
1601 }
1602  
1603 42 ▶sub random_successor {
Subroutine random_successor: called CC=1 SCAR=0.0
Pod documented
1604 42 my @S = &successors;
1605 42 @S[rand @S];
1606 }
1607  
1608 43 ▶sub random_predecessor {
Subroutine random_predecessor: called CC=1 SCAR=0.0
Pod documented
1609 43 my @P = &predecessors;
1610 43 @P[rand @P];
1611 }
1612  
1613 ###
1614 # Algorithms.
1615 #
1616  
1617 1679 ▶my $MST_comparator = sub { ($_[0] || 0) <=> ($_[1] || 0) };
Subroutine __ANON__: called CC=3 SCAR=12.0
Condition50%2/4
$_[0] || 0
l!l
16790
$_[1] || 0
l!l
16790
MC/DC0%0/1
$_[0] || 0
$_[0]
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
1618  
1619 23 ▶sub _MST_attr {
Subroutine _MST_attr: called CC=3 SCAR=11.4
1620 23 my $attr = shift;
1621 my $attribute =
1622 exists $attr->{attribute} ?
1623 23 ▶ $attr->{attribute} : $defattr;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'attribute'} ? : 0 23 23
1624 my $comparator =
1625 exists $attr->{comparator} ?
1626 23 ▶ $attr->{comparator} : $MST_comparator;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'comparator'} ? : 0 23 23
1627 23 return ($attribute, $comparator);
1628 }
1629  
1630 23 ▶sub _MST_edges {
Subroutine _MST_edges: called CC=1 SCAR=0.0
1631 23 my ($g, $attr) = @_;
1632 23 my ($attribute, $comparator) = _MST_attr($attr);
1633 map $_->[1],
1634 23 sort { $comparator->($a->[0], $b->[0], $a->[1], $b->[1]) }
1635 map [ $g->get_edge_attribute(@$_, $attribute), $_ ],
1636 &_edges05;
1637 }
1638  
1639 24 ▶sub MST_Kruskal {
Subroutine MST_Kruskal: called CC=3 SCAR=11.0
Pod documented
1640 24 &expect_undirected;
1641 23 my ($g, %attr) = @_;
1642 23 require Graph::UnionFind;
1643  
1644 23 my $MST = Graph->new(directed => 0);
1645  
1646 23 my $UF = Graph::UnionFind->new;
1647 23 $UF->add(&_vertices05);
1648  
1649 23 my @edges;
1650 23 for my $e ($g->_MST_edges(\%attr)) {
1651 1613 my ($u, $v) = @$e; # TODO: hyperedges
1652 1613 ▶ next if $UF->same( @$e );
Branch: 1 branch
BranchTrueFalseTotal
if $UF->same(@$e) 1159 454 1613
1653 454 $UF->union([$u, $v]);
1654 454 push @edges, [ $u, $v ];
1655 }
1656 23 $MST->add_edges(@edges);
1657  
1658 23 return $MST;
1659 }
1660  
1661 926 ▶sub _MST_add {
Subroutine _MST_add: called CC=2 SCAR=6.9
1662 926 my ($g, $h, $HF, $r, $attr, $unseen) = @_;
1663 $HF->add( Graph::MSTHeapElem->new( $r, $_, $g->get_edge_attribute( $r, $_, $attr ) ) )
1664 926 for grep exists $unseen->{ $_ }, $g->successors( $r );
1665 }
1666  
1667 244 ▶sub _next_alphabetic { shift; (sort keys %{ $_[0] })[0] }
Subroutine _next_alphabetic: called CC=1 SCAR=0.0
1668 5 ▶sub _next_numeric { shift; (sort { $a <=> $b } keys %{ $_[0] })[0] }
Subroutine _next_numeric: called CC=1 SCAR=0.0
1669 1099 ▶sub _next_random { shift; (values %{ $_[0] })[ rand keys %{ $_[0] } ] }
Subroutine _next_random: called CC=1 SCAR=0.0
1670  
1671 164 ▶sub _root_opt {
Subroutine _root_opt: called CC=9 SCAR=22.0
1672 164 my ($g, @args) = @_;
1673 164 ▶ my %opt = @args == 1 ? ( first_root => $args[0] ) : _get_options( \@args );
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 3 161 164
1674 164 my %unseen;
1675 164 my @unseen = $g->_vertices05;
1676 164 @unseen{ @unseen } = @unseen;
1677 164 @unseen = _shuffle @unseen;
1678 164 my $r;
1679 164 ▶ if (exists $opt{ start }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"start"}) 1 163 164
1680 1 $opt{ first_root } = delete $opt{ start };
1681 1 $opt{ next_root } = undef;
1682 }
1683 164 ▶ if (exists $opt{ first_root }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'first_root'}) { } 121 43 164
1684 121 ▶ if (ref $opt{ first_root } eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'first_root'} eq 'CODE') { } 1 120 121
1685 1 $r = $opt{ first_root }->( $g, \%unseen );
1686 } else {
1687 120 $r = $opt{ first_root };
1688 }
1689 } else {
1690 43 $r = shift @unseen;
1691 }
1692 my $next =
1693 exists $opt{ next_root } ?
1694 $opt{ next_root } :
1695 $opt{ next_alphabetic } ?
1696 \&_next_alphabetic :
1697 $opt{ next_numeric } ?
1698 164 ▶ \&_next_numeric :
Branch: 3 branches
BranchTrueFalseTotal
exists $opt{'next_root'} ? : 1 163 164
$opt{'next_alphabetic'} ? : 0 163 163
$opt{'next_numeric'} ? : 0 163 163
1699 \&_next_random;
1700 164 my $code = ref $next eq 'CODE';
1701 164 ▶ my $attr = exists $opt{ attribute } ? $opt{ attribute } : $defattr;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'attribute'} ? : 0 164 164
1702 164 return ( \%opt, \%unseen, \@unseen, $r, $next, $code, $attr );
1703 }
1704  
1705 86 ▶sub _heap_walk {
Subroutine _heap_walk: called CC=8 SCAR=20.9
1706 86 my ($g, $h, $add, $etc, $opt, $unseenh, $unseena, $r, $next, $code, $attr) = @_;
1707 86 require Heap::Fibonacci;
1708 86 my $HF = Heap::Fibonacci->new;
1709 86 ▶ while (defined $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 108 0 108
1710 # print "r = $r\n";
1711 108 $add->($g, $h, $HF, $r, $attr, $unseenh, $etc);
1712 108 delete $unseenh->{ $r };
1713 108 ▶ while (defined $HF->top) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $HF->top) 4127 107 4234
1714 4127 my $t = $HF->extract_top;
1715 # use Data::Dumper; print "t = ", Dumper($t);
1716 4127 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 4127 0 4127
1717 4127 my ($u, $v, $w) = $t->val;
1718 # print "extracted top: $u $v $w\n";
1719 4127 ▶ if (exists $unseenh->{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $unseenh->{$v}) 1909 2218 4127
1720 1909 $h->set_edge_attribute($u, $v, $attr, $w);
1721 1909 delete $unseenh->{ $v };
1722 1909 $add->($g, $h, $HF, $v, $attr, $unseenh, $etc);
1723 }
1724 }
1725 }
1726 107 ▶ return $h unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 1 106 107
1727 106 ▶ $r = $code ? $next->( $g, $unseenh ) : shift @$unseena;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 106 0 106
1728 106 ▶ last unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 84 22 106
1729 }
1730 84 return $h;
1731 }
1732  
1733 43 ▶sub MST_Prim {
Subroutine MST_Prim: called CC=1 SCAR=0.0
Pod documented
1734 43 &expect_undirected;
1735 42 require Graph::MSTHeapElem;
1736 42 $_[0]->_heap_walk(Graph->new(directed => 0), \&_MST_add, undef, &_root_opt);
1737 }
1738  
1739 84 *MST_Dijkstra = \&MST_Prim;
1740  
1741 84 *minimum_spanning_tree = \&MST_Prim;
1742  
1743 ###
1744 # Cycle detection.
1745 #
1746  
1747 84 *is_cyclic = \&has_a_cycle;
1748  
1749 13 ▶sub is_acyclic {
Subroutine is_acyclic: called CC=1 SCAR=0.0
Pod documented
1750 13 !&is_cyclic;
1751 }
1752  
1753 5 ▶sub is_dag {
Subroutine is_dag: called CC=3 SCAR=11.0
Pod documented
1754 5 ▶ &is_directed && &is_acyclic ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&is_directed && &is_acyclic ? : 2 3 5
Condition100%3/3
&is_directed and &is_acyclic
!ll&&!rl&&r
212
MC/DC100%2/2
&is_directed and &is_acyclic
&is_directed &is_acyclic
Truth table100%3/3
A and B
A= &is_directedB= &is_acyclic
ABresult
0X0
100
111
1755 }
1756  
1757 84 *is_directed_acyclic_graph = \&is_dag;
1758  
1759 ###
1760 # Simple DFS uses.
1761 #
1762  
1763 5 ▶sub topological_sort {
Subroutine topological_sort: called CC=3 SCAR=11.0
Pod documented
1764 5 my $g = shift;
1765 5 my %opt = _get_options( \@_ );
1766 5 my $eic = delete $opt{ empty_if_cyclic };
1767 5 my $hac;
1768 5 ▶ if ($eic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eic) { } 1 4 5
1769 1 $hac = $g->has_a_cycle;
1770 } else {
1771 4 $g->expect_dag;
1772 }
1773 2 require Graph::Traversal::DFS;
1774 2 my $t = Graph::Traversal::DFS->new($g, %opt);
1775 2 my @s = $t->dfs;
1776 2 ▶ $hac ? () : reverse @s;
Branch: 1 branch
BranchTrueFalseTotal
$hac ? : 1 1 2
1777 }
1778  
1779 84 *toposort = \&topological_sort;
1780  
1781 83 ▶sub _copy_vertices {
Subroutine _copy_vertices: called CC=8 SCAR=20.8
1782 83 my ($g, $gc, $attr_too) = @_;
1783 83 ▶ if (&is_multivertexed) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multivertexed) { } 10 73 83
1784 10 for my $v (&_vertices05) {
1785 33 ▶ if ($attr_too) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_too) { } 13 20 33
1786 $gc->set_vertex_attributes_by_id($v, $_, $g->get_vertex_attributes_by_id($v, $_))
1787 13 for $g->get_multivertex_ids($v);
1788 } else {
1789 20 $gc->add_vertex_by_id($v, $_) for $g->get_multivertex_ids($v);
1790 }
1791 }
1792 } else {
1793 73 ▶ if ($attr_too) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_too) { } 2 71 73
1794 2 $gc->set_vertex_attributes($_, $g->get_vertex_attributes($_)) for &_vertices05;
1795 } else {
1796 71 $gc->add_vertices(&_vertices05);
1797 }
1798 }
1799 }
1800  
1801 83 ▶sub _copy_edges {
Subroutine _copy_edges: called CC=12 SCAR=24.9
1802 83 my ($g, $gc, $attr_too, $mirror) = @_;
1803 83 my @edges = &_edges05;
1804 83 ▶ if (&is_multiedged) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_multiedged) { } 10 73 83
1805 10 for my $e (@edges) {
1806 28 for my $id ($g->get_multiedge_ids(@$e)) {
1807 28 ▶ if ($attr_too) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_too) { } 12 16 28
1808 12 $gc->set_edge_attributes_by_id(@$e, $id, $g->get_edge_attributes_by_id(@$e, $id));
1809 12 ▶ $gc->set_edge_attributes_by_id(reverse(@$e), $id, $g->get_edge_attributes_by_id(@$e, $id)) if $mirror;
Branch: 1 branch
BranchTrueFalseTotal
if $mirror 4 8 12
1810 } else {
1811 16 $gc->add_edge_by_id(@$e, $id);
1812 16 ▶ $gc->add_edge_by_id(reverse(@$e), $id) if $mirror;
Branch: 1 branch
BranchTrueFalseTotal
if $mirror 4 12 16
1813 }
1814 }
1815 }
1816 } else {
1817 73 ▶ if ($attr_too) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_too) { } 2 71 73
1818 $gc->set_edge_attributes(@$_, $g->get_edge_attributes(@$_))
1819 2 for @edges;
1820 2 ▶ if ($mirror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mirror) 0 2 2
1821 $gc->set_edge_attributes(reverse(@$_), $g->get_edge_attributes(@$_))
1822 0 for @edges;
1823 }
1824 } else {
1825 71 ▶ $gc->add_edges(@edges, !$mirror ? () : map [reverse @$_], @edges);
Branch: 1 branch
BranchTrueFalseTotal
!$mirror ? : 66 5 71
1826 }
1827 }
1828 }
1829  
1830 20 ▶sub undirected_copy {
Subroutine undirected_copy: called CC=1 SCAR=0.0
Pod documented
1831 20 &expect_directed;
1832 20 my $gc = $_[0]->new(undirected=>1);
1833 20 _copy_vertices($_[0], $gc);
1834 20 _copy_edges($_[0], $gc);
1835 20 $gc;
1836 }
1837  
1838 84 *undirected_copy_graph = \&undirected_copy;
1839  
1840 1 ▶sub undirected_copy_attributes {
Subroutine undirected_copy_attributes: called CC=1 SCAR=0.0
Pod documented
1841 1 &expect_directed;
1842 1 my $gc = $_[0]->new(undirected=>1);
1843 1 $gc->set_graph_attributes($_[0]->get_graph_attributes);
1844 1 _copy_vertices($_[0], $gc, 1);
1845 1 _copy_edges($_[0], $gc, 1);
1846 1 $gc;
1847 }
1848  
1849 6 ▶sub directed_copy {
Subroutine directed_copy: called CC=1 SCAR=0.0
Pod documented
1850 6 &expect_undirected;
1851 6 my $gc = $_[0]->new(undirected=>0);
1852 6 _copy_vertices($_[0], $gc);
1853 6 _copy_edges($_[0], $gc, 0, 1);
1854 6 $gc;
1855 }
1856  
1857 84 *directed_copy_graph = \&directed_copy;
1858  
1859 1 ▶sub directed_copy_attributes {
Subroutine directed_copy_attributes: called CC=1 SCAR=0.0
Pod documented
1860 1 &expect_undirected;
1861 1 my $gc = $_[0]->new(directed=>1);
1862 1 $gc->set_graph_attributes($_[0]->get_graph_attributes);
1863 1 _copy_vertices($_[0], $gc, 1);
1864 1 _copy_edges($_[0], $gc, 1, 1);
1865 1 $gc;
1866 }
1867  
1868 14 ▶sub is_bipartite {
Subroutine is_bipartite: called CC=1 SCAR=0.0
Pod documented
1869 14 &expect_undirected;
1870 14 my ($g) = @_;
1871 14 my $is_bipartite = 1;
1872 14 my %colors;
1873 my $operations = {
1874 416 ▶ tree_edge => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1875 416 my( $seen, $unseen ) = @_;
1876 416 ( $seen, $unseen ) = sort { exists $colors{$b} <=> exists $colors{$a} } ( $seen, $unseen );
1877 416 ▶ $colors{$seen} ||= -1;
Condition100%2/2
$colors{$seen} ||= -1
l!l
40511
MC/DC100%1/1
$colors{$seen} ||= -1
$colors{$seen}
Truth table100%2/2
A
A= $colors{$seen}
Aresult
11
00
1878 416 $colors{$unseen} = -$colors{$seen};
1879 },
1880 26778 ▶ non_tree_edge => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1881 26778 ▶ $is_bipartite = '' if $colors{$_[0]} == $colors{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $colors{$_[0]} == $colors{$_[1]} 13106 13672 26778
1882 },
1883 14 };
1884 14 require Graph::Traversal::DFS;
1885 14 Graph::Traversal::DFS->new( $g, %$operations )->dfs;
1886 14 return $is_bipartite;
1887 }
1888  
1889 8 ▶sub is_planar {
Subroutine is_planar: called CC=1 SCAR=0.0
Pod documented
1890 8 &expect_undirected;
1891 8 my ($g) = @_;
1892 8 my @paths_at = map [], 1..$g->vertices;
1893 8 my $path_graph = Graph->new(undirected => 1);
1894 8 my ($n, $d, %order) = (0, 0);
1895 my $operations = {
1896 98 ▶ pre => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1897 98 $order{$_[0]} = $n;
1898 98 $n++;
1899 },
1900 432 ▶ non_tree_edge => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1901 432 my( $i, $j ) = sort map { $order{$_} } @_[0..1];
1902 432 for (@{$paths_at[$i]}) { # for all crossed paths
1903 13520 $path_graph->add_edge( $_, $d );
1904 }
1905 432 for ($i+1..$j-1) {
1906 2756 push @{$paths_at[$_]}, $d;
1907 }
1908 432 $d++;
1909 },
1910 8 };
1911 8 require Graph::Traversal::DFS;
1912 8 Graph::Traversal::DFS->new( $g, %$operations )->dfs;
1913 8 return $path_graph->is_bipartite;
1914 }
1915  
1916 ###
1917 # Cache or not.
1918 #
1919  
1920 84 my %_cache_type =
1921 (
1922 'connectivity' => ['_ccc'],
1923 'strong_connectivity' => ['_scc'],
1924 'weak_connectivity_undirected_graph' => ['_wcug'],
1925 'biconnectivity' => ['_bcc'],
1926 'SPT_Dijkstra' => ['_spt_di', 'SPT_Dijkstra_root'],
1927 'SPT_Bellman_Ford' => ['_spt_bf', 'SPT_Bellman_Ford_root'],
1928 'transitive_closure_matrix' => ['_tcm'],
1929 );
1930  
1931 84 for my $t (keys %_cache_type) {
1932 84 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1933 588 my @attr = @{ $_cache_type{$t} };
1934 228 ▶ *{$t."_clear_cache"} = sub { $_[0]->delete_graph_attribute($_) for @attr };
Subroutine __ANON__: called CC=2 SCAR=6.9
1935 }
1936  
1937 2285 ▶sub _check_cache {
Subroutine _check_cache: called CC=9 SCAR=22.2
1938 2285 my ($g, $type, $extra_vals, $code, @args) = @_;
1939 2285 my $c = $_cache_type{$type};
1940 2285 ▶ __carp_confess "Graph: unknown cache type '$type'" if !defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 2285 2285
1941 2285 my ($main_key, @extra_keys) = @$c;
1942 2285 ▶ __carp_confess "Graph: wrong number of extra values (@extra_keys) vs (@$extra_vals)" if @extra_keys != @$extra_vals;
Branch: 1 branch
BranchTrueFalseTotal
if @extra_keys != @$extra_vals 0 2285 2285
1943 2285 my $a = $g->get_graph_attribute($main_key);
1944 2285 ▶ __carp_confess "$c attribute set to unexpected value $a"
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and ref $a ne "ARRAY" 0 2285 2285
Condition66%2/3
defined $a and ref $a ne "ARRAY"
!ll&&!rl&&r
41318720
MC/DC0%0/2
defined $a and ref $a ne "ARRAY"
defined $a ref $a ne "ARRAY"
Truth table66%2/3
A and B
A= defined $aB= ref $a ne "ARRAY"
ABresult
0X0
100
111
1945 if defined $a and ref $a ne 'ARRAY';
1946 2285 ▶ unless (defined $a && $a->[ 0 ] == $g->[ _G ]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a and $a->[0] == $g->[1]) 459 1826 2285
Condition100%3/3
defined $a and $a->[0] == $g->[1]
!ll&&!rl&&r
413461826
MC/DC100%2/2
defined $a and $a->[0] == $g->[1]
defined $a $a->[0] == $g->[1]
Truth table100%3/3
A and B
A= defined $aB= $a->[0] == $g->[1]
ABresult
0X0
100
111
1947 459 $g->set_graph_attribute($main_key, $a = [ $g->[ _G ], $code->( $g, @args ) ]);
1948 }
1949 2283 my $i = -1;
1950 my $extra_invalid = grep {
1951 2283 my $v = $a->[ 1 ]->get_graph_attribute($_);
1952 120 $i++; # here so still incremented even if short-cut
1953 120 ▶ !defined $v or $v ne $extra_vals->[$i];
Condition66%2/3
!defined($v) || $v ne $extra_vals->[$i]
l!l&&r!l&&!r
03189
MC/DC50%1/2
!defined($v) || $v ne $extra_vals->[$i]
!defined($v) $v ne $extra_vals->[$i]
Truth table66%2/3
not(A) || B
A= !defined($v)B= $v ne $extra_vals->[$i]
ABresult
1X1
011
000
1954 } @extra_keys;
1955 2283 ▶ if ($extra_invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_invalid) 31 2252 2283
1956 31 $g->set_graph_attribute($main_key, $a = [ $g->[ _G ], $code->( $g, @args ) ]);
1957 }
1958 2283 return $a->[ 1 ];
1959 }
1960  
1961 ###
1962 # Connected components.
1963 #
1964  
1965 45 ▶sub _connected_components_compute {
Subroutine _connected_components_compute: called CC=6 SCAR=17.9
1966 45 my $g = $_[0];
1967 45 my %v2c;
1968 my @c;
1969 45 ▶ return [ [], {} ] unless my @v = $g->unique_vertices;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@v) = $g->unique_vertices 6 39 45
1970 39 ▶ if (my $UF = &has_union_find) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $UF = &has_union_find) { } 9 30 39
1971 9 my $V = $g->[ _V ];
1972 9 my @ids = $V->get_ids_by_paths(\@v, 0);
1973 9 my ($counter, %cc2counter) = 0;
1974 9 my @cc = $UF->find(@ids);
1975 9 ▶ for (my $i = 0; $i <= $#v; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#v) 20 9 29
1976 20 my $cc = $cc[$i];
1977 20 ▶ __carp_confess "connected_component union-find did not have vertex '$v[$i]', please report"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cc 0 20 20
1978 if !defined $cc;
1979 20 ▶ $cc2counter{$cc} = $counter++ if !exists $cc2counter{$cc};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cc2counter{$cc} 13 7 20
1980 20 my $ci = $cc2counter{$cc};
1981 20 $v2c{ $v[$i] } = $ci;
1982 20 push @{ $c[$ci] }, $v[$i];
1983 }
1984 } else {
1985 30 require Graph::Traversal::DFS;
1986 30 my %r; @r{ @v } = @v;
1987 30 @c = [];
1988 my $t = Graph::Traversal::DFS->new(
1989 $g,
1990 30 ▶ first_root => sub { (each %r)[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
1991 40 ▶ next_root => sub { push @c, [] if keys %r; (each %r)[1]; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if keys %r 10 30 40
1992 106 ▶ pre => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1993 106 my ($v, $t) = @_;
1994 106 $v2c{ $v } = $#c;
1995 106 push @{ $c[-1] }, $v;
1996 106 delete $r{ $v };
1997 },
1998 30 @_[1..$#_]
1999 );
2000 30 $t->dfs;
2001 }
2002 39 return [ \@c, \%v2c ];
2003 }
2004  
2005 384 ▶sub _connected_components {
Subroutine _connected_components: called CC=1 SCAR=0.0
2006 384 my $ccc = _check_cache($_[0], 'connectivity', [],
2007 \&_connected_components_compute);
2008 384 return @{ $ccc };
2009 }
2010  
2011 82 ▶sub connected_component_by_vertex {
Subroutine connected_component_by_vertex: called CC=1 SCAR=0.0
Pod documented
2012 82 &expect_undirected;
2013 81 (&_connected_components)[1]->{ $_[1] };
2014 }
2015  
2016 58 ▶sub connected_component_by_index {
Subroutine connected_component_by_index: called CC=3 SCAR=11.3
Pod documented
2017 58 &expect_undirected;
2018 57 my $value = (&_connected_components)[0]->[$_[1]];
2019 57 ▶ $value ? @{ $value || _empty_array } : ();
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 41 16 57
Condition33%1/3
$value || &_empty_array()
l!l&&r!l&&!r
4100
MC/DC0%0/2
$value || &_empty_array()
$value &_empty_array()
Truth table33%1/3
A || B
A= $valueB= &_empty_array()
ABresult
1X1
011
000
2020 }
2021  
2022 41 ▶sub connected_components {
Subroutine connected_components: called CC=1 SCAR=0.0
Pod documented
2023 41 &expect_undirected;
2024 40 @{ (&_connected_components)[0] };
2025 }
2026  
2027 29 ▶sub same_connected_components {
Subroutine same_connected_components: called CC=4 SCAR=13.9
Pod documented
2028 29 &expect_undirected;
2029 28 my ($g, @args) = @_;
2030 28 my @components;
2031 28 ▶ if (my $UF = &has_union_find) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $UF = &has_union_find) { } 14 14 28
2032 14 my @ids = &_vertex_ids;
2033 14 ▶ return 0 if @ids != @args;
Branch: 1 branch
BranchTrueFalseTotal
if @ids != @args 4 10 14
2034 10 @components = $UF->find(@ids);
2035 } else {
2036 14 @components = @{ (&_connected_components)[1] }{ @args };
2037 }
2038 24 ▶ return 0 if grep !defined, @components;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), @components) 4 20 24
2039 20 require List::Util;
2040 20 List::Util::uniq( @components ) == 1;
2041 }
2042  
2043 40 ▶sub _super_component { join("+", sort @_) }
Subroutine _super_component: called CC=1 SCAR=0.0
2044  
2045 21 ▶sub connected_graph {
Subroutine connected_graph: called CC=5 SCAR=16.1
Pod documented
2046 21 &expect_undirected;
2047 20 my ($g, %opt) = @_;
2048 20 my $cg = Graph->new(undirected => 1);
2049 20 ▶ if ($g->has_union_find && $g->vertices == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->has_union_find and $g->vertices == 1) { } 2 18 20
Condition100%3/3
$g->has_union_find and $g->vertices == 1
!ll&&!rl&&r
1082
MC/DC100%2/2
$g->has_union_find and $g->vertices == 1
$g->has_union_find $g->vertices == 1
Truth table100%3/3
A and B
A= $g->has_union_findB= $g->vertices == 1
ABresult
0X0
100
111
2050 # TODO: super_component?
2051 2 $cg->add_vertices($g->vertices);
2052 } else {
2053 18 ▶ my $sc_cb = $opt{super_component} || \&_super_component;
Condition50%1/2
$opt{'super_component'} || \&Graph::_super_component
l!l
018
MC/DC0%0/1
$opt{'super_component'} || \&Graph::_super_component
$opt{'super_component'}
Truth table50%1/2
A
A= $opt{'super_component'}
Aresult
11
00
2054 $cg->set_vertex_attribute(scalar $sc_cb->(@$_), 'subvertices', $_)
2055 18 for $g->connected_components;
2056 }
2057 20 return $cg;
2058 }
2059  
2060 197 ▶sub is_connected {
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
2061 197 &expect_undirected;
2062 192 return @{ (&_connected_components)[0] } == 1;
2063 }
2064  
2065 10 ▶sub is_weakly_connected {
Subroutine is_weakly_connected: called CC=1 SCAR=0.0
Pod documented
2066 10 &expect_directed;
2067 9 splice @_, 0, 1, &undirected_copy;
2068 9 goto &is_connected;
2069 }
2070  
2071 84 *weakly_connected = \&is_weakly_connected;
2072  
2073 # because recreating undirected copy every time has different hash ordering
2074 # so weakly_connected_component_by_index etc would be unstable
2075 51 ▶sub _weakly_connected_undir_graph {
Subroutine _weakly_connected_undir_graph: called CC=1 SCAR=0.0
2076 51 _check_cache($_[0], 'weak_connectivity_undirected_graph', [],
2077 \&undirected_copy);
2078 }
2079  
2080 6 ▶sub weakly_connected_components {
Subroutine weakly_connected_components: called CC=1 SCAR=0.0
Pod documented
2081 6 &expect_directed;
2082 5 splice @_, 0, 1, &_weakly_connected_undir_graph;
2083 5 goto &connected_components;
2084 }
2085  
2086 21 ▶sub weakly_connected_component_by_vertex {
Subroutine weakly_connected_component_by_vertex: called CC=1 SCAR=0.0
Pod documented
2087 21 &expect_directed;
2088 20 splice @_, 0, 1, &_weakly_connected_undir_graph;
2089 20 goto &connected_component_by_vertex;
2090 }
2091  
2092 15 ▶sub weakly_connected_component_by_index {
Subroutine weakly_connected_component_by_index: called CC=1 SCAR=0.0
Pod documented
2093 15 &expect_directed;
2094 14 splice @_, 0, 1, &_weakly_connected_undir_graph;
2095 14 goto &connected_component_by_index;
2096 }
2097  
2098 8 ▶sub same_weakly_connected_components {
Subroutine same_weakly_connected_components: called CC=1 SCAR=0.0
Pod documented
2099 8 &expect_directed;
2100 7 splice @_, 0, 1, &_weakly_connected_undir_graph;
2101 7 goto &same_connected_components;
2102 }
2103  
2104 6 ▶sub weakly_connected_graph {
Subroutine weakly_connected_graph: called CC=1 SCAR=0.0
Pod documented
2105 6 &expect_directed;
2106 5 splice @_, 0, 1, &_weakly_connected_undir_graph;
2107 5 goto &connected_graph;
2108 }
2109  
2110 14 ▶sub _strongly_connected_components_compute {
Subroutine _strongly_connected_components_compute: called CC=1 SCAR=0.0
2111 14 my $g = $_[0];
2112 14 require Graph::Traversal::DFS;
2113 14 require List::Util;
2114 14 my $t = Graph::Traversal::DFS->new($g);
2115 14 my @d = reverse $t->dfs;
2116 14 my @c;
2117 my %v2c;
2118 my $u = Graph::Traversal::DFS->new(
2119 $g->transpose_graph,
2120 138 ▶ next_root => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2121 138 my ($t, $u) = @_;
2122 return if !defined(my $root = List::Util::first(
2123 3147 ▶ sub { exists $u->{$_} }, @d
Subroutine __ANON__: called CC=1 SCAR=0.0
2124 138 ▶ ));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $root = &List::Util::first(sub { exists $u->{$_}; } , @d)) 14 124 138
2125 124 push @c, [];
2126 124 return $root;
2127 },
2128 255 ▶ pre => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2129 255 my ($v, $t) = @_;
2130 255 push @{ $c[-1] }, $v;
2131 255 $v2c{$v} = $#c;
2132 },
2133 14 next_alphabetic => 1,
2134 );
2135 14 $u->dfs;
2136 14 return [ \@c, \%v2c ];
2137 }
2138  
2139 12 ▶sub _strongly_connected_components_v2c {
Subroutine _strongly_connected_components_v2c: called CC=1 SCAR=0.0
2140 12 &_strongly_connected_components->[1];
2141 }
2142  
2143 18 ▶sub _strongly_connected_components_arrays {
Subroutine _strongly_connected_components_arrays: called CC=1 SCAR=0.0
2144 18 @{ &_strongly_connected_components->[0] };
2145 }
2146  
2147 40 ▶sub _strongly_connected_components {
Subroutine _strongly_connected_components: called CC=1 SCAR=0.0
2148 40 _check_cache($_[0], 'strong_connectivity', [],
2149 \&_strongly_connected_components_compute);
2150 }
2151  
2152 19 ▶sub strongly_connected_components {
Subroutine strongly_connected_components: called CC=1 SCAR=0.0
Pod documented
2153 19 &expect_directed;
2154 18 goto &_strongly_connected_components_arrays;
2155 }
2156  
2157 5 ▶sub strongly_connected_component_by_vertex {
Subroutine strongly_connected_component_by_vertex: called CC=1 SCAR=0.0
Pod documented
2158 5 &expect_directed;
2159 4 &_strongly_connected_components_v2c->{$_[1]};
2160 }
2161  
2162 6 ▶sub strongly_connected_component_by_index {
Subroutine strongly_connected_component_by_index: called CC=2 SCAR=6.9
Pod documented
2163 6 &expect_directed;
2164 5 my $i = $_[1];
2165 5 ▶ return if !defined(my $c = &_strongly_connected_components->[0][ $i ]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $c = &_strongly_connected_components->[0][$i]) 1 4 5
2166 4 @$c;
2167 }
2168  
2169 8 ▶sub same_strongly_connected_components {
Subroutine same_strongly_connected_components: called CC=1 SCAR=0.0
Pod documented
2170 8 &expect_directed;
2171 8 my ($g, @args) = @_;
2172 8 require Set::Object;
2173 8 Set::Object->new(@{ &_strongly_connected_components_v2c }{@args})->size <= 1;
2174 }
2175  
2176 4 ▶sub is_strongly_connected {
Subroutine is_strongly_connected: called CC=1 SCAR=0.0
Pod documented
2177 4 &strongly_connected_components == 1;
2178 }
2179  
2180 84 *strongly_connected = \&is_strongly_connected;
2181  
2182 6 ▶sub strongly_connected_graph {
Subroutine strongly_connected_graph: called CC=2 SCAR=6.9
Pod documented
2183 6 &expect_directed;
2184 6 my ($g, %attr) = @_;
2185 6 my $sc_cb = \&_super_component;
2186 6 _opt_get(\%attr, super_component => \$sc_cb);
2187 6 _opt_unknown(\%attr);
2188 5 my ($c, $v2c) = @{ &_strongly_connected_components };
2189 5 my $s = Graph->new;
2190 5 my @s = map $sc_cb->(@$_), @$c;
2191 5 $s->set_vertex_attribute($s[$_], 'subvertices', $c->[$_]) for 0..$#$c;
2192 5 require List::Util;
2193 5 $s->add_edges(map [@s[ @$v2c{ @$_ } ]], grep List::Util::uniq( @$v2c{ @$_ } ) > 1, &_edges05);
2194 5 return $s;
2195 }
2196  
2197 ###
2198 # Biconnectivity.
2199 #
2200  
2201 14931 ▶sub _biconnectivity_out {
Subroutine _biconnectivity_out: called CC=5 SCAR=16.3
2202 14931 my ($state, $u, $v) = @_;
2203 14931 my @BC;
2204 14931 ▶ while (@{$state->{stack}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$state->{"stack"};}) 16855 0 16855
2205 16855 push @BC, my $e = pop @{$state->{stack}};
2206 16855 ▶ last if $e->[0] eq $u && $e->[1] eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $e->[0] eq $u and $e->[1] eq $v 14931 1924 16855
Condition66%2/3
$e->[0] eq $u and $e->[1] eq $v
!ll&&!rl&&r
1924014931
MC/DC50%1/2
$e->[0] eq $u and $e->[1] eq $v
$e->[0] eq $u $e->[1] eq $v
Truth table66%2/3
A and B
A= $e->[0] eq $uB= $e->[1] eq $v
ABresult
0X0
100
111
2207 }
2208 14931 ▶ push @{$state->{BC}}, \@BC if @BC;
Branch: 1 branch
BranchTrueFalseTotal
if @BC 14931 0 14931
2209 }
2210  
2211 17608 ▶sub _biconnectivity_dfs {
Subroutine _biconnectivity_dfs: called CC=7 SCAR=19.5
2212 17608 my ($E, $u, $state) = @_;
2213 17608 $state->{low}{$u} = $state->{num}{$u} = $state->{dfs}++;
2214 17608 for my $v ($E->successors($u)) {
2215 35320 ▶ if (!exists $state->{num}{$v}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $state->{'num'}{$v}) { } 15948 19372 35320
elsif (defined $state->{'pred'}{$u} and $state->{'pred'}{$u} ne $v and $state->{'num'}{$v} < $state->{'num'}{$u}) { } 907 34912 35819
2216 15948 push @{$state->{stack}}, [$u, $v];
2217 15948 $state->{pred}{$v} = $u;
2218 15948 _biconnectivity_dfs($E, $v, $state);
2219 15948 $state->{low}{$u} = List::Util::min(@{ $state->{low} }{$u, $v});
2220 _biconnectivity_out($state, $u, $v)
2221 15948 ▶ if $state->{low}{$v} >= $state->{num}{$u};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"low"}{$v} >= $state->{"num"}{$u} 14931 1017 15948
2222 ▶ } elsif (defined $state->{pred}{$u} &&
Condition100%6/6
defined $state->{"pred"}{$u} and $state->{"pred"}{$u} ne $v and $state->{"num"}{$v} < $state->{"num"}{$u}
!ll&&!rl&&r
164472018907
defined $state->{"pred"}{$u} and $state->{"pred"}{$u} ne $v
!ll&&!rl&&r
499159482925
MC/DC100%3/3
defined $state->{"pred"}{$u} and $state->{"pred"}{$u} ne $v and $state->{"num"}{$v} < $state->{"num"}{$u}
defined $state->{"pred"}{$u} $state->{"pred"}{$u} ne $v $state->{"num"}{$v} < $state->{"num"}{$u}
Truth table100%4/4
A and B and C
A= defined $state->{"pred"}{$u}B= $state->{"pred"}{$u} ne $vC= $state->{"num"}{$v} < $state->{"num"}{$u}
ABCresult
0XX0
10X0
1100
1111
2223 $state->{pred}{$u} ne $v &&
2224 $state->{num}{$v} < $state->{num}{$u}) {
2225 907 push @{$state->{stack}}, [$u, $v];
2226 907 $state->{low}{$u} = List::Util::min($state->{low}{$u}, $state->{num}{$v});
2227 }
2228 }
2229 }
2230  
2231 288 ▶sub _biconnectivity_compute {
Subroutine _biconnectivity_compute: called CC=12 SCAR=24.9
2232 288 require List::Util;
2233 288 my ($g) = @_;
2234 288 my ($V, $E) = @$g[ _V, _E ];
2235 288 my %state = (BC=>[], dfs=>0);
2236 288 my @u = $V->ids;
2237 288 for my $u (@u) {
2238 17608 ▶ next if exists $state{num}->{$u};
Branch: 1 branch
BranchTrueFalseTotal
if exists $state{"num"}{$u} 15948 1660 17608
2239 1660 _biconnectivity_dfs($E, $u, \%state);
2240 0 ▶ push @{$state{BC}}, delete $state{stack} if @{ $state{stack} || _empty_array };
Branch: 1 branch
BranchTrueFalseTotal
if @{$state{"stack"} or &_empty_array();} 0 1660 1660
Condition66%2/3
$state{'stack'} || &_empty_array()
l!l&&r!l&&!r
165910
MC/DC0%0/2
$state{'stack'} || &_empty_array()
$state{'stack'} &_empty_array()
Truth table66%2/3
A || B
A= $state{'stack'}B= &_empty_array()
ABresult
1X1
011
000
2241 }
2242  
2243 # Mark the components each vertex belongs to.
2244 288 my ($bci, %v2bc, %bc2v) = 0;
2245 288 for my $bc (@{$state{BC}}) {
2246 14931 $v2bc{$_}{$bci} = undef for map @$_, @$bc;
2247 14931 $bci++;
2248 }
2249  
2250 # Any isolated vertices get each their own component.
2251 288 $v2bc{$_}{$bci++} = undef for grep !exists $v2bc{$_}, @u;
2252  
2253 # build vector now we know how big to make it
2254 288 my ($Z, %v2bc_vec, @ap) = "\0" x (($bci + 7) / 8);
2255 288 @v2bc_vec{@u} = ($Z) x @u;
2256 288 for my $v (@u) {
2257 17608 my @components = keys %{ $v2bc{$v} };
2258 17608 vec($v2bc_vec{$v}, $_, 1) = 1 for @components;
2259 17608 $bc2v{$_}{$v}{$_} = undef for @components;
2260 # Articulation points / cut vertices are the vertices
2261 # which belong to more than one component.
2262 17608 ▶ push @ap, $v if @components > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @components > 1 3014 14594 17608
2263 }
2264  
2265 # Bridges / cut edges are the components of two vertices.
2266 288 my @br = grep @$_ == 2, map [keys %$_], values %bc2v;
2267  
2268 # Create the subgraph components.
2269 288 my @sg = map [ List::Util::uniq( map @$_, @$_ ) ], @{$state{BC}};
2270 288 my ($apdeep, $sgv, $brv) = $V->get_paths_by_ids([[\@ap], \@sg, \@br], 1);
2271 288 return [ @$apdeep, $sgv, $brv, \%v2bc, \%v2bc_vec, $Z ];
2272 }
2273  
2274 471 ▶sub biconnectivity {
Subroutine biconnectivity: called CC=2 SCAR=7.6
Pod documented
2275 471 &expect_undirected;
2276 470 ▶ @{ _check_cache($_[0], 'biconnectivity', [],
Condition33%1/3
_check_cache($_[0], 'biconnectivity', scalar [], \&Graph::_biconnectivity_compute, @_[1 .. $#_]) || &_empty_array()
l!l&&r!l&&!r
47000
MC/DC0%0/2
_check_cache($_[0], 'biconnectivity', scalar [], \&Graph::_biconnectivity_compute, @_[1 .. $#_]) || &_empty_array()
_check_cache($_[0], 'biconnectivity', scalar [], \&Graph::_biconnectivity_compute, @_[1 .. $#_]) &_empty_array()
Truth table33%1/3
A || B
A= _check_cache($_[0], 'biconnectivity', scalar [], \&Graph::_biconnectivity_compute, @_[1 .. $#_])B= &_empty_array()
ABresult
1X1
011
000
2277 \&_biconnectivity_compute, @_[1..$#_]) || _empty_array };
2278 }
2279  
2280 13 ▶sub is_biconnected {
Subroutine is_biconnected: called CC=2 SCAR=6.9
Pod documented
2281 13 ▶ &edges >= 2 ? @{ (&biconnectivity)[0] } == 0 : undef ;
Branch: 1 branch
BranchTrueFalseTotal
&edges >= 2 ? : 10 3 13
2282 }
2283  
2284 13 ▶sub is_edge_connected {
Subroutine is_edge_connected: called CC=2 SCAR=6.9
Pod documented
2285 13 ▶ &edges >= 2 ? @{ (&biconnectivity)[2] } == 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
&edges >= 2 ? : 10 3 13
2286 }
2287  
2288 13 ▶sub is_edge_separable {
Subroutine is_edge_separable: called CC=2 SCAR=6.9
Pod documented
2289 13 ▶ &edges >= 2 ? @{ (&biconnectivity)[2] } > 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
&edges >= 2 ? : 10 3 13
2290 }
2291  
2292 248 ▶sub articulation_points {
Subroutine articulation_points: called CC=1 SCAR=0.0
Pod documented
2293 248 @{ (&biconnectivity)[0] };
2294 }
2295  
2296 84 *cut_vertices = \&articulation_points;
2297  
2298 14 ▶sub biconnected_components {
Subroutine biconnected_components: called CC=1 SCAR=0.0
Pod documented
2299 14 @{ (&biconnectivity)[1] };
2300 }
2301  
2302 16 ▶sub biconnected_component_by_index {
Subroutine biconnected_component_by_index: called CC=1 SCAR=0.0
Pod documented
2303 16 my ($i) = splice @_, 1, 1;
2304 16 (&biconnectivity)[1]->[ $i ];
2305 }
2306  
2307 2 ▶sub biconnected_component_by_vertex {
Subroutine biconnected_component_by_vertex: called CC=2 SCAR=7.0
Pod documented
2308 2 my ($v) = splice @_, 1, 1;
2309 2 my $v2bc = (&biconnectivity)[3];
2310 2 splice @_, 1, 0, $v;
2311 2 my $V = $_[0]->[ _V ];
2312 2 ($v) = $V->get_ids_by_paths([$v]);
2313 2 ▶ return defined $v2bc->{ $v } ? keys %{ $v2bc->{ $v } } : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $v2bc->{$v} ? : 2 0 2
2314 }
2315  
2316 5 ▶sub same_biconnected_components {
Subroutine same_biconnected_components: called CC=3 SCAR=11.0
Pod documented
2317 5 my ($v2bc, $Z) = (&biconnectivity)[4,5];
2318 5 my $V = $_[0]->[ _V ];
2319 5 my @vs = $V->get_ids_by_paths([@_[1..$#_]]);
2320 5 ▶ return 0 if grep !defined, my @vecs = @$v2bc{ @vs };
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), my(@vecs) = @$v2bc{@vs}) 0 5 5
2321 5 my $accumulator = $vecs[0];
2322 5 $accumulator &= $_ for @vecs[1..$#vecs]; # accumulate 0s -> all in same
2323 5 $accumulator ne $Z;
2324 }
2325  
2326 1 ▶sub biconnected_graph {
Subroutine biconnected_graph: called CC=6 SCAR=17.9
Pod documented
2327 1 my ($g, %opt) = @_;
2328 1 my $bc = (&biconnectivity)[1];
2329 1 my $bcg = Graph->new(directed => 0);
2330 1 ▶ my $sc_cb = $opt{super_component} || \&_super_component;
Condition50%1/2
$opt{'super_component'} || \&Graph::_super_component
l!l
01
MC/DC0%0/1
$opt{'super_component'} || \&Graph::_super_component
$opt{'super_component'}
Truth table50%1/2
A
A= $opt{'super_component'}
Aresult
11
00
2331 1 my @s = map $sc_cb->(@$_), @$bc;
2332 1 $bcg->set_vertex_attribute($s[$_], 'subvertices', $bc->[$_]) for 0..$#$bc;
2333 1 my @edges;
2334 1 for my $i (0..$#$bc) {
2335 5 my @u = @{ $bc->[ $i ] };
2336 5 for my $j (0..$i-1) {
2337 10 my %j; @j{ @{ $bc->[ $j ] } } = ();
2338 10 ▶ next if !grep exists $j{ $_ }, @u;
Branch: 1 branch
BranchTrueFalseTotal
unless grep exists $j{$_}, @u 6 4 10
2339 4 push @edges, [ @s[$i, $j] ];
2340 }
2341 }
2342 1 $bcg->add_edges(@edges);
2343 1 return $bcg;
2344 }
2345  
2346 59 ▶sub bridges {
Subroutine bridges: called CC=2 SCAR=8.1
Pod documented
2347 59 ▶ @{ (&biconnectivity)[2] || _empty_array };
Condition33%1/3
(&biconnectivity)[2] || &_empty_array()
l!l&&r!l&&!r
5900
MC/DC0%0/2
(&biconnectivity)[2] || &_empty_array()
(&biconnectivity)[2] &_empty_array()
Truth table33%1/3
A || B
A= (&biconnectivity)[2]B= &_empty_array()
ABresult
1X1
011
000
2348 }
2349  
2350 ###
2351 # SPT.
2352 #
2353  
2354 1091 ▶sub _SPT_add {
Subroutine _SPT_add: called CC=8 SCAR=20.8
2355 1091 my ($g, $h, $HF, $r, $attr, $unseen, $etc) = @_;
2356 1091 ▶ my $etc_r = $etc->{ $r } || 0;
Condition100%2/2
$etc->{$r} || 0
l!l
102566
MC/DC100%1/1
$etc->{$r} || 0
$etc->{$r}
Truth table100%2/2
A
A= $etc->{$r}
Aresult
11
00
2357 1091 for my $s ( grep exists $unseen->{ $_ }, $g->successors( $r ) ) {
2358 0 my ($t) = sort {$a<=>$b} $g->get_edge_attribute_all($r, $s, $attr);
2359 5278 ▶ $t = 1 unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 5155 123 5278
2360 5278 ▶ __carp_confess "Graph::SPT_Dijkstra: edge $r-$s is negative ($t)"
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 1 5277 5278
2361 if $t < 0;
2362 5277 ▶ if (!defined($etc->{ $s }) || ($etc_r + $t) < $etc->{ $s }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $etc->{$s} or $etc_r + $t < $etc->{$s}) 1035 4242 5277
Condition100%3/3
not defined $etc->{$s} or $etc_r + $t < $etc->{$s}
l!l&&r!l&&!r
1025104242
MC/DC100%2/2
not defined $etc->{$s} or $etc_r + $t < $etc->{$s}
not defined $etc->{$s} $etc_r + $t < $etc->{$s}
Truth table100%3/3
not(A) or B
A= not defined $etc->{$s}B= $etc_r + $t < $etc->{$s}
ABresult
1X1
011
000
2363 1035 ▶ my $etc_s = $etc->{ $s } || 0;
Condition100%2/2
$etc->{$s} || 0
l!l
101025
MC/DC100%1/1
$etc->{$s} || 0
$etc->{$s}
Truth table100%2/2
A
A= $etc->{$s}
Aresult
11
00
2364 1035 $etc->{ $s } = $etc_r + $t;
2365 # print "$r - $s : setting $s to $etc->{ $s } ($etc_r, $etc_s)\n";
2366 1035 $h->set_vertex_attributes($s, { $attr=>$etc->{ $s }, 'p', $r });
2367 1035 $HF->add( Graph::SPTHeapElem->new($r, $s, $etc->{ $s }) );
2368 }
2369 }
2370 }
2371  
2372 44 ▶sub _SPT_Dijkstra_compute {
Subroutine _SPT_Dijkstra_compute: called CC=1 SCAR=0.0
2373 44 require Graph::SPTHeapElem;
2374 44 my $sptg = $_[0]->_heap_walk($_[0]->new(multiedged=>0), \&_SPT_add, {}, @_[1..$#_]);
2375 43 $sptg->set_graph_attribute('SPT_Dijkstra_root', $_[4]);
2376 43 $sptg;
2377 }
2378  
2379 88 ▶sub SPT_Dijkstra {
Subroutine SPT_Dijkstra: called CC=1 SCAR=0.0
Pod documented
2380 88 my $g = $_[0];
2381 88 my @args = &_root_opt;
2382 88 _check_cache($g, 'SPT_Dijkstra', [$args[3]],
2383 \&_SPT_Dijkstra_compute, @args);
2384 }
2385  
2386 84 *SSSP_Dijkstra = \&SPT_Dijkstra;
2387  
2388 84 *single_source_shortest_paths = \&SPT_Dijkstra;
2389  
2390 74 ▶sub SP_Dijkstra {
Subroutine SP_Dijkstra: called CC=7 SCAR=19.5
Pod documented
2391 74 my ($g, $u, $v) = @_;
2392 74 my $sptg = $g->SPT_Dijkstra(first_root => $u);
2393 74 my @path = ($v);
2394 74 require Set::Object;
2395 74 my $seen = Set::Object->new;
2396 74 my $V = $g->vertices;
2397 74 my $p;
2398 74 ▶ while (defined($p = $sptg->get_vertex_attribute($v, 'p'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($p = $sptg->get_vertex_attribute($v, "p"))) 92 45 137
2399 92 ▶ last if $seen->contains($p);
Branch: 1 branch
BranchTrueFalseTotal
if $seen->contains($p) 0 92 92
2400 92 push @path, $p;
2401 92 $v = $p;
2402 92 $seen->insert($p);
2403 92 ▶ last if $seen->size == $V || $u eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->size == $V or $u eq $v 29 63 92
Condition66%2/3
$seen->size == $V or $u eq $v
l!l&&r!l&&!r
02963
MC/DC50%1/2
$seen->size == $V or $u eq $v
$seen->size == $V $u eq $v
Truth table66%2/3
A or B
A= $seen->size == $VB= $u eq $v
ABresult
1X1
011
000
2404 }
2405 74 ▶ return if !@path or $path[-1] ne $u;
Branch: 1 branch
BranchTrueFalseTotal
if not @path or $path[-1] ne $u 41 33 74
Condition66%2/3
not @path or $path[-1] ne $u
l!l&&r!l&&!r
04133
MC/DC50%1/2
not @path or $path[-1] ne $u
not @path $path[-1] ne $u
Truth table66%2/3
not(A) or B
A= not @pathB= $path[-1] ne $u
ABresult
1X1
011
000
2406 33 return reverse @path;
2407 }
2408  
2409 2184 ▶sub __SPT_Bellman_Ford {
Subroutine __SPT_Bellman_Ford: called CC=7 SCAR=19.6
2410 2184 my ($g, $u, $v, $attr, $d, $p, $c0, $c1) = @_;
2411 2184 ▶ return unless $c0->{ $u };
Branch: 1 branch
BranchTrueFalseTotal
unless $c0->{$u} 1958 226 2184
2412 0 my ($w) = sort {$a<=>$b} $g->get_edge_attribute_all($u, $v, $attr);
2413 226 ▶ $w = 1 unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 151 75 226
2414 226 ▶ if (defined $d->{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{$v}) { } 147 79 226
2415 147 ▶ if (defined $d->{ $u }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{$u}) 147 0 147
2416 147 ▶ if ($d->{ $v } > $d->{ $u } + $w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{$v} > $d->{$u} + $w) 15 132 147
2417 15 $d->{ $v } = $d->{ $u } + $w;
2418 15 $p->{ $v } = $u;
2419 15 $c1->{ $v }++;
2420 }
2421 } # else !defined $d->{ $u } && defined $d->{ $v }
2422 } else {
2423 79 ▶ if (defined $d->{ $u }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{$u}) 79 0 79
2424 # defined $d->{ $u } && !defined $d->{ $v }
2425 79 $d->{ $v } = $d->{ $u } + $w;
2426 79 $p->{ $v } = $u;
2427 79 $c1->{ $v }++;
2428 } # else !defined $d->{ $u } && !defined $d->{ $v }
2429 }
2430 }
2431  
2432 12 ▶sub _SPT_Bellman_Ford {
Subroutine _SPT_Bellman_Ford: called CC=11 SCAR=24.0
2433 12 my ($g, $opt, $unseenh, $unseena, $r, $next, $code, $attr) = @_;
2434 12 my %d;
2435 12 ▶ return unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 12 12
2436 12 $d{ $r } = 0;
2437 12 my %p;
2438 12 my $V = $g->vertices;
2439 12 my %c0; # Changed during the last iteration?
2440 12 $c0{ $r }++;
2441 12 ▶ for (my $i = 0; $i < $V; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $V) 95 12 107
2442 95 my %c1;
2443 95 for my $e ($g->edges) {
2444 1612 my ($u, $v) = @$e;
2445 1612 __SPT_Bellman_Ford($g, $u, $v, $attr, \%d, \%p, \%c0, \%c1);
2446 1612 ▶ __SPT_Bellman_Ford($g, $v, $u, $attr, \%d, \%p, \%c0, \%c1)
Branch: 1 branch
BranchTrueFalseTotal
if $g->undirected 572 1040 1612
2447 if $g->undirected;
2448 }
2449 95 ▶ %c0 = %c1 unless $i == $V - 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $V - 1 83 12 95
2450 }
2451  
2452 12 for my $e ($g->edges) {
2453 172 my ($u, $v) = @$e;
2454 172 ▶ if (defined $d{ $u } && defined $d{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d{$u} and defined $d{$v}) 159 13 172
Condition66%2/3
defined $d{$u} and defined $d{$v}
!ll&&!rl&&r
130159
MC/DC50%1/2
defined $d{$u} and defined $d{$v}
defined $d{$u} defined $d{$v}
Truth table66%2/3
A and B
A= defined $d{$u}B= defined $d{$v}
ABresult
0X0
100
111
2455 0 my ($d) = sort {$a<=>$b} $g->get_edge_attribute_all($u, $v, $attr);
2456 __carp_confess "Graph::SPT_Bellman_Ford: negative cycle exists"
2457 159 ▶ if defined $d && $d{ $v } > $d{ $u } + $d;
Branch: 1 branch
BranchTrueFalseTotal
if defined $d and $d{$v} > $d{$u} + $d 1 158 159
Condition100%3/3
defined $d and $d{$v} > $d{$u} + $d
!ll&&!rl&&r
99591
MC/DC100%2/2
defined $d and $d{$v} > $d{$u} + $d
defined $d $d{$v} > $d{$u} + $d
Truth table100%3/3
A and B
A= defined $dB= $d{$v} > $d{$u} + $d
ABresult
0X0
100
111
2458 }
2459 }
2460  
2461 11 return (\%p, \%d);
2462 }
2463  
2464 12 ▶sub _SPT_Bellman_Ford_compute {
Subroutine _SPT_Bellman_Ford_compute: called CC=2 SCAR=6.9
2465 12 my ($g, @args) = @_;
2466 12 my ($p, $d) = $g->_SPT_Bellman_Ford(@args);
2467 11 my $h = $g->new(multiedged=>0);
2468 11 for my $v (keys %$p) {
2469 74 my $u = $p->{ $v };
2470 0 my ($w) = sort {$a<=>$b} $g->get_edge_attribute_all($u, $v, $args[6]);
2471 74 $h->set_edge_attribute( $u, $v, $args[6], $w);
2472 74 $h->set_vertex_attributes( $v, { $args[6], $d->{ $v }, p => $u } );
2473 }
2474 11 $h->set_graph_attribute('SPT_Bellman_Ford_root', $args[3]);
2475 11 $h;
2476 }
2477  
2478 34 ▶sub SPT_Bellman_Ford {
Subroutine SPT_Bellman_Ford: called CC=1 SCAR=0.0
Pod documented
2479 34 my @args = &_root_opt;
2480 34 _check_cache($_[0], 'SPT_Bellman_Ford', [$args[3]],
2481 \&_SPT_Bellman_Ford_compute, @args);
2482 }
2483  
2484 84 *SSSP_Bellman_Ford = \&SPT_Bellman_Ford;
2485  
2486 24 ▶sub SP_Bellman_Ford {
Subroutine SP_Bellman_Ford: called CC=4 SCAR=13.9
Pod documented
2487 24 my ($g, $u, $v) = @_;
2488 24 my $sptg = $g->SPT_Bellman_Ford(first_root => $u);
2489 24 my @path = ($v);
2490 24 require Set::Object;
2491 24 my $seen = Set::Object->new;
2492 24 my $V = $g->vertices;
2493 24 my $p;
2494 24 ▶ while (defined($p = $sptg->get_vertex_attribute($v, 'p'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($p = $sptg->get_vertex_attribute($v, "p"))) 40 24 64
2495 40 ▶ last if $seen->contains($p);
Branch: 1 branch
BranchTrueFalseTotal
if $seen->contains($p) 0 40 40
2496 40 push @path, $p;
2497 40 $v = $p;
2498 40 $seen->insert($p);
2499 40 ▶ last if $seen->size == $V;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->size == $V 0 40 40
2500 }
2501 # @path = () if @path && "$path[-1]" ne "$u";
2502 24 return reverse @path;
2503 }
2504  
2505 ###
2506 # Transitive Closure.
2507 #
2508  
2509 19 ▶sub TransitiveClosure_Floyd_Warshall {
Subroutine TransitiveClosure_Floyd_Warshall: called CC=1 SCAR=0.0
Pod documented
2510 19 my $self = shift;
2511 19 require Graph::TransitiveClosure;
2512 19 Graph::TransitiveClosure->new($self, @_);
2513 }
2514  
2515 84 *transitive_closure = \&TransitiveClosure_Floyd_Warshall;
2516  
2517 37 ▶sub APSP_Floyd_Warshall {
Subroutine APSP_Floyd_Warshall: called CC=1 SCAR=0.0
Pod documented
2518 37 my $self = shift;
2519 37 require Graph::TransitiveClosure;
2520 37 Graph::TransitiveClosure->new($self, path => 1, @_);
2521 }
2522  
2523 84 *all_pairs_shortest_paths = \&APSP_Floyd_Warshall;
2524  
2525 22 ▶sub _transitive_closure_matrix_compute {
Subroutine _transitive_closure_matrix_compute: called CC=1 SCAR=0.0
2526 22 &APSP_Floyd_Warshall->transitive_closure_matrix;
2527 }
2528  
2529 1143 ▶sub transitive_closure_matrix {
Subroutine transitive_closure_matrix: called CC=1 SCAR=0.0
Pod documented
2530 1143 _check_cache($_[0], 'transitive_closure_matrix', [],
2531 \&_transitive_closure_matrix_compute, @_[1..$#_]);
2532 }
2533  
2534 1532 ▶sub path_length {
Subroutine path_length: called CC=1 SCAR=0.0
Pod documented
2535 1532 shift->transitive_closure_matrix->path_length(@_);
2536 }
2537  
2538 27 ▶sub path_successor {
Subroutine path_successor: called CC=1 SCAR=0.0
Pod documented
2539 27 shift->transitive_closure_matrix->path_successor(@_);
2540 }
2541  
2542 205 ▶sub path_vertices {
Subroutine path_vertices: called CC=1 SCAR=0.0
Pod documented
2543 205 shift->transitive_closure_matrix->path_vertices(@_);
2544 }
2545  
2546 25 ▶sub all_paths {
Subroutine all_paths: called CC=1 SCAR=0.0
Pod documented
2547 25 shift->transitive_closure_matrix->all_paths(@_);
2548 }
2549  
2550 12568 ▶sub is_reachable {
Subroutine is_reachable: called CC=1 SCAR=0.0
Pod documented
2551 12568 shift->transitive_closure_matrix->is_reachable(@_);
2552 }
2553  
2554 34 ▶sub for_shortest_paths {
Subroutine for_shortest_paths: called CC=3 SCAR=11.0
Pod documented
2555 34 my $g = shift;
2556 34 my $c = shift;
2557 34 my $t = $g->transitive_closure_matrix;
2558 34 my @v = $g->vertices;
2559 34 my $n = 0;
2560 34 for my $u (@v) {
2561 183 $c->($t, $u, $_, ++$n) for grep $t->is_reachable($u, $_), @v;
2562 }
2563 34 return $n;
2564 }
2565  
2566 25 ▶sub _minmax_path {
Subroutine _minmax_path: called CC=1 SCAR=0.0
2567 25 my $g = shift;
2568 25 my $min;
2569 my $max;
2570 25 my $minp;
2571 25 my $maxp;
2572 628 ▶ $g->for_shortest_paths(sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
2573 628 my ($t, $u, $v, $n) = @_;
2574 628 my $l = $t->path_length($u, $v);
2575 628 ▶ return unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 0 628 628
2576 628 my $p;
2577 628 ▶ if ($u ne $v && (!defined $max || $l > $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u ne $v and !defined($max) || $l > $max) 50 578 628
Condition100%6/6
$u ne $v and !defined($max) || $l > $max
!ll&&!rl&&r
10047850
not defined $max or $l > $max
l!l&&r!l&&!r
1535478
MC/DC100%3/3
$u ne $v and !defined($max) || $l > $max
$u ne $v not defined $max $l > $max
Truth table100%4/4
A and not(B) or C
A= $u ne $vB= not defined $maxC= $l > $max
ABCresult
0XX0
1000
11X1
1011
2578 50 $max = $l;
2579 50 $maxp = $p = [ $t->path_vertices($u, $v) ];
2580 }
2581 628 ▶ if ($u ne $v && (!defined $min || $l < $min)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u ne $v and !defined($min) || $l < $min) 18 610 628
Condition100%6/6
$u ne $v and !defined($min) || $l < $min
!ll&&!rl&&r
10051018
not defined $min or $l < $min
l!l&&r!l&&!r
153510
MC/DC100%3/3
$u ne $v and !defined($min) || $l < $min
$u ne $v not defined $min $l < $min
Truth table100%4/4
A and not(B) or C
A= $u ne $vB= not defined $minC= $l < $min
ABCresult
0XX0
1000
11X1
1011
2582 18 $min = $l;
2583 18 ▶ $minp = $p || [ $t->path_vertices($u, $v) ];
Condition100%2/2
$p || [$t->path_vertices($u, $v)]
l!l
153
MC/DC100%1/1
$p || [$t->path_vertices($u, $v)]
$p
Truth table100%2/2
A
A= $p
Aresult
11
00
2584 }
2585 25 });
2586 25 return ($min, $max, $minp, $maxp);
2587 }
2588  
2589 15 ▶sub diameter {
Subroutine diameter: called CC=3 SCAR=11.1
Pod documented
2590 15 my $g = shift;
2591 15 my ($min, $max, $minp, $maxp) = $g->_minmax_path(@_);
2592 15 ▶ return defined $maxp ? (wantarray ? @$maxp : $max) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $maxp ? : 11 4 15
wantarray ? : 0 11 11
2593 }
2594  
2595 84 *graph_diameter = \&diameter;
2596  
2597 5 ▶sub longest_path {
Subroutine longest_path: called CC=17 SCAR=28.7
Pod documented
2598 5 my ($g, $u, $v) = @_;
2599 5 my $t = $g->transitive_closure_matrix;
2600 5 ▶ if (defined $u) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u) 2 3 5
2601 2 ▶ return wantarray ? $t->path_vertices($u, $v) : $t->path_length($u, $v)
Branch: 2 branches
BranchTrueFalseTotal
if defined $v 1 1 2
wantarray ? : 0 1 1
2602 if defined $v;
2603 1 my $max;
2604 my @max;
2605 1 for my $v (grep $u ne $_, $g->vertices) {
2606 9 my $l = $t->path_length($u, $v);
2607 9 ▶ next if !(defined $l && (!defined $max || $l > $max));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and !defined($max) || $l > $max 6 3 9
Condition83%5/6
defined $l and !defined($max) || $l > $max
!ll&&!rl&&r
063
not defined $max or $l > $max
l!l&&r!l&&!r
126
MC/DC66%2/3
defined $l and !defined($max) || $l > $max
defined $l not defined $max $l > $max
Truth table75%3/4
A and not(B) or C
A= defined $lB= not defined $maxC= $l > $max
ABCresult
0XX0
1000
11X1
1011
2608 3 $max = $l;
2609 3 @max = $t->path_vertices($u, $v);
2610 }
2611 1 ▶ return wantarray ? @max : $max;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
2612 }
2613 3 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 1 2 3
2614 1 my $max;
2615 my @max;
2616 1 for my $u (grep $_ ne $v, $g->vertices) {
2617 9 my $l = $t->path_length($u, $v);
2618 9 ▶ next if !(defined $l && (!defined $max || $l > $max));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and !defined($max) || $l > $max 7 2 9
Condition83%5/6
defined $l and !defined($max) || $l > $max
!ll&&!rl&&r
072
not defined $max or $l > $max
l!l&&r!l&&!r
117
MC/DC66%2/3
defined $l and !defined($max) || $l > $max
defined $l not defined $max $l > $max
Truth table75%3/4
A and not(B) or C
A= defined $lB= not defined $maxC= $l > $max
ABCresult
0XX0
1000
11X1
1011
2619 2 $max = $l;
2620 2 @max = $t->path_vertices($u, $v);
2621 }
2622 1 ▶ return wantarray ? @max : @max - 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
2623 }
2624 2 my ($min, $max, $minp, $maxp) = $g->_minmax_path(@_);
2625 2 ▶ return defined $maxp ? (wantarray ? @$maxp : $max) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $maxp ? : 2 0 2
wantarray ? : 0 2 2
2626 }
2627  
2628 165 ▶sub vertex_eccentricity {
Subroutine vertex_eccentricity: called CC=7 SCAR=19.5
Pod documented
2629 165 &expect_undirected;
2630 165 my ($g, $u) = @_;
2631 165 ▶ return Infinity() if !&is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_connected 7 158 165
2632 158 my $max;
2633 158 for my $v (grep $u ne $_, $g->vertices) {
2634 1095 my $l = $g->path_length($u, $v);
2635 1095 ▶ next if !(defined $l && (!defined $max || $l > $max));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and !defined($max) || $l > $max 729 366 1095
Condition83%5/6
defined $l and !defined($max) || $l > $max
!ll&&!rl&&r
0729366
not defined $max or $l > $max
l!l&&r!l&&!r
155211729
MC/DC66%2/3
defined $l and !defined($max) || $l > $max
defined $l not defined $max $l > $max
Truth table75%3/4
A and not(B) or C
A= defined $lB= not defined $maxC= $l > $max
ABCresult
0XX0
1000
11X1
1011
2636 366 $max = $l;
2637 }
2638 158 ▶ return defined $max ? $max : Infinity();
Branch: 1 branch
BranchTrueFalseTotal
defined $max ? : 155 3 158
2639 }
2640  
2641 11 ▶sub shortest_path {
Subroutine shortest_path: called CC=17 SCAR=28.7
Pod documented
2642 11 &expect_undirected;
2643 11 my ($g, $u, $v) = @_;
2644 11 my $t = $g->transitive_closure_matrix;
2645 11 ▶ if (defined $u) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u) 2 9 11
2646 2 ▶ return wantarray ? $t->path_vertices($u, $v) : $t->path_length($u, $v)
Branch: 2 branches
BranchTrueFalseTotal
if defined $v 1 1 2
wantarray ? : 0 1 1
2647 if defined $v;
2648 1 my $min;
2649 my @min;
2650 1 for my $v (grep $u ne $_, $g->vertices) {
2651 9 my $l = $t->path_length($u, $v);
2652 9 ▶ next if !(defined $l && (!defined $min || $l < $min));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and !defined($min) || $l < $min 8 1 9
Condition66%4/6
defined $l and !defined($min) || $l < $min
!ll&&!rl&&r
081
not defined $min or $l < $min
l!l&&r!l&&!r
108
MC/DC33%1/3
defined $l and !defined($min) || $l < $min
defined $l not defined $min $l < $min
Truth table50%2/4
A and not(B) or C
A= defined $lB= not defined $minC= $l < $min
ABCresult
0XX0
1000
11X1
1011
2653 1 $min = $l;
2654 1 @min = $t->path_vertices($u, $v);
2655 }
2656 # print "min/1 = @min\n";
2657 1 ▶ return wantarray ? @min : $min;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
2658 }
2659 9 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 1 8 9
2660 1 my $min;
2661 my @min;
2662 1 for my $u (grep $_ ne $v, $g->vertices) {
2663 9 my $l = $t->path_length($u, $v);
2664 9 ▶ next if !(defined $l && (!defined $min || $l < $min));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and !defined($min) || $l < $min 6 3 9
Condition83%5/6
defined $l and !defined($min) || $l < $min
!ll&&!rl&&r
063
not defined $min or $l < $min
l!l&&r!l&&!r
126
MC/DC66%2/3
defined $l and !defined($min) || $l < $min
defined $l not defined $min $l < $min
Truth table75%3/4
A and not(B) or C
A= defined $lB= not defined $minC= $l < $min
ABCresult
0XX0
1000
11X1
1011
2665 3 $min = $l;
2666 3 @min = $t->path_vertices($u, $v);
2667 }
2668 # print "min/2 = @min\n";
2669 1 ▶ return wantarray ? @min : $min;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
2670 }
2671 8 my ($min, $max, $minp, $maxp) = $g->_minmax_path(@_);
2672 8 ▶ return if !defined $minp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minp 6 2 8
2673 2 ▶ wantarray ? @$minp : $min;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
2674 }
2675  
2676 17 ▶sub radius {
Subroutine radius: called CC=4 SCAR=13.9
Pod documented
2677 17 &expect_undirected;
2678 17 my $g = shift;
2679 17 my ($center, $radius) = (undef, Infinity());
2680 17 for my $v ($g->vertices) {
2681 89 my $x = $g->vertex_eccentricity($v);
2682 89 ▶ ($center, $radius) = ($v, $x) if defined $x && $x < $radius;
Branch: 1 branch
BranchTrueFalseTotal
if defined $x and $x < $radius 20 69 89
Condition66%2/3
defined $x and $x < $radius
!ll&&!rl&&r
06920
MC/DC50%1/2
defined $x and $x < $radius
defined $x $x < $radius
Truth table66%2/3
A and B
A= defined $xB= $x < $radius
ABresult
0X0
100
111
2683 }
2684 17 return $radius;
2685 }
2686  
2687 10 ▶sub center_vertices {
Subroutine center_vertices: called CC=8 SCAR=21.1
Pod documented
2688 10 &expect_undirected;
2689 10 my ($g, $delta) = @_;
2690 10 ▶ $delta = 0 unless defined $delta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delta 9 1 10
2691 10 $delta = abs($delta);
2692 10 my @c;
2693 10 my $Inf = Infinity();
2694 10 my $r = $g->radius;
2695 10 ▶ if (defined $r && $r != $Inf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r != $Inf) 7 3 10
Condition66%2/3
defined $r and $r != $Inf
!ll&&!rl&&r
037
MC/DC50%1/2
defined $r and $r != $Inf
defined $r $r != $Inf
Truth table66%2/3
A and B
A= defined $rB= $r != $Inf
ABresult
0X0
100
111
2696 7 for my $v ($g->vertices) {
2697 53 my $e = $g->vertex_eccentricity($v);
2698 53 ▶ next unless defined $e && $e != $Inf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e and $e != $Inf 0 53 53
Condition33%1/3
defined $e and $e != $Inf
!ll&&!rl&&r
0053
MC/DC0%0/2
defined $e and $e != $Inf
defined $e $e != $Inf
Truth table33%1/3
A and B
A= defined $eB= $e != $Inf
ABresult
0X0
100
111
2699 53 ▶ push @c, $v if abs($e - $r) <= $delta;
Branch: 1 branch
BranchTrueFalseTotal
if abs $e - $r <= $delta 15 38 53
2700 }
2701 }
2702 10 return @c;
2703 }
2704  
2705 84 *centre_vertices = \&center_vertices;
2706  
2707 9 ▶sub average_path_length {
Subroutine average_path_length: called CC=2 SCAR=6.9
Pod documented
2708 9 my $g = shift;
2709 9 my @A = @_;
2710 9 my $d = 0;
2711 9 my $m = 0;
2712 809 ▶ $g->for_shortest_paths(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
2713 809 my ($t, $u, $v, $n) = @_;
2714 809 ▶ return unless my $l = $t->path_length($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless my $l = $t->path_length($u, $v) 83 726 809
2715 726 ▶ return if defined $A[0] && $u ne $A[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $A[0] and $u ne $A[0] 418 308 726
Condition100%3/3
defined $A[0] and $u ne $A[0]
!ll&&!rl&&r
27038418
MC/DC100%2/2
defined $A[0] and $u ne $A[0]
defined $A[0] $u ne $A[0]
Truth table100%3/3
A and B
A= defined $A[0]B= $u ne $A[0]
ABresult
0X0
100
111
2716 308 ▶ return if defined $A[1] && $v ne $A[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $A[1] and $v ne $A[1] 163 145 308
Condition100%3/3
defined $A[1] and $v ne $A[1]
!ll&&!rl&&r
12619163
MC/DC100%2/2
defined $A[1] and $v ne $A[1]
defined $A[1] $v ne $A[1]
Truth table100%3/3
A and B
A= defined $A[1]B= $v ne $A[1]
ABresult
0X0
100
111
2717 145 $d += $l;
2718 145 $m++;
2719 9 });
2720 9 ▶ return $m ? $d / $m : undef;
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 8 1 9
2721 }
2722  
2723 ###
2724 # Simple tests.
2725 #
2726  
2727 32 ▶sub is_multi_graph {
Subroutine is_multi_graph: called CC=6 SCAR=17.9
Pod documented
2728 32 ▶ return 0 unless &is_multiedged || &is_countedged;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multiedged or &is_countedged 16 16 32
Condition100%3/3
&is_multiedged or &is_countedged
l!l&&r!l&&!r
8816
MC/DC100%2/2
&is_multiedged or &is_countedged
&is_multiedged &is_countedged
Truth table100%3/3
A or B
A= &is_multiedgedB= &is_countedged
ABresult
1X1
011
000
2729 16 my $g = $_[0];
2730 16 my $multiedges = 0;
2731 16 for my $e (&_edges05) {
2732 14 my ($u, @v) = @$e;
2733 14 ▶ return 0 if grep $u eq $_, @v;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($u eq $_), @v) 8 6 14
2734 6 ▶ $multiedges++ if $g->get_edge_count(@$e) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $g->get_edge_count(@$e) > 1 2 4 6
2735 }
2736 8 return $multiedges;
2737 }
2738  
2739 32 ▶sub is_simple_graph {
Subroutine is_simple_graph: called CC=4 SCAR=13.9
Pod documented
2740 32 ▶ return 1 unless &is_multiedged || &is_countedged;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_multiedged or &is_countedged 16 16 32
Condition100%3/3
&is_multiedged or &is_countedged
l!l&&r!l&&!r
8816
MC/DC100%2/2
&is_multiedged or &is_countedged
&is_multiedged &is_countedged
Truth table100%3/3
A or B
A= &is_multiedgedB= &is_countedged
ABresult
1X1
011
000
2741 16 my $g = $_[0];
2742 16 ▶ return 0 if grep $g->get_edge_count(@$_) > 1, &_edges05;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($g->get_edge_count(@$_) > 1), &_edges05) 4 12 16
2743 12 return 1;
2744 }
2745  
2746 32 ▶sub is_pseudo_graph {
Subroutine is_pseudo_graph: called CC=6 SCAR=17.9
Pod documented
2747 32 ▶ my $m = &is_countedged || &is_multiedged;
Condition100%3/3
&is_countedged || &is_multiedged
l!l&&r!l&&!r
8816
MC/DC100%2/2
&is_countedged || &is_multiedged
&is_countedged &is_multiedged
Truth table100%3/3
A || B
A= &is_countedgedB= &is_multiedged
ABresult
1X1
011
000
2748 32 my $g = $_[0];
2749 32 for my $e (&_edges05) {
2750 28 my ($u, @v) = @$e;
2751 28 ▶ return 1 if grep $u eq $_, @v;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($u eq $_), @v) 16 12 28
2752 12 ▶ return 1 if $m && $g->get_edge_count($u, @v) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $m and $g->get_edge_count($u, @v) > 1 2 10 12
Condition100%3/3
$m and $g->get_edge_count($u, @v) > 1
!ll&&!rl&&r
642
MC/DC100%2/2
$m and $g->get_edge_count($u, @v) > 1
$m $g->get_edge_count($u, @v) > 1
Truth table100%3/3
A and B
A= $mB= $g->get_edge_count($u, @v) > 1
ABresult
0X0
100
111
2753 }
2754 14 return 0;
2755 }
2756  
2757 ###
2758 # Rough isomorphism guess.
2759 #
2760  
2761 84 my %_factorial = (0 => 1, 1 => 1);
2762  
2763 4 ▶sub __factorial {
Subroutine __factorial: called CC=3 SCAR=11.0
2764 4 my $n = shift;
2765 4 ▶ for (my $i = 2; $i <= $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 14 4 18
2766 14 ▶ next if exists $_factorial{$i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_factorial{$i} 7 7 14
2767 7 $_factorial{$i} = $i * $_factorial{$i - 1};
2768 }
2769 4 $_factorial{$n};
2770 }
2771  
2772 39 ▶sub _factorial {
Subroutine _factorial: called CC=3 SCAR=11.0
2773 39 my $n = int(shift);
2774 39 ▶ __carp_confess "factorial of a negative number" if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 39 39
2775 39 ▶ __factorial($n) unless exists $_factorial{$n};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_factorial{$n} 4 35 39
2776 39 return $_factorial{$n};
2777 }
2778  
2779 31 ▶sub could_be_isomorphic {
Subroutine could_be_isomorphic: called CC=16 SCAR=28.4
Pod documented
2780 31 my ($g0, $g1) = @_;
2781 31 ▶ return 0 unless &vertices == $g1->vertices;
Branch: 1 branch
BranchTrueFalseTotal
unless &vertices == $g1->vertices 8 23 31
2782 23 ▶ return 0 unless &_edges05 == $g1->_edges05;
Branch: 1 branch
BranchTrueFalseTotal
unless &_edges05 == $g1->_edges05 6 17 23
2783 17 my %d0;
2784 17 $d0{ $g0->in_degree($_) }{ $g0->out_degree($_) }++ for &vertices;
2785 17 my %d1;
2786 17 $d1{ $g1->in_degree($_) }{ $g1->out_degree($_) }++ for $g1->vertices;
2787 17 ▶ return 0 unless keys %d0 == keys %d1;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %d0 == keys %d1 0 17 17
2788 17 for my $da (keys %d0) {
2789 return 0
2790 unless exists $d1{$da} &&
2791 31 ▶ keys %{ $d0{$da} } == keys %{ $d1{$da} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $d1{$da} and keys %{$d0{$da};} == keys %{$d1{$da};} 0 31 31
Condition33%1/3
exists $d1{$da} and keys %{$d0{$da};} == keys %{$d1{$da};}
!ll&&!rl&&r
0031
MC/DC0%0/2
exists $d1{$da} and keys %{$d0{$da};} == keys %{$d1{$da};}
exists $d1{$da} keys %{$d0{$da};} == keys %{$d1{$da};}
Truth table33%1/3
A and B
A= exists $d1{$da}B= keys %{$d0{$da};} == keys %{$d1{$da};}
ABresult
0X0
100
111
2792 return 0
2793 if grep !(exists $d1{$da}{$_} && $d0{$da}{$_} == $d1{$da}{$_}),
2794 31 ▶ keys %{ $d0{$da} };
Branch: 1 branch
BranchTrueFalseTotal
if grep((!(exists $d1{$da}{$_} && $d0{$da}{$_} == $d1{$da}{$_})), keys %{$d0{$da};}) 4 27 31
Condition66%2/3
exists $d1{$da}{$_} && $d0{$da}{$_} == $d1{$da}{$_}
!ll&&!rl&&r
4041
MC/DC50%1/2
exists $d1{$da}{$_} && $d0{$da}{$_} == $d1{$da}{$_}
exists $d1{$da}{$_} $d0{$da}{$_} == $d1{$da}{$_}
Truth table66%2/3
A && B
A= exists $d1{$da}{$_}B= $d0{$da}{$_} == $d1{$da}{$_}
ABresult
0X0
100
111
2795 }
2796 13 for my $da (keys %d0) {
2797 27 ▶ return 0 if grep $d1{$da}{$_} != $d0{$da}{$_}, keys %{ $d0{$da} };
Branch: 1 branch
BranchTrueFalseTotal
if grep(($d1{$da}{$_} != $d0{$da}{$_}), keys %{$d0{$da};}) 0 27 27
2798 27 delete $d1{$da};
2799 }
2800 13 ▶ return 0 unless keys %d1 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %d1 == 0 0 13 13
2801 13 my $f = 1;
2802 13 for my $da (keys %d0) {
2803 27 $f *= _factorial(abs($d0{$da}{$_})) for keys %{ $d0{$da} };
2804 }
2805 13 return $f;
2806 }
2807  
2808 ###
2809 # Analysis functions.
2810  
2811 17 ▶sub subgraph_by_radius {
Subroutine subgraph_by_radius: called CC=1 SCAR=0.0
Pod documented
2812 17 $_[0]->subgraph([ @_[1..$#_-1], &reachable_by_radius ]);
2813 }
2814  
2815 2 ▶sub clustering_coefficient {
Subroutine clustering_coefficient: called CC=8 SCAR=20.8
Pod documented
2816 2 my ($g) = @_;
2817 2 ▶ return unless my @v = $g->vertices;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@v) = $g->vertices 1 1 2
2818 1 require Set::Object;
2819 1 my %clustering;
2820  
2821 1 my $gamma = 0;
2822  
2823 1 for my $n (@v) {
2824 15 my $gamma_v = 0;
2825 15 my @neigh = $g->successors($n);
2826 15 my $c = Set::Object->new;
2827 15 for my $u (@neigh) {
2828 29 ▶ for my $v (grep +(!$c->contains("$u-$_") && $g->has_edge($u, $_)), @neigh) {
Condition100%3/3
!$c->contains("$u-$_") && $g->has_edge($u, $_)
!ll&&!rl&&r
65815
MC/DC100%2/2
!$c->contains("$u-$_") && $g->has_edge($u, $_)
!$c->contains("$u-$_") $g->has_edge($u, $_)
Truth table100%3/3
not(A) && B
A= !$c->contains("$u-$_")B= $g->has_edge($u, $_)
ABresult
0X0
100
111
2829 15 $gamma_v++;
2830 15 $c->insert("$u-$v");
2831 15 $c->insert("$v-$u");
2832 }
2833 }
2834 15 ▶ if (@neigh > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@neigh > 1) { } 9 6 15
2835 9 $clustering{$n} = $gamma_v/(@neigh * (@neigh - 1) / 2);
2836 9 $gamma += $gamma_v/(@neigh * (@neigh - 1) / 2);
2837 } else {
2838 6 $clustering{$n} = 0;
2839 }
2840 }
2841  
2842 1 $gamma /= @v;
2843  
2844 1 ▶ return wantarray ? ($gamma, %clustering) : $gamma;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
2845 }
2846  
2847 1 ▶sub betweenness {
Subroutine betweenness: called CC=13 SCAR=25.6
Pod documented
2848 1 my $g = shift;
2849  
2850 1 my @V = $g->vertices();
2851  
2852 1 my %Cb; # C_b{w} = 0
2853  
2854 1 @Cb{@V} = ();
2855  
2856 1 for my $s (@V) {
2857 15 my @S; # stack (unshift, shift)
2858  
2859 my %P; # P{w} = empty list
2860 15 $P{$_} = [] for @V;
2861  
2862 15 my %sigma; # \sigma{t} = 0
2863 15 $sigma{$_} = 0 for @V;
2864 15 $sigma{$s} = 1;
2865  
2866 15 my %d; # d{t} = -1;
2867 15 $d{$_} = -1 for @V;
2868 15 $d{$s} = 0;
2869  
2870 15 my @Q; # queue (push, shift)
2871 15 push @Q, $s;
2872  
2873 15 ▶ while (@Q) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Q) 172 15 187
2874 172 my $v = shift @Q;
2875 172 unshift @S, $v;
2876 172 for my $w ($g->successors($v)) {
2877 # w found for first time
2878 341 ▶ if ($d{$w} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d{$w} < 0) 157 184 341
2879 157 push @Q, $w;
2880 157 $d{$w} = $d{$v} + 1;
2881 }
2882 # Shortest path to w via v
2883 341 ▶ if ($d{$w} == $d{$v} + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d{$w} == $d{$v} + 1) 173 168 341
2884 173 $sigma{$w} += $sigma{$v};
2885 173 push @{ $P{$w} }, $v;
2886 }
2887 }
2888 }
2889  
2890 15 my %delta;
2891 15 $delta{$_} = 0 for @V;
2892  
2893 15 ▶ while (@S) {
Branch: 1 branch
BranchTrueFalseTotal
if (@S) 172 15 187
2894 172 my $w = shift @S;
2895 $delta{$_} += $sigma{$_}/$sigma{$w} * (1 + $delta{$w})
2896 172 for @{ $P{$w} };
2897 172 ▶ $Cb{$w} += $delta{$w} if $w ne $s;
Branch: 1 branch
BranchTrueFalseTotal
if $w ne $s 157 15 172
2898 }
2899 }
2900  
2901 1 return %Cb;
2902 }
2903  
2904 3 ▶sub connected_subgraphs {
Subroutine connected_subgraphs: called CC=5 SCAR=16.1
Pod documented
2905 3 my $g = shift;
2906 3 require Set::Object;
2907 3 my @subgraphs = ( [ map { Set::Object->new($_) } $g->vertices ] );
2908 3 for (2..scalar $g->vertices) {
2909 21 my %seen;
2910 21 for my $subgraph (@{$subgraphs[-1]}) {
2911 1196 for my $neighbour ((Set::Object->new( map { $g->neighbours($_) } $subgraph->members ) - $subgraph)->members) {
2912 4674 my $new_subgraph = Set::Object->new($subgraph->members, $neighbour);
2913 4674 my $key = join '|', @$new_subgraph;
2914 4674 ▶ next if exists $seen{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$key} 3499 1175 4674
2915 1175 $seen{$key} = $new_subgraph;
2916 }
2917 }
2918 21 push @subgraphs, [values %seen];
2919 }
2920 3 return map { $g->subgraph([$_->members]) } map { @$_ } @subgraphs;
2921 }
2922  
2923 84 1;
« Graph/TransitiveClosure.pm ↑ Summary Graph/UnionFind.pm »