@@ -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.
165164Given 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 ())
0 commit comments