| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Algorithm::SocialNetwork; | 
| 2 | 1 |  |  | 1 |  | 1115 | use Spiffy -Base; | 
|  | 1 |  |  |  |  | 4853 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 3 | 1 |  |  | 1 |  | 2364 | our $VERSION = '0.01_01'; | 
|  | 1 |  |  | 1 |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
|  | 1 |  |  |  |  | 19 |  | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 428 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | field graph => {}, | 
| 6 |  |  |  |  |  |  | -init => 'Graph->new()'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | ### negative value doesn't make sense for Bc | 
| 9 |  |  |  |  |  |  | ### Un-normlized result. | 
| 10 | 0 |  |  | 0 | 0 |  | sub BetweenessCentrality { | 
| 11 | 0 |  |  |  |  |  | my @V  = $self->graph->vertices; | 
| 12 | 0 |  |  |  |  |  | my %CB; @CB{@V}=map{0}@V; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 13 | 0 |  |  |  |  |  | for my $s (@V) { | 
| 14 | 0 |  |  |  |  |  | my (@S,$P,%sigma,%d,@Q); | 
| 15 | 0 |  |  |  |  |  | $P->{$_} = [] for (@V); | 
| 16 | 0 |  |  |  |  |  | @sigma{@V} = map{0}@V; $sigma{$s} = 1; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 17 | 0 |  |  |  |  |  | @d{@V} = map{-1}@V; $d{$s} = 0; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 18 | 0 |  |  |  |  |  | push @Q,$s; | 
| 19 | 0 |  |  |  |  |  | while(@Q) { | 
| 20 | 0 |  |  |  |  |  | my $v = shift @Q; | 
| 21 | 0 |  |  |  |  |  | push @S,$v; | 
| 22 | 0 |  |  |  |  |  | for my $w ($self->graph->neighbors($v)) { | 
| 23 | 0 | 0 |  |  |  |  | if($d{$w} < 0) { | 
| 24 | 0 |  |  |  |  |  | push @Q,$w; | 
| 25 | 0 |  |  |  |  |  | $d{$w} = $d{$v} + 1; | 
| 26 |  |  |  |  |  |  | } | 
| 27 | 0 | 0 |  |  |  |  | if($d{$w} == $d{$v} + 1) { | 
| 28 | 0 |  |  |  |  |  | $sigma{$w} += $sigma{$v}; | 
| 29 | 0 |  |  |  |  |  | push @{$P->{$w}},$v; | 
|  | 0 |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | } | 
| 33 | 0 |  |  |  |  |  | my %rho; $rho{$_} = 0 for(@V); | 
|  | 0 |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | while(@S) { | 
| 35 | 0 |  |  |  |  |  | my $w = pop @S; | 
| 36 | 0 |  |  |  |  |  | for my $v (@{$P->{$w}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 37 | 0 |  |  |  |  |  | $rho{$v} += ($sigma{$v}/$sigma{$w})*(1+$rho{$w}); | 
| 38 |  |  |  |  |  |  | } | 
| 39 | 0 | 0 |  |  |  |  | $CB{$w} += $rho{$w} unless $w eq $s; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 0 |  |  |  |  |  | return $CB{$_} for(@_); | 
| 43 | 0 | 0 |  |  |  |  | return @_? @CB{@_} : \%CB; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | __END__ |