-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.2 (najwiekszaKlika).py
130 lines (81 loc) · 2.53 KB
/
4.2 (najwiekszaKlika).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
from dimacs import *
import os
#Graf musi być przekątniowy
def lexBFS(G, v):
visited = [False for _ in range(len(G))]
path = [-1 for _ in range(len(G))]
#elementy lexBFS
lista = []
lista.append({i for i in range(1, len(G))})
lista.append({0})
res = []
while len(lista) > 0:
#print( lista)
temp = lista[-1].pop()
#print(temp)
visited[temp] = True
res.append(temp)
#print(lista[-1])
nowaLista = []
#Tworzę zbiór dzieci, które mogę odwiedzić
A = set()
for i in range(len(G[temp])):
if visited[G[temp][i]] == False:
A.add(G[temp][i])
for i in range(len(lista)):
#Usuwam część wspólną
nowaLista.append(lista[i] - A)
nowaLista.append(lista[i] & A)
#lista.insert(A)
lista = list(filter(lambda a : len(a) > 0, nowaLista))
return res
def listify(V, L):
G = [[] for _ in range(V)]
for v1, v2, w in L:
G[v1 - 1].append( (v2 - 1) )
G[v2 - 1].append( (v1 - 1) )
return G
def perfectEliminationOrder(G, lex):
RN_parent = [set() for i in range(len(lex))]
maxi = float('-inf')
for j in range(len(lex)):
#Liczę zbiór RN(v), gdzie RN - wierzchołki sąsadujące z v, występujące przed nim w zbiorze lex
neighbours = set()
for i in range(len(G[lex[j]])):
neighbours.add(G[lex[j]][i])
predecessors = set()
for i in range(0, j):
predecessors.add(lex[i])
RN_v = predecessors & neighbours
if len(RN_v) + 1 > maxi:
maxi = len(RN_v) + 1
return maxi
Graphs = []
directory = "graphs-lab4/maxclique/"
# iterate over files in
# that directory
for filename in os.scandir(directory):
if filename.is_file():
Graphs.append(filename.path)
trues = 0
falses = 0
#Graphs = []
#Graphs.append(directory + "simple-noninterval2")
for str in Graphs:
V, L = loadWeightedGraph(str)
G = listify(V, L)
lex = lexBFS(G, 0)
print(lex)
result = perfectEliminationOrder(G, lex)
print("Rozwiazanie dla ", str, ": ", result)
x = readSolution(str)
x = int(x)
if result == x:
trues += 1
print("Zgodne ze wzrorcowym rozwiązaniem")
else:
falses += 1
print("Niezgodne ze wzorcowym rozwiązaniem! Jest: ", result, ", powinno być: ", x)
print("")
print("Zaliczone testy: ", trues)
print("Niezaliczone testy: ", falses)