| 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::BulgarianSolitaire; | ||
| 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 Carp 'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use List::Util 'sum'; |
|
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 | # $aref is an arrayref to a partition, with terms in ascending order. | ||
| 46 | # Eg. [1,3,3,5] | ||
| 47 | # Modify the contents of $aref to the lexicographically next partition. | ||
| 48 | # Return 1 if that is done, or return 0 if no more partitions. | ||
| 49 | # The first (lex smallest) partition is [1,1,...,1] and iteration can | ||
| 50 | # proceed from there | ||
| 51 | # | ||
| 52 | # partition last terms ..., x, y | ||
| 53 | # x increment +1 as long as x+1 <= y-1, which is x<=y-2 | ||
| 54 | # otherwise x increment +y, to make one last term x+y | ||
| 55 | # after x += increment and y -= increment, remaining y is applied as terms | ||
| 56 | # x,x,...,x, r where r>=x (the new incremented x that is) | ||
| 57 | # | ||
| 58 | sub _partition_next { | ||
| 59 | my ($aref) = @_; | ||
| 60 | ### _partition_next(): join(',',@$aref) | ||
| 61 | |||
| 62 | if (@$aref < 2) { | ||
| 63 | ### no more ... | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | my $y = pop @$aref; | ||
| 67 | my $x = $aref->[-1]; | ||
| 68 | my $inc = ($x <= $y-2 ? 1 : $y); | ||
| 69 | $aref->[-1] = ($x += $inc); | ||
| 70 | while ($y -= $inc) { | ||
| 71 | $inc = ($y >= $x<<1 ? $x : $y); | ||
| 72 | push @$aref, $inc; | ||
| 73 | } | ||
| 74 | ### ret: join(',',@$aref) | ||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | # start 1 1 1 1 1 | ||
| 79 | # 1 0 1 0 1 | ||
| 80 | # ending 1, decrement by last term add to prev | ||
| 81 | # 1 0 1 0 0 | ||
| 82 | # ending 0, decrement by second last +1, and all 1s | ||
| 83 | # | ||
| 84 | sub _composition_next { | ||
| 85 | my ($aref) = @_; | ||
| 86 | if (@$aref < 2) { | ||
| 87 | ### no more ... | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | my $y = pop @$aref; | ||
| 91 | $aref->[-1]++; | ||
| 92 | push @$aref, (1) x ($y-1); | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | # composition parameter, whether to push new term onto end of partition, | ||
| 97 | # otherwise unshift onto start | ||
| 98 | my %compositions_to_end = (0 => 1, | ||
| 99 | append => 1, | ||
| 100 | prepend => 0); | ||
| 101 | |||
| 102 | sub init { | ||
| 103 | my ($self, %params) = @_; | ||
| 104 | ### BulgarianSolitaire init ... | ||
| 105 | |||
| 106 | my $N = delete($params{'N'}) || 0; | ||
| 107 | my $compositions = delete($params{'compositions'}) || 0; | ||
| 108 | my $no_self_loop = delete($params{'no_self_loop'}) || 0; | ||
| 109 | |||
| 110 | # $end true if new term goes at the end of the list | ||
| 111 | my $end = $compositions_to_end{$compositions}; | ||
| 112 | if (! defined $end) { | ||
| 113 | croak "Unrecognised compositions type: ", $compositions; | ||
| 114 | } | ||
| 115 | |||
| 116 | my $graph = _make_graph(\%params); | ||
| 117 | $graph->set_graph_attribute | ||
| 118 | (name => "Bulgarian Solitaire N=$N" | ||
| 119 | . ($compositions ? ", Compositions ".ucfirst($compositions) : '')); | ||
| 120 | |||
| 121 | ### $N | ||
| 122 | |||
| 123 | my $next = $compositions ? \&_composition_next : \&_partition_next; | ||
| 124 | my @from = (1) x $N; | ||
| 125 | do { | ||
| 126 | my $from_str = join(',',@from); | ||
| 127 | |||
| 128 | my @to = map {$_ - 1} @from; # decrement each term | ||
| 129 | splice @to, $end ? scalar(@to) : 0, 0, scalar(@from); # new term | ||
| 130 | @to = grep {$_} @to; # discard 0s | ||
| 131 | unless ($compositions) { | ||
| 132 | # sort for partitions, no sort for compositions | ||
| 133 | @to = sort {$a<=>$b} @to; | ||
| 134 | } | ||
| 135 | my $to_str = join(',',@to); | ||
| 136 | ### $to_str | ||
| 137 | |||
| 138 | if ($no_self_loop && $from_str eq $to_str) { | ||
| 139 | $graph->add_vertex ($from_str); | ||
| 140 | } else { | ||
| 141 | $graph->add_edge ($from_str, $to_str); | ||
| 142 | } | ||
| 143 | } while ($next->(\@from)); | ||
| 144 | |||
| 145 | ### total vertices: scalar $graph->vertices | ||
| 146 | ### total edges : scalar $graph->edges | ||
| 147 | |||
| 148 | return $graph; | ||
| 149 | } | ||
| 150 | |||
| 151 | Graph::Maker->add_factory_type('Bulgarian_solitaire' => __PACKAGE__); | ||
| 152 | 1; | ||
| 153 | |||
| 154 | __END__ | ||