| 1 | package SNA::Network::Generator::ConfigurationModel; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 15 | require Exporter; | |||||||||||||||||||
| 7 | 15 | ▶ | use base qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 15 | our @EXPORT = qw(generate_by_configuration_model); | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 15 | ▶ | use List::Util qw(min shuffle); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | SNA::Network::Generator::ConfigurationModel - Generate random networks according to the configuration model | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | use SNA::Network; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | my $net = SNA::Network->new(); | ||||||||||||||||||||
| 23 | $net->generate_by_configuration_model( $other_network ); | ||||||||||||||||||||
| 24 | ... | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 METHODS | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | The following methods are added to L<SNA::Network>. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head2 generate_by_configuration_model | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | Generates a network according to the configuration model. | ||||||||||||||||||||
| 35 | This means that the random network will have the same degree sequence | ||||||||||||||||||||
| 36 | as the base network which is passed as a parameter. | ||||||||||||||||||||
| 37 | So for each node from the base network, the random network | ||||||||||||||||||||
| 38 | will have a corresponding node with the same in- and outdegree. | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | Note however that this method is prone to produce a few edges less, | ||||||||||||||||||||
| 41 | that cannot be matched in the end because they would introduce cycles | ||||||||||||||||||||
| 42 | or double edges otherwise. | ||||||||||||||||||||
| 43 | So in practice the random network will be only very close to the base networks degree sequence | ||||||||||||||||||||
| 44 | in most cases. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 1 | ▶ | sub generate_by_configuration_model { | ||||||||||||||||||
|
Subroutine
generate_by_configuration_model: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 49 | 1 | my ($self, $base_network) = @_; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # create nodes and stubs | ||||||||||||||||||||
| 52 | 1 | foreach my $base_node ($base_network->nodes) { | |||||||||||||||||||
| 53 | 7 | my $random_node = $self->create_node_at_index( | |||||||||||||||||||
| 54 | index => $base_node->index, | ||||||||||||||||||||
| 55 | name => 'random', | ||||||||||||||||||||
| 56 | _missing_inbound_links => $base_node->in_degree, | ||||||||||||||||||||
| 57 | _target_out_degree => $base_node->out_degree, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # create edges | ||||||||||||||||||||
| 62 | 1 | foreach my $node (shuffle $self->nodes) { | |||||||||||||||||||
| 63 | my @destinations = grep { | ||||||||||||||||||||
| 64 | 7 | $_->{_missing_inbound_links} > 0 && $_->{index} != $node->{index} | |||||||||||||||||||
| 65 | 49 | ▶ | } shuffle $self->nodes; | ||||||||||||||||||
|
Condition100%3/3
$_->{'_missing_inbound_links'} > 0 && $_->{'index'} != $node->{'index'}
MC/DC100%2/2
$_->{'_missing_inbound_links'} > 0 && $_->{'index'} != $node->{'index'}
$_->{'_missing_inbound_links'} > 0
$_->{'index'} != $node->{'index'}
Truth table100%3/3
A && B
A= $_->{'_missing_inbound_links'} > 0B= $_->{'index'} != $node->{'index'}
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 7 | my $last_index = min(@destinations - 1, $node->{_target_out_degree} - 1); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 7 | foreach ( @destinations[0 .. $last_index] ) { | |||||||||||||||||||
| 70 | 7 | $self->create_edge( source_index => $node->{index}, target_index => $_->index ); | |||||||||||||||||||
| 71 | 7 | $_->{_missing_inbound_links} -= 1; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 AUTHOR | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 BUGS | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through | ||||||||||||||||||||
| 84 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll | ||||||||||||||||||||
| 85 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head1 SUPPORT | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | perldoc SNA::Network | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | You can also look for information at: | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =over 4 | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network> | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | L<http://annocpan.org/dist/SNA-Network> | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =item * CPAN Ratings | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | L<http://cpanratings.perl.org/d/SNA-Network> | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =item * Search CPAN | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | L<http://search.cpan.org/dist/SNA-Network> | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =back | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 128 | under the same terms as Perl itself. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 15 | 1; # End of SNA::Network::Generator::ConfigurationModel | |||||||||||||||||||
| 134 | |||||||||||||||||||||