MostMaximumMatchingsTree.pm

filter: Statementstmt 94.1% 162 / 172 Branchbran 91.1% 62 / 68 Conditioncond 78.9% 15 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 91.4% 266 / 291 CC 45 SCAR 28.9
CC
45
Cov
92%
init
32.8
_rat_to_xy
6.0
__ANON__
3.1
CRAP 17.9
« Hanoi.pm ↑ Summary KnightGrid.pm »
1 # Copyright 2019, 2020, 2021 Kevin Ryde
2 #
3 # This file is part of Graph-Maker-Other.
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Graph-Maker-Other. See the file COPYING. If not, see
17 # <http://www.gnu.org/licenses/>.
18  
19 package Graph::Maker::MostMaximumMatchingsTree;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 $VERSION = 19;
27 1 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 57 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 57 require Graph;
35 57 Graph->new(@_);
36 }
37 57 ▶sub _make_graph {
Subroutine _make_graph: called CC=2 SCAR=7.1
38 57 my ($params) = @_;
39 57 ▶ my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::MostMaximumMatchingsTree::_default_graph_maker
l!l
057
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::MostMaximumMatchingsTree::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
Aresult
11
00
40 57 return $graph_maker->(%$params);
41 }
42  
43 57 ▶sub init {
Subroutine init: called CC=32 SCAR=34.9
Pod undocumented
44 57 my ($self, %params) = @_;
45 57 ▶ my $coordinate_type = delete($params{'coordinate_type'}) || '';
Condition50%1/2
delete $params{'coordinate_type'} || ''
l!l
057
MC/DC0%0/1
delete $params{'coordinate_type'} || ''
delete $params{'coordinate_type'}
Truth table50%1/2
A
A= delete $params{'coordinate_type'}
Aresult
11
00
46 57 my $N = delete($params{'N'});
47 57 my $graph = _make_graph(\%params);
48 57 $graph->set_graph_attribute (name => "Most Maximum Matchings Tree, N=$N");
49  
50 57 ▶ my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_directed ? : 51 6 57
51  
52 # Add a new vertex attached to $p and located at offset $dx,$dy.
53 # Return the new vertex number.
54 57 my $upto = 1;
55 1240 ▶ my $add = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
56 1240 my ($p, $dx,$dy) = @_;
57 ### add: "parent $p at ".$graph->get_vertex_attribute($p,'x').' '.$graph->get_vertex_attribute($p,'y')." dxdy $dx $dy"
58 1240 my $v = $upto++;
59 1240 my $x = 0;
60 1240 my $y = 0;
61 1240 ▶ if (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 1240 0 1240
62 1240 $graph->$add_edge ($p,$v);
63 1240 ▶ if ($coordinate_type eq 'HW') {
Branch: 1 branch
BranchTrueFalseTotal
if ($coordinate_type eq "HW") 0 1240 1240
64 0 $x = $graph->get_vertex_attribute($p,'x') + $dx;
65 0 $y = $graph->get_vertex_attribute($p,'y') + $dy;
66 }
67 }
68 1240 $graph->set_vertex_attribute($v, x => $x);
69 1240 $graph->set_vertex_attribute($v, y => $y);
70 1240 return $v;
71 57 };
72  
73 57 my $s = 1;
74 123 ▶ my $one_C = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
75 123 my ($p, $dx,$dy) = @_;
76 123 $p = $add->($p, $s*$dx,$s*$dy); # middle
77 123 $s = 1;
78 123 ▶ my $hx = ($dx ? 0 : -1); # left or down
Branch: 1 branch
BranchTrueFalseTotal
$dx ? : 84 39 123
79 123 ▶ my $hy = ($dx ? -1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$dx ? : 84 39 123
80 123 $add->($p, -$hx,-$hy); # right
81 {
82 123 my $t = $add->($p, $hx,$hy); # left
83 123 $t = $add->($t, $hx,$hy);
84 123 $add->($t, $hx-$dx/2, $hy-$dy/2);
85 123 $add->($t, $hx+$dx/2, $hy+$dy/2);
86 }
87 123 return $add->($p, $dx,$dy); # below
88 57 };
89  
90 122 ▶ my $chain_C = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
91 122 my ($p, $len, $dx,$dy) = @_;
92 ### chain_C: "dxdy=$dx,$dy s=$s"
93 122 foreach (1 .. $len) {
94 123 $p = $one_C->($p, $dx,$dy);
95 }
96 122 return $p;
97 57 };
98 # add a chain of $len many C and final L (which is a single vertex),
99 # starting from vertex $p
100 109 ▶ my $CL = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 109 my ($p, $len, $dx,$dy) = @_;
102 109 $p = $add->($p, $s*$dx,$s*$dy); # between
103 109 $s = 1;
104 109 return $chain_C->($p, $len, $dx,$dy);
105 57 };
106  
107 # add a fork F attached to vertex $p
108 59 ▶ my $F = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 59 my ($p, $dx,$dy) = @_;
110 59 $p = $add->($p, $dx,$dy);
111 59 $add->($p, $dx+$dy/2, $dy-$dx/2);
112 59 $add->($p, $dx-$dy/2, $dy+$dx/2);
113 57 };
114  
115 # add a chain of $len many C and final F,
116 # starting from attached to vertex $p
117 51 ▶ my $CF = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 51 my ($p, $len, $dx,$dy, $s) = @_;
119 51 $p = $CL->($p, $len, $dx,$dy, $s);
120 51 $F->($p, $dx,$dy);
121 57 };
122  
123 57 ▶ if ($N) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N) 55 2 57
124 55 my $m = $upto++;
125 55 $graph->add_vertex($m);
126 55 $graph->set_vertex_attribute($m, x => 0);
127 55 $graph->set_vertex_attribute($m, y => 0);
128 55 my $N7 = $N % 7;
129  
130 55 ▶ if ($N <= 6) {
Branch: 11 branches
BranchTrueFalseTotal
if ($N <= 6) { } 11 44 55
elsif ($N eq '6.5') { } 0 44 44
elsif ($N == 10) { } 1 43 44
elsif ($N7 == 6 and $N <= 20) { } 2 41 43
elsif ($N7 == 1) { } 7 34 41
elsif ($N7 == 2) { } 6 28 34
elsif ($N7 == 3) { } 5 23 28
elsif ($N7 == 4) { } 6 17 23
elsif ($N7 == 5) { } 6 11 17
elsif ($N == 34.5 or $N7 == 6) { } 4 7 11
elsif ($N7 == 0) { } 7 0 7
131 # star figure 5(a) T6,1 star-6
132 # other N=0 to 5 also star
133 11 ▶ if ($N >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N >= 2) 10 1 11
134 10 foreach my $i (0 .. $N-2) {
135 30 $add->($m, _rat_to_xy($i, $N-1));
136 }
137 }
138  
139 } elsif ($N eq '6.5') {
140 # * figure 5(b) T6,2
141 # | different from F ends of 6 mod 7
142 # m--*--*--*
143 # |
144 # *
145 0 $m = $add->($m, 1,0);
146 0 $m = $add->($m, 1,0);
147 0 $m = $add->($m, 1,0);
148 0 $add->($m, 0, 1);
149 0 $add->($m, 0, -1);
150  
151 } elsif ($N == 10) {
152 # L figure 5(c) N = 10
153 # | different from N==3 mod 7
154 # F -- m -- F
155 1 $F->($add->($m,-1,0), -1,0);
156 1 $F->($add->($m, 1,0), 1,0);
157 1 $add->($m, 0,1); # top L
158  
159 ▶ } elsif ($N7 == 6 && $N <= 20) {
Condition100%3/3
$N7 == 6 and $N <= 20
!ll&&!rl&&r
3742
MC/DC100%2/2
$N7 == 6 and $N <= 20
$N7 == 6 $N <= 20
Truth table100%3/3
A and B
A= $N7 == 6B= $N <= 20
ABresult
0X0
100
111
160 # * * * or CL figure 5(d) T13
161 # | | | figure 5(e) T20
162 # m--*--*--*--*--*--* different from F ends of 6 mod 7
163 # | | |
164 # * * *
165 2 foreach my $i (1 .. 3) {
166 6 $m = $add->($m, 1,0); # right to centre
167 6 $add->($m, 0,-1); # down
168 6 ▶ $CL->($m, $i==3 && $N==20, 0,1); # up L or CL
Condition100%3/3
$i == 3 && $N == 20
!ll&&!rl&&r
411
MC/DC100%2/2
$i == 3 && $N == 20
$i == 3 $N == 20
Truth table100%3/3
A && B
A= $i == 3B= $N == 20
ABresult
0X0
100
111
169 6 $m = $add->($m, 1,0); # go right
170 }
171  
172 #===================
173 # Now general cases:
174  
175 } elsif ($N7 == 1) {
176 # CL theorem 3.3 case 1 1,8,15,22,29
177 7 $chain_C->($m, int(($N-1)/7), 1,0);
178  
179 } elsif ($N7 == 2) {
180 # C3L C4L figure 4(a) N = 2 mod 7
181 # | | 9,16,23,30,37,44,51,72,107
182 # L -- m -- C0 -- r -- L
183 # | |
184 # C1L C2L
185 # GP-Test my(n=30,j=1); (n-9+7*j)\35 == 0
186 # GP-Test my(n=30,j=4); (n-9+7*j)\35 == 1
187 6 ▶ my $k = sub { my($j)=@_; int(($N - ($N>=37 ? 2:9) + 7*$j)/35); };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$N >= 37 ? : 12 24 36
188 6 ▶ if ($k->(3)) { $s = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(3)) 4 2 6
189 6 ▶ my $r = $CL->($m, $N<37 ? 0 : int(($N-37)/35), # C0 across to r
Branch: 1 branch
BranchTrueFalseTotal
$N < 37 ? : 4 2 6
190 1,0);
191 6 ▶ $r = $add->($r, $k->(4)==0 ? 1 : 4, 0);
Branch: 1 branch
BranchTrueFalseTotal
&$k(4) == 0 ? : 1 5 6
192 6 $CL->($m, $k->(1), 0,-1); # C1L
193 6 $CL->($r, $k->(2), 0,-1); # C2L
194 6 $CL->($m, $k->(3), 0,1);
195 6 $CL->($r, $k->(4), 0,1);
196 6 $add->($m, -1,0); # left L most
197 6 $add->($r, 1,0); # right L
198  
199 } elsif ($N7 == 3) {
200 # C2F figure 4(b) N == 3 mod 7 10,17,24,31,73
201 # |
202 # C1F -- m -- C3F
203 # |
204 # C0F
205 5 ▶ my $k = sub { my($j)=@_; int(($N - 17 + 7*$j)/28); };
Subroutine __ANON__: called CC=1 SCAR=0.0
206 5 $CF->($m, $k->(0), 0,-1);
207 5 ▶ if ($k->(2)) { $s = 4; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(2)) 3 2 5
208 5 $CF->($m, $k->(1), -1,0); # left C1F
209 5 $CF->($m, $k->(2), 0,1);
210 5 ▶ if ($k->(2)) { $s = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(2)) 3 2 5
211 5 $CF->($m, $k->(3), 1,0); # right C3F
212  
213 } elsif ($N7 == 4) {
214 # CF theorem 3.3 case 4 N == 4 mod 7 74
215 6 $m = $chain_C->($m, int(($N-4)/7), 1,0);
216 6 $F->($m, 1,0);
217  
218 } elsif ($N7 == 5) {
219 # C2L figure 4(c) N == 5 mod 7 75
220 # |
221 # C1L -- m -- L
222 # |
223 # C0L
224 6 ▶ my $k = sub { my($j)=@_; int(($N-5+7*$j)/21); };
Subroutine __ANON__: called CC=1 SCAR=0.0
225 6 $CL->($m, $k->(0), 0,-1);
226 6 ▶ if ($k->(1) && $k->(2)) { $s = 4; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(1) and &$k(2)) 5 1 6
Condition66%2/3
&$k(1) and &$k(2)
!ll&&!rl&&r
105
MC/DC50%1/2
&$k(1) and &$k(2)
&$k(1) &$k(2)
Truth table66%2/3
A and B
A= &$k(1)B= &$k(2)
ABresult
0X0
100
111
227 6 $CL->($m, $k->(1), -1,0);
228 6 $CL->($m, $k->(2), 0,1);
229 6 $add->($m, 1,0); # right L
230  
231 ▶ } elsif ($N==34.5 || $N7 == 6) {
Condition66%2/3
$N == "34.5" or $N7 == 6
l!l&&r!l&&!r
047
MC/DC50%1/2
$N == "34.5" or $N7 == 6
$N == "34.5" $N7 == 6
Truth table66%2/3
A or B
A= $N == "34.5"B= $N7 == 6
ABresult
1X1
011
000
232 # C5F C6F figure 4(d) N == 6 mod 7
233 # | | and N >= 27
234 # C3F -- m -- C0 -- r -- C4F 76
235 # | |
236 # C1F C2F
237 44 ▶ my $k = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
238 44 my($j)=@_;
239 0 ▶ if ($N==34.5) { return $j==0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($N == "34.5") 0 44 44
240 44 int(($N - 27 + 7*$j)/49);
241 4 };
242 4 ▶ if ($k->(5)) { $s = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(5)) 2 2 4
243 4 my $r = $CL->($m, $k->(0), 1,0);
244 4 ▶ $r = $add->($r, $k->(6)==0 ? 1 : 4, 0);
Branch: 1 branch
BranchTrueFalseTotal
&$k(6) == 0 ? : 1 3 4
245 4 $CF->($m, $k->(1), 0,-1);
246 4 $CF->($r, $k->(2), 0,-1);
247 4 ▶ if ($k->(5)) { $s = 4; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(5)) 2 2 4
248 4 $CF->($m, $k->(3), -1,0);
249 0 ▶ if ($k->(2)) { $s = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (&$k(2)) 0 4 4
250 4 $CF->($r, $k->(4), 1,0);
251 4 $CF->($m, $k->(5), 0,1);
252 4 $CF->($r, $k->(6), 0,1);
253  
254 } elsif ($N7 == 0) {
255 # L figure 4(e) N == 0 mod 7
256 # |
257 # m -- CF
258 # |
259 # L
260 7 $add->($m, 0,1);
261 7 $add->($m, 0,-1);
262 7 $CF->($m, int(($N-7)/7), 1,0);
263  
264 } else {
265 0 croak "Unrecognised N=",$N;
266 }
267 }
268 57 return $graph;
269 }
270  
271 # $num/$den is a rational fraction of a 360 degree circle.
272 # Return a pair $x,$y of coordinates for the point at that angle and unit
273 # distance.
274 # $den==2 or 4 are forced to exact values where floating point would
275 # otherwise round-off 90 degrees or 180 degrees in radians to something not
276 # quite exact.
277 # $num==0 which is $a==0 comes out exact from sin() and cos() already.
278 #
279 30 ▶sub _rat_to_xy {
Subroutine _rat_to_xy: called CC=6 SCAR=17.9
280 30 my ($num,$den) = @_;
281 ### _rat_to_xy(): "$num $den"
282 30 ▶ if (4*$num == $den) { return (0,1); } # 90 degrees
Branch: 1 branch
BranchTrueFalseTotal
if (4 * $num == $den) 2 28 30
283 28 ▶ if (4*$num == 2*$den) { return (-1,0); } # 180 degrees
Branch: 1 branch
BranchTrueFalseTotal
if (4 * $num == 2 * $den) 4 24 28
284 24 ▶ if (4*$num == 3*$den) { return (0,-1); } # 270 degrees
Branch: 1 branch
BranchTrueFalseTotal
if (4 * $num == 3 * $den) 2 22 24
285 22 my $a = 6.283185 * $num / $den;
286 22 my $x = cos($a);
287 22 my $y = sin($a);
288 22 ▶ if (3*$num == $den || 3*$num == 2*$den) { $x = -.5; } # 120,240 degrees
Branch: 1 branch
BranchTrueFalseTotal
if (3 * $num == $den or 3 * $num == 2 * $den) 4 18 22
Condition100%3/3
3 * $num == $den or 3 * $num == 2 * $den
l!l&&r!l&&!r
2218
MC/DC100%2/2
3 * $num == $den or 3 * $num == 2 * $den
3 * $num == $den 3 * $num == 2 * $den
Truth table100%3/3
A or B
A= 3 * $num == $denB= 3 * $num == 2 * $den
ABresult
1X1
011
000
289 22 return ($x,$y);
290 }
291  
292 1 Graph::Maker->add_factory_type('most_maximum_matchings_tree' => __PACKAGE__);
293 1 1;
294  
295 __END__
« Hanoi.pm ↑ Summary KnightGrid.pm »