| 1 | # Copyright 2015, 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::Petersen; | ||
| 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 | # uncomment this to run the ### lines | ||
| 30 | # use Smart::Comments; | ||
| 31 | |||
| 32 | |||
| 33 | # Usage: $graph = $self->make_graph(key=>value) | ||
| 34 | # Create and return a graph with given key/value parameters. | ||
| 35 | # Parameter graph_maker is used (and removed) if given, or Graph.pm otherwise. | ||
| 36 | sub make_graph { | ||
| 37 | my ($self, %params) = @_; | ||
| 38 | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||
| 39 | return $graph_maker->(%params); | ||
| 40 | } | ||
| 41 | sub _default_graph_maker { | ||
| 42 | require Graph; | ||
| 43 | return Graph->new(@_); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub init { | ||
| 47 | my ($self, %params) = @_; | ||
| 48 | my $N = delete($params{'N'}) || 5; | ||
| 49 | my $K = delete($params{'K'}) || 2; | ||
| 50 | |||
| 51 | my $graph = $self->make_graph(%params); | ||
| 52 | $graph->set_graph_attribute (name => ($N==5 && $K==2 ? "Petersen" : "Petersen $N,$K")); | ||
| 53 | |||
| 54 | $K = min($K%$N, (-$K)%$N); | ||
| 55 | ### K reduced: $K | ||
| 56 | ### cf N/2: $N/2 | ||
| 57 | |||
| 58 | $graph->add_cycle(1 .. $N); # outer cycle | ||
| 59 | foreach my $i (1 .. $N) { | ||
| 60 | $graph->add_edge($i,$i+$N); # outer to inner | ||
| 61 | } | ||
| 62 | foreach my $i (0 .. ($K==$N/2 ? $N/2 : $N)-1) { | ||
| 63 | $graph->add_edge($N+1+$i, $N+1+(($i+$K)%$N)); # inner circulant | ||
| 64 | } | ||
| 65 | if ($graph->is_directed) { | ||
| 66 | $graph->add_edges(map {[reverse @$_]} $graph->edges); | ||
| 67 | } | ||
| 68 | return $graph; | ||
| 69 | } | ||
| 70 | |||
| 71 | Graph::Maker->add_factory_type('Petersen' => __PACKAGE__); | ||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ | ||