Maker/SmallWorldHK.pm

filter: Statementstmt 100% 77 / 77 Branchbran 73.0% 19 / 26 Conditioncond 50.0% 12 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 79.0% 121 / 153 CC 28 SCAR 32.3
CC
28
Cov
85%
init
32.9
__ANON__
1.1
__ANON__
1.1
CRAP 25.2
↑ Summary Maker/SmallWorldK.pm »
1 package Graph::Maker::SmallWorldHK;
2
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw/Graph::Maker/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Graph::Maker::Cycle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Math::Random qw/random_uniform random_uniform_integer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 3 our $VERSION = '0.01';
12
13 sub init
14 4 ▶{
Subroutine init: called CC=28 SCAR=34.9
Pod undocumented
15 4 my ($self, %params) = @_;
16
17 4 ▶ my $n = delete($params{N}) || 0;
Condition50%1/2
delete $params{'N'} || 0
l!l
40
MC/DC0%0/1
delete $params{'N'} || 0
delete $params{'N'}
Truth table50%1/2
A
A= delete $params{'N'}
Aresult
11
00
18 4 ▶ my $m = delete($params{M}) || 1;
Condition50%1/2
delete $params{'M'} || 1
l!l
40
MC/DC0%0/1
delete $params{'M'} || 1
delete $params{'M'}
Truth table50%1/2
A
A= delete $params{'M'}
Aresult
11
00
19 4 ▶ my $m_0 = delete($params{M_0}) || $m;
Condition33%1/3
delete $params{'M_0'} || $m
l!l&&r!l&&!r
400
MC/DC0%0/2
delete $params{'M_0'} || $m
delete $params{'M_0'} $m
Truth table33%1/3
A || B
A= delete $params{'M_0'}B= $m
ABresult
1X1
011
000
20 4 ▶ my $p = delete($params{PR}) || 0;
Condition100%2/2
delete $params{'PR'} || 0
l!l
13
MC/DC100%1/1
delete $params{'PR'} || 0
delete $params{'PR'}
Truth table100%2/2
A
A= delete $params{'PR'}
Aresult
11
00
21 4 ▶ my $callback = delete($params{callback}) || sub {};
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
delete $params{'callback'} || sub { }
l!l&&r!l&&!r
040
MC/DC0%0/2
delete $params{'callback'} || sub { }
delete $params{'callback'} sub { }
Truth table33%1/3
A || B
A= delete $params{'callback'}B= sub { }
ABresult
1X1
011
000
22 4 my $gm = delete($params{graph_maker});
23 4 ▶ croak "graph_maker must be a reference to a function that creates a Graph.\n" if $gm && ref($gm) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $gm and ref $gm ne "CODE" 0 4 4
Condition33%1/3
$gm and ref $gm ne "CODE"
!ll&&!rl&&r
400
MC/DC0%0/2
$gm and ref $gm ne "CODE"
$gm ref $gm ne "CODE"
Truth table33%1/3
A and B
A= $gmB= ref $gm ne "CODE"
ABresult
0X0
100
111
24 4 ▶ $gm ||= sub { new Graph(@_); };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$gm ||= sub { "Graph"->new(@_); }
l!l&&r!l&&!r
040
MC/DC0%0/2
$gm ||= sub { "Graph"->new(@_); }
$gm sub { "Graph"->new(@_); }
Truth table33%1/3
A ||= B
A= $gmB= sub { "Graph"->new(@_); }
ABresult
1X1
011
000
25
26 4 ▶ $m = $n if $m > $n;
Branch: 1 branch
BranchTrueFalseTotal
if $m > $n 0 4 4
27
28 4 my $g = $gm->(%params);
29 4 my @v = (1..$n);
30
31 4 my %deg;
32 4 $deg{$_} = 0 foreach (1..$n);
33
34
35 # MUST start with the $m_0 nodes connected to guarentee connectivity
36 4 for my $i(1..$m_0)
37 {
38 10 ▶ my $j = $i == $m_0 ? 1 : $i+1;
Branch: 1 branch
BranchTrueFalseTotal
$i == $m_0 ? : 4 6 10
39 10 $g->add_edge($i, $j);
40 10 ▶ $g->add_edge($j, $i) unless $g->is_undirected();
Branch: 1 branch
BranchTrueFalseTotal
unless $g->is_undirected 6 4 10
41 10 $deg{$i}++;
42 10 $deg{$j}++;
43 }
44
45 # Preferential Attachment (PA) Growth
46 4 my $num_steps = $n - $m_0;
47 4 foreach my $t(1..$num_steps)
48 {
49 190 my $new_node = $m_0 + $t;
50 190 my $sel_node;
51
52 190 foreach my $j(1..$m)
53 {
54 # Triad Formation
55 380 ▶ if($sel_node && random_uniform() < $p)
Branch: 1 branch
BranchTrueFalseTotal
if ($sel_node and random_uniform() < $p) 22 358 380
Condition100%3/3
$sel_node and random_uniform() < $p
!ll&&!rl&&r
19016822
MC/DC100%2/2
$sel_node and random_uniform() < $p
$sel_node random_uniform() < $p
Truth table100%3/3
A and B
A= $sel_nodeB= random_uniform() < $p
ABresult
0X0
100
111
56 {
57 22 my @succs = grep {$_ != $new_node} $g->successors($sel_node);
58 22 ▶ my $tri_node = $succs[random_uniform_integer(1, 0, @succs ? @succs-1 : 0)];
Branch: 1 branch
BranchTrueFalseTotal
@succs ? : 22 0 22
59 22 ▶ if($tri_node && !$g->has_edge($new_node, $tri_node))
Branch: 1 branch
BranchTrueFalseTotal
if ($tri_node and not $g->has_edge($new_node, $tri_node)) 22 0 22
Condition33%1/3
$tri_node and not $g->has_edge($new_node, $tri_node)
!ll&&!rl&&r
0022
MC/DC0%0/2
$tri_node and not $g->has_edge($new_node, $tri_node)
$tri_node not $g->has_edge($new_node, $tri_node)
Truth table33%1/3
A and not(B)
A= $tri_nodeB= not $g->has_edge($new_node, $tri_node)
ABresult
0X0
100
111
60 {
61 22 $g->add_edge($new_node, $tri_node);
62 22 ▶ $g->add_edge($tri_node, $new_node) unless $g->is_undirected();
Branch: 1 branch
BranchTrueFalseTotal
unless $g->is_undirected 22 0 22
63 22 $deg{$new_node}++;
64 22 $deg{$tri_node}++;
65 22 next;
66 }
67 }
68
69 # Preferential Attachment
70 do
71 358 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$new_node != $sel_node or do { do { my $R = 0; $R += $deg{$_} foreach (1 .. $t); $R *= random_uniform(); my $i = 1; my $cs = 0; while ($cs < $R) { $cs += $deg{$i}; ++$i; } ; $sel_node = $i > 1 ? $i - 1 : random_uniform_integer(1, 1, $m_0) } } 358 0 358
72 358 my $R = 0;
73 358 $R += $deg{$_} for (1..$t);
74 358 $R *= random_uniform();
75
76 358 my $i = 1;
77 358 my $cs = 0;
78 358 ▶ while($cs < $R)
Branch: 1 branch
BranchTrueFalseTotal
if ($cs < $R) 3203 358 3561
79 {
80 3203 $cs += $deg{$i};
81 3203 $i++;
82 }
83 358 ▶ $sel_node = $i > 1 ? $i-1 : random_uniform_integer(1, 1, $m_0);
Branch: 1 branch
BranchTrueFalseTotal
$i > 1 ? : 358 0 358
84 } until($new_node != $sel_node);
85
86 358 ▶ unless($g->has_edge($new_node, $sel_node))
Branch: 1 branch
BranchTrueFalseTotal
unless ($g->has_edge($new_node, $sel_node)) 342 16 358
87 {
88 342 $g->add_edge($new_node, $sel_node);
89 342 ▶ $g->add_edge($sel_node, $new_node) unless $g->is_undirected();
Branch: 1 branch
BranchTrueFalseTotal
unless $g->is_undirected 159 183 342
90 342 $deg{$new_node}++;
91 342 $deg{$sel_node}++;
92 }
93 }
94 190 $callback->($g, $new_node);
95 }
96
97 4 return $g;
98 }
99
100 3 Graph::Maker->add_factory_type( 'small_world_hk' => __PACKAGE__ );
101
102 3 1;
103
104 __DATA__
↑ Summary Maker/SmallWorldK.pm »