| 1 | # Copyright 2018, 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 | |||
| 20 | package Graph::Maker::Catalans; | ||
| 21 | 1 | ▶ | use 5.004; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 1 | ▶ | use constant 1.02; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use Carp 'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 1 | ▶ | use Graph::Maker; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 26 | |||
| 27 | use vars '$VERSION','@ISA'; | ||
| 28 | $VERSION = 17; | ||
| 29 | @ISA = ('Graph::Maker'); | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | # use Smart::Comments; | ||
| 33 | |||
| 34 | |||
| 35 | sub _default_graph_maker { | ||
| 36 | require Graph; | ||
| 37 | return Graph->new(@_); | ||
| 38 | } | ||
| 39 | sub _make_graph { | ||
| 40 | my ($params) = @_; | ||
| 41 | my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker; | ||
| 42 | return $graph_maker->(%$params); | ||
| 43 | } | ||
| 44 | |||
| 45 | # This finds the last run of 1s and moves the first of them up and the rest | ||
| 46 | # of them to the end and spread out. See Math::NumSeq::BalancedBinary for | ||
| 47 | # more description. | ||
| 48 | # | ||
| 49 | # M.C. Er, "Enumerating Ordered Trees Lexicographically", The Computer | ||
| 50 | # Journal, volume 28, number 5, 1985. Procedure GenSuc is this algorithm, | ||
| 51 | # but applying all change once the run of 1s is located. Here the 1s are | ||
| 52 | # moved to the end while their extent is still being sought. | ||
| 53 | # | ||
| 54 | sub _balanced_next { | ||
| 55 | my ($aref) = @_; | ||
| 56 | ### _balanced_next(): join('',@$aref) | ||
| 57 | foreach my $i (reverse 1 .. $#$aref-1) { | ||
| 58 | if ($aref->[$i]) { | ||
| 59 | ### low 1 at: $i | ||
| 60 | my $o = 0; | ||
| 61 | do { | ||
| 62 | $aref->[$i--] = 0; | ||
| 63 | if ($i<0) { | ||
| 64 | ### no more ... | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | $aref->[$o-=2] = 1; | ||
| 68 | } while ($aref->[$i]); | ||
| 69 | $aref->[$i] = 1; | ||
| 70 | $aref->[$o] = 0; | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | ### no more ... | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | #---------------- | ||
| 79 | # Vertex Names | ||
| 80 | # | ||
| 81 | # Other possibilities | ||
| 82 | # N+1 elements with parens like 1(2(34))5 | ||
| 83 | # same as balanced with () for each elem ? | ||
| 84 | # | ||
| 85 | # one-sequence = position in balanced of each 1 | ||
| 86 | # zero-sequence = position in balanced of each 0 | ||
| 87 | # L-sequence = preorder, at each left child (internal or external), | ||
| 88 | # num externals below | ||
| 89 | # | ||
| 90 | # Zerling, rotation coding by rotate root until its subtree sizes are right, | ||
| 91 | # then recurse to left and then to right. Code as how many rotates applied. | ||
| 92 | # Write those from last to first, so first is on a left and so always 0. | ||
| 93 | # Regenerate by first to last code many right rotates. | ||
| 94 | # | ||
| 95 | # Description in Lucas, van Baronaigien, and Ruskey. | ||
| 96 | # Sequence c1 .. cn where sum(c1..cj) <= j. | ||
| 97 | # Start left path n vertices. | ||
| 98 | # For i=2..n, perform ci many right rotations at the root of T[1..i]. | ||
| 99 | # T[1..i] changes due to this. | ||
| 100 | # | ||
| 101 | # * figure 7 | ||
| 102 | # / \ Zerling 00 111 00 103 | ||
| 103 | # * * Weights 1,2,1,2,3,6,1,1,2,1 = Lweights | ||
| 104 | # / \ \ Distance 0,0,1,1,1,0,1,2,2,3 = Rdepths_inorder | ||
| 105 | # * * * | ||
| 106 | # / / \ | ||
| 107 | # * * * | ||
| 108 | # / | ||
| 109 | # * | ||
| 110 | # | ||
| 111 | # Or similar counts rotates but applied to the last edge of the tree. | ||
| 112 | # Becomes Rdepths, maybe. | ||
| 113 | |||
| 114 | |||
| 115 | my @pm_one = (-1, 1); | ||
| 116 | |||
| 117 | sub _vertex_name_type_balanced { | ||
| 118 | my ($aref) = @_; | ||
| 119 | return @$aref; | ||
| 120 | } | ||
| 121 | sub _vertex_name_type_balanced_postorder { | ||
| 122 | my ($aref) = @_; | ||
| 123 | ### _vertex_name_type_balanced_postorder(): join(',',@$aref) | ||
| 124 | |||
| 125 | # preorder 1 left 0 right 10 10 | ||
| 126 | # postorder left 1 right 0 1 10 0 | ||
| 127 | # | ||
| 128 | # 0 = end of depth d | ||
| 129 | # emit 1, remember 0 for end of depth d-1 | ||
| 130 | # 1 = start of new depth | ||
| 131 | # | ||
| 132 | my @ret; | ||
| 133 | my @pending = (0); | ||
| 134 | foreach my $bit (@$aref) { | ||
| 135 | ### at: "bit $bit pending ".join(',',@pending)." ret=".join('',@ret) | ||
| 136 | if ($bit) { | ||
| 137 | push @pending, 0; | ||
| 138 | } else { | ||
| 139 | ### end: "emit pending 0s $pending[-1], and 1" | ||
| 140 | push @ret, (0) x pop @pending, 1; | ||
| 141 | $pending[-1]++; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | ### final pending: join(',',@pending) | ||
| 145 | ### assert: scalar(@pending)==1 | ||
| 146 | push @ret, (0) x $pending[0]; | ||
| 147 | |||
| 148 | ### ret: join('',@ret) | ||
| 149 | return @ret; | ||
| 150 | } | ||
| 151 | |||
| 152 | # form used by Sapounakis et al for filling | ||
| 153 | sub _vertex_name_type_run1s { | ||
| 154 | my ($aref) = @_; | ||
| 155 | my $r = 0; | ||
| 156 | my @ret; | ||
| 157 | foreach my $bit (@$aref) { | ||
| 158 | if ($bit) { | ||
| 159 | $r++; | ||
| 160 | } else { | ||
| 161 | push @ret, $r; | ||
| 162 | $r = 0; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | return @ret; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Knuth volume 4A section 7.2.1.6 after algorithm P at equation (6), which | ||
| 169 | # is in pre-fascicle 4A | ||
| 170 | # http://www-cs-faculty.stanford.edu/~knuth/fasc4a.ps.gz draft of section | ||
| 171 | # 7.2.1.6 page 3, run length encode 0-bits | ||
| 172 | # | ||
| 173 | # d1 d1 dn | ||
| 174 | # ( ) ( ) ... ( ) | ||
| 175 | # | ||
| 176 | # where )^d[i] means d[i] many closes at that point. Such a run is after | ||
| 177 | # each open, and can have d[i]=0 for no closes there. | ||
| 178 | # | ||
| 179 | # Per exercise 30 in the same section (draft page 35): | ||
| 180 | # An ordered forest childful is where d[i]=0, for a new left descent. | ||
| 181 | # childless is where d[i]!=0, for step up to higher sibling. | ||
| 182 | # | ||
| 183 | # Other vertex name forms in Knuth: | ||
| 184 | # z1,z2,...,zn positions 0 to 2n-1 of the 1s. | ||
| 185 | # run1s[i] = z[i+1] - z[i] - 1 | ||
| 186 | # | ||
| 187 | sub _vertex_name_type_run0s { | ||
| 188 | my ($aref) = @_; | ||
| 189 | my $r = 0; | ||
| 190 | my @ret; | ||
| 191 | foreach my $bit (reverse @$aref) { | ||
| 192 | if ($bit) { | ||
| 193 | push @ret, $r; | ||
| 194 | $r = 0; | ||
| 195 | } else { | ||
| 196 | $r++; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return reverse @ret; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _vertex_name_type_Ldepths { | ||
| 203 | my ($aref) = @_; | ||
| 204 | my @ret; | ||
| 205 | my $d = 0; | ||
| 206 | foreach my $bit (@$aref) { | ||
| 207 | if ($bit) { push @ret, $d; } | ||
| 208 | $d += $pm_one[$bit]; | ||
| 209 | } | ||
| 210 | return @ret; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _vertex_name_type_Ldepths_inorder { | ||
| 214 | my ($aref) = @_; | ||
| 215 | # This code uses the form noted in the POD, at each 0 how many unclosed 1s | ||
| 216 | # precede, with the 0 itself counted as a closing, so done after pm_one. | ||
| 217 | my @ret; | ||
| 218 | my $d = 0; | ||
| 219 | foreach my $bit (@$aref) { | ||
| 220 | $d += $pm_one[$bit]; | ||
| 221 | unless ($bit) { | ||
| 222 | push @ret, $d; # at each 0, num unclosed 0s after, not incl self | ||
| 223 | } | ||
| 224 | } | ||
| 225 | return @ret; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub _vertex_name_type_Bdepths_inorder { | ||
| 229 | return _vertex_name_type_Rdepths_inorder(@_,1); | ||
| 230 | } | ||
| 231 | sub _vertex_name_type_Rdepths_inorder { | ||
| 232 | my ($aref, $l) = @_; | ||
| 233 | # $l = 1 if count left steps too, giving Bdepths | ||
| 234 | # = 0 if no count left step, giving Rdepths | ||
| 235 | $l ||= 0; | ||
| 236 | ### _vertex_name_type_Rdepths(): "l=$l ".join('',@$aref) | ||
| 237 | my @pos = (0); | ||
| 238 | my @ret; | ||
| 239 | my $d = 0; | ||
| 240 | foreach my $bit (@$aref) { | ||
| 241 | ### at: "bit $bit d=$d pos ".join(',',@pos) | ||
| 242 | if ($bit) { | ||
| 243 | push @pos, $pos[-1]; # copy | ||
| 244 | $d += $l; | ||
| 245 | } else { | ||
| 246 | $d -= $l; | ||
| 247 | pop @pos; # backtrack and go right | ||
| 248 | push @ret, ($pos[-1]++) + $d; | ||
| 249 | ### ret: $ret[-1] | ||
| 250 | } | ||
| 251 | } | ||
| 252 | ### return: join(',',@ret) | ||
| 253 | return @ret; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub _vertex_name_type_Rdepths_postorder { | ||
| 257 | my ($aref) = @_; | ||
| 258 | ### _vertex_name_type_Rdepths_postorder(): join(',',@$aref) | ||
| 259 | |||
| 260 | # $pending[] is how many right descents above the present vertex. | ||
| 261 | # Preorder balanced binary is then: | ||
| 262 | # 1 = internal vertex, descend to the left, so new 0 many rights. | ||
| 263 | # 0 = external vertex | ||
| 264 | # pop @pending for how many rights to go up, each of them is a depth | ||
| 265 | # then next descent is a right so increment now top $pending[-1] | ||
| 266 | # $d = right steps depth of current internal vertex | ||
| 267 | # Similar to _vertex_name_type_balanced_postorder(), but there just pushing | ||
| 268 | # all 1s for the pop @pending rights to go up, not depths. | ||
| 269 | # | ||
| 270 | my @ret; | ||
| 271 | my @pending = (0); | ||
| 272 | my $d = 0; | ||
| 273 | foreach my $bit (@$aref) { | ||
| 274 | ### at: "bit $bit d=$d pending ".join(',',@pending) | ||
| 275 | if ($bit) { | ||
| 276 | push @pending, 0; | ||
| 277 | } else { | ||
| 278 | ### end: "emit $pending[-1] many" | ||
| 279 | my $new_d = $d - pop @pending; | ||
| 280 | push @ret, reverse $new_d .. $d-1; | ||
| 281 | $d = $new_d + 1; | ||
| 282 | $pending[-1]++; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | ### end pending: join('',@pending) | ||
| 286 | ### end ret: join('',@ret) | ||
| 287 | ### $d | ||
| 288 | ### assert: @pending==1 | ||
| 289 | return @ret, reverse 0 .. $pending[0]-1; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub _vertex_name_type_vpar { | ||
| 293 | my ($aref) = @_; | ||
| 294 | my @vpar; | ||
| 295 | my $p = 0; | ||
| 296 | foreach my $bit (@$aref) { | ||
| 297 | if ($bit) { | ||
| 298 | push @vpar, $p; | ||
| 299 | $p = scalar @vpar; | ||
| 300 | } else { | ||
| 301 | $p = $vpar[$p-1]; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | return @vpar; | ||
| 305 | } | ||
| 306 | sub _vertex_name_type_vpar_postorder { | ||
| 307 | my ($aref) = @_; | ||
| 308 | ### _vertex_name_type_vpar_postorder(): join('',@$aref) | ||
| 309 | my @vpar; | ||
| 310 | my $p = 0; | ||
| 311 | my $v = scalar(@$aref) >> 1; | ||
| 312 | foreach my $bit (reverse @$aref) { | ||
| 313 | if ($bit) { | ||
| 314 | $p = $vpar[$p-1]; | ||
| 315 | } else { | ||
| 316 | $vpar[$v-1] = $p; | ||
| 317 | $p = $v; | ||
| 318 | $v--; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | ### @vpar | ||
| 322 | return @vpar; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _vertex_name_type_Lweights { | ||
| 326 | my ($aref) = @_; | ||
| 327 | my @ret; | ||
| 328 | my @sizes = (1); | ||
| 329 | foreach my $bit (@$aref) { | ||
| 330 | ### at: "bit $bit sizes ".join(',',@sizes) | ||
| 331 | if ($bit) { | ||
| 332 | push @sizes, 1; | ||
| 333 | } else { | ||
| 334 | my $size = pop @sizes; | ||
| 335 | push @ret, $size; | ||
| 336 | $sizes[-1] += $size; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | return @ret; | ||
| 340 | } | ||
| 341 | sub _vertex_name_type_Rweights { | ||
| 342 | my ($aref) = @_; | ||
| 343 | ### _vertex_name_type_Rweights(): join('',@$aref) | ||
| 344 | my @ret; | ||
| 345 | my @Rweights = (1); | ||
| 346 | foreach my $bit (reverse @$aref) { | ||
| 347 | ### at: "bit $bit Rweights ".join(',',@Rweights) | ||
| 348 | if ($bit) { | ||
| 349 | ### add to above ... | ||
| 350 | my $Rweights = pop @Rweights; | ||
| 351 | $Rweights[-1] += $Rweights; | ||
| 352 | } else { | ||
| 353 | ### push ret: $Rweights[-1] | ||
| 354 | push @ret, $Rweights[-1]; | ||
| 355 | push @Rweights, 1; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | ### return: join(',',reverse @ret) | ||
| 359 | return reverse @ret; | ||
| 360 | } | ||
| 361 | |||
| 362 | # Adaricheva arxiv 1101.1536v3 "right bracketing" is parens in postorder | ||
| 363 | # balanced. | ||
| 364 | # | ||
| 365 | # * ((01)(23)) | ||
| 366 | # / \ 0(1)(2(3)) | ||
| 367 | # * * preorder 110010 | ||
| 368 | # / \ / \ postorder 101100 | ||
| 369 | # 0 1 2 3 ()(()) | ||
| 370 | # | ||
| 371 | # * ((ab)((cd)e)) | ||
| 372 | # / \ F = 4,2,4,4 | ||
| 373 | # * * preorder 11001100 | ||
| 374 | # / \ / \ postorder 10 110100 | ||
| 375 | # a b * e () (()()) | ||
| 376 | # / \ a(b)(c(d)(e)) | ||
| 377 | # c d 1 2 3 4 | ||
| 378 | # 2 4 3 4 | ||
| 379 | # | ||
| 380 | # * ((a((bc)d))e) | ||
| 381 | # / \ E = 3,2,3,4 | ||
| 382 | # * e preorder 11011000 | ||
| 383 | # / \ (()(())) | ||
| 384 | # a * postorder 11010010 | ||
| 385 | # / \ (()())() | ||
| 386 | # * d a(b(c)(d))(e) | ||
| 387 | # / \ 1 2 3 4 | ||
| 388 | # b c 3 2 3 4 | ||
| 389 | |||
| 390 | # =head3 Bracketing | ||
| 391 | # | ||
| 392 | # Option C<vertex_name_type =E<gt> 'bracketing'> is parenthesized terms like | ||
| 393 | # "((1,2)3)". Each external vertex is numbered 1 to N+1 left to right. | ||
| 394 | # Each internal vertex is parens "(left right)". A comma (L</Comma> below) | ||
| 395 | # is between numbers when a vertex has two externals (ie. is a leaf). | ||
| 396 | # | ||
| 397 | # There are N pairs of parens. When N=0 there are no parens and the sole | ||
| 398 | # vertex name is "1". N=1 is one pair and sole vertex name "(1,2)". | ||
| 399 | # | ||
| 400 | # Option C<vertex_name_type =E<gt> 'bracketing_reduced'> removes paren pairs | ||
| 401 | # containing the final N+1 external. For example | ||
| 402 | # | ||
| 403 | # (1((2,3)(4,5))) bracketing | ||
| 404 | # 1(2,3)4,5 bracketing_reduced | ||
| 405 | # | ||
| 406 | # This reduction is unambiguous. The reduction leaves consecutive terms "T1 | ||
| 407 | # T2 ... Tk". Each is either an external, or a parenthesized internal. The | ||
| 408 | # full bracketing has parens T1..Tk then T2..Tk then T3..Tk etc. | ||
| 409 | # | ||
| 410 | # In terms of the binary tree, each of these T is a left child subtree of | ||
| 411 | # the rightmost arm descents. | ||
| 412 | # | ||
| 413 | # cf | ||
| 414 | # Tamari and Huang, r.b and g.b. | ||
| 415 | # | ||
| 416 | # UNDOCUMENTED | ||
| 417 | sub _vertex_name_type_bracketing_reduced { | ||
| 418 | return _vertex_name_type_bracketing(@_,1); | ||
| 419 | } | ||
| 420 | # FIXME: Want outer parens so N=0 is "1" and N=1 is (12) ? | ||
| 421 | # Omitting outermost would be inconsistent between N=0 and N=1. | ||
| 422 | # Including means 2N parens. | ||
| 423 | # | ||
| 424 | # UNDOCUMENTED | ||
| 425 | sub _vertex_name_type_bracketing { | ||
| 426 | my ($aref,$reduced) = @_; | ||
| 427 | ### _vertex_name_type_bracketing(): join('',@$aref) | ||
| 428 | |||
| 429 | my @ret; # array of strings to return | ||
| 430 | my $ret = ''; # current string being built | ||
| 431 | my $leaf = 1; # leaf vertex number 1 .. N+1 | ||
| 432 | my $d = 0; # Ldepth = how many left steps down = excess 1s over 0s | ||
| 433 | my $sep; # 1 = prev was number so want separator if next is a number | ||
| 434 | my @closes = (0,0); | ||
| 435 | |||
| 436 | foreach my $bit (@$aref, 0) { | ||
| 437 | ### at: "bit $bit ret=$ret" | ||
| 438 | if ($bit) { | ||
| 439 | if ($d || !$reduced) { | ||
| 440 | $ret .= '('; | ||
| 441 | $sep = 0; | ||
| 442 | } | ||
| 443 | push @closes, 0; | ||
| 444 | $d++; | ||
| 445 | } else { | ||
| 446 | if ($sep) { | ||
| 447 | push @ret, $ret; | ||
| 448 | $ret = ''; | ||
| 449 | } | ||
| 450 | $ret .= $leaf++; | ||
| 451 | $sep = 1; | ||
| 452 | |||
| 453 | $d--; | ||
| 454 | unless ($reduced && $d < 0) { | ||
| 455 | my $closes = pop @closes; | ||
| 456 | foreach (1 .. $closes) { | ||
| 457 | $ret .= ')'; | ||
| 458 | $sep = 0; | ||
| 459 | } | ||
| 460 | $closes[-1]++; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | } | ||
| 464 | return @ret, $ret; | ||
| 465 | } | ||
| 466 | |||
| 467 | #--------- | ||
| 468 | # Rotate | ||
| 469 | |||
| 470 | sub _rotate_at { | ||
| 471 | my ($aref, $i) = @_; | ||
| 472 | ### assert: 1 <= $i | ||
| 473 | ### assert: $i <= $#$aref-1 | ||
| 474 | ### assert: $aref->[$i] == 0 | ||
| 475 | ### assert: $aref->[$i+1] == 1 | ||
| 476 | |||
| 477 | my @new_array = @$aref; | ||
| 478 | my $d = 0; | ||
| 479 | while ($d += $pm_one[ $new_array[$i+1] = $new_array[$i] ]) { | ||
| 480 | $i--; | ||
| 481 | } | ||
| 482 | return \@new_array; | ||
| 483 | } | ||
| 484 | |||
| 485 | use constant _rel_type_name_rotate => 'Rotate'; | ||
| 486 | sub _rel_type_rotate { | ||
| 487 | my ($aref, $one) = @_; # $one meaning rotate_first only | ||
| 488 | my @ret; | ||
| 489 | foreach my $i (1 .. $#$aref-1) { | ||
| 490 | if (!$aref->[$i] && $aref->[$i+1]) { # want 01 | ||
| 491 | push @ret, _rotate_at($aref, $i); | ||
| 492 | last if $one; | ||
| 493 | } | ||
| 494 | } | ||
| 495 | return @ret; | ||
| 496 | } | ||
| 497 | |||
| 498 | use constant _rel_type_name_rotate_first => 'Rotate First'; | ||
| 499 | sub _rel_type_rotate_first { | ||
| 500 | my ($aref) = @_; | ||
| 501 | _rel_type_rotate($aref,1); | ||
| 502 | } | ||
| 503 | |||
| 504 | use constant _rel_type_name_rotate_last => 'Rotate Last'; | ||
| 505 | sub _rel_type_rotate_last { | ||
| 506 | my ($aref) = @_; | ||
| 507 | foreach my $i (reverse 1 .. $#$aref-1) { | ||
| 508 | if (!$aref->[$i] && $aref->[$i+1]) { # last 01 | ||
| 509 | return _rotate_at($aref, $i); | ||
| 510 | } | ||
| 511 | } | ||
| 512 | return (); | ||
| 513 | } | ||
| 514 | |||
| 515 | use constant _rel_type_name_rotate_rightarm => 'Rotate Right Arm'; | ||
| 516 | sub _rel_type_rotate_rightarm { | ||
| 517 | my ($aref) = @_; | ||
| 518 | my @ret; | ||
| 519 | my $d = 1; | ||
| 520 | foreach my $i (1 .. $#$aref-1) { | ||
| 521 | $d += $pm_one[$aref->[$i]]; | ||
| 522 | unless ($aref->[$i] || $d) { # at bit=0 and d==0 | ||
| 523 | push @ret, _rotate_at($aref, $i); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | return @ret; | ||
| 527 | } | ||
| 528 | |||
| 529 | # 11 1 A 0 1 B 0 C 1 1 A 0 B 0 C | ||
| 530 | # ^^^^^ ^^^^^ | ||
| 531 | # | ||
| 532 | # 11010010 | ||
| 533 | # ^^ | ||
| 534 | # ^^ | ||
| 535 | use constant _rel_type_name_rotate_leftarm => 'Rotate Left Arm'; | ||
| 536 | sub _rel_type_rotate_leftarm { | ||
| 537 | my ($aref) = @_; | ||
| 538 | my @ret; | ||
| 539 | my $i = 0; | ||
| 540 | for ( ; $i <= $#$aref; $i++) { | ||
| 541 | unless ($aref->[$i]) { last; } | ||
| 542 | } | ||
| 543 | my $lefts = $i; # how many initial 1s | ||
| 544 | ### $lefts | ||
| 545 | my $d = $i; | ||
| 546 | for ( ; $i < $#$aref; $i++) { | ||
| 547 | ### at: "i=$i d=$d" | ||
| 548 | $d += $pm_one[$aref->[$i]]; | ||
| 549 | if ($d < $lefts) { | ||
| 550 | ### at lefts level: $lefts | ||
| 551 | ### assert: ! $aref->[$i] | ||
| 552 | if ($aref->[$i+1]) { # 01 for rotate | ||
| 553 | push @ret, _rotate_at($aref, $i); | ||
| 554 | } | ||
| 555 | $lefts-- or last; # stop when back to zero line | ||
| 556 | } | ||
| 557 | } | ||
| 558 | return @ret; | ||
| 559 | } | ||
| 560 | |||
| 561 | # A-empty and 101 is not allowed to be first gives disconnected with N | ||
| 562 | # predecessorless. | ||
| 563 | # | ||
| 564 | use constant _rel_type_name_rotate_Aempty => 'Rotate A-Empty'; | ||
| 565 | sub _rel_type_rotate_Aempty { | ||
| 566 | my ($aref) = @_; | ||
| 567 | # from 1 A 0 1 B 0 C from 1 0 1 B 0 C | ||
| 568 | # to 1 1 A 0 B 0 C to 1 1 0 B 0 C | ||
| 569 | # ^^^^^ | ||
| 570 | my @ret; | ||
| 571 | foreach my $i (0 .. $#$aref-2) { | ||
| 572 | if ($aref->[$i] && !$aref->[$i+1] && $aref->[$i+2]) { | ||
| 573 | my @new_array = @$aref; | ||
| 574 | $new_array[$i+1] = 1; | ||
| 575 | $new_array[$i+2] = 0; | ||
| 576 | push @ret, \@new_array; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | return @ret; | ||
| 580 | } | ||
| 581 | |||
| 582 | use constant _rel_type_name_rotate_Bempty => 'Rotate B-Empty'; | ||
| 583 | sub _rel_type_rotate_Bempty { | ||
| 584 | my ($aref) = @_; | ||
| 585 | # 1 A 0 1 B 0 C 1 A 0 1 0 C | ||
| 586 | # ^^^^^ | ||
| 587 | my @ret; | ||
| 588 | foreach my $i (1 .. $#$aref-2) { | ||
| 589 | if (!$aref->[$i] && $aref->[$i+1] && !$aref->[$i+2]) { # 010 | ||
| 590 | push @ret, _rotate_at($aref, $i); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | return @ret; | ||
| 594 | } | ||
| 595 | |||
| 596 | use constant _rel_type_name_rotate_Cempty => 'Rotate C-Empty'; | ||
| 597 | sub _rel_type_rotate_Cempty { | ||
| 598 | my ($aref) = @_; | ||
| 599 | # i | ||
| 600 | # from 1 A 0 1 B 0 C 1 A 0 1 B 0 (0 or end) | ||
| 601 | # to 1 1 A 0 B 0 C 1 1 A 0 B 0 | ||
| 602 | my @ret; | ||
| 603 | foreach my $i (1 .. $#$aref-1) { | ||
| 604 | if (!$aref->[$i] && $aref->[$i+1]) { | ||
| 605 | my $d = 1; | ||
| 606 | my $j = _balanced_end($aref,$i+1) + 1; | ||
| 607 | unless ($aref->[$j]) { # not a following 1, must either 0 or end | ||
| 608 | push @ret, _rotate_at($aref, $i); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | } | ||
| 612 | return @ret; | ||
| 613 | } | ||
| 614 | |||
| 615 | # $aref is an arrayref to balanced binary 0s and 1s. | ||
| 616 | # $i is index into $aref of a 1. | ||
| 617 | # Return the index of the matching closing 0 for that 1. | ||
| 618 | sub _balanced_end { | ||
| 619 | my ($aref, $i) = @_; | ||
| 620 | ### assert: $aref->[$i] == 1 | ||
| 621 | my $d = 1; | ||
| 622 | while ($d += $pm_one[$aref->[++$i]]) {} | ||
| 623 | return $i; | ||
| 624 | } | ||
| 625 | |||
| 626 | #---------------- | ||
| 627 | # Chapoton dexter | ||
| 628 | # Movable x is 0, x, (1 or end) | ||
| 629 | # /\/\ | ||
| 630 | # /\/ \/\ | ||
| 631 | # ****** | ||
| 632 | # ** | ||
| 633 | # NOT: ** followed by 0 | ||
| 634 | # | ||
| 635 | # from 000 1xx0 (1 or end) | ||
| 636 | # to 1xx0 000 | ||
| 637 | # | ||
| 638 | # forwards form as used here: | ||
| 639 | # from (0 or start) 1xx0 111 | ||
| 640 | # to 111 1xx0 | ||
| 641 | # | ||
| 642 | # Decomposition 1 A 0 1 B 0 1 C 0 ... = left child subtrees off the right arm. | ||
| 643 | # Block indecomposable 1 A 1 B 1 C ... 0 0 0, string ending k+1 many 0s. | ||
| 644 | # Level decomposition = A, B, C | ||
| 645 | |||
| 646 | use constant _rel_type_name_dexter => 'Dexter'; | ||
| 647 | sub _rel_type_dexter { | ||
| 648 | my ($aref) = @_; | ||
| 649 | ### _rel_type_shift(): join('',@$aref) | ||
| 650 | my @ret; | ||
| 651 | foreach my $i (1 .. $#$aref-1) { | ||
| 652 | if (!$aref->[$i] && $aref->[$i+1]) { # 01 of rotate | ||
| 653 | ### $i | ||
| 654 | for (my $e = $i+1; $aref->[$e]; $e++) { # one or more 1s after | ||
| 655 | # $j = start of balanced | ||
| 656 | # $i = end of balanced | ||
| 657 | # $i+1 = first 1 after | ||
| 658 | # $e = end of 1s after, being position of last 1 | ||
| 659 | my $d = 0; | ||
| 660 | foreach my $j (reverse 0 .. $i) { | ||
| 661 | unless ($d += $pm_one[$aref->[$j]]) { # first return to zero | ||
| 662 | if ($j == 0 || $aref->[$j-1]==0) { # only 0 or start preceding | ||
| 663 | push @ret, [ @{$aref}[0..$j-1], | ||
| 664 | @{$aref}[$i+1..$e], | ||
| 665 | @{$aref}[$j..$i], | ||
| 666 | @{$aref}[$e+1..$#$aref] ]; | ||
| 667 | } | ||
| 668 | last; | ||
| 669 | } | ||
| 670 | } | ||
| 671 | } | ||
| 672 | } | ||
| 673 | } | ||
| 674 | return @ret; | ||
| 675 | } | ||
| 676 | |||
| 677 | #---------------- | ||
| 678 | # Split = Kreweras | ||
| 679 | |||
| 680 | # Refinement of partition is when one or more partition sets split into | ||
| 681 | # other sets. | ||
| 682 | # Coarsening is combining one or more sets. | ||
| 683 | # Order by refinement is Kreweras. | ||
| 684 | |||
| 685 | use constant _rel_type_name_split => 'Split'; | ||
| 686 | sub _rel_type_split { | ||
| 687 | my ($aref) = @_; | ||
| 688 | ### _rel_type_split(): join('',@$aref) | ||
| 689 | my @ret; | ||
| 690 | foreach my $i (1 .. $#$aref-1) { | ||
| 691 | if (!$aref->[$i] && $aref->[$i+1]) { | ||
| 692 | ### $i | ||
| 693 | my $e = $i+2; | ||
| 694 | while ($aref->[$e]) { $e++; } | ||
| 695 | $e--; | ||
| 696 | |||
| 697 | # $j = start of balanced | ||
| 698 | # $i = end of balanced | ||
| 699 | # $i+1 = first 1 after | ||
| 700 | # $e = end of 1s after | ||
| 701 | my $d = 0; | ||
| 702 | foreach my $j (reverse 0 .. $i) { | ||
| 703 | unless ($d += $pm_one[$aref->[$j]]) { | ||
| 704 | push @ret, [ @{$aref}[0..$j-1], | ||
| 705 | @{$aref}[$i+1..$e], | ||
| 706 | @{$aref}[$j..$i], | ||
| 707 | @{$aref}[$e+1..$#$aref] ]; | ||
| 708 | } | ||
| 709 | last if $d > 0; | ||
| 710 | } | ||
| 711 | } | ||
| 712 | } | ||
| 713 | return @ret; | ||
| 714 | } | ||
| 715 | |||
| 716 | #--------- | ||
| 717 | # Flip = Stanley | ||
| 718 | |||
| 719 | # Richard P. Stanley, "The Fibonacci Lattice", Fibonacci Quarterly, volume | ||
| 720 | # 13, number 3, October 1975, pages 215-232. | ||
| 721 | # https://fq.math.ca/13-3.html | ||
| 722 | # https://fq.math.ca/Scanned/13-3/stanley.pdf | ||
| 723 | # | ||
| 724 | # order ideal of lattice = subset of vertices, and all preceding all of them | ||
| 725 | # J(lattice) = lattice of order ideals, ordered by set inclusion | ||
| 726 | # T2 = infinite complete binary tree, W-ordered covers 1 smaller and 2 bigger | ||
| 727 | # order ideal of T2 = binary tree | ||
| 728 | # j[k] = num order ideals of T2 of cardinality k = Catalan(k) | ||
| 729 | # J(S(k-1)) = Stanley lattice | ||
| 730 | # S(P) = set of segments (or intervals) of P, ordered by inclusion | ||
| 731 | # . | ||
| 732 | # | ||
| 733 | # . . | ||
| 734 | # | ||
| 735 | # . * . 0 segs | ||
| 736 | # / \ | ||
| 737 | # . * * . 1 segs L or R | ||
| 738 | # / \ / \ | ||
| 739 | # . * * * . 2 segs LL L+R RR | ||
| 740 | # S(3) | ||
| 741 | # | ||
| 742 | # Order ideal subset of S(3) vertices - start outside, step to first of S(3) | ||
| 743 | # left arm, follow its vertices stepwise to right arm, exit there. | ||
| 744 | # Is 4 steps across and 4 steps down per Dyck crossing of 4x4 half grid. | ||
| 745 | # | ||
| 746 | # J(S(3)) = Stanley N=4 | ||
| 747 | # 4-elem sets of S(3) | ||
| 748 | |||
| 749 | |||
| 750 | use constant _rel_type_name_flip => 'Flip'; | ||
| 751 | sub _rel_type_flip { | ||
| 752 | my ($aref) = @_; | ||
| 753 | my @ret; | ||
| 754 | foreach my $i (1 .. $#$aref-1) { | ||
| 755 | if (!$aref->[$i] && $aref->[$i+1]) { | ||
| 756 | my @new_aref = @$aref; | ||
| 757 | $new_aref[$i] = 1; | ||
| 758 | $new_aref[$i+1] = 0; | ||
| 759 | push @ret, \@new_aref; | ||
| 760 | } | ||
| 761 | } | ||
| 762 | return @ret; | ||
| 763 | } | ||
| 764 | |||
| 765 | #--------- | ||
| 766 | # Filling = all flips | ||
| 767 | |||
| 768 | use constant _rel_type_name_filling => 'Filling'; | ||
| 769 | sub _rel_type_filling { | ||
| 770 | my ($aref) = @_; | ||
| 771 | my @new_aref = @$aref; | ||
| 772 | my $seen; | ||
| 773 | foreach my $i (1 .. $#$aref-1) { | ||
| 774 | if (!$aref->[$i] && $aref->[$i+1]) { | ||
| 775 | $new_aref[$i] = 1; | ||
| 776 | $new_aref[$i+1] = 0; | ||
| 777 | $seen = 1; | ||
| 778 | } | ||
| 779 | } | ||
| 780 | return ($seen ? \@new_aref : ()); | ||
| 781 | } | ||
| 782 | |||
| 783 | #------------------ | ||
| 784 | |||
| 785 | sub init { | ||
| 786 | my ($self, %params) = @_; | ||
| 787 | ### Catalans init ... | ||
| 788 | |||
| 789 | my $N = delete($params{'N'}) || 0; | ||
| 790 | |||
| 791 | my $rel_type = delete($params{'rel_type'}) || 'rotate'; | ||
| 792 | my $rel_type_func = $self->can("_rel_type_$rel_type") | ||
| 793 | || croak "Unrecognised rel_type: ",$rel_type; | ||
| 794 | |||
| 795 | my $rel_direction = delete($params{'rel_direction'}) || 'up'; | ||
| 796 | |||
| 797 | my $vertex_name_type = delete($params{'vertex_name_type'}) || 'balanced'; | ||
| 798 | my $order = delete($params{'order'}) || 'pre'; | ||
| 799 | my $vertex_name_func = $self->can("_vertex_name_type_$vertex_name_type") | ||
| 800 | || croak "Unrecognised vertex_name_type: ",$vertex_name_type; | ||
| 801 | |||
| 802 | my $comma = delete($params{'comma'}); | ||
| 803 | unless (defined $comma) { | ||
| 804 | $comma = ($vertex_name_type eq 'balanced' | ||
| 805 | || $vertex_name_type eq 'balanced_postorder' | ||
| 806 | ? '' : ','); | ||
| 807 | } | ||
| 808 | |||
| 809 | my $graph = _make_graph(\%params); | ||
| 810 | $graph->set_graph_attribute | ||
| 811 | (name => "Catalans N=$N, " . $self->can("_rel_type_name_$rel_type")->()); | ||
| 812 | |||
| 813 | unless ($graph->is_directed) { | ||
| 814 | $rel_direction = 'up'; | ||
| 815 | } | ||
| 816 | my $up = ($rel_direction ne 'down'); | ||
| 817 | my $down = ($rel_direction ne 'up'); | ||
| 818 | |||
| 819 | my @array = (1,0) x $N; | ||
| 820 | do { | ||
| 821 | my $from = join($comma,$vertex_name_func->(\@array)); | ||
| 822 | $graph->add_vertex($from); | ||
| 823 | |||
| 824 | ### array: join('',@array) | ||
| 825 | ### $from | ||
| 826 | foreach my $to_aref ($rel_type_func->(\@array)) { | ||
| 827 | my $to = join($comma,$vertex_name_func->($to_aref)); | ||
| 828 | ### to array: join('',@$to_aref) | ||
| 829 | ### $to | ||
| 830 | if ($up) { $graph->add_edge($from,$to); } | ||
| 831 | if ($down) { $graph->add_edge($to,$from); } | ||
| 832 | } | ||
| 833 | } while (_balanced_next(\@array)); | ||
| 834 | |||
| 835 | ### total vertices: scalar $graph->vertices | ||
| 836 | ### total edges : scalar $graph->edges | ||
| 837 | |||
| 838 | return $graph; | ||
| 839 | } | ||
| 840 | |||
| 841 | Graph::Maker->add_factory_type('Catalans' => __PACKAGE__); | ||
| 842 | 1; | ||
| 843 | |||
| 844 | __END__ | ||