-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoints_distribution.py
More file actions
71 lines (62 loc) · 1.66 KB
/
points_distribution.py
File metadata and controls
71 lines (62 loc) · 1.66 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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 16 00:06:09 2022
@author: Souvik Bhattacharya
"""
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())
L = []
for c in nodes:
L.append(100)
return L
def keep_distributing(points,G):
while(1):
new_points = distribute_points(points,G)
print(new_points)
points = new_points
ch = input("0 to stop or any key to continue :")
if(ch=='0'):
break
return new_points
def distribute_points(points,G):
nodes = list(G.nodes())
new_point = []
for i in range(len(nodes)):
new_point.append(0)
for i in nodes:
out = list(G.out_edges(i))
if(len(out) == 0):
new_point[i] = new_point[i] + points[i]
else:
share = points[i]/len(out)
for (source,target) in out:
new_point[target] += share
return new_point
def rank_by_points(final_points):
dict = {}
for i in range(len(final_points)):
dict[i] = final_points[i]
print(sorted(dict.items(),key = lambda f:f[1]))
import networkx as nx
import random
import matplotlib.pyplot as plt
n = int(input())
G = nx.DiGraph()
G.add_nodes_from([i for i in range(n)])
G = add_edges()
nx.draw(G,with_labels=True)
plt.show()
points = assign_points(G)
final_points = keep_distributing(points,G)
rank_by_points(final_points)
org_results = nx.pagerank(G)
print(sorted(org_results.items(),key = lambda f:f[1]))