-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.py
395 lines (298 loc) · 10.5 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
import random
import time
from consts import *
import pickle
####################################
####################################
## CLASSES
####################################
####################################
class VerticesList(set):
def __init__(self):
super().__init__(self)
def __getitem__(self, key):
for k in self:
if isinstance(key, Vertex):
if key.id == k.id:
return k
else:
if key == k.id:
return k
def add(self, value):
assert isinstance(value, Vertex), "Value should be of type Vertex, "+type(value).__name__+" found"
super().add(value)
def __str__(self):
return str(list(self))
def __repr__(self):
return str(self)
####################################
class EdgesSet(set):
def __init__(self):
super().__init__(self)
def add(self, edge):
assert len(edge) == 2, "The length of the edge should be 2, "+str(len(edge))+" found"
assert all(isinstance(vertex, Vertex) for vertex in edge), "Values should be of type Vertex, "+type(edge[0]).__name__+" and "+type(edge[1]).__name__+" found"
super().add(tuple(sorted(list(edge))))
####################################
class Graph(dict):
def __init__(self, vertices=VerticesList(), edges=EdgesSet()):
super().__init__(self)
self.vertices = VerticesList()
self.edges = EdgesSet()
for v in vertices:
self.add_vertex(v)
for e in edges:
self.add_edge(e)
def add_vertex(self, vertex):
self[vertex] = VerticesList()
self.vertices.add(vertex)
def add_edge(self, edge):
assert all(vertex in self.vertices for vertex in edge), "One of the vertices of the edge is not set in the graph"
self[edge[0]].add(edge[1])
self[edge[1]].add(edge[0])
self.edges.add(edge)
def del_vertex(self, vertex):
for edge in self.edges:
if vertex in edge:
self.del_edge(edge)
self.vertices.remove(vertex)
def del_edge(self, edge):
self.edges.remove(edge)
def graph_from_dict(graph):
g = Graph()
for vertex in graph:
newV = Vertex(vertex)
g.add_vertex(newV)
for vertex in graph:
v = g.vertices[vertex]
for neighbor in graph[vertex]:
n = g.vertices[neighbor]
g.add_edge([v, n])
return g
def save(self, filename):
pickle.dump(self, "saves/"+filename+".g", pickle.HIGHEST_PROTOCOL)
def load(filename):
return pickle.load("saves/"+filename+".g")
####################################
class Vertex:
def __init__(self, id, value=None, name=None, color=NONE_COLOR):
assert type(id) in (int, str), "Vertex.id should be int or string, "+type(id).__name__+" found"
self.id = id
self.name = str(name or id)
self.color = color
self.value = value
def __lt__(self, other):
if isinstance(other, Vertex):
return self.id < other.id
return False
def __eq__(self, other):
if isinstance(other, str):
return other==self.id
if isinstance(other, Vertex):
return other.id==self.id
return False
def __hash__(self):
return hash(self.id)
def __str__(self):
return 'V('+str(self.name)+')'
def __repr__(self):
return 'V('+str(self.name)+')'
####################################
####################################
## FUNCTIONS
####################################
####################################
def graph_is_valid(graph):
"""
def dfs(graph, current=None, visited=[]):
if current == None:
current = random.choice(list(graph.keys()))
visited.append(current)
for neighbor in graph[current]:
if neighbor not in visited:
visited = dfs(graph, neighbor, visited)
return visited
dfsResult = dfs(graph)
if any(vertex not in dfsResult for vertex in graph.keys()):
return False
"""
if graph is None:
return False
# Get all neighbors to avoid a missing vertex
allNeighbors = set(vertex for neighbors in graph.values() for vertex in neighbors)
return all(neighbor in graph.keys() for neighbor in allNeighbors)
####################################
def graph_is_oriented(graph):
# Check if each vertex is a neighbor of its own neighbors
for vertex, neighbors in graph.items():
for neighbor in neighbors:
if vertex not in graph[neighbor]:
return True
return False
####################################
def get_cycles(graph):
def rec_get_cycles(current, visited, maxCycle=[]):
visited.append(current)
for neighbor in graph[current]:
if neighbor == visited[0] and len(visited)>=3:
if len(visited) > len(maxCycle):
maxCycle = visited.copy()
if neighbor in visited:
continue
maxCycle = rec_get_cycles(neighbor, visited.copy(), maxCycle)
return maxCycle
vertices = list(graph.keys())
cycles = []
while len(vertices) > 0:
startVertex = vertices[0]
cycle = rec_get_cycles(startVertex, [])
if cycle == []:
vertices.remove(startVertex)
continue
cycles.append(cycle)
for c in cycle:
if c in vertices:
vertices.remove(c)
return cycles
####################################
def get_chords_from_cycle(graph, cycle):
chords = set()
for vertex in cycle:
cIdx = cycle.index(vertex)
for neighbor in graph[vertex]:
if neighbor in cycle:
if neighbor not in [cycle[(cIdx+1)%len(cycle)], cycle[cIdx-1]]:
chords.add(tuple(sorted([vertex, neighbor])))
return chords
####################################
def graph_is_bipartite(graph):
current, other = set(), set()
pile = set()
pile.add(list(graph.keys())[0])
visited = []
while len(pile) != 0:
newPile = set()
print(current, other, pile, visited)
for vertex in pile-(set(visited)):
current.update(graph[vertex])
if any(v in other for v in graph[vertex]):
print(vertex, graph[vertex])
return False
newPile.update(graph[vertex])
visited.append(vertex)
pile = newPile.copy()
current, other = other.copy(), current.copy()
return True
####################################
def generate_random_graph(method="NEIGHBORS_AMOUNT", *args, **kwargs):
def generate_random_by_neighbors_amount(n=DEFAULT_N_VERTICES_RANDGRAPH, minNei=DEFAULT_NEIGHBORS_INTERVAL[0], maxNei=DEFAULT_NEIGHBORS_INTERVAL[1]):
assert n >= 2, "n has to be at least 2"
graph = {k: [] for k in range(n)}
adjacentMatrix = [[0 for i in range(n)] for j in range(n)]
allVertices = []
for i in range(maxNei):
allVertices += list(range(n))
i = 0
while i < len(allVertices):
if adjacentMatrix[allVertices[i]].count(1) >= maxNei:
i+=1
continue
b = random.getrandbits(1) if adjacentMatrix[allVertices[i]].count(1) >= minNei else 1
if b:
idx = random.randint(0, len(allVertices)-1)
while allVertices[idx] == allVertices[i]:
idx = random.randint(0, len(allVertices)-1)
if adjacentMatrix[allVertices[idx]].count(1) >= maxNei:
idx = i
v1 = allVertices.pop(max(i,idx))
v2 = allVertices.pop(min(i,idx))
adjacentMatrix[v1][v2] = b
adjacentMatrix[v2][v1] = b
i -= 1
i += 1
for i in range(n):
graph[i] = [idx for idx in range(n) if adjacentMatrix[i][idx] == 1]
return Graph.graph_from_dict(graph)
def generate_random_by_random_delete_probability(n=DEFAULT_N_VERTICES_RANDGRAPH, p=DEFAULT_DELETE_PROBABILITY):
graph = {k: [] for k in range(n)}
adjacentMatrix = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(i,n):
if i != j:
b = random.choices([0,1], [p, 1-p])[0]
adjacentMatrix[i][j] = b
adjacentMatrix[j][i] = b
for i in range(n):
graph[i] = [idx for idx in range(n) if adjacentMatrix[i][idx] == 1]
return Graph.graph_from_dict(graph)
graph = None
t0 = time.time()
while not graph_is_valid(graph) and time.time()-t0 < 2000:
if method.upper() == "NEIGHBORS_AMOUNT":
graph = generate_random_by_neighbors_amount(*args, **kwargs)
elif method.upper() == "DELETE_PROBABILITY":
graph = generate_random_by_random_delete_probability(*args, **kwargs)
else:
graph = generate_random_by_neighbors_amount(*args)
return graph
####################################
if __name__ == '__main__':
graph = {
'A': ['C', 'E',"B"],
'B': ['F'],
'C': ['A', 'G'],
'D': ['B'],
'E': ['A', 'F'],
'F': ['B', 'E'],
'G': ['C']
}
"""
[ a b c d
a [0,0,1,0],
b [0,0,0,0],
c [1,0,0,0],
d [1,0,0,0],
]
vertices: [1,2,3,4]
edge: [(1,2), (2,1), (2,3), (3,2)]
"""
"""
print(graph_is_valid(graph))
print(graph_is_oriented(graph))
print('{')
for i in (g := generate_random_graph("NEIGHBORS_AMOUNT", 10, maxNei=4)):
print(f" {i}: {g[i]}")
print('}')
print('{')
for i in (g := generate_random_graph("DELETE_PROBABILITY", 10, maxNei=4)):
print(f" {i}: {g[i]}")
print('}')
"""
"""
graph = {
'A': ['B', 'E', 'F', 'G'],
'B': ['A', 'F'],
'C': ['F', 'I'],
'D': ['E', 'H', 'J'],
'E': ['A', 'D'],
'F': ['A', 'B', 'C'],
'G': ['A'] ,
'H': ['D'] ,
'I': ['C'] ,
'J': ['D']
}
print(graph_is_barpartite(graph))
"""
"""
graph = Graph()
a = Vertex(1, name="a")
b = Vertex(2, name="b")
graph.add_vertex(a)
graph.add_vertex(b)
graph.add_edge([a,1])
print(graph)
"""
g = generate_random_graph()
print(g.vertices)
print(g.edges)
print(g)