-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_rank.py
185 lines (130 loc) · 5.06 KB
/
page_rank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from GraphBLAS import *
from GraphBLAS.operators import *
def page_rank(
graph: Matrix,
page_rank: Vector,
damping_factor=0.85,
threshold=1.e-5,
max_iters = None):
rows, cols = graph.shape
if rows != cols or page_rank.shape[0] != rows:
raise Error()
m = Matrix(shape=graph.shape, dtype=float)
m[None] = apply(Identity, graph)
# Normalize the edge weights of the graph by the vertices out-degree
utilities.normalize_rows(m)
times_damping_factor = UnaryOp("Times", damping_factor)
# scale the normalized edge weights by the damping factor
m[None] = apply(times_damping_factor, m)
add_scaled_teleport = UnaryOp("Plus", (1.0 - damping_factor) / rows)
page_rank[:] = 1.0 / rows
new_rank = Vector(shape=page_rank.shape, dtype=m.dtype)
delta = Vector(shape=page_rank.shape, dtype=m.dtype)
i = 0
while i != max_iters:
#for i in range(0, max_iters):
# Compute the new rank: [1 x M][M x N] = [1 x N]
with Accumulator("Second"), ArithmeticSemiring:
new_rank[None] += page_rank @ m
# [1 x M][M x 1] = [1 x 1] = always (1 - damping_factor)
# rank*(m + scaling_mat*teleport): [1 x 1][1 x M] + [1 x N] = [1 x M]
new_rank[None] = apply(add_scaled_teleport, new_rank)
# Test for convergence - compute squared error
# @todo should be mean squared error. (divide r2/N)
squared_error = 0.0
with BinaryOp("Minus"):
delta[None] = page_rank + new_rank
with BinaryOp("Times"):
delta[None] = delta * delta
squared_error = reduce(PlusMonoid, delta).eval(0.0)
page_rank[:] = new_rank
# check mean-squared error
if (squared_error / rows) < threshold:
return page_rank
# for any elements missing from page rank vector we need to set
# to scaled teleport.
new_rank[:] = (1.0 - damping_factor) / rows
with BinaryOp("Plus"):
page_rank[~page_rank] = page_rank + new_rank
def page_rank_naive(
graph: Matrix,
page_rank: Vector,
damping_factor=0.85,
threshold=1.e-5,
max_iters = None):
rows, cols = graph.shape
if rows != cols or page_rank.shape[0] != rows:
raise Error()
m = Matrix(shape=graph.shape, dtype=float)
m = apply(Identity, graph)
# Normalize the edge weights of the graph by the vertices out-degree
utilities.normalize_rows(m)
times_damping_factor = UnaryOp("Times", damping_factor)
# scale the normalized edge weights by the damping factor
m = apply(times_damping_factor, m)
add_scaled_teleport = UnaryOp("Plus", (1.0 - damping_factor) / rows)
page_rank[:] = 1.0 / rows
new_rank = Vector(shape=page_rank.shape, dtype=m.dtype)
delta = Vector(shape=page_rank.shape, dtype=m.dtype)
i = 0
while i != max_iters:
#for i in range(0, max_iters):
# Compute the new rank: [1 x M][M x N] = [1 x N]
with Accumulator("Second"), ArithmeticSemiring:
new_rank[None] += page_rank @ m
# [1 x M][M x 1] = [1 x 1] = always (1 - damping_factor)
# rank*(m + scaling_mat*teleport): [1 x 1][1 x M] + [1 x N] = [1 x M]
new_rank = apply(add_scaled_teleport, new_rank)
# Test for convergence - compute squared error
# @todo should be mean squared error. (divide r2/N)
squared_error = 0.0
with BinaryOp("Minus"):
delta = page_rank + new_rank
with BinaryOp("Times"):
delta = delta * delta
squared_error = reduce(PlusMonoid, delta).eval(0.0)
page_rank[:] = new_rank
# check mean-squared error
if (squared_error / rows) < threshold:
return page_rank
# for any elements missing from page rank vector we need to set
# to scaled teleport.
new_rank[:] = (1.0 - damping_factor) / rows
with BinaryOp("Plus"):
page_rank[~page_rank] = page_rank + new_rank
if __name__=="__main__":
NUM_NODES = 12;
i = [
0, 0, 0, 0,
1, 1, 1,
2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4,
5, 5,
6, 6, 6,
7, 7, 7, 7,
8, 8, 8, 8,
9, 9, 9,
10,10,10,10,
11,11]
j = [
1, 5, 6, 9,
0, 2, 4,
1, 3, 4,
2, 7, 8, 10,
1, 2, 6, 7,
0, 9,
0, 4, 9,
3, 4, 8, 10,
3, 7, 10, 11,
0, 5, 6,
3, 7, 8, 11,
8, 10]
v = [1.0] * len(i)
m1 = Matrix((v, (i, j)), shape=(NUM_NODES, NUM_NODES))
#print(m1)
rank = Vector(shape=NUM_NODES, dtype=float)
page_rank(m1, rank)
print(rank)
rank = Vector(shape=NUM_NODES, dtype=float)
algorithms.page_rank(m1, rank)