-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank_vac.py
47 lines (41 loc) · 1.1 KB
/
pagerank_vac.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
import sys
input = sys.stdin.readline
def step(adj,PR,outdeg):
new_PR = [0.]*len(PR)
for children,points,deg in zip(adj,PR,outdeg):
cut = points/deg
for child in children:
new_PR[child] += cut
return new_PR
def main():
##########
# Inputs #
##########
N,E,M = map(int,input().split())
P,I = map(int,input().split())
input()
adj = [list(map(int,input().split())) for i in range(N)]
############
# PageRank #
############
outdeg = [len(i) for i in adj]
PR = [1.]*N
for k in range(1000):
new_PR = step(adj,PR,outdeg)
error = sum(abs(i-j) for i,j in zip(new_PR,PR))
PR = new_PR
#print(k,error)
if error < N*1e-7:
break
vaccinated = sorted([(rank,i) for i,rank in enumerate(PR)], reverse=True)[:M]
vaccinated = [i[1] for i in vaccinated]
###########
# Outputs #
###########
print(N,E,M)
print(P,I)
print(' '.join(str(j) for j in vaccinated))
for i in adj:
print(' '.join(str(j) for j in i))
if __name__ == "__main__":
main()