22
33
44def cleanup (G ):
5+ """
6+ Iteratively removes nodes with degree <= 1 from the graph.
7+
8+ Args:
9+ G (nx.Graph): The input graph to clean.
10+
11+ Returns:
12+ nx.Graph: The cleaned graph.
13+ """
514 queue = [n for n in G .nodes if G .degree (n ) <= 1 ]
615
716 while len (queue ) > 0 :
@@ -19,6 +28,17 @@ def cleanup(G):
1928
2029
2130def find_semidisjoint_cycle (G ):
31+ """
32+ Identifies a semi-disjoint cycle.
33+ A semi-disjoint cycle is a cycle formed only by degree-2 nodes, with one possible exception.
34+
35+ Args:
36+ G (nx.Graph): The input graph.
37+
38+ Returns:
39+ list or None: A list of nodes forming the semi-disjoint cycle or path+junction,
40+ or None if no such structure is found.
41+ """
2242 degree_2 = {n for n , d in G .degree () if d == 2 }
2343 if len (degree_2 ) == 0 :
2444 return None
@@ -53,6 +73,15 @@ def find_semidisjoint_cycle(G):
5373
5474
5575def get_fvs (og_G ):
76+ """
77+ Computes an approximate FVS using a weight-reduction approach described in Bafna et al.'s paper.
78+
79+ Args:
80+ og_G (nx.Graph): The input graph.
81+
82+ Returns:
83+ set: A set of nodes forming the approximate FVS.
84+ """
5685 F = set ()
5786 stack = []
5887 G = og_G .copy ()
@@ -85,6 +114,15 @@ def get_fvs(og_G):
85114
86115
87116def approx_decycling_number_bafna (G ):
117+ """
118+ Approximates the decycling number of a graph using Bafna's algorithm.
119+
120+ Args:
121+ G (nx.Graph): The input graph.
122+
123+ Returns:
124+ int: An approximated decycling number.
125+ """
88126 clean_G = cleanup (G .copy ())
89127 fvs = get_fvs (clean_G )
90128 return len (fvs )
0 commit comments