| 1 | package SNA::Network::Algorithm::Betweenness; | ||||||||||||||||||||||||||||
| 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_betweenness); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | SNA::Network::Algorithm::Betweenness - Calculate betweenneess values for all nodes | ||||||||||||||||||||||||||||
| 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 $r = $net->calculate_betweenness; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The following methods are added to L<SNA::Network>. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head2 calculate_betweenness | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Calculates exact betweenness centrality values for all nodes and edges. | ||||||||||||||||||||||||||||
| 33 | Stores the values under the hash entry B<betweenness> for each node and edge object. | ||||||||||||||||||||||||||||
| 34 | Uses the algorithm published by Ulrik Brandes in 2001. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | You can pass named parameters to control the algorithm: | ||||||||||||||||||||||||||||
| 37 | B<normalise> specifies whether you want the betweenness values as the plain number of shortest paths | ||||||||||||||||||||||||||||
| 38 | going through the node or edge object (B<normalise => 0>), | ||||||||||||||||||||||||||||
| 39 | or to be normalised to the interval between 0 and 1 (B<normalise => 1>). | ||||||||||||||||||||||||||||
| 40 | It defaults to 1. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | sub calculate_betweenness { | ||||||||||||||||||||||||||
|
Subroutine
calculate_betweenness: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 2 | my ($self, %options) = @_; | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | $options{normalise} = 1 unless defined $options{normalise}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | foreach ($self->nodes, $self->edges) { | |||||||||||||||||||||||||||
| 49 | 16 | $_->{betweenness} = 0; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | foreach my $source ($self->nodes) { | |||||||||||||||||||||||||||
| 53 | 8 | foreach ($self->nodes) { | |||||||||||||||||||||||||||
| 54 | 32 | $_->{_pre} = []; | |||||||||||||||||||||||||||
| 55 | 32 | $_->{_sigma} = 0; | |||||||||||||||||||||||||||
| 56 | 32 | $_->{_delta} = 0; | |||||||||||||||||||||||||||
| 57 | 32 | $_->{_distance} = -1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 8 | $source->{_sigma} = 1; | |||||||||||||||||||||||||||
| 60 | 8 | $source->{_distance} = 0; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | my @stack = (); | |||||||||||||||||||||||||||
| 63 | 8 | my @queue = ($source); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 18 | my $v = shift @queue; | |||||||||||||||||||||||||||
| 67 | 18 | push @stack, $v; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 18 | foreach my $edge ($v->outgoing_edges) { | |||||||||||||||||||||||||||
| 70 | 12 | my $succ = $edge->target; | |||||||||||||||||||||||||||
| 71 | 12 | ▶ | if ($succ->{_distance} < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 10 | push @queue, $succ; | |||||||||||||||||||||||||||
| 73 | 10 | $succ->{_distance} = $v->{_distance} + 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 12 | ▶ | if ( $succ->{_distance} == $v->{_distance} + 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 12 | $succ->{_sigma} += $v->{_sigma}; | |||||||||||||||||||||||||||
| 78 | 12 | $edge->{_sigma} += $v->{_sigma}; | |||||||||||||||||||||||||||
| 79 | 12 | push @{ $succ->{_pre} }, $edge; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 8 | foreach my $w (reverse @stack) { | |||||||||||||||||||||||||||
| 85 | 18 | foreach my $pre ( @{ $w->{_pre} } ) { | |||||||||||||||||||||||||||
| 86 | 12 | $pre->{betweenness} += ( $pre->source->{_sigma} / $w->{_sigma} ) * ( 1 + $w->{_delta} ); | |||||||||||||||||||||||||||
| 87 | 12 | $pre->source->{_delta} += ( $pre->source->{_sigma} / $w->{_sigma} ) * ( 1 + $w->{_delta} ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 18 | ▶ | $w->{betweenness} += $w->{_delta} if $w != $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # normalise & clean up | ||||||||||||||||||||||||||||
| 94 | 2 | my $n = int $self->nodes; | |||||||||||||||||||||||||||
| 95 | 2 | foreach ($self->nodes) { | |||||||||||||||||||||||||||
| 96 | 8 | delete $_->{_pre}; | |||||||||||||||||||||||||||
| 97 | 8 | delete $_->{_sigma}; | |||||||||||||||||||||||||||
| 98 | 8 | delete $_->{_delta}; | |||||||||||||||||||||||||||
| 99 | 8 | delete $_->{_distance}; | |||||||||||||||||||||||||||
| 100 | 8 | ▶ | if ($options{normalise} and $n > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"normalise"} and $n > 2
MC/DC50%1/2
$options{"normalise"} and $n > 2
$options{"normalise"}
$n > 2
Truth table66%2/3
A and B
A= $options{"normalise"}B= $n > 2
| |||||||||||||||||||||||||||||
| 101 | 4 | $_->{betweenness} /= ($n - 1) * ($n - 2); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 2 | ▶ | if ($options{normalise} and $n > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"normalise"} and $n > 1
MC/DC50%1/2
$options{"normalise"} and $n > 1
$options{"normalise"}
$n > 1
Truth table66%2/3
A and B
A= $options{"normalise"}B= $n > 1
| |||||||||||||||||||||||||||||
| 105 | 1 | $_->{betweenness} /= $n * ($n - 1) foreach ($self->edges); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 BUGS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 117 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 118 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | perldoc SNA::Network | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | You can also look for information at: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | L<http://annocpan.org/dist/SNA-Network> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | L<http://cpanratings.perl.org/d/SNA-Network> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | L<http://search.cpan.org/dist/SNA-Network> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 161 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 15 | 1; # End of SNA::Network::Algorithm::PageRank | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||