-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.py
128 lines (108 loc) · 3.65 KB
/
graph.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import networkx as nx
import json
import csv
target = open('newjson.txt', 'r') # Open files for reading
target2 = open('json.txt', 'r')
tar = target.read()
tar2 = target2.read()
search = json.loads(tar)
search2 = json.loads(tar2) # Loading json format to dict search
g2 = nx.Graph()
g = nx.Graph() # Creating graphs
# Adding EDGES to graphs g and g2 with thier respective weights
for x in search.keys():
# if len(dict(search[x]).keys()) >= 0:
if type(search[x]) == dict:
for y in search[x].keys():
# z = {search[x][y]}
g.add_edge(x, y, weight=search[x][y])
# print(x, y, g.get_edge_data(x, y))
for x in search2.keys():
# if len(dict(search[x]).keys()) >= 0:
if type(search2[x]) == dict:
for y in search2[x].keys():
# z = {search[x][y]}
g2.add_edge(x, y, weight=search2[x][y])
# print(x, y, g.get_edge_data(x, y))
# Method Defined to printgraph
def printgraph(g):
for x, ys in g.adjacency_iter():
for y, edge in ys.items():
print('(%s, %s, %f)' % (x, y, edge['weight']))
# Another Method Defined to printgraph
def printedges(g):
for x, y, z in g.edges(data='weight'):
print('(%s, %s, %d)' % (x, y, z))
'''
# These are the various In-built methods of Networkx tested on graphs
# nx.draw(g)
# printedges(g)
# print(nx.clustering(g), '\n\n', nx.clustering(g2), '\n\n\n\n')
# print(nx.connected_components(g))
# pprint(nx.degree(g2))
# print(max(nx.degree(g).values()))
# print(nx.degree(g), '\n\n', nx.degree(g2), '\n\n')
# print(nx.average_shortest_path_length(g), '\n\n', nx.average_shortest_path_length(g2), '\n\n')
# print(nx.triangles(g), '\n\n', nx.triangles(g2))
# print(g.get_edge_data())
'''
target.close()
# Opening the other 2 perceptual files
file = open('edgesFlav.csv', 'r', encoding="utf8")
file2 = open('edgesSuperSc.csv', 'r', encoding="utf8")
read = csv.reader(file, delimiter=',')
reading = csv.reader(file2, delimiter=',')
g3 = nx.Graph() # Creating their graphs
g4 = nx.Graph()
yp = []
for row in read:
yp.extend(row)
# print(yp)
zp = []
for row in reading:
zp.extend(row)
'''Adding Edges to Other graphs to compare with g and g2 graphs'''
for row in yp:
# print(type(row[count]))
# zz = tuple(row)
# print(type(row))
x, y = row.split(',')
# y = row[count][1]
xn = x[2:-1]
yn = y[2:-2]
# print(xn)
g3.add_edge(xn, yn, weight=1)
# printgraph(g3)
for row in zp:
x, y = row.split(',')
xn = x[2:-1]
yn = y[2:-2]
g4.add_edge(xn, yn, weight=1)
# Defined Method of Eigen Values for Compaison of Graph
def select_k(spectrum, minimum_energy=0.9):
running_total = 0.0
total = sum(spectrum)
if total == 0.0:
return len(spectrum)
for i in range(len(spectrum)):
running_total += spectrum[i]
if running_total / total >= minimum_energy:
return i + 1
return len(spectrum)
# Calculating laplacian using networkx
laplacian1 = nx.spectrum.laplacian_spectrum(g)
laplacian2 = nx.spectrum.laplacian_spectrum(g3)
k1 = select_k(laplacian1)
k2 = select_k(laplacian2)
k = min(k1, k2)
# Finding similairty using minimum k
similarity = sum((laplacian1[:k] - laplacian2[:k])**2)
print("Similarity[0,inf) in Flavournet Graphs is ", similarity)
# nx.draw(g3)
laplacian3 = nx.spectrum.laplacian_spectrum(g2)
laplacian4 = nx.spectrum.laplacian_spectrum(g4)
k3 = select_k(laplacian3)
k4 = select_k(laplacian4)
k = min(k3, k4)
similarity = sum((laplacian3[:k] - laplacian4[:k])**2)
print("Similarity[0,inf) in Superscent Graphs is ", similarity)