-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculating Page Rank.py
69 lines (52 loc) · 1.48 KB
/
Calculating 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
#!/usr/bin/env python
# coding: utf-8
#!/usr/bin/env python
#Encoding: utf-8
#Authors: P. Mayilvahanan and M. Mazourik
#Numerical Algorithms Autumn Semester
## Imports and splitting links file at '\n'
import numpy as np
with open('links.txt') as f:
links = f.read()
links=links.split('\n')[1:-1]
## Generating a dictionary of each page with its links
n=15
link_dic={}
for i in range(n):
link_dic[i+1]=[]
for i in links:
page=int(i.split(' ')[0])
back_link=int(i.split(' ')[1])
link_dic[page].append(back_link)
## Computing the ranking
def compute_ranking(link_dic=link_dic):
# Computing A
A=np.zeros([n,n])
for i in link_dic:
length=len(link_dic[i])
for j in link_dic[i]:
A[j-1][i-1]=1/length
# mew
mew=0.15
# Generating e of 1s
e=np.array([1/n for i in range(15)])
x_new=np.array([1/n for i in range(15)])
inf_norm=1999
iterations=0
# Iterating until inf norm condition is satisfied
while inf_norm >= 1e-8:
iterations+=1
x_old=x_new.copy()
x_new=(1-mew)*np.matmul(A,x_new)+mew*e
inf_norm=np.max(np.abs(x_new-x_old))
# Ranking and printing
ranking=np.argsort(x_new)[::-1]+1
print('Number of Iterations', iterations)
print('Eigen Vector', x_new)
print('Ranking', ranking)
# Printing for first case
compute_ranking(link_dic)
link_dic[14]+=[14]
# Printing after adding link to 14
print('--------------')
compute_ranking(link_dic)