ExcessConfigurations.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« BinomialBoth.pm ↑ Summary Hanoi.pm »
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::ExcessConfigurations;
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 List::Util 'sum';
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 # uncomment this to run the ### lines
31 # use Smart::Comments;
32  
33  
34 sub _default_graph_maker {
35 require Graph;
36 return Graph->new(@_);
37 }
38 sub _make_graph {
39 my ($params) = @_;
40 my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
41 return $graph_maker->(%$params);
42 }
43  
44 sub _step {
45 my ($conf) = @_;
46 ### _step(): join(',',@$conf)
47 my @ret;
48 {
49 my @new_conf = @$conf;
50 $new_conf[0]++; # new r=1
51 push @ret, \@new_conf;
52 ### incr bicyclic: join(',',@new_conf)
53 }
54 foreach my $i (0 .. $#$conf) {
55 if ($conf->[$i]) {
56 {
57 my @new_conf = @$conf;
58 $new_conf[$i]--; # raise r to r+1
59 $new_conf[$i+1]++;
60 push @ret, \@new_conf;
61 ### raise: "i=$i to ".join(',',@new_conf)
62 }
63 foreach my $j ($i .. $#$conf) {
64 if ($conf->[$j] > ($i==$j)) {
65 my @new_conf = @$conf;
66 $new_conf[$i]--;
67 $new_conf[$j]--;
68 my $t = $i+$j+2;
69 while ($#new_conf < $t) { push @new_conf, 0; }
70 $new_conf[$t]++;
71 push @ret, \@new_conf;
72 ### join: "i=$i j=$j to ".join(',',@new_conf)
73 }
74 }
75 }
76 }
77 return @ret;
78 }
79 # i=0 is r=1
80 # GP-Test i+1 + j+1 + 1 - 1 == i+j+2
81  
82 my %compositions_to_end = (0 => 1,
83 append => 1,
84 prepend => 0);
85 sub init {
86 my ($self, %params) = @_;
87 ### ExcessConfigurations init ...
88  
89 my $N = delete($params{'N'}) || 0;
90  
91 my $graph = _make_graph(\%params);
92 $graph->set_graph_attribute
93 (name => "Excess Configurations N=$N");
94  
95 my @pending = ([0]);
96 $graph->add_vertex('0');
97 foreach (1 .. $N) {
98 ### $_
99 my @new_pending;
100 foreach my $from (@pending) {
101 my $from_str = join(',',@$from);
102 foreach my $to (_step($from)) {
103 my $to_str = join(',',@$to);
104 unless ($graph->has_vertex($to_str)) { push @new_pending, $to; }
105 $graph->add_edge($from_str,$to_str);
106 }
107 }
108 @pending = @new_pending;
109 }
110  
111 ### total vertices: scalar $graph->vertices
112 ### total edges : scalar $graph->edges
113  
114 return $graph;
115 }
116  
117 Graph::Maker->add_factory_type('excess_configurations' => __PACKAGE__);
118 1;
119  
120 __END__
« BinomialBoth.pm ↑ Summary Hanoi.pm »