| 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 | package Graph::Maker::Circulant; | ||
| 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 Graph::Maker; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use List::Util 'min'; | ||
| 24 | |||
| 25 | use vars '$VERSION','@ISA'; | ||
| 26 | $VERSION = 16; | ||
| 27 | @ISA = ('Graph::Maker'); | ||
| 28 | |||
| 29 | |||
| 30 | sub _default_graph_maker { | ||
| 31 | require Graph; | ||
| 32 | return Graph->new(@_); | ||
| 33 | } | ||
| 34 | sub _make_graph { | ||
| 35 | my ($params) = @_; | ||
| 36 | my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker; | ||
| 37 | return $graph_maker->(%$params); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub init { | ||
| 41 | my ($self, %params) = @_; | ||
| 42 | |||
| 43 | my $N = delete($params{'N'}) || 0; | ||
| 44 | my $offset_list = delete($params{'offset_list'}); | ||
| 45 | my $graph = _make_graph(\%params); | ||
| 46 | $graph->set_graph_attribute | ||
| 47 | (name => "Circulant $N Offset ".join(',',@$offset_list)); | ||
| 48 | |||
| 49 | my $directed = $graph->is_directed; | ||
| 50 | $graph->add_vertices(1 .. $N); | ||
| 51 | my %seen; | ||
| 52 | my $half = $N/2; | ||
| 53 | foreach my $o (@$offset_list) { | ||
| 54 | my $o = min($o%$N, (-$o)%$N); | ||
| 55 | next if $seen{$o}++; | ||
| 56 | foreach my $from (1 .. ($o==$half ? $half : $N)) { | ||
| 57 | my $to = ($from + $o - 1) % $N + 1; # 1 to N modulo | ||
| 58 | $graph->add_edge($from,$to); | ||
| 59 | if ($directed && $o) { $graph->add_edge($to,$from); } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | return $graph; | ||
| 63 | } | ||
| 64 | |||
| 65 | Graph::Maker->add_factory_type('circulant' => __PACKAGE__); | ||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ | ||