line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::PageRank; |
2
|
|
|
|
|
|
|
$|++; |
3
|
2
|
|
|
2
|
|
57796
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
82
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1494
|
use fields qw(graph prvect size); |
|
2
|
|
|
|
|
3371
|
|
|
2
|
|
|
|
|
13
|
|
8
|
|
|
|
|
|
|
|
9
|
0
|
|
|
0
|
|
|
sub new { bless {}, shift } |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
183
|
use List::Util; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
180
|
|
12
|
2
|
|
|
2
|
|
5090
|
use PDL; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#use Data::Dumper; |
14
|
|
|
|
|
|
|
#use PDL::IO::Dumper; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#*STDERR = \*STDOUT; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $d_factor = 0.05; # dampening factor |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub graph { |
21
|
|
|
|
|
|
|
my $self = shift; |
22
|
|
|
|
|
|
|
my $graph = shift; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
die "Odd number of node numbers is not valid\n" if scalar(@$graph)%2; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $size = List::Util::max(@$graph)+1; |
27
|
|
|
|
|
|
|
$self->{size} = $size; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$self->{graph} = zeroes($size, $size); |
30
|
|
|
|
|
|
|
for (my $i = 0 ; $i<@$graph ; $i+=2){ |
31
|
|
|
|
|
|
|
$self->{graph}->index2d( |
32
|
|
|
|
|
|
|
$graph->[$i], |
33
|
|
|
|
|
|
|
$graph->[$i+1], |
34
|
|
|
|
|
|
|
) .= 1; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
foreach my $i (0..$self->{size}-1){ |
38
|
|
|
|
|
|
|
my $outdeg_sum = sum $self->{graph}->slice(join q/:/, $i, $i); |
39
|
|
|
|
|
|
|
if($outdeg_sum){ |
40
|
|
|
|
|
|
|
$self->{graph}->slice(join q/:/, $i, $i) /= |
41
|
|
|
|
|
|
|
$outdeg_sum; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$self->{graph} = transpose $self->{graph}; |
46
|
|
|
|
|
|
|
$self->{prvect} = ones($size) / $size; # the initial pagerank |
47
|
|
|
|
|
|
|
# print $self->{graph}->slice(":"); |
48
|
|
|
|
|
|
|
# print sdump $self; |
49
|
|
|
|
|
|
|
# print $self->{prvect}->slice(":"); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub iterate { |
53
|
|
|
|
|
|
|
my $self = shift; |
54
|
|
|
|
|
|
|
my $iter = shift || 100; |
55
|
|
|
|
|
|
|
my $normal_factor = $d_factor/$self->{size}; |
56
|
|
|
|
|
|
|
my $inv_d_factor = 1 - $d_factor; |
57
|
|
|
|
|
|
|
# print $self->{prvect}->slice(":"); |
58
|
|
|
|
|
|
|
# print $self->{graph}->slice(":"); |
59
|
|
|
|
|
|
|
foreach (1..$iter){ |
60
|
|
|
|
|
|
|
$self->{prvect} = |
61
|
|
|
|
|
|
|
$inv_d_factor * $self->{prvect} x $self->{graph} + $normal_factor * $self->{prvect}; |
62
|
|
|
|
|
|
|
# print $self->{prvect}->slice(":"); |
63
|
|
|
|
|
|
|
# print sdump (($d_factor/$self->{size}) * $self->{prvect}); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub result { |
69
|
|
|
|
|
|
|
my $self = shift; |
70
|
|
|
|
|
|
|
$self->{prvect}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
__END__ |