Skip to content

Commit 981af26

Browse files
authored
Merge pull request #12 from SebVde/exact-deterministic
Exact deterministic
2 parents dda72ae + 8725290 commit 981af26

3 files changed

Lines changed: 25 additions & 25 deletions

File tree

exact_mif.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_cnf(G, T):
2222
components = list(nx.connected_components(nx.subgraph(G, T)))
2323

2424
if len(components) > 0:
25-
for v in set(G.nodes) - T:
25+
for v in sorted(set(G.nodes) - T):
2626
# For each connected component from T, check if v has 2 or more neighbors in the component
2727
for comp in components:
2828
neighbors_in_comp = set(nx.neighbors(G, v)) & comp # & = intersection
@@ -126,17 +126,17 @@ def find_mif_len(G, T, K):
126126
v = None
127127
bnd = get_bnd(G, T)
128128
if len(bnd) > 0:
129-
v = next(iter(bnd))
129+
v = list(sorted(bnd))[0]
130130
else:
131-
v = next(iter(set(G.nodes) - (T | K)))
131+
v = list(sorted(set(G.nodes) - (T | K)))[0]
132132

133133
new_G, new_T, new_K = T_update(G, T, K, v)
134134
new_S = find_mif_len(new_G, new_T, new_K)
135135

136136
W = get_K_connected(G, K, v) & set(G.nodes) - (T | K | {v})
137137

138138
match len(W):
139-
case 0, 1:
139+
case 0 | 1:
140140
return new_S
141141
case 2:
142142
sg = nx.subgraph(G, set(G.nodes) - {v})

exact_mif_v2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def construct_H(G, F, nb):
2121

2222

2323
def is_foldable(G, v):
24-
nb = set(nx.neighbors(G, v))
24+
nb = sorted(nx.neighbors(G, v))
2525
for x, y, z in itertools.combinations(nb, 3):
2626
if not (G.has_edge(x, y) or G.has_edge(y, z) or G.has_edge(z, x)):
2727
# If no edges between x-y-z, there is an anti-triangle so not foldable
@@ -31,7 +31,7 @@ def is_foldable(G, v):
3131

3232

3333
def get_anti_edges(G, v):
34-
nb = set(nx.neighbors(G, v))
34+
nb = nx.neighbors(G, v)
3535
anti_edges = set()
3636
for x, y in itertools.combinations(nb, 2):
3737
if not G.has_edge(x, y):
@@ -97,7 +97,7 @@ def get_max_indep_set(G):
9797

9898
return res
9999

100-
for u, v in itertools.combinations(G.nodes, 2):
100+
for u, v in itertools.combinations(sorted(G.nodes), 2):
101101
nb_u = set(nx.neighbors(G, u))
102102
nb_v = set(nx.neighbors(G, v))
103103
if (nb_v | {v}).issubset(nb_u | {u}):
@@ -168,7 +168,7 @@ def get_mif_len(G, F, active_v):
168168
if connections > 1:
169169
vertices_to_remove.add(v)
170170

171-
v_T = next(iter(T))
171+
v_T = list(sorted(T))[0]
172172

173173
for n in T - {v_T}:
174174
new_G = nx.contracted_nodes(new_G, v_T, n, self_loops=False)
@@ -198,33 +198,33 @@ def main_procedure(G, F, active_v):
198198
if max_degree < 2:
199199
return len(G.nodes)
200200
else:
201-
t = next(n for n, d in G.degree() if d >= 2)
201+
t = list(sorted(n for n, d in G.degree() if d >= 2))[0]
202202
new_G = nx.subgraph(G, G.nodes - {t})
203203
return max(
204204
get_mif_len(G, F | {t}, active_v), get_mif_len(new_G, F, active_v)
205205
)
206206

207207
if active_v is None:
208-
active_v = next(iter(F))
208+
active_v = list(sorted(F))[0]
209209

210210
nb = set(nx.neighbors(G, active_v))
211211
if set(G.nodes) - F == nb:
212212
return len(F) + get_max_indep_set(construct_H(G, F - {active_v}, nb))
213213

214-
for v in nb:
214+
for v in sorted(nb):
215215
gen_nb = get_generalized_neighbors(G, F, active_v, v)
216216
if len(gen_nb) < 2:
217217
return get_mif_len(G, F | {v}, active_v)
218218

219-
for v in nb:
219+
for v in sorted(nb):
220220
gen_nb = get_generalized_neighbors(G, F, active_v, v)
221221
if len(gen_nb) > 3:
222222
return max(
223223
get_mif_len(G, F | {v}, active_v),
224224
get_mif_len(nx.subgraph(G, G.nodes - {v}), F, active_v),
225225
)
226226

