| 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
MC/DC0%0/1
delete $params{'N'} || 0
delete $params{'N'}
Truth table50%1/2
A
A= delete $params{'N'}
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | my $m = delete($params{M}) || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'M'} || 1
MC/DC0%0/1
delete $params{'M'} || 1
delete $params{'M'}
Truth table50%1/2
A
A= delete $params{'M'}
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | my $m_0 = delete($params{M_0}) || $m; | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $params{'M_0'} || $m
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
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | my $p = delete($params{PR}) || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'PR'} || 0
MC/DC100%1/1
delete $params{'PR'} || 0
delete $params{'PR'}
Truth table100%2/2
A
A= delete $params{'PR'}
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | my $callback = delete($params{callback}) || sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Condition33%1/3
delete $params{'callback'} || sub {
}
MC/DC0%0/2
delete $params{'callback'} || sub {
}
delete $params{'callback'}
sub {
}
Truth table33%1/3
A || B
A= delete $params{'callback'}B= sub {
}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$gm and ref $gm ne "CODE"
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"
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | $gm ||= sub { new Graph(@_); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$gm ||= sub {
"Graph"->new(@_);
}
MC/DC0%0/2
$gm ||= sub {
"Graph"->new(@_);
}
$gm
sub {
"Graph"->new(@_);
}
Truth table33%1/3
A ||= B
A= $gmB= sub {
"Graph"->new(@_);
}
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | $m = $n if $m > $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 39 | 10 | $g->add_edge($i, $j); | |||||||||||||||||||||||||||
| 40 | 10 | ▶ | $g->add_edge($j, $i) unless $g->is_undirected(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$sel_node and random_uniform() < $p
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | 22 | ▶ | if($tri_node && !$g->has_edge($new_node, $tri_node)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tri_node and not $g->has_edge($new_node, $tri_node)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | } until($new_node != $sel_node); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 358 | ▶ | unless($g->has_edge($new_node, $sel_node)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||