| 1 | # Copyright 2016, 2017, 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 | package Graph::Maker::BinaryBeanstalk; | ||
| 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=2.6
| |||
| 24 | |||
| 25 | use vars '$VERSION','@ISA'; | ||
| 26 | $VERSION = 17; | ||
| 27 | @ISA = ('Graph::Maker'); | ||
| 28 | |||
| 29 | |||
| 30 | # uncomment this to run the ### lines | ||
| 31 | # use Smart::Comments; | ||
| 32 | |||
| 33 | |||
| 34 | sub _default_graph_maker { | ||
| 35 | require Graph; | ||
| 36 | Graph->new(@_); | ||
| 37 | } | ||
| 38 | sub _make_graph { | ||
| 39 | my ($self, %params) = @_; | ||
| 40 | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||
| 41 | return $graph_maker->(%params); | ||
| 42 | } | ||
| 43 | sub _add_edge_reverse { | ||
| 44 | my ($graph, $u, $v) = @_; | ||
| 45 | $graph->add_edge($v,$u); # reverse | ||
| 46 | } | ||
| 47 | my %add_edge_method = (smaller => \&_add_edge_reverse, | ||
| 48 | parent => \&_add_edge_reverse, | ||
| 49 | bigger => 'add_edge', | ||
| 50 | child => 'add_edge', | ||
| 51 | both => 'add_cycle'); | ||
| 52 | |||
| 53 | sub init { | ||
| 54 | my ($self, %params) = @_; | ||
| 55 | |||
| 56 | my $height = delete($params{'height'}); | ||
| 57 | my $N = delete($params{'N'}); | ||
| 58 | my $direction_type = delete($params{'direction_type'}) || 'both'; | ||
| 59 | my $graph = $self->_make_graph(%params); | ||
| 60 | |||
| 61 | if ((defined $height && $height > 0) | ||
| 62 | || (defined $N && $N > 0)) { | ||
| 63 | $graph->add_vertex(0); | ||
| 64 | |||
| 65 | my $add_edge = ($graph->is_undirected ? 'add_edge' | ||
| 66 | : $add_edge_method{$direction_type} | ||
| 67 | || croak "Unrecognised direction_type ",$direction_type); | ||
| 68 | my $row_start = 0; | ||
| 69 | my $v = 1; | ||
| 70 | my $h = 1; | ||
| 71 | for (;;) { | ||
| 72 | my $parent = $v - _count_1_bits($v); | ||
| 73 | ### at: "$v parent $parent h=$h" | ||
| 74 | if ($parent >= $row_start) { | ||
| 75 | $row_start = $v; | ||
| 76 | |||
| 77 | $h++; | ||
| 78 | if (defined $height && $h > $height) { | ||
| 79 | # stop for height limit | ||
| 80 | $graph->set_graph_attribute(name=>"Binary Beanstalk, Height $height"); | ||
| 81 | last; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | if (defined $N && $v >= $N) { | ||
| 85 | # stop for N limit | ||
| 86 | $graph->set_graph_attribute (name => "Binary Beanstalk, $N Vertices"); | ||
| 87 | last; | ||
| 88 | } | ||
| 89 | |||
| 90 | $graph->$add_edge($parent, $v); | ||
| 91 | $v++; | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | $graph->set_graph_attribute (name => "Binary Beanstalk, Empty"); | ||
| 95 | } | ||
| 96 | return $graph; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _count_1_bits { | ||
| 100 | my ($n) = @_; | ||
| 101 | my $count = 0; | ||
| 102 | while ($n) { | ||
| 103 | $count += ($n & 1); | ||
| 104 | $n >>= 1; | ||
| 105 | } | ||
| 106 | return $count; | ||
| 107 | } | ||
| 108 | |||
| 109 | Graph::Maker->add_factory_type('binary_beanstalk' => __PACKAGE__); | ||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ | ||