| 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 | use 5.004; | ||
| 21 | use strict; | ||
| 22 | use Carp 'croak'; | ||
| 23 | use Graph::Maker; | ||
| 24 | |||
| 25 | use vars '$VERSION','@ISA'; | ||
| 26 | $VERSION = 16; | ||
| 27 | @ISA = ('Graph::Maker'); | ||
| 28 | |||
| 29 | # uncomment this to run the ### lines | ||
| 30 | # use Smart::Comments; | ||
| 31 | |||
| 32 | |||
| 33 | sub _default_graph_maker { | ||
| 34 | require Graph; | ||
| 35 | Graph->new(@_); | ||
| 36 | } | ||
| 37 | sub _make_graph { | ||
| 38 | my ($params) = @_; | ||
| 39 | my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker; | ||
| 40 | return $graph_maker->(%$params); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub init { | ||
| 44 | my ($self, %params) = @_; | ||
| 45 | my $coordinate_type = delete($params{'coordinate_type'}) || ''; | ||
| 46 | my $N = delete($params{'N'}); | ||
| 47 | my $graph = _make_graph(\%params); | ||
| 48 | $graph->set_graph_attribute (name => "Most Maximum Matchings Tree, N=$N"); | ||
| 49 | |||
| 50 | my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge'); | ||
| 51 | |||
| 52 | # Add a new vertex attached to $p and located at offset $dx,$dy. | ||
| 53 | # Return the new vertex number. | ||
| 54 | my $upto = 1; | ||
| 55 | my $add = sub { | ||
| 56 | 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 | my $v = $upto++; | ||
| 59 | my $x = 0; | ||
| 60 | my $y = 0; | ||
| 61 | if (defined $p) { | ||
| 62 | $graph->$add_edge ($p,$v); | ||
| 63 | if ($coordinate_type eq 'HW') { | ||
| 64 | $x = $graph->get_vertex_attribute($p,'x') + $dx; | ||
| 65 | $y = $graph->get_vertex_attribute($p,'y') + $dy; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | $graph->set_vertex_attribute($v, x => $x); | ||
| 69 | $graph->set_vertex_attribute($v, y => $y); | ||
| 70 | return $v; | ||
| 71 | }; | ||
| 72 | |||
| 73 | my $s = 1; | ||
| 74 | my $one_C = sub { | ||
| 75 | my ($p, $dx,$dy) = @_; | ||
| 76 | $p = $add->($p, $s*$dx,$s*$dy); # middle | ||
| 77 | $s = 1; | ||
| 78 | my $hx = ($dx ? 0 : -1); # left or down | ||
| 79 | my $hy = ($dx ? -1 : 0); | ||
| 80 | $add->($p, -$hx,-$hy); # right | ||
| 81 | { | ||
| 82 | my $t = $add->($p, $hx,$hy); # left | ||
| 83 | $t = $add->($t, $hx,$hy); | ||
| 84 | $add->($t, $hx-$dx/2, $hy-$dy/2); | ||
| 85 | $add->($t, $hx+$dx/2, $hy+$dy/2); | ||
| 86 | } | ||
| 87 | return $add->($p, $dx,$dy); # below | ||
| 88 | }; | ||
| 89 | |||
| 90 | my $chain_C = sub { | ||
| 91 | my ($p, $len, $dx,$dy) = @_; | ||
| 92 | ### chain_C: "dxdy=$dx,$dy s=$s" | ||
| 93 | foreach (1 .. $len) { | ||
| 94 | $p = $one_C->($p, $dx,$dy); | ||
| 95 | } | ||
| 96 | return $p; | ||
| 97 | }; | ||
| 98 | # add a chain of $len many C and final L (which is a single vertex), | ||
| 99 | # starting from vertex $p | ||
| 100 | my $CL = sub { | ||
| 101 | my ($p, $len, $dx,$dy) = @_; | ||
| 102 | $p = $add->($p, $s*$dx,$s*$dy); # between | ||
| 103 | $s = 1; | ||
| 104 | return $chain_C->($p, $len, $dx,$dy); | ||
| 105 | }; | ||
| 106 | |||
| 107 | # add a fork F attached to vertex $p | ||
| 108 | my $F = sub { | ||
| 109 | my ($p, $dx,$dy) = @_; | ||
| 110 | $p = $add->($p, $dx,$dy); | ||
| 111 | $add->($p, $dx+$dy/2, $dy-$dx/2); | ||
| 112 | $add->($p, $dx-$dy/2, $dy+$dx/2); | ||
| 113 | }; | ||
| 114 | |||
| 115 | # add a chain of $len many C and final F, | ||
| 116 | # starting from attached to vertex $p | ||
| 117 | my $CF = sub { | ||
| 118 | my ($p, $len, $dx,$dy, $s) = @_; | ||
| 119 | $p = $CL->($p, $len, $dx,$dy, $s); | ||
| 120 | $F->($p, $dx,$dy); | ||
| 121 | }; | ||
| 122 | |||
| 123 | if ($N) { | ||
| 124 | my $m = $upto++; | ||
| 125 | $graph->add_vertex($m); | ||
| 126 | $graph->set_vertex_attribute($m, x => 0); | ||
| 127 | $graph->set_vertex_attribute($m, y => 0); | ||
| 128 | my $N7 = $N % 7; | ||
| 129 | |||
| 130 | if ($N <= 6) { | ||
| 131 | # star figure 5(a) T6,1 star-6 | ||
| 132 | # other N=0 to 5 also star | ||
| 133 | if ($N >= 2) { | ||
| 134 | foreach my $i (0 .. $N-2) { | ||
| 135 | $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 | $m = $add->($m, 1,0); | ||
| 146 | $m = $add->($m, 1,0); | ||
| 147 | $m = $add->($m, 1,0); | ||
| 148 | $add->($m, 0, 1); | ||
| 149 | $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 | $F->($add->($m,-1,0), -1,0); | ||
| 156 | $F->($add->($m, 1,0), 1,0); | ||
| 157 | $add->($m, 0,1); # top L | ||
| 158 | |||
| 159 | } elsif ($N7 == 6 && $N <= 20) { | ||
| 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 | foreach my $i (1 .. 3) { | ||
| 166 | $m = $add->($m, 1,0); # right to centre | ||
| 167 | $add->($m, 0,-1); # down | ||
| 168 | $CL->($m, $i==3 && $N==20, 0,1); # up L or CL | ||
| 169 | $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 | $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 | my $k = sub { my($j)=@_; int(($N - ($N>=37 ? 2:9) + 7*$j)/35); }; | ||
| 188 | if ($k->(3)) { $s = 2; } | ||
| 189 | my $r = $CL->($m, $N<37 ? 0 : int(($N-37)/35), # C0 across to r | ||
| 190 | 1,0); | ||
| 191 | $r = $add->($r, $k->(4)==0 ? 1 : 4, 0); | ||
| 192 | $CL->($m, $k->(1), 0,-1); # C1L | ||
| 193 | $CL->($r, $k->(2), 0,-1); # C2L | ||
| 194 | $CL->($m, $k->(3), 0,1); | ||
| 195 | $CL->($r, $k->(4), 0,1); | ||
| 196 | $add->($m, -1,0); # left L most | ||
| 197 | $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 | my $k = sub { my($j)=@_; int(($N - 17 + 7*$j)/28); }; | ||
| 206 | $CF->($m, $k->(0), 0,-1); | ||
| 207 | if ($k->(2)) { $s = 4; } | ||
| 208 | $CF->($m, $k->(1), -1,0); # left C1F | ||
| 209 | $CF->($m, $k->(2), 0,1); | ||
| 210 | if ($k->(2)) { $s = 2; } | ||
| 211 | $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 | $m = $chain_C->($m, int(($N-4)/7), 1,0); | ||
| 216 | $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 | my $k = sub { my($j)=@_; int(($N-5+7*$j)/21); }; | ||
| 225 | $CL->($m, $k->(0), 0,-1); | ||
| 226 | if ($k->(1) && $k->(2)) { $s = 4; } | ||
| 227 | $CL->($m, $k->(1), -1,0); | ||
| 228 | $CL->($m, $k->(2), 0,1); | ||
| 229 | $add->($m, 1,0); # right L | ||
| 230 | |||
| 231 | } elsif ($N==34.5 || $N7 == 6) { | ||
| 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 | my $k = sub { | ||
| 238 | my($j)=@_; | ||
| 239 | if ($N==34.5) { return $j==0; } | ||
| 240 | int(($N - 27 + 7*$j)/49); | ||
| 241 | }; | ||
| 242 | if ($k->(5)) { $s = 2; } | ||
| 243 | my $r = $CL->($m, $k->(0), 1,0); | ||
| 244 | $r = $add->($r, $k->(6)==0 ? 1 : 4, 0); | ||
| 245 | $CF->($m, $k->(1), 0,-1); | ||
| 246 | $CF->($r, $k->(2), 0,-1); | ||
| 247 | if ($k->(5)) { $s = 4; } | ||
| 248 | $CF->($m, $k->(3), -1,0); | ||
| 249 | if ($k->(2)) { $s = 2; } | ||
| 250 | $CF->($r, $k->(4), 1,0); | ||
| 251 | $CF->($m, $k->(5), 0,1); | ||
| 252 | $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 | $add->($m, 0,1); | ||
| 261 | $add->($m, 0,-1); | ||
| 262 | $CF->($m, int(($N-7)/7), 1,0); | ||
| 263 | |||
| 264 | } else { | ||
| 265 | croak "Unrecognised N=",$N; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | 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 | sub _rat_to_xy { | ||
| 280 | my ($num,$den) = @_; | ||
| 281 | ### _rat_to_xy(): "$num $den" | ||
| 282 | if (4*$num == $den) { return (0,1); } # 90 degrees | ||
| 283 | if (4*$num == 2*$den) { return (-1,0); } # 180 degrees | ||
| 284 | if (4*$num == 3*$den) { return (0,-1); } # 270 degrees | ||
| 285 | my $a = 6.283185 * $num / $den; | ||
| 286 | my $x = cos($a); | ||
| 287 | my $y = sin($a); | ||
| 288 | if (3*$num == $den || 3*$num == 2*$den) { $x = -.5; } # 120,240 degrees | ||
| 289 | return ($x,$y); | ||
| 290 | } | ||
| 291 | |||
| 292 | Graph::Maker->add_factory_type('most_maximum_matchings_tree' => __PACKAGE__); | ||
| 293 | 1; | ||
| 294 | |||
| 295 | __END__ |