line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Graph::Similarity::SimRank; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4207
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
5
|
1
|
|
|
1
|
|
357
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Graph; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Graph::Similarity::Method'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has 'graph' => (is => 'rw', isa => 'Graph', required => 1); |
13
|
|
|
|
|
|
|
has 'constant' => (is => 'rw', isa => 'Num', default => 0.6); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
16
|
|
|
|
|
|
|
no Moose; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Set constant value. In the paper, it's 0.8 |
19
|
|
|
|
|
|
|
sub setConst { |
20
|
|
|
|
|
|
|
my ($self, $value) = @_; |
21
|
|
|
|
|
|
|
$self->constant($value); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub calculate { |
25
|
|
|
|
|
|
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $g = $self->graph; |
28
|
|
|
|
|
|
|
my $c = $self->constant; |
29
|
|
|
|
|
|
|
my $itr = $self->num_of_iteration; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Initialize |
32
|
|
|
|
|
|
|
my @all = $g->vertices; |
33
|
|
|
|
|
|
|
my %R; |
34
|
|
|
|
|
|
|
for my $v1 (@all) { |
35
|
|
|
|
|
|
|
for my $v2 (@all) { |
36
|
|
|
|
|
|
|
if ($v1 eq $v2){ |
37
|
|
|
|
|
|
|
$R{$v1}{$v2} = 1; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else { |
40
|
|
|
|
|
|
|
$R{$v1}{$v2} = 0; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
for(my $i=0; $i<$itr; $i++){ |
46
|
|
|
|
|
|
|
for my $v1 (@all) { |
47
|
|
|
|
|
|
|
for my $v2 (@all) { |
48
|
|
|
|
|
|
|
if ($v1 eq $v2){ |
49
|
|
|
|
|
|
|
$R{$v1}{$v2} = 1; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
|
|
|
|
|
|
my @p1 = $g->predecessors($v1); |
53
|
|
|
|
|
|
|
my @p2 = $g->predecessors($v2); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
if (scalar(@p1) == 0 || scalar(@p2) == 0){ |
56
|
|
|
|
|
|
|
$R{$v1}{$v2} = 0; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
|
|
|
|
|
|
my $sum = 0; |
60
|
|
|
|
|
|
|
for my $p1 (@p1){ |
61
|
|
|
|
|
|
|
for my $p2 (@p2) { |
62
|
|
|
|
|
|
|
$sum += $R{$p1}{$p2}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
$R{$v1}{$v2} = ( $c / (scalar(@p1) * scalar(@p2)) ) * $sum; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
$self->_setSimilarity(\%R); |
72
|
|
|
|
|
|
|
return \%R; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Graph::Similarity::SimRank - SimRank implementation |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 VERSION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Version 0.02 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 SYNOPSIS |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Please see L<Graph::Similarity> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This is the implementation of the below paper. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
B<Glen Jeh, Jennifer Widon, "SimRank: A Measure of Structural-Context Similarity"> |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 METHODS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 setConst($const) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
The const value is 0.6 by default becasue wikipedia mentions this is more acturate value, |
99
|
|
|
|
|
|
|
whereas the paper uses 0.8. |
100
|
|
|
|
|
|
|
You can change this to what you want. The value should be between 0 and 1. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 calculate() |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This calculates SimRank. The algorithm is Naive Method in section 4.4.1 in the paper. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 AUTHOR |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Shohei Kameda, C<< <shoheik at cpan.org> >> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Copyright 2012 Shohei Kameda. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
115
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
116
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; # End of Graph::Similarity::SimRank |