-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
57 lines (49 loc) · 1.58 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
'''
created on 01 February 2020
@author: Gergely
'''
from tables.synced import SynchronizedTable
import random
def table_to_graph(wait_for_table: SynchronizedTable):
elems = wait_for_table.get()
if elems is None:
return set()
nodes = {entry.trans_has_lock for entry in elems}.union({entry.trans_waits_lock for entry in elems})
graph = {entry: [] for entry in nodes}
for entry in elems:
graph[entry.trans_has_lock].append(entry.trans_waits_lock)
return graph
def detect_cycle(graph):
'''
topological sort to detect cycles
:param graph
:return:
'''
in_degree = {key: 0 for key in graph}
for a in graph:
for b in graph[a]:
in_degree[b] += 1
queue = [tr_id for tr_id in graph if in_degree[tr_id] == 0]
visited = 0
topological_order = []
while queue:
front = queue.pop(0)
topological_order.append(front)
for node in graph[front]:
in_degree[node] -= 1
if in_degree[node] == 0:
queue.append(node)
visited += 1
if visited != len(graph):
return list(set(graph.keys()).difference(set(topological_order)))
return None
def without_cycles(graph):
cycle_nodes = detect_cycle(graph)
to_delete = []
while cycle_nodes is not None:
node_to_delete = random.choice(cycle_nodes)
to_delete.append(node_to_delete)
del graph[node_to_delete]
graph = {key: [x for x in graph[key] if x != node_to_delete] for key in graph}
cycle_nodes = detect_cycle(graph)
return to_delete