-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagerank_points_distribution_method.py
More file actions
75 lines (66 loc) · 1.73 KB
/
pagerank_points_distribution_method.py
File metadata and controls
75 lines (66 loc) · 1.73 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 11 22:13:39 2021
@author: DELL
"""
import matplotlib.pyplot as plt
import random
import networkx as nx
def add_edges():
nodes=list(G.nodes())
for s in nodes:
for t in nodes:
if s!=t:
r=random.random()
if r<=0.5:
G.add_edge(s,t)
return G
def assign_points(G):
nodes=list(G.nodes())
p=[]
for each in nodes:
p.append(100)
return p
def distribute_points(G, points):
nodes=list(G.nodes())
new_points=[]
for i in range(len(nodes)):
new_points.append(0)
for n in nodes:
out=list(G.out_edges(n))
if(len(out)==0):
new_points=new_points[n]+points[n]
else:
share=points[n]/len(out)
for (src,tgt) in out:
new_points[tgt]=new_points[tgt]+share
return new_points
def keep_distributing(points,G):
while(1):
new_points=distribute_points(G,points)
print(new_points)
points=new_points
stop=input("Press # to stop or any other key to continue: ")
if stop=='#':
break
return new_points
def rank_by_points(final_points):
d={}
for i in range(len(final_points)):
d[i]=final_points[i]
print(sorted(d.items(),key=lambda f:f[1]))# 1 for value, 0 for key
#Created a directed graph
G=nx.DiGraph()
G.add_nodes_from([i for i in range(10)])
G=add_edges()
#Visualize the graph
nx.draw(G, with_labels=True)
plt.show()
#assign initial values
points=assign_points(G)
#keep distributing
final_points=keep_distributing(points, G)
rank_by_points(final_points)
#default network function
result=nx.pagerank(G)
print(sorted(result.items(),key=lambda f:f[1]))