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