-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruteforce.py
55 lines (44 loc) · 1.46 KB
/
bruteforce.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
import itertools as it
from graph_utils import paint_lgraph, print_graph, read_graph
def get_lgraph(g):
n = len(g)
for order in it.permutations(range(n)):
for heights in it.permutations(range(n)):
lengths = try_order_and_heights(order, heights, g)
if not lengths:
continue
paint_lgraph(order, heights, lengths)
return True
print('given graph is not an L-graph')
return False
def try_order_and_heights(order, heights, g):
n = len(g)
lengths = []
for i in range(n):
lengths.append(i)
for node_order in range(n):
end = False
node = order[node_order]
for i in range(node_order+1, n):
other_node = order[i]
if other_node in g[node]:
if heights[node_order] < heights[i]:
# ok, predlzim ciaru
if end:
# uz sa neda ist dalej, lebo pretnem ciaru co nemam
return False
lengths[node_order] = i
else:
# nedaju sa spojit vrcholy
return False
else:
if heights[node_order] < heights[i]:
# nemozem ist dalej, lebo pretnem ciaru co nemam
end = True
return lengths
def main():
graph = read_graph()
print_graph(graph)
get_lgraph(graph)
if __name__ == "__main__":
main()