Graph/TransitiveClosure/Matrix.pm

filter: Statementstmt 100% 143 / 143 Branchbran 91.3% 95 / 104 Conditioncond 91.4% 32 / 35 MC/DCmcdc 80.0% 16 / 20 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 95.1% 311 / 327 CC 66 SCAR 29.8
CC
66
Cov
96%
_new
37.1
new
9.0
path_vertices
7.0
CRAP 19.6
↑ Summary Graph/Traversal.pm »
1 package Graph::TransitiveClosure::Matrix;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Graph::AdjacencyMatrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Graph::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _A() { 0 } # adjacency
12 sub _D() { 1 } # distance
13 sub _S() { 2 } # successors
14 sub _V() { 3 } # vertices
15 sub _G() { 4 } # the original graph (OG)
16  
17 70 ▶sub _new {
Subroutine _new: called CC=37 SCAR=36.1
18 70 my ($g, $class, $am_opt, $want_transitive, $want_reflexive, $want_path, $want_path_vertices, $want_path_count) = @_;
19 70 my $m = Graph::AdjacencyMatrix->new($g, %$am_opt);
20 70 my @V = $g->vertices;
21 70 my %v2i; @v2i{ @V } = 0..$#V; # paths are in array -> stable ordering
22 70 my $am = $m->adjacency_matrix;
23 70 $am->[1] = \%v2i;
24 70 my ($dm, @di); # The distance matrix.
25 70 my ($sm, @si); # The successor matrix.
26 # directly use (not via API) arrays of bit-vectors etc for speed.
27 # the API is so low-level it adds no clarity anyway
28 70 my @ai = @{ $am->[0] };
29 70 my $multi = $g->multiedged;
30 70 ▶ unless ($want_transitive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($want_transitive) 60 10 70
31 60 ▶ $dm = $m->distance_matrix || Graph::Matrix->new($g); # if no distance_matrix in AM, we make our own
Condition66%2/3
$m->distance_matrix || 'Graph::Matrix'->new($g)
l!l&&r!l&&!r
5910
MC/DC0%0/2
$m->distance_matrix || 'Graph::Matrix'->new($g)
$m->distance_matrix 'Graph::Matrix'->new($g)
Truth table66%2/3
A || B
A= $m->distance_matrixB= 'Graph::Matrix'->new($g)
ABresult
1X1
011
000
32 60 ▶ if ($want_path_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_path_count) { } 1 59 60
33 # force defined
34 1 @di = map [ (0) x @V ], 0..$#V;
35 } else {
36 59 @di = @{ $dm->[0] };
37 }
38 60 $sm = Graph::Matrix->new($g);
39 60 $dm->[1] = $sm->[1] = \%v2i;
40 60 @si = @{ $sm->[0] };
41 60 ▶ for (my $iu = $#V; $iu >= 0; $iu--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iu >= 0) 437 60 497
42 437 ▶ vec($ai[$iu], $iu, 1) = 1 if $want_reflexive;
Branch: 1 branch
BranchTrueFalseTotal
if $want_reflexive 425 12 437
43 437 ▶ for (my $iv = $#V; $iv >= 0; $iv--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iv >= 0) 17011 437 17448
44 17011 ▶ next unless vec($ai[$iu], $iv, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $ai[$iu], $iv, 1 15847 1164 17011
45 1164 ▶ if ($want_path_count or !defined $di[$iu][$iv]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want_path_count or not defined $di[$iu][$iv]) { } 231 933 1164
elsif ($multi and ref $di[$iu][$iv] eq 'HASH') { } 4 1854 1858
Condition100%3/3
$want_path_count or not defined $di[$iu][$iv]
l!l&&r!l&&!r
8223933
MC/DC100%2/2
$want_path_count or not defined $di[$iu][$iv]
$want_path_count not defined $di[$iu][$iv]
Truth table100%3/3
A or not(B)
A= $want_path_countB= not defined $di[$iu][$iv]
ABresult
1X1
011
000
46 231 ▶ $di[$iu][$iv] = $iu == $iv ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$iu == $iv ? : 5 226 231
47 ▶ } elsif ($multi and ref($di[$iu][$iv]) eq 'HASH') {
Condition100%3/3
$multi and ref $di[$iu][$iv] eq "HASH"
!ll&&!rl&&r
92544
MC/DC100%2/2
$multi and ref $di[$iu][$iv] eq "HASH"
$multi ref $di[$iu][$iv] eq "HASH"
Truth table100%3/3
A and B
A= $multiB= ref $di[$iu][$iv] eq "HASH"
ABresult
0X0
100
111
48 4 $di[$iu][$iv] = min values %{ $di[$iu][$iv] };
49 }
50 1164 ▶ $si[$iu]->[$iv] = $V[$iv] unless $iu == $iv;
Branch: 1 branch
BranchTrueFalseTotal
unless $iu == $iv 739 425 1164
51 }
52 }
53 }
54 # naming here is u = start, v = midpoint, w = endpoint
55 70 ▶ for (my $iv = $#V; $iv >= 0; $iv--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iv >= 0) 466 66 532
56 466 my $div = $di[$iv];
57 466 my $aiv = $ai[$iv];
58 466 ▶ for (my $iu = $#V; $iu >= 0; $iu--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iu >= 0) 17115 462 17577
59 17115 my $aiu = $ai[$iu];
60 17115 ▶ next if !vec($aiu, $iv, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $aiu, $iv, 1 15105 2010 17115
61 2010 ▶ if ($want_transitive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_transitive) 55 1955 2010
62 55 ▶ for (my $iw = $#V; $iw >= 0; $iw--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iw >= 0) 195 51 246
63 195 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $iw != $iv and vec $aiv, $iw, 1 and not vec($aiu, $iw, 1) 4 191 195
Condition100%6/6
$iw != $iv and vec $aiv, $iw, 1 and not vec($aiu, $iw, 1)
!ll&&!rl&&r
124674
$iw != $iv and vec $aiv, $iw, 1
!ll&&!rl&&r
527271
MC/DC100%3/3
$iw != $iv and vec $aiv, $iw, 1 and not vec($aiu, $iw, 1)
$iw != $iv vec $aiv, $iw, 1 not vec($aiu, $iw, 1)
Truth table100%4/4
A and B and not(C)
A= $iw != $ivB= vec $aiv, $iw, 1C= not vec($aiu, $iw, 1)
ABCresult
0XX0
10X0
1100
1111
64 if $iw != $iv &&
65 vec($aiv, $iw, 1) &&
66 !vec($aiu, $iw, 1);
67 }
68 51 next;
69 }
70 1955 my $aiuo = $aiu;
71 1955 $aiu |= $aiv;
72 1955 ▶ if ($aiu ne $aiuo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aiu ne $aiuo) 978 977 1955
73 978 $ai[$iu] = $aiu;
74 978 ▶ $aiv = $aiu if $iv == $iu;
Branch: 1 branch
BranchTrueFalseTotal
if $iv == $iu 0 978 978
75 }
76 1955 ▶ next if !$want_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_path 20 1935 1955
77 1935 my $diu = $di[$iu];
78 1935 my $d1a = $diu->[$iv];
79 1935 ▶ for (my $iw = $#V; $iw >= 0; $iw--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iw >= 0) 94495 1935 96430
80 94495 ▶ next unless vec($aiv, $iw, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $aiv, $iw, 1 66268 28227 94495
81 28227 ▶ if ($want_path_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_path_count) 13 28214 28227
82 13 ▶ $diu->[$iw]++ if $iu != $iv and $iv != $iw and $iw != $iu;
Branch: 1 branch
BranchTrueFalseTotal
if $iu != $iv and $iv != $iw and $iw != $iu 1 12 13
Condition83%5/6
$iu != $iv and $iv != $iw and $iw != $iu
!ll&&!rl&&r
1201
$iu != $iv and $iv != $iw
!ll&&!rl&&r
841
MC/DC66%2/3
$iu != $iv and $iv != $iw and $iw != $iu
$iu != $iv $iv != $iw $iw != $iu
Truth table75%3/4
A and B and C
A= $iu != $ivB= $iv != $iwC= $iw != $iu
ABCresult
0XX0
10X0
1100
1111
83 13 next;
84 }
85 28214 my $d0 = $diu->[$iw];
86 28214 my $d1b = $div->[$iw];
87 28214 my $d1 = $d1a + $d1b;
88 28214 ▶ if (!defined $d0 || ($d1 < $d0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $d0 or $d1 < $d0) 7336 20878 28214
Condition100%3/3
not defined $d0 or $d1 < $d0
l!l&&r!l&&!r
6036130020878
MC/DC100%2/2
not defined $d0 or $d1 < $d0
not defined $d0 $d1 < $d0
Truth table100%3/3
not(A) or B
A= not defined $d0B= $d1 < $d0
ABresult
1X1
011
000
89 # print "d1 = $d1a ($V[$iu], $V[$iv]) + $d1b ($V[$iv], $V[$iw]) = $d1 ($V[$iu], $V[$iw]) (".(defined$d0?$d0:"-").") (propagate=".($aiu ne $aiuo?1:0).")\n";
90 7336 $diu->[$iw] = $d1;
91 7336 ▶ $si[$iu]->[$iw] = $si[$iu]->[$iv]
Branch: 1 branch
BranchTrueFalseTotal
if $want_path_vertices 7335 1 7336
92 if $want_path_vertices;
93 }
94 }
95 }
96 }
97 66 ▶ return 1 if $want_transitive;
Branch: 1 branch
BranchTrueFalseTotal
if $want_transitive 6 60 66
98 60 my %V; @V{ @V } = @V;
99 60 $am->[0] = \@ai;
100 60 ▶ $dm->[0] = \@di if defined $dm;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dm 60 0 60
101 60 ▶ $sm->[0] = \@si if defined $sm;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sm 60 0 60
102 60 weaken(my $og = $g);
103 60 bless [ $am, $dm, $sm, \%V, $og ], $class;
104 }
105  
106 70 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
107 70 my ($class, $g, %opt) = @_;
108 70 my %am_opt = (distance_matrix => 1);
109 $am_opt{attribute_name} = delete $opt{attribute_name}
110 70 ▶ if exists $opt{attribute_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"attribute_name"} 1 69 70
111 $am_opt{distance_matrix} = delete $opt{distance_matrix}
112 70 ▶ if $opt{distance_matrix};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"distance_matrix"} 0 70 70
113 $opt{path_length} = $opt{path_vertices} = delete $opt{path}
114 70 ▶ if exists $opt{path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"path"} 29 41 70
115 70 my $want_path_length = delete $opt{path_length};
116 70 my $want_path_count = delete $opt{path_count};
117 70 my $want_path_vertices = delete $opt{path_vertices};
118 70 my $want_reflexive = delete $opt{reflexive};
119 $am_opt{is_transitive} = my $want_transitive = delete $opt{is_transitive}
120 70 ▶ if exists $opt{is_transitive};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"is_transitive"} 10 60 70
121 70 Graph::_opt_unknown(\%opt);
122 70 ▶ $want_reflexive = 1 unless defined $want_reflexive;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want_reflexive 10 60 70
123 70 ▶ my $want_path = $want_path_length || $want_path_vertices || $want_path_count;
Condition83%5/6
$want_path_length || $want_path_vertices || $want_path_count
l!l&&r!l&&!r
55015
$want_path_length or $want_path_vertices
l!l&&r!l&&!r
342115
MC/DC66%2/3
$want_path_length || $want_path_vertices || $want_path_count
$want_path_length $want_path_vertices $want_path_count
Truth table75%3/4
A or B || C
A= $want_path_lengthB= $want_path_verticesC= $want_path_count
ABCresult
1XX1
01X1
0011
0000
124 # $g->expect_dag if $want_path;
125 70 ▶ $am_opt{distance_matrix} = 0 if $want_path_count;
Branch: 1 branch
BranchTrueFalseTotal
if $want_path_count 1 69 70
126 70 _new($g, $class,
127 \%am_opt,
128 $want_transitive, $want_reflexive,
129 $want_path, $want_path_vertices, $want_path_count);
130 }
131  
132 17736 ▶sub has_vertices {
Subroutine has_vertices: called CC=3 SCAR=11.0
Pod documented
133 17736 my $tc = shift;
134 17736 for my $v (@_) {
135 35466 ▶ return 0 unless exists $tc->[ _V ]->{ $v };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tc->[3]{$v} 9 35457 35466
136 }
137 17727 return 1;
138 }
139  
140 14352 ▶sub is_reachable {
Subroutine is_reachable: called CC=3 SCAR=11.0
Pod documented
141 14352 my ($tc, $u, $v) = @_;
142 14352 ▶ return undef unless $tc->has_vertices($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->has_vertices($u, $v) 6 14346 14352
143 14346 ▶ return 1 if $u eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $u eq $v 866 13480 14346
144 13480 $tc->[ _A ]->get($u, $v);
145 }
146  
147 16 ▶sub is_transitive {
Subroutine is_transitive: called CC=3 SCAR=11.0
Pod documented
148 16 ▶ return __PACKAGE__->new($_[0], is_transitive => 1) if @_ == 1; # Any graph
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 10 6 16
149 # A TC graph
150 6 my ($tc, $u, $v) = @_;
151 6 ▶ return undef unless $tc->has_vertices($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->has_vertices($u, $v) 0 6 6
152 6 $tc->[ _A ]->get($u, $v);
153 }
154  
155 603 ▶sub vertices {
Subroutine vertices: called CC=1 SCAR=0.0
Pod documented
156 603 my $tc = shift;
157 603 values %{ $tc->[3] };
158 }
159  
160 3007 ▶sub path_length {
Subroutine path_length: called CC=3 SCAR=11.0
Pod documented
161 3007 my ($tc, $u, $v) = @_;
162 3007 ▶ return undef unless $tc->has_vertices($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->has_vertices($u, $v) 3 3004 3007
163 3004 ▶ return 0 if $u eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $u eq $v 256 2748 3004
164 2748 $tc->[ _D ]->get($u, $v);
165 }
166  
167 380 ▶sub path_successor {
Subroutine path_successor: called CC=3 SCAR=11.0
Pod documented
168 380 my ($tc, $u, $v) = @_;
169 380 ▶ return undef if $u eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $u eq $v 9 371 380
170 371 ▶ return undef unless $tc->has_vertices($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->has_vertices($u, $v) 0 371 371
171 371 $tc->[ _S ]->get($u, $v);
172 }
173  
174 267 ▶sub path_vertices {
Subroutine path_vertices: called CC=7 SCAR=19.5
Pod documented
175 267 my ($tc, $u, $v) = @_;
176 267 ▶ return unless $tc->is_reachable($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->is_reachable($u, $v) 15 252 267
177 252 ▶ return wantarray ? () : 0 if $u eq $v;
Branch: 2 branches
BranchTrueFalseTotal
if $u eq $v 45 207 252
wantarray ? : 40 5 45
178 207 my @v = ( $u );
179 207 ▶ while ($u ne $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u ne $v) 353 207 560
180 353 ▶ last unless defined($u = $tc->path_successor($u, $v));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($u = $tc->path_successor($u, $v)) 0 353 353
181 353 push @v, $u;
182 }
183 207 ▶ $tc->[ _S ]->set($u, $v, [ @v ]) if @v;
Branch: 1 branch
BranchTrueFalseTotal
if @v 207 0 207
184 207 return @v;
185 }
186  
187 29 ▶sub all_paths {
Subroutine all_paths: called CC=6 SCAR=17.9
Pod documented
188 29 my ($tc, $u, $v, $seen) = @_;
189 29 ▶ return if $u eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $u eq $v 0 29 29
190 29 ▶ $seen ||= {};
Condition100%2/2
$seen ||= {}
l!l
425
MC/DC100%1/1
$seen ||= {}
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
191 29 ▶ return if exists $seen->{$u};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen->{$u} 1 28 29
192 28 $seen = { %$seen, $u => undef }; # accumulate, but don't mutate
193 28 my @found;
194 28 ▶ push @found, [$u, $v] if $tc->[ _G ]->has_edge($u, $v);
Branch: 1 branch
BranchTrueFalseTotal
if $tc->[4]->has_edge($u, $v) 12 16 28
195 28 ▶ push @found,
Condition100%3/3
$_ ne $v && $_ ne $u
!ll&&!rl&&r
12318
MC/DC100%2/2
$_ ne $v && $_ ne $u
$_ ne $v $_ ne $u
Truth table100%3/3
A && B
A= $_ ne $vB= $_ ne $u
ABresult
0X0
100
111
196 map [$u, @$_],
197 map $tc->all_paths($_, $v, $seen),
198 grep $tc->is_reachable($_, $v),
199 grep $_ ne $v && $_ ne $u, $tc->[ _G ]->successors($u);
200 28 @found;
201 }
202  
203 6 1;
204 __END__
↑ Summary Graph/Traversal.pm »