line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Graph::TransitiveClosure::Matrix; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
38
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
158
|
|
4
|
6
|
|
|
6
|
|
26
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
137
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
2504
|
use Graph::AdjacencyMatrix; |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
153
|
|
7
|
6
|
|
|
6
|
|
35
|
use Graph::Matrix; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
112
|
|
8
|
6
|
|
|
6
|
|
28
|
use Scalar::Util qw(weaken); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
399
|
|
9
|
6
|
|
|
6
|
|
35
|
use List::Util qw(min); |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
8199
|
|
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
|
|
|
|
|
|
|
sub _new { |
18
|
70
|
|
|
70
|
|
213
|
my ($g, $class, $am_opt, $want_transitive, $want_reflexive, $want_path, $want_path_vertices, $want_path_count) = @_; |
19
|
70
|
|
|
|
|
451
|
my $m = Graph::AdjacencyMatrix->new($g, %$am_opt); |
20
|
70
|
|
|
|
|
233
|
my @V = $g->vertices; |
21
|
70
|
|
|
|
|
139
|
my %v2i; @v2i{ @V } = 0..$#V; # paths are in array -> stable ordering |
|
70
|
|
|
|
|
378
|
|
22
|
70
|
|
|
|
|
274
|
my $am = $m->adjacency_matrix; |
23
|
70
|
|
|
|
|
229
|
$am->[1] = \%v2i; |
24
|
70
|
|
|
|
|
239
|
my ($dm, @di); # The distance matrix. |
25
|
70
|
|
|
|
|
0
|
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
|
|
|
|
|
134
|
my @ai = @{ $am->[0] }; |
|
70
|
|
|
|
|
341
|
|
29
|
70
|
|
|
|
|
206
|
my $multi = $g->multiedged; |
30
|
70
|
100
|
|
|
|
208
|
unless ($want_transitive) { |
31
|
60
|
|
66
|
|
|
184
|
$dm = $m->distance_matrix || Graph::Matrix->new($g); # if no distance_matrix in AM, we make our own |
32
|
60
|
100
|
|
|
|
151
|
if ($want_path_count) { |
33
|
|
|
|
|
|
|
# force defined |
34
|
1
|
|
|
|
|
6
|
@di = map [ (0) x @V ], 0..$#V; |
35
|
|
|
|
|
|
|
} else { |
36
|
59
|
|
|
|
|
91
|
@di = @{ $dm->[0] }; |
|
59
|
|
|
|
|
171
|
|
37
|
|
|
|
|
|
|
} |
38
|
60
|
|
|
|
|
491
|
$sm = Graph::Matrix->new($g); |
39
|
60
|
|
|
|
|
246
|
$dm->[1] = $sm->[1] = \%v2i; |
40
|
60
|
|
|
|
|
105
|
@si = @{ $sm->[0] }; |
|
60
|
|
|
|
|
138
|
|
41
|
60
|
|
|
|
|
271
|
for (my $iu = $#V; $iu >= 0; $iu--) { |
42
|
430
|
100
|
|
|
|
1010
|
vec($ai[$iu], $iu, 1) = 1 if $want_reflexive; |
43
|
430
|
|
|
|
|
950
|
for (my $iv = $#V; $iv >= 0; $iv--) { |
44
|
16102
|
100
|
|
|
|
28194
|
next unless vec($ai[$iu], $iv, 1); |
45
|
1150
|
100
|
100
|
|
|
3394
|
if ($want_path_count or !defined $di[$iu][$iv]) { |
|
|
100
|
100
|
|
|
|
|
46
|
231
|
100
|
|
|
|
361
|
$di[$iu][$iv] = $iu == $iv ? 0 : 1; |
47
|
|
|
|
|
|
|
} elsif ($multi and ref($di[$iu][$iv]) eq 'HASH') { |
48
|
4
|
|
|
|
|
6
|
$di[$iu][$iv] = min values %{ $di[$iu][$iv] }; |
|
4
|
|
|
|
|
19
|
|
49
|
|
|
|
|
|
|
} |
50
|
1150
|
100
|
|
|
|
2655
|
$si[$iu]->[$iv] = $V[$iv] unless $iu == $iv; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
# naming here is u = start, v = midpoint, w = endpoint |
55
|
70
|
|
|
|
|
226
|
for (my $iv = $#V; $iv >= 0; $iv--) { |
56
|
459
|
|
|
|
|
646
|
my $div = $di[$iv]; |
57
|
459
|
|
|
|
|
569
|
my $aiv = $ai[$iv]; |
58
|
459
|
|
|
|
|
830
|
for (my $iu = $#V; $iu >= 0; $iu--) { |
59
|
16206
|
|
|
|
|
19015
|
my $aiu = $ai[$iu]; |
60
|
16206
|
100
|
|
|
|
31080
|
next if !vec($aiu, $iv, 1); |
61
|
1920
|
100
|
|
|
|
2655
|
if ($want_transitive) { |
62
|
55
|
|
|
|
|
97
|
for (my $iw = $#V; $iw >= 0; $iw--) { |
63
|
195
|
100
|
100
|
|
|
578
|
return 0 |
|
|
|
100
|
|
|
|
|
64
|
|
|
|
|
|
|
if $iw != $iv && |
65
|
|
|
|
|
|
|
vec($aiv, $iw, 1) && |
66
|
|
|
|
|
|
|
!vec($aiu, $iw, 1); |
67
|
|
|
|
|
|
|
} |
68
|
51
|
|
|
|
|
91
|
next; |
69
|
|
|
|
|
|
|
} |
70
|
1865
|
|
|
|
|
2028
|
my $aiuo = $aiu; |
71
|
1865
|
|
|
|
|
2294
|
$aiu |= $aiv; |
72
|
1865
|
100
|
|
|
|
2748
|
if ($aiu ne $aiuo) { |
73
|
905
|
|
|
|
|
1123
|
$ai[$iu] = $aiu; |
74
|
905
|
50
|
|
|
|
1264
|
$aiv = $aiu if $iv == $iu; |
75
|
|
|
|
|
|
|
} |
76
|
1865
|
100
|
|
|
|
2570
|
next if !$want_path; |
77
|
1845
|
|
|
|
|
2093
|
my $diu = $di[$iu]; |
78
|
1845
|
|
|
|
|
2197
|
my $d1a = $diu->[$iv]; |
79
|
1845
|
|
|
|
|
3120
|
for (my $iw = $#V; $iw >= 0; $iw--) { |
80
|
83136
|
100
|
|
|
|
143525
|
next unless vec($aiv, $iw, 1); |
81
|
26494
|
100
|
|
|
|
34836
|
if ($want_path_count) { |
82
|
13
|
100
|
100
|
|
|
35
|
$diu->[$iw]++ if $iu != $iv and $iv != $iw and $iw != $iu; |
|
|
|
66
|
|
|
|
|
83
|
13
|
|
|
|
|
21
|
next; |
84
|
|
|
|
|
|
|
} |
85
|
26481
|
|
|
|
|
29374
|
my $d0 = $diu->[$iw]; |
86
|
26481
|
|
|
|
|
28948
|
my $d1b = $div->[$iw]; |
87
|
26481
|
|
|
|
|
27956
|
my $d1 = $d1a + $d1b; |
88
|
26481
|
100
|
100
|
|
|
63046
|
if (!defined $d0 || ($d1 < $d0)) { |
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
|
6961
|
|
|
|
|
8143
|
$diu->[$iw] = $d1; |
91
|
6961
|
100
|
|
|
|
16090
|
$si[$iu]->[$iw] = $si[$iu]->[$iv] |
92
|
|
|
|
|
|
|
if $want_path_vertices; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
66
|
100
|
|
|
|
257
|
return 1 if $want_transitive; |
98
|
60
|
|
|
|
|
99
|
my %V; @V{ @V } = @V; |
|
60
|
|
|
|
|
503
|
|
99
|
60
|
|
|
|
|
214
|
$am->[0] = \@ai; |
100
|
60
|
50
|
|
|
|
222
|
$dm->[0] = \@di if defined $dm; |
101
|
60
|
50
|
|
|
|
203
|
$sm->[0] = \@si if defined $sm; |
102
|
60
|
|
|
|
|
292
|
weaken(my $og = $g); |
103
|
60
|
|
|
|
|
923
|
bless [ $am, $dm, $sm, \%V, $og ], $class; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub new { |
107
|
70
|
|
|
70
|
1
|
288
|
my ($class, $g, %opt) = @_; |
108
|
70
|
|
|
|
|
208
|
my %am_opt = (distance_matrix => 1); |
109
|
|
|
|
|
|
|
$am_opt{attribute_name} = delete $opt{attribute_name} |
110
|
70
|
100
|
|
|
|
198
|
if exists $opt{attribute_name}; |
111
|
|
|
|
|
|
|
$am_opt{distance_matrix} = delete $opt{distance_matrix} |
112
|
70
|
50
|
|
|
|
173
|
if $opt{distance_matrix}; |
113
|
|
|
|
|
|
|
$opt{path_length} = $opt{path_vertices} = delete $opt{path} |
114
|
70
|
100
|
|
|
|
223
|
if exists $opt{path}; |
115
|
70
|
|
|
|
|
133
|
my $want_path_length = delete $opt{path_length}; |
116
|
70
|
|
|
|
|
125
|
my $want_path_count = delete $opt{path_count}; |
117
|
70
|
|
|
|
|
153
|
my $want_path_vertices = delete $opt{path_vertices}; |
118
|
70
|
|
|
|
|
132
|
my $want_reflexive = delete $opt{reflexive}; |
119
|
|
|
|
|
|
|
$am_opt{is_transitive} = my $want_transitive = delete $opt{is_transitive} |
120
|
70
|
100
|
|
|
|
175
|
if exists $opt{is_transitive}; |
121
|
70
|
|
|
|
|
227
|
Graph::_opt_unknown(\%opt); |
122
|
70
|
100
|
|
|
|
177
|
$want_reflexive = 1 unless defined $want_reflexive; |
123
|
70
|
|
66
|
|
|
296
|
my $want_path = $want_path_length || $want_path_vertices || $want_path_count; |
124
|
|
|
|
|
|
|
# $g->expect_dag if $want_path; |
125
|
70
|
100
|
|
|
|
156
|
$am_opt{distance_matrix} = 0 if $want_path_count; |
126
|
70
|
|
|
|
|
237
|
_new($g, $class, |
127
|
|
|
|
|
|
|
\%am_opt, |
128
|
|
|
|
|
|
|
$want_transitive, $want_reflexive, |
129
|
|
|
|
|
|
|
$want_path, $want_path_vertices, $want_path_count); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub has_vertices { |
133
|
17271
|
|
|
17271
|
1
|
19564
|
my $tc = shift; |
134
|
17271
|
|
|
|
|
23181
|
for my $v (@_) { |
135
|
34536
|
100
|
|
|
|
60002
|
return 0 unless exists $tc->[ _V ]->{ $v }; |
136
|
|
|
|
|
|
|
} |
137
|
17262
|
|
|
|
|
27129
|
return 1; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub is_reachable { |
141
|
13887
|
|
|
13887
|
1
|
22049
|
my ($tc, $u, $v) = @_; |
142
|
13887
|
100
|
|
|
|
18715
|
return undef unless $tc->has_vertices($u, $v); |
143
|
13881
|
100
|
|
|
|
22794
|
return 1 if $u eq $v; |
144
|
13036
|
|
|
|
|
24011
|
$tc->[ _A ]->get($u, $v); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub is_transitive { |
148
|
16
|
100
|
|
16
|
1
|
92
|
return __PACKAGE__->new($_[0], is_transitive => 1) if @_ == 1; # Any graph |
149
|
|
|
|
|
|
|
# A TC graph |
150
|
6
|
|
|
|
|
14
|
my ($tc, $u, $v) = @_; |
151
|
6
|
50
|
|
|
|
17
|
return undef unless $tc->has_vertices($u, $v); |
152
|
6
|
|
|
|
|
27
|
$tc->[ _A ]->get($u, $v); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub vertices { |
156
|
596
|
|
|
596
|
1
|
715
|
my $tc = shift; |
157
|
596
|
|
|
|
|
644
|
values %{ $tc->[3] }; |
|
596
|
|
|
|
|
3261
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub path_length { |
161
|
3007
|
|
|
3007
|
1
|
4202
|
my ($tc, $u, $v) = @_; |
162
|
3007
|
100
|
|
|
|
4538
|
return undef unless $tc->has_vertices($u, $v); |
163
|
3004
|
100
|
|
|
|
4915
|
return 0 if $u eq $v; |
164
|
2748
|
|
|
|
|
4826
|
$tc->[ _D ]->get($u, $v); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub path_successor { |
168
|
380
|
|
|
380
|
1
|
613
|
my ($tc, $u, $v) = @_; |
169
|
380
|
100
|
|
|
|
678
|
return undef if $u eq $v; |
170
|
371
|
50
|
|
|
|
600
|
return undef unless $tc->has_vertices($u, $v); |
171
|
371
|
|
|
|
|
833
|
$tc->[ _S ]->get($u, $v); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub path_vertices { |
175
|
267
|
|
|
267
|
1
|
568
|
my ($tc, $u, $v) = @_; |
176
|
267
|
100
|
|
|
|
551
|
return unless $tc->is_reachable($u, $v); |
177
|
252
|
100
|
|
|
|
723
|
return wantarray ? () : 0 if $u eq $v; |
|
|
100
|
|
|
|
|
|
178
|
207
|
|
|
|
|
391
|
my @v = ( $u ); |
179
|
207
|
|
|
|
|
419
|
while ($u ne $v) { |
180
|
353
|
50
|
|
|
|
640
|
last unless defined($u = $tc->path_successor($u, $v)); |
181
|
353
|
|
|
|
|
784
|
push @v, $u; |
182
|
|
|
|
|
|
|
} |
183
|
207
|
50
|
|
|
|
830
|
$tc->[ _S ]->set($u, $v, [ @v ]) if @v; |
184
|
207
|
|
|
|
|
923
|
return @v; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub all_paths { |
188
|
29
|
|
|
29
|
1
|
72
|
my ($tc, $u, $v, $seen) = @_; |
189
|
29
|
50
|
|
|
|
75
|
return if $u eq $v; |
190
|
29
|
|
100
|
|
|
119
|
$seen ||= {}; |
191
|
29
|
100
|
|
|
|
69
|
return if exists $seen->{$u}; |
192
|
28
|
|
|
|
|
104
|
$seen = { %$seen, $u => undef }; # accumulate, but don't mutate |
193
|
28
|
|
|
|
|
36
|
my @found; |
194
|
28
|
100
|
|
|
|
107
|
push @found, [$u, $v] if $tc->[ _G ]->has_edge($u, $v); |
195
|
28
|
|
100
|
|
|
102
|
push @found, |
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
|
|
|
|
|
133
|
@found; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
1; |
204
|
|
|
|
|
|
|
__END__ |