File Coverage

blib/lib/Graph/Maker/Petersen.pm
Criterion Covered Total %
statement 6 8 75.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 9 11 81.8


line stmt bran cond sub pod time code
1             # Copyright 2015, 2016, 2017 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             # .
18              
19             package Graph::Maker::Petersen;
20 1     1   91375 use 5.004;
  1         3  
21 1     1   5 use strict;
  1         2  
  1         16  
22 1     1   607 use Graph::Maker;
  0            
  0            
23              
24             use vars '$VERSION','@ISA';
25             $VERSION = 7;
26             @ISA = ('Graph::Maker');
27              
28              
29             sub _default_graph_maker {
30             require Graph;
31             return Graph->new(@_);
32             }
33             sub _make_graph {
34             my ($params) = @_;
35             my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
36             return $graph_maker->(%$params);
37             }
38              
39             sub init {
40             my ($self, %params) = @_;
41              
42             my $N = delete($params{'N'}) || 5;
43             my $K = delete($params{'K'}) || 2;
44              
45             my $graph = _make_graph(\%params);
46             $graph->set_graph_attribute (name => ($N==5 && $K==2 ? "Petersen" : "Petersen $N,$K"));
47              
48             $graph->add_cycle(1 .. $N);
49             foreach my $i (1 .. $N) {
50             my $j = $i + $N;
51             my $f = $N+1 + (($i-1+$K) % $N);
52             if ($f > 2*$N) { $f -= $N; }
53             $graph->add_edges([$i,$j],[$j,$f]);
54             }
55              
56             # $graph->add_cycle(1,2,3,4,5);
57             # $graph->add_cycle(6,8,10,7,9);
58             # $graph->add_edges([1,6],[2,7],[3,8],[4,9],[5,10]);
59              
60             if ($graph->is_directed) {
61             $graph->add_edges(map {[reverse @$_]} $graph->edges);
62             }
63             return $graph;
64             }
65              
66             Graph::Maker->add_factory_type('Petersen' => __PACKAGE__);
67             1;
68              
69             __END__