-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArisnumber.py
68 lines (61 loc) · 3.02 KB
/
Arisnumber.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
from collections import defaultdict
import networkx as nx
import heapq
# This class finds Aris number and the group number of each node of graph, using a given set of author
class Shortest_Path():
def __init__(self, graph):
self.graph=graph
# This function makes a dictionary that nodes appears as keys and tuples of connected nodes
# as values in this form: ("connected node", "weight")
def create_dict_connection(self):
edges=nx.to_edgelist(self.graph.to_directed())
dic = defaultdict(list)
for id_1,id_2,weight in edges:
dic[id_1].append((id_2,weight['weight']))
return dic
# This function makes a good output for the shortest path
def path_list(path,lst_path):
if path==():
return lst_path[::-1]
lst_path.append(path[0])
return Shortest_Path.path_list(path[1],lst_path)
# This function calculates the shortest distance between an author and the other nodes, using heap;
# it returns a dictionary with the shortest path and the corresponding weight in a tuple as value
def dijkstrapath(self,start):
weight_dic=self.create_dict_connection()
# Initialize queue
queue= [(0,start,())]
# Set of visited nodes
visited=set()
dict_path={}
while queue:
# Take the element with the minimum weight from the queue
weight,vertex1,path = heapq.heappop(queue)
if vertex1 not in visited:
visited.add(vertex1)
path = (vertex1, path)
dict_path[vertex1]=(weight, Shortest_Path.path_list(path,[]))
# Take into account the linked nodes with the vertex1 node
for vertex2, c in weight_dic.get(vertex1,()):
if vertex2 not in visited:
# Push every nodes, which is not in my visited set, in my queue
heapq.heappush(queue, (weight+c, vertex2, path))
return dict_path
# This function calculates the Groupnumber of each node of the graph
def GroupNumber(self,set_id_author):
# Dictionary with all shortest paths for the nodes of the input set
dict_set={node_set:self.dijkstrapath(node_set) for node_set in set_id_author}
# Dictionary of all shortest path between each node of the graph and the nodes of the set
total_group_number=defaultdict(list)
for node_set in set_id_author:
for node in self.graph.nodes():
if node not in dict_set[node_set].keys():
continue
elif node!= node_set:
total_group_number[node].append(dict_set[node_set][node])
# Modify the previous dictionary with only the shortest path with the minimum weight
for node in total_group_number.keys():
if len(total_group_number[node])!=0:
total_group_number[node].sort(key=lambda x:x[0])
total_group_number[node]=total_group_number[node][0]
return total_group_number