1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
package Graph::Maker::SmallWorldK;
use strict;
use warnings;
use Carp qw/croak/;
use base qw/Graph::Maker/;
use Graph;
use Graph::Maker;
use Graph::Maker::Grid;
use Math::Random::Free qw/random_uniform random_uniform_integer/;
our $VERSION = '0.01';
sub init
{
my ($self, %params) = @_;
my $n = delete($params{N}) || 0;
my $p = delete($params{P}) || 1;
my $q = delete($params{Q}) || 0;
my $alpha = delete($params{alpha}) || 2;
my $cyclic = delete($params{cyclic});
my $g = new Graph::Maker('grid', dims => [$n, $n], cyclic => $cyclic, %params);
my ($v, $na, $nb);
for my $i(1..$n)
{
for my $j(1..$n)
{
$v = ($i-1)*$n+$j;
# Set the positions
$g->set_vertex_attribute($v, 'pos', [$i, $j]);
# Handle the extra local contacts
if($p > 1)
{
for my $A(0..$p)
{
for my $B(0..$p)
{
next if ($A == 0 && $B == 0) || $A+$B <= 1;
for my $a($A, -$A)
{
for my $b($B, -$B)
{
$na = $i + $a;
$nb = $j + $b;
# print "\t\t[$i $j] [$a $b] [$na $nb]\n";
next if ($na > $n || $nb > $n) && !$cyclic;
$na = ($na % ($n-1))+1 if $na > $n;
$nb = ($nb % ($n-1))+1 if $nb > $n;
# print "\t\tNeg?\n";
next if ($na <= 0 || $nb <= 0) && !$cyclic;
$na = $n+$na if $na <= 0;
$nb = $n+$nb if $nb <= 0;
if(dist($cyclic, $n, $i, $j, $na, $nb) <= $p)
{
unless($g->has_edge($v, ($na-1)*$n + $nb))
{
$g->add_edge($v, ($na-1)*$n + $nb);
$g->add_edge(($na-1)*$n + $nb, $v) unless $g->is_undirected();;
}
}
# if($v == 4)
# {
# print "\tPOS: [$i, $j] + [$a, $b] = [" . join(', ', ($na, $nb)) . "] = " . (($na-1)*$n+$nb) . " " . dist($cyclic, $n, $i, $j, $na, $nb, 1) . "\n";
# }
}
}
}
}
}
# handle the remote connections
my $Q = 0;
until($Q >= $q)
{
my @a = random_uniform_integer(2, 1, $n);
$na = ($a[0]-1) * $n + $a[1];
next if $na == $v || $g->has_edge($v, $na) || random_uniform() > dist($cyclic, $n, $i, $j, $a[0], $a[1])**-$alpha;
$g->add_edge($v, $na);
$g->add_edge($na, $v) unless $g->is_undirected();;
$Q++;
}
}
}
return $g;
}
sub dist
{
my ($cyclic, $n, $i, $j, $k, $l, $degb) = @_;
return abs($i-$k) + abs($j-$l) if !$cyclic;
#print qq{\t\tmin(abs($i-$k), $n - abs($i-$k)) = } . min(abs($i-$k), $n - abs($i-$k)) if $degb;
#print qq{ min(abs($j-$k), $n - abs($j-$k)) = } . min(abs($j-$k), $n - abs($j-$k)) . "\n" if $degb;
return min(abs($i-$k), $n - abs($i-$k)) + min(abs($j-$l), $n - abs($j-$l));
}
sub min { $_[0] < $_[1] ? $_[0] : $_[1]; }
Graph::Maker->add_factory_type( 'small_world_k' => __PACKAGE__ );
1;
__DATA__
=head1 NAME
Graph::Maker::SmallWorldK - Creates a small world graph using Kleinberg's model in 2-dimensions
=head1 VERSION
Version 0.01
=cut
=head1 SYNOPSIS
Creates a small world graph according to Kleinberg's long-range connection model.
In Kleinberg's model a small world graph is connected to nodes within manhattan (L1) distance P and has Q long range contacts
referred to as K(N, P, Q, alpha) or K*(N, P, Q, alpha) (if it wraps-around).
In addition Kleinberg's model gives all nodes a position so that routing can be done efficiently using a greedy algorithm,
these positions are set in the 'pos' attribute for the vertices.
If the graph is directed then edges are added in both directions to create an undirected graph.
use strict;
use warnings;
use Graph;
use Graph::Maker;
use Graph::Maker::SmallWorldK;
my $g = new Graph::Maker('small_world_k', N => 10, P => 2, undirected => 1);
my $g2 = new Graph::Maker('small_world_k', N => 10, P => 2, Q => 1, alpha => 2.1, undirected => 1);
# work with the graph
=head1 FUNCTIONS
=head2 new %params
Creates a small world graph with N^2 nodes in a 2-d grid, with connections to nodes
within P manhattan units of distance, and Q random
long-range connections to nodes determined by d(u, v) ** -alpha (where d is the
manhattan distance) according to
Kleinberg's model (the grid wraps-around if cyclic is specified).
The recognized parameters are N, P, Q, cyclic, graph_maker, and alpha
any others are passed onto L<Graph>'s constructor.
If N is not given it defaults to 0.
If P is not given it defaults to 1.
If Q is not given it defaults to 0.
If alpha is not given it defaults to 2 (2 <= alpha <= 3 allows poly-logarithmic routing using a local greedy algorithm).
If cyclic is set then the "edges" of the grid are connected.
The vertex attribute pos will be set to an array reference of the nodes d-dimensional position.
If graph_maker is specified it will be called to create the Graph class as desired (for example if you have a
subclass of Graph), this defaults to create a Graph with the parameters specified.
=cut
=head1 AUTHOR
Matt Spear, C<< <batman900+cpan at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-graph-maker-SmallWorldK at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Graph-Maker>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 COPYRIGHT & LICENSE
Copyright 2008 Matt Spear, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of Graph::Maker::SmallWorldK
|