227-
for v in nb:
227+
for v in sorted(nb):
228228
gen_nb = get_generalized_neighbors(G, F, active_v, v)
229229
if len(gen_nb) == 2:
230230
return max(

exact_mif_v3.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_non_trivial_components(G):
2222

2323

2424
def find_short_pair(G, F, active_v):
25-
for u, v in itertools.combinations(set(G.nodes) - F, 2):
25+
for u, v in itertools.combinations(sorted(set(G.nodes) - F), 2):
2626
nb_u = set(nx.neighbors(G, u))
2727
nb_v = set(nx.neighbors(G, v))
2828
# If parallel edges between u and v (so u,v is a short-cycle) or they have a common neighbor in F (also short-cycle)
@@ -37,18 +37,18 @@ def find_short_pair(G, F, active_v):
3737

3838
def is_trigger_vertex(G, F, active_v, v):
3939
nb_active = set(nx.neighbors(G, active_v))
40-
for u in nb_active - F:
40+
for u in sorted(nb_active - F):
4141
gen_nb = get_generalized_neighbors(G, F, active_v, u)
4242
if len(gen_nb - nb_active) >= 3 and v in gen_nb:
4343
nb_v = set(nx.neighbors(G, v))
4444
nb_u = set(nx.neighbors(G, u))
4545
s_set = F - nb_v
4646
v_prime_set = F & nb_v
47-
for s in s_set:
47+
for s in sorted(s_set):
4848
if nb_u == {active_v, v, s}:
4949
return True
5050

51-
for v_prime in v_prime_set:
51+
for v_prime in sorted(v_prime_set):
5252
d_v_prime = G.degree(v_prime)
5353
if d_v_prime == 2 and (
5454
nb_u == {active_v, v_prime, s}
@@ -63,7 +63,7 @@ def find_optimal_v(G, F, active_v):
6363
nb_active = set(nx.neighbors(G, active_v))
6464
G_not_F = set(G.nodes) - F
6565
possible = set()
66-
for v in G_not_F:
66+
for v in sorted(G_not_F):
6767
gen_nb = get_generalized_neighbors(G, F, active_v, v)
6868
if len(gen_nb) < 3:
6969
continue
@@ -101,7 +101,7 @@ def main_procedure(G, F, active_v):
101101

102102
cut_v = set(nx.articulation_points(G))
103103
if len(cut_v) > 0:
104-
v = next(iter(cut_v))
104+
v = list(sorted(cut_v))[0]
105105
components = list(nx.connected_components(nx.subgraph(G, set(G.nodes) - {v})))
106106
H = min(components, key=lambda x: len(x))
107107

@@ -143,10 +143,10 @@ def main_procedure(G, F, active_v):
143143
return max(get_mif_len(G, F | {v}, v), get_mif_len(new_G, F, active_v))
144144

145145
if active_v is None:
146-
active_v = next(iter(F))
146+
active_v = list(sorted(F))[0]
147147

148148
nb_active = set(nx.neighbors(G, active_v))
149-
for v in nb_active:
149+
for v in sorted(nb_active):
150150
gen_nb = get_generalized_neighbors(G, F, active_v, v)
151151
d_v = G.degree(v)
152152

@@ -156,7 +156,7 @@ def main_procedure(G, F, active_v):
156156
get_mif_len(nx.subgraph(G, set(G.nodes) - {v}), F, active_v),
157157
)
158158

159-
for v in nb_active:
159+
for v in sorted(nb_active):
160160
gen_nb = get_generalized_neighbors(G, F, active_v, v)
161161
if len(gen_nb) == 2:
162162
if not nx.is_forest(nx.subgraph(G, F | gen_nb)):
@@ -230,7 +230,7 @@ def get_mif_len(G, F, active_v):
230230
non_trivial_components = get_non_trivial_components(sg_F)
231231
if len(non_trivial_components) > 0:
232232
T = non_trivial_components[0]
233-
v = next(iter(T))
233+
v = list(sorted(T))[0]
234234
if (active_v is not None) and (active_v in T):
235235
v = active_v
236236

@@ -244,9 +244,9 @@ def get_mif_len(G, F, active_v):
244244
# Step 2
245245
v = None
246246
# If we find a node v not in new_F that has 2 parallel edges to a node u in new_F, we remove it from new_G
247-
for n in set(new_G.nodes) - new_F:
247+
for n in sorted(set(new_G.nodes) - new_F):
248248
nb_in_F = set(new_G.neighbors(n)) & new_F
249-
for u in nb_in_F:
249+
for u in sorted(nb_in_F):
250250
if new_G.number_of_edges(u, n) > 1:
251251
v = n
252252
break

0 commit comments

Comments
 (0)