| 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 | |||
| 20 | package Graph::Maker::BinomialBoth; | ||
| 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 Graph::Maker; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | |||
| 26 | use vars '$VERSION','@ISA'; | ||
| 27 | $VERSION = 17; | ||
| 28 | @ISA = ('Graph::Maker'); | ||
| 29 | |||
| 30 | # GP-DEFINE default(strictargs,1); | ||
| 31 | # GP-DEFINE read("my-oeis.gp"); | ||
| 32 | # GP-DEFINE read("memoize.gp"); | ||
| 33 | |||
| 34 | # uncomment this to run the ### lines | ||
| 35 | # use Smart::Comments; | ||
| 36 | |||
| 37 | |||
| 38 | sub _default_graph_maker { | ||
| 39 | require Graph; | ||
| 40 | Graph->new(@_); | ||
| 41 | } | ||
| 42 | sub _make_graph { | ||
| 43 | my ($params) = @_; | ||
| 44 | my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker; | ||
| 45 | return $graph_maker->(%$params); | ||
| 46 | } | ||
| 47 | sub _add_edge_reverse { | ||
| 48 | my ($graph, $u, $v) = @_; | ||
| 49 | $graph->add_edge($v,$u); # reverse | ||
| 50 | } | ||
| 51 | my %add_edge_func = (smaller => \&_add_edge_reverse, | ||
| 52 | bigger => 'add_edge', | ||
| 53 | both => 'add_cycle'); | ||
| 54 | |||
| 55 | sub _count_1bits { | ||
| 56 | my ($n) = @_; | ||
| 57 | my $ret = 0; | ||
| 58 | while ($n) { $ret += $n&1; $n >>= 1; } | ||
| 59 | return $ret; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub init { | ||
| 63 | my ($self, %params) = @_; | ||
| 64 | |||
| 65 | my $order = delete($params{'order'}); | ||
| 66 | my $direction_type = delete($params{'direction_type'}) || 'both'; | ||
| 67 | my $coordinate_type = delete($params{'coordinate_type'}) || ''; | ||
| 68 | my $graph = _make_graph(\%params); | ||
| 69 | |||
| 70 | my $add_edge = ($graph->is_undirected ? 'add_edge' | ||
| 71 | : $add_edge_func{$direction_type} | ||
| 72 | || croak "Unrecognised direction_type ",$direction_type); | ||
| 73 | |||
| 74 | my $limit = (1<<$order) - 1; | ||
| 75 | $graph->set_graph_attribute (name => "Binomial Both, Order $order"); | ||
| 76 | |||
| 77 | $graph->add_vertex(0); | ||
| 78 | my $directed = $graph->is_directed; | ||
| 79 | |||
| 80 | foreach my $i (1 .. $limit) { | ||
| 81 | my $from = $i & ~($i ^ ($i-1)); # clear lowest 1-bit | ||
| 82 | ### edge: "$from down to $i" | ||
| 83 | $graph->$add_edge($from, $i); # from smaller | ||
| 84 | } | ||
| 85 | for (my $i = 1; $i < $limit; $i += 2) { # odds | ||
| 86 | my $to = $i | ($i ^ ($i+1)); # set lowest 0-bit | ||
| 87 | ### edge: "$to up to $i" | ||
| 88 | $graph->$add_edge($i, $to); # to bigger | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($coordinate_type eq 'across') { | ||
| 92 | foreach my $i (0 .. $limit) { | ||
| 93 | $graph->set_vertex_attribute($i, x => $i>>1); | ||
| 94 | $graph->set_vertex_attribute($i, y => - _count_1bits($i)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | return $graph; | ||
| 98 | } | ||
| 99 | |||
| 100 | Graph::Maker->add_factory_type('binomial_both' => __PACKAGE__); | ||
| 101 | 1; | ||
| 102 | |||
| 103 | __END__ | ||