blib/lib/SNA/Network/Generator/ByDensity.pm

filter: Statementstmt 100% 22 / 22 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 91.6% 33 / 36 CC 7 SCAR 16.5
CC
7
Cov
97%
generate_by_density
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
« blib/lib/SNA/Network/Filter/Pajek.pm ↑ Summary Build »
1 package SNA::Network::Generator::ByDensity;
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_density);
9  
10  
11 =head1 NAME
12  
13 SNA::Network::Generator::ByDensity - Generate random networks by density
14  
15  
16 =head1 SYNOPSIS
17  
18 use SNA::Network;
19  
20 my $net = SNA::Network->new();
21 $net->generate_by_density( nodes => 100, density => 0.05);
22 # or
23 $net->generate_by_density( nodes => 100, edges => 445);
24 ...
25  
26  
27 =head1 METHODS
28  
29 The following methods are added to L<SNA::Network>.
30  
31  
32 =head2 generate_by_density
33  
34 Generates a network with the given number of nodes
35 and the given density OR number of edges.
36 The network is not guaranteed to have the resulting number of edges,
37 but it it stochasticly expected to have them.
38 Expected Degrees are the same for all nodes.
39  
40 =cut
41  
42 sub generate_by_density {
43 2 ▶ my ($self, %params) = @_;
Subroutine generate_by_density: called CC=7 SCAR=19.5
Pod documented
44  
45 2 for (0 .. $params{nodes} - 1) {
46 200 $self->create_node_at_index(index => $_);
47 }
48
49 2 my $density = $params{density};
50 2 ▶ $density ||= $params{edges} / ($params{nodes} ** 2 - $params{nodes});
Condition66%2/3
$density ||= $params{"edges"} / ($params{"nodes"} ** 2 - $params{"nodes"})
l!l&&r!l&&!r
110
MC/DC0%0/2
$density ||= $params{"edges"} / ($params{"nodes"} ** 2 - $params{"nodes"})
$density $params{"edges"} / ($params{"nodes"} ** 2 - $params{"nodes"})
Truth table66%2/3
A ||= B
A= $densityB= $params{"edges"} / ($params{"nodes"} ** 2 - $params{"nodes"})
ABresult
1X1
011
000
51
52 2 for my $s (0 .. $params{nodes} - 1) {
53 200 for my $t (0 .. $params{nodes} - 1) {
54 20000 ▶ next if $s == $t;
Branch: 1 branch
BranchTrueFalseTotal
if $s == $t 200 19800 20000
55 19800 ▶ if ( rand() < $density ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $density) 891 18909 19800
56 891 $self->create_edge( source_index => $s, target_index => $t );
57 }
58 }
59 }
60 }
61  
62  
63  
64 =head1 AUTHOR
65  
66 Darko Obradovic, C<< <dobradovic at gmx.de> >>
67  
68 =head1 BUGS
69  
70 Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through
71 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll
72 automatically be notified of progress on your bug as I make changes.
73  
74  
75  
76  
77 =head1 SUPPORT
78  
79 You can find documentation for this module with the perldoc command.
80  
81 perldoc SNA::Network
82  
83  
84 You can also look for information at:
85  
86 =over 4
87  
88 =item * RT: CPAN's request tracker
89  
90 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network>
91  
92 =item * AnnoCPAN: Annotated CPAN documentation
93  
94 L<http://annocpan.org/dist/SNA-Network>
95  
96 =item * CPAN Ratings
97  
98 L<http://cpanratings.perl.org/d/SNA-Network>
99  
100 =item * Search CPAN
101  
102 L<http://search.cpan.org/dist/SNA-Network>
103  
104 =back
105  
106  
107 =head1 ACKNOWLEDGEMENTS
108  
109  
110 =head1 COPYRIGHT & LICENSE
111  
112 Copyright 2009 Darko Obradovic, all rights reserved.
113  
114 This program is free software; you can redistribute it and/or modify it
115 under the same terms as Perl itself.
116  
117  
118 =cut
119  
120 15 1; # End of SNA::Network::Generator::ByDensity
121  
« blib/lib/SNA/Network/Filter/Pajek.pm ↑ Summary Build »