-
Notifications
You must be signed in to change notification settings - Fork 9
/
graph.py
408 lines (391 loc) · 14.3 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
Simple class for working with unweighted undirected graphs
@author: Oleksii Kuchaiev; http://www.kuchaev.com
'''
import random
class graph(object):
'''
A class for representing and manipulation undirected, unweighted simple graphs without self-loops
'''
def __init__(self, userID=None):
'''
Constructor
'''
if userID==None:
self.Id=random.randint(0,10000000)
else:
self.Id=userID
self.Nodes=set() #set of nodes
self.AdjList=dict() #Adjacency list
def add_node(self,node):
'''
Adds node to the graph.
'''
if node in self.Nodes:
raise Exception("Node "+node+" is already present in the graph.")
else:
self.Nodes.add(node)
self.AdjList[node]=set()
def add_edge(self,nd1,nd2):
'''
Adds edge (nd1,nd2) to the graph.
'''
if nd1 not in self.Nodes:
raise Exception("Node "+nd1+" is not present in the graph.")
if nd2 not in self.Nodes:
raise Exception("Node "+nd2+" is not present in the graph.")
if nd1 not in self.AdjList.keys():
self.AdjList[nd1]=set()
self.AdjList[nd1].add(nd2)
else:
self.AdjList[nd1].add(nd2)
if nd2 not in self.AdjList.keys():
self.AdjList[nd2]=set()
self.AdjList[nd2].add(nd1)
else:
self.AdjList[nd2].add(nd1)
def readFromEdgeList(self,path):
'''
Read graph from the file where it is represented as an edge list.
The lines of the file should be formated as:
node1[space]node2[newline]
Duplicate edges and self loops are ignored.
'''
inp_file=open(path,'r')
_Nodes=set() # this will replace self.Nodes in case of success
_AdjList=dict() # this will replace self.AdjList in case of success
for line in inp_file:
nodes=line.split()
if len(nodes)<2:
raise Exception("There is an incorrectly formatted line in the edge list file")
nd1=nodes[0]
nd2=nodes[1]
if nd1==nd2:
continue
if (nd1 in _Nodes):
if (nd2 not in _AdjList[nd1]):
_AdjList[nd1].add(nd2)
else:
_Nodes.add(nd1)
_AdjList[nd1]=set()
_AdjList[nd1].add(nd2)
if (nd2 in _Nodes):
if (nd1 not in _AdjList[nd2]):
_AdjList[nd2].add(nd1)
else:
_Nodes.add(nd2)
_AdjList[nd2]=set()
_AdjList[nd2].add(nd1)
inp_file.close()
self.Nodes.clear()
self.AdjList.clear()
self.Nodes=_Nodes
self.AdjList=_AdjList
def get_edge_set(self):
'''
Returns set of edges in the graph.
'''
Edges=set()
for nd1 in self.Nodes:
N=self.get_node_neighbors(nd1)
for nd2 in N:
if (nd2,nd1) not in Edges:
Edges.add((nd1,nd2))
return Edges
def saveAsEdgeList(self,path):
'''
Saves graph as edge list
'''
out=open(path,'w')
EdgeList=self.get_edge_set()
for edge in EdgeList:
line = edge[0]+" "+edge[1]+"\n"
out.write(line)
out.close()
def number_of_nodes(self):
'''
Returns number of nodes in the graph.
'''
return len(self.Nodes)
def number_of_edges(self):
'''
Returns number of edges in the graph.
'''
num_edg=0.0;
for key in self.AdjList.keys():
num_edg=num_edg+(float(len(self.AdjList[key]))/2)
return int(num_edg)
def degree(self,node):
'''
Returns the degree of a node
'''
if node not in self.Nodes:
raise Exception("There is no node with name: "+str(node)+" in this graph. The id of the graph is: "+str(self.Id))
return len(self.AdjList[node])
def get_node_clust_coef(self,node):
'''
Returns the clustering coefficient of the node
'''
deg=self.degree(node)
if deg<=1:
return 0
Ev=0
neighbors=self.get_node_neighbors(node)
for nd in neighbors:
if self.are_adjacent(node, nd):
Ev+=1
cc=float(2*Ev)/(deg*(deg-1))
return cc
def get_node_eccentricity(self,node):
'''
Returns the eccentricity of the node.
Note that this function returns the eccentricity of a node within its
connected component
'''
D=self.BFS(node)
ec=0
for key, value in D:
if value>ec:
ec=value
return ec
def get_node_eccentricity_avg(self,node):
'''
Returns the averaged eccentricity of the node. That is, "avg", not "max" distance
Note that this function returns the eccentricity of a node within its
connected component
'''
D=self.BFS(node)
ec=0.0
counter=0.0
for key, value in D.items():
if value>0:
ec+=value
counter+=1
if counter>0:
return ec/counter
else:
return 0
def get_node_eccentricities_both(self,node):
'''
This function is for performance purposes.
This is function returns standard and averaged eccentricities of the node.
Note that both eccentricities of the node are within its connected component
'''
D=self.BFS(node)
ec=0
ecA=0.0
counter=0.0
for key, value in D.items():
if value>0:
ecA+=value
counter+=1
if value>ec:
ec=value
if counter>0:
return (ec,ecA/counter)
else:
return (ec,0)
def are_adjacent(self,nd1,nd2):
'''
Checks if nd1 and nd2 are connected
'''
if nd1 not in self.Nodes:
raise Exception("Node "+str(nd1)+" is not in the graph with id="+str(self.Id))
if nd2 not in self.Nodes:
raise Exception("Node "+str(nd2)+" is not in the graph with id="+str(self.Id))
if nd2 in self.AdjList[nd1]:
return True
else:
return False
def get_node_neighbors(self,nd):
'''
Returns set of node neighbors
'''
#if nd not in self.Nodes:
# raise Exception("Node "+str(nd)+" is not in the graph with id="+str(self.Id))
return self.AdjList[nd]
def BFS(self,source):
'''
Implements Breadth-first search from node 'source' in graph 'self'.
Returns dictionary D {node: distance from source}
distance=-1 if 'node' is unreachable from 'source'
'''
#if source not in self.Nodes:
# raise Exception("Node "+str(source)+" is not in the graph with id="+str(self.Id))
D=dict();
for node in self.Nodes:
D[node]=-1
level=0;
Que0=set()
Que0.add(source)
Que1=set()
while len(Que0)!=0:
while len(Que0)!=0:
cur_node=Que0.pop()
D[cur_node]=level
N=self.AdjList[cur_node]
for nd in N:
if D[nd]==-1:
Que1.add(nd)
level=level+1
Que0=Que1
Que1=set()
return D
def dist(self,nd1,nd2):
'''
Returns shortest-path distance between nd1 and nd2
'''
if nd1 not in self.Nodes:
raise Exception("Node "+str(nd1)+" is not in the graph with id="+str(self.Id))
if nd2 not in self.Nodes:
raise Exception("Node "+str(nd2)+" is not in the graph with id="+str(self.Id))
D=dict();
for node in self.Nodes:
D[node]=-1
level=0;
Que0=set()
Que0.add(nd1)
Que1=set()
while len(Que0)!=0:
while len(Que0)!=0:
cur_node=Que0.pop()
D[cur_node]=level
if cur_node==nd2:
return level
N=self.get_node_neighbors(cur_node)
for nd in N:
if D[nd]==-1:
Que1.add(nd)
level=level+1;
Que0=Que1;
Que1=set()
return -1
def all_pairs_dist(self):
'''
Returns dictionary of all-pairs shortest paths in 'self'
The dictionary has format {t=(nd1,nd2): distance},
where t is a tuple.
'''
Distances=dict()
count=0
for nd in self.Nodes:
DD1=self.BFS(nd)
for key, value in DD1.items():
t1=nd, key
t2=key, nd
Distances[t1]=float(value)
Distances[t2]=float(value)
return Distances
def find_all_cliques(self):
'''
Implements Bron-Kerbosch algorithm, Version 2
'''
Cliques=[]
Stack=[]
nd=None
disc_num=len(self.Nodes)
search_node=(set(),set(self.Nodes),set(),nd,disc_num)
Stack.append(search_node)
while len(Stack)!=0:
(c_compsub,c_candidates,c_not,c_nd,c_disc_num)=Stack.pop()
if len(c_candidates)==0 and len(c_not)==0:
if len(c_compsub)>2:
Cliques.append(c_compsub)
continue
for u in list(c_candidates):
if (c_nd==None) or (not self.are_adjacent(u, c_nd)):
c_candidates.remove(u)
Nu=self.get_node_neighbors(u)
new_compsub=set(c_compsub)
new_compsub.add(u)
new_candidates=set(c_candidates.intersection(Nu))
new_not=set(c_not.intersection(Nu))
if c_nd!=None:
if c_nd in new_not:
new_disc_num=c_disc_num-1
if new_disc_num>0:
new_search_node=(new_compsub,new_candidates,new_not,c_nd,new_disc_num)
Stack.append(new_search_node)
else:
new_disc_num=len(self.Nodes)
new_nd=c_nd
for cand_nd in new_not:
cand_disc_num=len(new_candidates)-len(new_candidates.intersection(self.get_node_neighbors(cand_nd)))
if cand_disc_num<new_disc_num:
new_disc_num=cand_disc_num
new_nd=cand_nd
new_search_node=(new_compsub,new_candidates,new_not,new_nd,new_disc_num)
Stack.append(new_search_node)
else:
new_search_node=(new_compsub,new_candidates,new_not,c_nd,c_disc_num)
Stack.append(new_search_node)
c_not.add(u)
new_disc_num=0
for x in c_candidates:
if not self.are_adjacent(x, u):
new_disc_num+=1
if new_disc_num<c_disc_num and new_disc_num>0:
new1_search_node=(c_compsub,c_candidates,c_not,u,new_disc_num)
Stack.append(new1_search_node)
else:
new1_search_node=(c_compsub,c_candidates,c_not,c_nd,c_disc_num)
Stack.append(new1_search_node)
return Cliques
def create_empty_graph(self,n):
'''
creates graph with n nodes but without edges
'''
G=graph()
for i in range(1,n+1):
nd=str(i)
G.add_node(nd)
return G
def create_path_graph(self,n):
'''
Create path-graph on n nodes
'''
if n<2:
raise Exception("Can't create a path graph with less than 2 nodes.")
G=self.create_empty_graph(n)
for i in range(1,n):
nd1=str(i)
nd2=str(i+1)
G.add_edge(nd1, nd2)
return G
def create_cycle_graph(self,n):
'''
Creates graph-cycle on n nodes.
Nodes are numbered from 1 to n.
'''
if n<3:
raise Exception("Can't create a cycle with less than 3 nodes.")
G=self.create_path_graph(n)
G.add_edge(str(1), str(n))
return G
def create_circulant_graph(self,n,j):
'''
Creates a circulant graph with n nodes and m edges.
Nodes are numbered from 1 to n.
The chords are defined by parameters j.
That is node i is connected to (i-j) mod n and (i+j) mod n.
Note that not always the exact match in terms of edges is possible!
Check the output graph for the number of edges!
'''
G=self.create_cycle_graph(n) # node numbers starts from 1
for i in range(0,n):
ndi=str(i % n + 1)
nd2 = str(( i - j ) % n + 1)
nd3 = str(( i + j ) % n + 1)
G.add_edge(ndi, nd2)
G.add_edge(ndi, nd3)
return G
def create_complete_graph(self,n):
'''
creates complete graph on n nodes
'''
G=self.create_empty_graph(n)
L=list(G.Nodes)
for i in range(0,len(L)):
for j in range(i+1,len(L)):
G.add_edge(L[i], L[j])
return G