| 1 | package Graph::Maker::SmallWorldK; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base qw/Graph::Maker/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Graph; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Graph::Maker; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Graph::Maker::Grid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Math::Random qw/random_uniform random_uniform_integer/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | sub init | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=33 SCAR=35.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | my $p = delete($params{P}) || 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'P'} || 1
MC/DC100%1/1
delete $params{'P'} || 1
delete $params{'P'}
Truth table100%2/2
A
A= delete $params{'P'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | my $q = delete($params{Q}) || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'Q'} || 0
MC/DC100%1/1
delete $params{'Q'} || 0
delete $params{'Q'}
Truth table100%2/2
A
A= delete $params{'Q'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | my $alpha = delete($params{alpha}) || 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'alpha'} || 2
MC/DC0%0/1
delete $params{'alpha'} || 2
delete $params{'alpha'}
Truth table50%1/2
A
A= delete $params{'alpha'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | my $cyclic = delete($params{cyclic}); | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | my $g = new Graph::Maker('grid', dims => [$n, $n], cyclic => $cyclic, %params); | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | my ($v, $na, $nb); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | for my $i(1..$n) | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 24 | for my $j(1..$n) | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 96 | $v = ($i-1)*$n+$j; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Set the positions | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 96 | $g->set_vertex_attribute($v, 'pos', [$i, $j]); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # Handle the extra local contacts | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 96 | ▶ | if($p > 1) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 64 | for my $A(0..$p) | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 192 | for my $B(0..$p) | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 576 | ▶ | next if ($A == 0 && $B == 0) || $A+$B <= 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$A == 0 and $B == 0 or $A + $B <= 1
$A == 0 and $B == 0
MC/DC100%3/3
$A == 0 and $B == 0 or $A + $B <= 1
$A == 0
$B == 0
$A + $B <= 1
Truth table100%5/5
A and B or C
A= $A == 0B= $B == 0C= $A + $B <= 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 384 | for my $a($A, -$A) | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 768 | for my $b($B, -$B) | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1536 | $na = $i + $a; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1536 | $nb = $j + $b; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # print "\t\t[$i $j] [$a $b] [$na $nb]\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1536 | ▶ | next if ($na > $n || $nb > $n) && !$cyclic; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$na > $n || $nb > $n and not $cyclic
$na > $n or $nb > $n
MC/DC100%3/3
$na > $n || $nb > $n and not $cyclic
$na > $n
$nb > $n
not $cyclic
Truth table100%5/5
A or B and not(C)
A= $na > $nB= $nb > $nC= not $cyclic
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1179 | ▶ | $na = ($na % ($n-1))+1 if $na > $n; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1179 | ▶ | $nb = ($nb % ($n-1))+1 if $nb > $n; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # print "\t\tNeg?\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 1179 | ▶ | next if ($na <= 0 || $nb <= 0) && !$cyclic; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$na <= 0 || $nb <= 0 and not $cyclic
$na <= 0 or $nb <= 0
MC/DC100%3/3
$na <= 0 || $nb <= 0 and not $cyclic
$na <= 0
$nb <= 0
not $cyclic
Truth table100%5/5
A or B and not(C)
A= $na <= 0B= $nb <= 0C= not $cyclic
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 876 | ▶ | $na = $n+$na if $na <= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 876 | ▶ | $nb = $n+$nb if $nb <= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 876 | ▶ | if(dist($cyclic, $n, $i, $j, $na, $nb) <= $p) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 540 | ▶ | unless($g->has_edge($v, ($na-1)*$n + $nb)) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 157 | $g->add_edge($v, ($na-1)*$n + $nb); | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 157 | ▶ | $g->add_edge(($na-1)*$n + $nb, $v) unless $g->is_undirected();; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # if($v == 4) | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # { | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # print "\tPOS: [$i, $j] + [$a, $b] = [" . join(', ', ($na, $nb)) . "] = " . (($na-1)*$n+$nb) . " " . dist($cyclic, $n, $i, $j, $na, $nb, 1) . "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # handle the remote connections | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 96 | my $Q = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 96 | ▶ | until($Q >= $q) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 791 | my @a = random_uniform_integer(2, 1, $n); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 791 | $na = ($a[0]-1) * $n + $a[1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 791 | ▶ | next if $na == $v || $g->has_edge($v, $na) || random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1])**-$alpha; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$na == $v or $g->has_edge($v, $na) or random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1]) ** (-$alpha)
$na == $v or $g->has_edge($v, $na)
MC/DC100%3/3
$na == $v or $g->has_edge($v, $na) or random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1]) ** (-$alpha)
$na == $v
$g->has_edge($v, $na)
random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1]) ** (-$alpha)
Truth table100%4/4
A or B or C
A= $na == $vB= $g->has_edge($v, $na)C= random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1]) ** (-$alpha)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 32 | $g->add_edge($v, $na); | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 32 | ▶ | $g->add_edge($na, $v) unless $g->is_undirected();; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 32 | $Q++; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | return $g; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | sub dist | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1243 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dist: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1243 | my ($cyclic, $n, $i, $j, $k, $l, $degb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1243 | ▶ | return abs($i-$k) + abs($j-$l) if !$cyclic; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | #print qq{\t\tmin(abs($i-$k), $n - abs($i-$k)) = } . min(abs($i-$k), $n - abs($i-$k)) if $degb; | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | #print qq{ min(abs($j-$k), $n - abs($j-$k)) = } . min(abs($j-$k), $n - abs($j-$k)) . "\n" if $degb; | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 384 | return min(abs($i-$k), $n - abs($i-$k)) + min(abs($j-$l), $n - abs($j-$l)); | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 768 | ▶ | sub min { $_[0] < $_[1] ? $_[0] : $_[1]; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | Graph::Maker->add_factory_type( 'small_world_k' => __PACKAGE__ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||||||