-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstanojevic.py
More file actions
40 lines (30 loc) · 926 Bytes
/
Copy pathstanojevic.py
File metadata and controls
40 lines (30 loc) · 926 Bytes
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
import networkx as nx
def find_min_fvs(G):
"""
Finds an approximate minimal FVS for the input graph.
Args:
G (nx.Graph): The input graph.
Returns:
set: A set of vertices forming the approximate minimal FVS.
"""
fvs = set()
while True:
try:
cycle = nx.find_cycle(G)
except nx.NetworkXNoCycle:
break
cycle_nodes = {node for e in cycle for node in e}
# Select the node with the highest degree to remove
v = max(cycle_nodes, key=lambda x: G.degree(x))
fvs.add(v)
G.remove_node(v)
return fvs
def approx_decycling_number_stanojevic(G):
"""
Approximates the decycling number of the graph using Stanojevic's algorithm.
Args:
G (nx.Graph): The input graph.
Returns:
int: The approximate decycling number of the graph.
"""
return len(find_min_fvs(G.copy()))