Skip to content

Commit d0b8363

Browse files
authored
Merge pull request #4 from SebVde/mif_return_decycling
Mif return decycling
2 parents cdf86aa + dd93d2d commit d0b8363

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

exact_mif.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ def K_update(G, T, K, W):
110110
return None # If the subgraph contains cycles
111111

112112

113-
def find_mif(G, T, K):
113+
def find_mif_len(G, T, K):
114114
"""
115-
Searches for a (T union K)-MIF of the graph G.
115+
Searches for the length of a (T union K)-MIF of the graph G.
116116
:param G: a networkx graph
117117
:param T: a subset of vertices in G
118118
:param K: a subset of vertices in G
119-
:return: a set of vertices representing a (T union K)-MIF of G
119+
:return: the length of a (T union K)-MIF of G
120120
"""
121121

122122
if T | K == set(G.nodes):
123-
return T | K
123+
return len(T | K)
124124

125125
else:
126126
v = None
@@ -131,7 +131,7 @@ def find_mif(G, T, K):
131131
v = random.choice(list(G.nodes - (T | K)))
132132

133133
new_G, new_T, new_K = T_update(G, T, K, v)
134-
new_S = find_mif(new_G, new_T, new_K)
134+
new_S = find_mif_len(new_G, new_T, new_K)
135135

136136
W = get_K_connected(
137137
nx.subgraph_view(
@@ -148,36 +148,37 @@ def find_mif(G, T, K):
148148
sg = nx.subgraph(G, G.nodes - {v})
149149
result = K_update(sg, T, K, W)
150150
if result is not None:
151-
return max(new_S, find_mif(*result), key=lambda x: len(x))
151+
return max(new_S, find_mif_len(*result))
152152
else:
153153
return new_S
154154
case _:
155155
return max(
156156
new_S,
157-
find_mif(nx.subgraph(G, G.nodes - {v}), T, K),
158-
key=lambda x: len(x),
157+
find_mif_len(nx.subgraph(G, G.nodes - {v}), T, K),
159158
)
160159

161160

162-
def main_mif(G, K):
161+
def get_mif_len(G, K):
163162
"""
164-
Main function to find a K-MIF of a graph G.
163+
Main function to get the length of a K-MIF of a graph G.
165164
Given a subset K of V(G), a K-MIF is a largest superset of K such that the subgraph of G induced by K is acyclic.
166165
:param G: a networkx graph
167166
:param K: a subset of vertices in G
168167
:raises CycleDetected: if the subgraph induced by K contains cycles
169-
:return: a set of vertices representing a K-MIF of G
168+
:return: the length of a K-MIF of G
170169
"""
171170

172171
if len(K) == 0 or nx.is_forest(nx.subgraph(G, K)):
173172
sg_nodes = G.nodes - get_cnf(G, K)
174173
sg = nx.subgraph(G, sg_nodes)
175-
result = sorted(find_mif(sg, set(), K))
176-
177-
print("K-MIF found:", result)
174+
result = find_mif_len(sg, set(), K)
178175
return result
179176

180177
else:
181178
raise CycleDetected(
182179
"No K-MIF of G can be found because the subgraph induced by K contains cycles."
183180
)
181+
182+
183+
def get_decycling_number_mif(G):
184+
return len(G.nodes) - get_mif_len(G, set())

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import networkx as nx
22
from pyvis.network import Network
3-
from exact_mif import main_mif
3+
from exact_mif import get_decycling_number_mif
44
from naive import get_decycling_number
55

66
nt = nx.erdos_renyi_graph(12, 0.89)
@@ -20,7 +20,7 @@
2020
# ("V6", "V8"),
2121
# ])
2222

23-
print(len(nt.nodes) - len(main_mif(nt, set())))
23+
print(get_decycling_number_mif(nt))
2424
print(get_decycling_number(nt))
2525

2626
# graph = Network()

0 commit comments

Comments
 (0)