| 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. | ||||||||||
| 33 | Stores the values under the hash entry B<betweenness> for each node object. | ||||||||||
| 34 | Uses the algorithm published by Ulrik Brandes in 2001. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | sub calculate_betweenness { | ||||||||||
| 39 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
calculate_betweenness: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | 1 | foreach ($self->nodes) { | |||||||||
| 42 | 4 | $_->{betweenness} = 0; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | foreach my $source ($self->nodes) { | |||||||||
| 46 | 4 | foreach ($self->nodes) { | |||||||||
| 47 | 16 | $_->{_predecessors} = []; | |||||||||
| 48 | 16 | $_->{_sigma} = 0; | |||||||||
| 49 | 16 | $_->{_delta} = 0; | |||||||||
| 50 | 16 | $_->{_distance} = -1; | |||||||||
| 51 | } | ||||||||||
| 52 | 4 | $source->{_sigma} = 1; | |||||||||
| 53 | 4 | $source->{_distance} = 0; | |||||||||
| 54 | |||||||||||
| 55 | 4 | my @stack = (); | |||||||||
| 56 | 4 | my @queue = ($source); | |||||||||
| 57 | |||||||||||
| 58 | 4 | ▶ | while (@queue) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 9 | my $v = shift @queue; | |||||||||
| 60 | 9 | push @stack, $v; | |||||||||
| 61 | |||||||||||
| 62 | 9 | foreach my $succ ($v->outgoing_nodes) { | |||||||||
| 63 | 6 | ▶ | if ($succ->{_distance} < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 5 | push @queue, $succ; | |||||||||
| 65 | 5 | $succ->{_distance} = $v->{_distance} + 1; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 6 | ▶ | if ( $succ->{_distance} == $v->{_distance} + 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 6 | $succ->{_sigma} += $v->{_sigma}; | |||||||||
| 70 | 6 | push @{ $succ->{_predecessors} }, $v; | |||||||||
| 71 | } | ||||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 4 | foreach my $w (reverse @stack) { | |||||||||
| 76 | 9 | foreach my $pre ( @{ $w->{_predecessors} } ) { | |||||||||
| 77 | 6 | $pre->{_delta} += ( $pre->{_sigma} / $w->{_sigma} ) * ( 1 + $w->{_delta} ); | |||||||||
| 78 | } | ||||||||||
| 79 | 9 | ▶ | $w->{betweenness} += $w->{_delta} if $w != $source; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | # normalise & clean up | ||||||||||
| 84 | 1 | my $n = int $self->nodes; | |||||||||
| 85 | # my $factor = 1 / ( ($n - 1) * ($n - 2) ); | ||||||||||
| 86 | 1 | my $factor = ($n - 1) * ($n - 2); | |||||||||
| 87 | 1 | foreach ($self->nodes) { | |||||||||
| 88 | 4 | delete $_->{_predecessors}; | |||||||||
| 89 | 4 | delete $_->{_sigma}; | |||||||||
| 90 | 4 | delete $_->{_delta}; | |||||||||
| 91 | 4 | delete $_->{_distance}; | |||||||||
| 92 | 4 | $_->{betweenness} /= $factor; | |||||||||
| 93 | } | ||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | =head1 AUTHOR | ||||||||||
| 98 | |||||||||||
| 99 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||
| 100 | |||||||||||
| 101 | =head1 BUGS | ||||||||||
| 102 | |||||||||||
| 103 | Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through | ||||||||||
| 104 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll | ||||||||||
| 105 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | =head1 SUPPORT | ||||||||||
| 111 | |||||||||||
| 112 | You can find documentation for this module with the perldoc command. | ||||||||||
| 113 | |||||||||||
| 114 | perldoc SNA::Network | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | You can also look for information at: | ||||||||||
| 118 | |||||||||||
| 119 | =over 4 | ||||||||||
| 120 | |||||||||||
| 121 | =item * RT: CPAN's request tracker | ||||||||||
| 122 | |||||||||||
| 123 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network> | ||||||||||
| 124 | |||||||||||
| 125 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 126 | |||||||||||
| 127 | L<http://annocpan.org/dist/SNA-Network> | ||||||||||
| 128 | |||||||||||
| 129 | =item * CPAN Ratings | ||||||||||
| 130 | |||||||||||
| 131 | L<http://cpanratings.perl.org/d/SNA-Network> | ||||||||||
| 132 | |||||||||||
| 133 | =item * Search CPAN | ||||||||||
| 134 | |||||||||||
| 135 | L<http://search.cpan.org/dist/SNA-Network> | ||||||||||
| 136 | |||||||||||
| 137 | =back | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 144 | |||||||||||
| 145 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||
| 146 | |||||||||||
| 147 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 148 | under the same terms as Perl itself. | ||||||||||
| 149 | |||||||||||
| 150 | |||||||||||
| 151 | =cut | ||||||||||
| 152 | |||||||||||
| 153 | 15 | 1; # End of SNA::Network::Algorithm::PageRank | |||||||||
| 154 | |||||||||||