-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiener.py
39 lines (27 loc) · 1.18 KB
/
wiener.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
import networkx as nx
import os
import json
from networkx.readwrite import json_graph
path_to_json = './Topologias'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith(
'.json') and not pos_json.endswith('nodes.json')]
with open('./wienerIndexes.json', 'w') as wienerIndexes:
with open('./graphDegres.json', 'w') as graphDegres:
wienerIndexesDict = {}
graphDegreeDict = {}
for fileName in json_files:
print(fileName)
with open('./Topologias/' + fileName, 'r') as json_file:
JSONLinks = json.load(json_file)
G = nx.Graph()
G.add_weighted_edges_from(
(elem['From'], elem['To'], 1)
for elem in JSONLinks
)
wienerIndex = nx.wiener_index(G)
graphDegree = sum(
map(lambda v: v[1], G.degree())) / len(G.nodes)
wienerIndexesDict[fileName] = wienerIndex
graphDegreeDict[fileName] = graphDegree
json.dump(wienerIndexesDict, wienerIndexes, sort_keys=True)
json.dump(graphDegreeDict, graphDegres, sort_keys=True)