blib/lib/SNA/Network/Algorithm/Cores.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 90.5% 48 / 53 CC 10 SCAR 21.0
CC
10
Cov
90%
calculate_in_ccs
10.3
BEGIN
1.0
BEGIN
1.0
CRAP 8.1
« blib/lib/SNA/Network/Algorithm/Betweenness.pm ↑ Summary blib/lib/SNA/Network/Filter/Guess.pm »
1 package SNA::Network::Algorithm::Cores;
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(calculate_in_ccs);
9  
10  
11 =head1 NAME
12  
13 SNA::Network::Algorithm::Cores - calculate core collapse sequences (CCS)
14  
15  
16 =head1 SYNOPSIS
17  
18 use SNA::Network;
19  
20 my $net = SNA::Network->new();
21 $net->load_from_pajek_net($filename);
22 ...
23 my $k_max = $net->calculate_in_ccs();
24  
25  
26 =head1 METHODS
27  
28 The following methods are added to L<SNA::Network>.
29  
30  
31 =head2 calculate_in_ccs
32  
33 Calculates the in-core collapse sequence of the graph.
34 All nodes get a maximum core membership k, starting from 0 on.
35 Stores the k value under the hash entry B<k_in_core> for each node object.
36 Returns the maximum k in the network.
37  
38 =cut
39  
40 sub calculate_in_ccs {
41 1 ▶ my ($self) = @_;
Subroutine calculate_in_ccs: called CC=10 SCAR=23.3
Pod documented
42
43 1 foreach ($self->nodes) {
44 10 undef $_->{k_in_core};
45 10 $_->{_open_pres} = int $_->incoming_nodes;
46 }
47
48 1 my $k = 0;
49 1 my @open = $self->nodes;
50
51 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@open and do { do { $k += 1; my(@recheck) = (); foreach my $node (@open) { if ($node->{"_open_pres"} < $k) { $node->{"k_in_core"} = $k - 1; foreach $_ ($node->outgoing_nodes) { $_->{"_open_pres"} -= 1; } ; push @recheck, $node->outgoing_nodes; } ; } ; RECHECK: while (@recheck) { my $node = shift @recheck; next RECHECK if defined $node->{"k_in_core"}; if ($node->{"_open_pres"} < $k) { $node->{"k_in_core"} = $k - 1; foreach $_ ($node->outgoing_nodes) { $_->{"_open_pres"} -= 1; } ; push @recheck, $node->outgoing_nodes; } ; } ; @open = grep({not defined $_->{"k_in_core"};} @open) } } 3 1 4
52 4 $k += 1;
53 4 my @recheck = ();
54
55 4 foreach my $node (@open) {
56 28 ▶ if ($node->{_open_pres} < $k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_open_pres"} < $k) 10 18 28
57 10 $node->{k_in_core} = $k - 1;
58 10 foreach ($node->outgoing_nodes) {
59 23 $_->{_open_pres} -= 1; # unless $_->{k_in_core};
60 }
61 10 push @recheck, $node->outgoing_nodes;
62 #TODO check with smaller id only
63 }
64 }
65  
66 RECHECK:
67 4 ▶ while (@recheck) {
Branch: 1 branch
BranchTrueFalseTotal
if (@recheck) 23 4 27
68 23 my $node = shift @recheck;
69 23 ▶ next RECHECK if defined $node->{k_in_core};
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->{"k_in_core"} 18 5 23
70
71 5 ▶ if ($node->{_open_pres} < $k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_open_pres"} < $k) 0 5 5
72 0 $node->{k_in_core} = $k - 1;
73 0 foreach ($node->outgoing_nodes) {
74 0 $_->{_open_pres} -= 1; # unless $_->{k_in_core};
75 }
76 0 push @recheck, $node->outgoing_nodes;
77 }
78 }
79
80 4 @open = grep { !defined $_->{k_in_core} } @open;
81
82 } while (@open);
83
84 1 return $k - 1;
85 }
86  
87 #TODO try counting
88  
89  
90 =head1 AUTHOR
91  
92 Darko Obradovic, C<< <dobradovic at gmx.de> >>
93  
94 =head1 BUGS
95  
96 Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through
97 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll
98 automatically be notified of progress on your bug as I make changes.
99  
100  
101  
102  
103 =head1 SUPPORT
104  
105 You can find documentation for this module with the perldoc command.
106  
107 perldoc SNA::Network
108  
109  
110 You can also look for information at:
111  
112 =over 4
113  
114 =item * RT: CPAN's request tracker
115  
116 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network>
117  
118 =item * AnnoCPAN: Annotated CPAN documentation
119  
120 L<http://annocpan.org/dist/SNA-Network>
121  
122 =item * CPAN Ratings
123  
124 L<http://cpanratings.perl.org/d/SNA-Network>
125  
126 =item * Search CPAN
127  
128 L<http://search.cpan.org/dist/SNA-Network>
129  
130 =back
131  
132  
133 =head1 ACKNOWLEDGEMENTS
134  
135  
136 =head1 COPYRIGHT & LICENSE
137  
138 Copyright 2009 Darko Obradovic, all rights reserved.
139  
140 This program is free software; you can redistribute it and/or modify it
141 under the same terms as Perl itself.
142  
143  
144 =cut
145  
146 15 1; # End of SNA::Network::Algorithm::Cores
147  
« blib/lib/SNA/Network/Algorithm/Betweenness.pm ↑ Summary blib/lib/SNA/Network/Filter/Guess.pm »