-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdominating_set.py
50 lines (46 loc) · 1.37 KB
/
dominating_set.py
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
41
42
43
44
45
46
47
48
49
50
import networkx as nx
from utils import *
import random
import numpy as np
def dominate(G):
def prune(g):
nonlocal min_dist, minT
copy = g.copy()
try:
cycles = nx.find_cycle(g)
for cycle in cycles:
e = random.choice(cycle)
copy.remove_edge(e[0], e[1])
if is_valid_network(G, copy):
d = average_pairwise_distance_fast(copy)
if d < min_dist:
min_dist = d
minT = copy.copy()
prune(copy)
except:
e = random.choice(g.edges())
copy.remove_edge(e[0], e[1])
if is_valid_network(G, copy):
d = average_pairwise_distance_fast(copy)
if d < min_dist:
min_dist = d
minT = copy.copy()
prune(copy)
S = nx.dominating_set(G)
edges_between = []
for e in G.edges():
if e[0] in S and e[1] in S:
edges_between.append
T = nx.Graph()
for node in S:
T.add_node(node)
for e in edges_between:
T.add_edge(e[0], e[1])
if is_valid_network(G, T):
minT = T.copy()
else:
minT = nx.minimum_spanning_tree(G)
min_dist = average_pairwise_distance(minT)
for _ in range(300):
prune(T)
return minT