| 1 | package SNA::Network::Algorithm::HITS; | ||||||||||
| 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_authorities_and_hubs _kleinberg_iteration); | |||||||||
| 9 | |||||||||||
| 10 | 15 | ▶ | use List::Util qw(sum); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | SNA::Network::Algorithm::HITS - implementation of Kleinberg's HITS algorithm | ||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | use SNA::Network; | ||||||||||
| 21 | |||||||||||
| 22 | my $net = SNA::Network->new(); | ||||||||||
| 23 | $net->load_from_pajek_net($filename); | ||||||||||
| 24 | ... | ||||||||||
| 25 | $net->calculate_authorities_and_hubs(); | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | =head1 METHODS | ||||||||||
| 29 | |||||||||||
| 30 | The following methods are added to L<SNA::Network>. | ||||||||||
| 31 | |||||||||||
| 32 | =head2 calculate_authorities_and_hubs | ||||||||||
| 33 | |||||||||||
| 34 | Calculates authority and hub coefficients for all nodes. | ||||||||||
| 35 | Stores the coefficients under the hash entries B<authority> and B<hub> for each node object. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | sub calculate_authorities_and_hubs { | ||||||||||
| 40 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
calculate_authorities_and_hubs: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 41 | |||||||||||
| 42 | # arbitrary start vectors | ||||||||||
| 43 | 1 | my $x = [ (1) x int $self->nodes() ]; | |||||||||
| 44 | 1 | my $y = [ (1) x int $self->nodes() ]; | |||||||||
| 45 | |||||||||||
| 46 | # 20 iterations by default, see paper | ||||||||||
| 47 | 1 | for (1 .. 20) { | |||||||||
| 48 | 20 | ($x, $y) = $self->_kleinberg_iteration($x, $y); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1 | my $index = 0; | |||||||||
| 52 | 1 | foreach my $node ($self->nodes()) { | |||||||||
| 53 | 4 | $node->{authority} = $x->[$index]; | |||||||||
| 54 | 4 | $node->{hub} = $y->[$index++]; | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | sub _kleinberg_iteration { | ||||||||||
| 60 | 20 | ▶ | my ($self, $x_in, $y_in) = @_; | ||||||||
|
Subroutine
_kleinberg_iteration: called CC=3 SCAR=11.0
| |||||||||||
| 61 | |||||||||||
| 62 | # calculate new authority coefficients | ||||||||||
| 63 | my @x_new = map { | ||||||||||
| 64 | 20 | my $x_node = $self->node_at_index($_); | |||||||||
| 65 | sum( | ||||||||||
| 66 | map { | ||||||||||
| 67 | 80 | ▶ | $y_in->[$_->index()] # y's current hub coefficient | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | } $x_node->incoming_nodes() | ||||||||||
| 69 | ) || 0 | ||||||||||
| 70 | } (0 .. int $self->nodes() - 1); | ||||||||||
| 71 | |||||||||||
| 72 | # calculate new hub coefficients | ||||||||||
| 73 | my @y_new = map { | ||||||||||
| 74 | 20 | my $y_node = $self->node_at_index($_); | |||||||||
| 75 | sum( | ||||||||||
| 76 | map { | ||||||||||
| 77 | 80 | ▶ | $x_in->[$_->index()] # x's current authority coefficient | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } $y_node->outgoing_nodes() | ||||||||||
| 79 | ) || 0 | ||||||||||
| 80 | } (0 .. int $self->nodes() - 1); | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | # _normalise vectors so sum of squares is 1 | ||||||||||
| 84 | 20 | _normalise(\@x_new); | |||||||||
| 85 | 20 | _normalise(\@y_new); | |||||||||
| 86 | |||||||||||
| 87 | 20 | return (\@x_new, \@y_new); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | sub _normalise { | ||||||||||
| 92 | 40 | ▶ | my ($vector) = @_; | ||||||||
|
Subroutine
_normalise: called CC=2 SCAR=6.9
| |||||||||||
| 93 | my $squared_sum = sum( | ||||||||||
| 94 | 40 | map { $_ ** 2 } @{$vector} | |||||||||
| 95 | ); | ||||||||||
| 96 | 40 | my $normalisation = 1 / sqrt $squared_sum; | |||||||||
| 97 | 40 | foreach (@{$vector}) { | |||||||||
| 98 | 160 | $_ *= $normalisation; | |||||||||
| 99 | } | ||||||||||
| 100 | 40 | return $vector; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | =head1 AUTHOR | ||||||||||
| 105 | |||||||||||
| 106 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||
| 107 | |||||||||||
| 108 | =head1 BUGS | ||||||||||
| 109 | |||||||||||
| 110 | Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through | ||||||||||
| 111 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll | ||||||||||
| 112 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | =head1 SUPPORT | ||||||||||
| 118 | |||||||||||
| 119 | You can find documentation for this module with the perldoc command. | ||||||||||
| 120 | |||||||||||
| 121 | perldoc SNA::Network | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | You can also look for information at: | ||||||||||
| 125 | |||||||||||
| 126 | =over 4 | ||||||||||
| 127 | |||||||||||
| 128 | =item * RT: CPAN's request tracker | ||||||||||
| 129 | |||||||||||
| 130 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network> | ||||||||||
| 131 | |||||||||||
| 132 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 133 | |||||||||||
| 134 | L<http://annocpan.org/dist/SNA-Network> | ||||||||||
| 135 | |||||||||||
| 136 | =item * CPAN Ratings | ||||||||||
| 137 | |||||||||||
| 138 | L<http://cpanratings.perl.org/d/SNA-Network> | ||||||||||
| 139 | |||||||||||
| 140 | =item * Search CPAN | ||||||||||
| 141 | |||||||||||
| 142 | L<http://search.cpan.org/dist/SNA-Network> | ||||||||||
| 143 | |||||||||||
| 144 | =back | ||||||||||
| 145 | |||||||||||
| 146 | |||||||||||
| 147 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 148 | |||||||||||
| 149 | |||||||||||
| 150 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 151 | |||||||||||
| 152 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||
| 153 | |||||||||||
| 154 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 155 | under the same terms as Perl itself. | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =cut | ||||||||||
| 159 | |||||||||||
| 160 | 15 | 1; # End of SNA::Network::Algorithm::HITS | |||||||||
| 161 | |||||||||||