-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_3_B.py
60 lines (42 loc) · 1.78 KB
/
part_3_B.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
# coding: utf-8
# In[ ]:
import pickle
import numpy as np
from sklearn.cluster import KMeans
import seaborn as sns
import load_save
from load_save import read_graph
from heapq import *
import part_3_A
from tqdm import tqdm
def group_number(subset,D_dict,graph):
'''Return a dictionary with authors ID of the subset as keys, and for values the node that have for GroupNumber that key'''
groupNumbers = {}
paths= {}
cc=0
for e in subset:
# Modify the ID of author, adding sub before
# It is necessary because it's better work with keys of the dict as string
strings = 'sub'+str(e)
# Csll the dijkistra function for each node in the subset
paths[strings]= part_3_A.dijkstra_heap2(D_dict,e)
# Initialize groupNumbers in position string with an empty list
groupNumbers[strings] = []
# Iterate on each node of the graph in input
for node in tqdm(graph):
# mini is an auxiliar variable
mini= []
# Put mini is a heap data stracture
heapify(mini)
# Iterate on each node in the subgraph
for path in paths.keys():
# Evaluate if the node has a distance with the node (path) of the subgraph
# If the node is not in paths[path].keys(), it means that there is no path between node and path
if node in paths[path].keys():
heappush(mini,(paths[path][node], path))
# len(mini) is bigger than 0 if at least one of the subgraph node has path with the node
# node is the one of the iteration in the graph
if len(mini)>0:
number= heappop(mini)
groupNumbers[number[1]].append(node)
return groupNumbers