Skip to content

Commit

Permalink
Add: page rank
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyBag committed Nov 30, 2020
1 parent 24512da commit cd89fe5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 21.PageRank/PageRank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np
import sys
import os
from pathlib import Path
sys.path.append(str(Path(os.path.abspath(__file__)).parent.parent))
from utils import *

def pageRank(graph, d, max_iteration=1000, epsilon=1e-8):
"""
given a n * n link graph
graph[i, j] = 1 means that there is a link from i to j
d is the proportion of neighbours in the definition of page rank
return the probablisitic for a user visiting each page
"""
n, _ = graph.shape
p = np.ones(n) / n
graph /= (graph.sum(axis=-1, keepdims=True) + epsilon)
graph = graph.T
for i in range(max_iteration):
pre_p = p
p = d * graph @ p + (1 - d) / n
if max(p - pre_p) < epsilon:
break
return p

if __name__ == '__main__':
def demonstrate(graph, d, desc):
print(desc)
p = pageRank(graph, d=d)
print('The probability of each node is', np.round(p, 2))

graph = np.array(
[[0, 1, 1, 1],
[1, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 1, 0]]
).astype(float)
demonstrate(graph, .8, 'Example 1')

graph = np.array(
[[0, 1, 1],
[0, 0, 1],
[1, 0, 0]]
).astype(float)
demonstrate(graph, .85, 'Example 2')

0 comments on commit cd89fe5

Please sign in to comment.