forked from XzrTGMu/twin-nphard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheuristics_mwcds.py
executable file
·686 lines (627 loc) · 25.8 KB
/
heuristics_mwcds.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import networkx as nx
import dwave_networkx as dnx
import igraph as ig
import pulp as plp
from pulp import GLPK
import numpy as np
import pandas as pd
import scipy.sparse as sp
import copy
import time
print(nx.__version__)
def zero_rows(M, rows):
diag = sp.eye(M.shape[0]).tolil()
for r in rows:
diag[r, r] = 0
return diag.dot(M)
def zero_columns(M, columns):
diag = sp.eye(M.shape[1]).tolil()
for c in columns:
diag[c, c] = 0
return M.dot(diag)
# From @wim's post
def nunique(a):
df = pd.DataFrame(a.T)
return df.nunique().to_numpy(dtype=np.float)
def steiner_terminal_2hop(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# verts = np.array(range(wts_0.size))
terminals = set()
adj_2 = adj_0.dot(adj_0)
verts = wts_0.argsort()
verts = verts.tolist()
while len(verts) > 0:
i = verts.pop(0)
_, nb1_set = np.nonzero(adj_0[i])
_, nb2_set = np.nonzero(adj_2[i])
terminals.add(i)
verts = list(set(verts) - set(nb1_set))
verts = list(set(verts) - set(nb2_set))
return terminals
def mwds_greedy_mis(adj, wts):
'''
Ref: Ant Colony Optimization Applied to Minimum Weighted Dominating Set Problem
Color: White: Uncovered, Black: Dominating, Gray: Covered
Return MWDS set and the total weights of MWDS
:param adj: adjacency matrix (sparse)
:param wts: weights of vertices
:return: mwds, total_wt
'''
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
verts = np.array(range(wts_0.size))
mwds = set()
gray_set = set()
white_set = set(verts)
wts_1 = wts_0.copy()
while len(white_set) > 0:
covers = adj_0.dot(wts_1)
# covers = adj_0.dot(np.diag(np.eye(wts_0.size)))
weights = np.divide(wts_0, 1 + covers)
# weights = wts_0 - covers
# weights = np.divide(1 + covers, wts_0)
weights[list(gray_set)] = np.Inf
weights[list(mwds)] = np.Inf
# weights[covers == 0] = -1
i = np.argmin(weights)
# if covers[i] == 0:
# continue
_, nb_set = np.nonzero(adj_0[i])
mwds.add(i)
nb_set = set(nb_set).intersection(white_set)
gray_set = gray_set.union(nb_set)
nb_set.add(i)
white_set = white_set - nb_set - mwds
rm_set = list(nb_set)
wts_1[rm_set] = 0
# adj_0 = zero_rows(adj_0, rm_set)
# adj_0 = zero_columns(adj_0, rm_set)
total_ws = np.sum(wts[list(mwds)])
return mwds, gray_set, total_ws
def mwds_greedy(adj, wts):
'''
Ref: Ant Colony Optimization Applied to Minimum Weighted Dominating Set Problem
Color: White: Uncovered, Black: Dominating, Gray: Covered
Return MWDS set and the total weights of MWDS
:param adj: adjacency matrix (sparse)
:param wts: weights of vertices
:return: mwds, total_wt
'''
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
verts = np.array(range(wts_0.size))
mwds = set()
gray_set = set()
white_set = set(verts)
wts_1 = wts_0.copy() + 1e-6
degrees = adj_0.sum(axis=1)
degrees = np.asarray(degrees).flatten()
while len(white_set) > 0:
covers = adj_0.dot(wts_1)
# covers = adj_0.dot(np.diag(np.eye(wts_0.size)))
weights = np.divide(wts_0, 1 + covers)
# weights = wts_0 - covers
weights[list(mwds)] = np.Inf
weights[np.logical_and(covers == 0, degrees > 0)] = np.Inf
i = np.argmin(weights)
_, nb_set = np.nonzero(adj_0[i])
mwds.add(i)
nb_set = set(nb_set).intersection(white_set)
gray_set = gray_set.union(nb_set)
nb_set.add(i)
white_set = white_set - nb_set - mwds
rm_set = list(nb_set)
wts_1[rm_set] = 0
total_ws = np.sum(wts[list(mwds)])
return mwds, gray_set, total_ws
def node_weight_steiner_set(adj, wts, mis):
# Shortest Path Heuristic
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
nodes = np.array(range(wts_0.size))
mwcds = copy.deepcopy(mis)
mwcds_list = list(mwcds)
gray_set = set(nodes) - mwcds
nodes_forest = np.full_like(nodes, np.nan, dtype=np.double)
nodes_forest[mwcds_list] = mwcds_list
nodes_forest += 1.0 # forest id initialized as node id
cover_forest = np.full_like(nodes, np.nan, dtype=np.double)
# Merge any connected trees
for i in mwcds_list:
_, nb_set = np.nonzero(adj_0[i])
nb_tid = nodes_forest[nb_set]
nb_tids = nb_tid[~np.isnan(nb_tid)]
cover_forest[i] = len(set(nb_set).intersection(gray_set))
# nb_trees = nb_set[~np.isnan(nb_tid)]
if len(nb_tids) > 0:
fid_max = np.nanmax(nb_tid)
nodes_forest[i] = fid_max
for tid in nb_tids:
nodes_forest[nodes_forest == tid] = fid_max
# Creates a quotient graph
tids = np.unique(nodes_forest[mwcds_list])
trees = [set(np.argwhere(nodes_forest==tid).flatten()) for tid in tids]
forest = []
for tree in trees:
tree_dict = {'head': max(tree), 'member': tree, 'cover': np.sum(cover_forest[list(tree)])}
forest.append(tree_dict)
wts_st = copy.deepcopy(wts_0)
wts_st[mwcds_list] = 0
def edge_weight(s, d, attr):
return wts_st[s]+wts_st[d]
g = nx.from_scipy_sparse_matrix(adj_0)
start = max(forest, key=lambda x: x['cover'])
forest.remove(start)
while len(forest) > 0:
dists = []
paths = []
for tree in forest:
src = start['head']
dst = tree['head']
path = nx.shortest_path(g, src, dst, weight=edge_weight)
dists.append(np.sum(wts_st[path]))
paths.append(path)
pid = np.argmin(dists)
path = paths[pid]
tree = forest[pid]
wts_st[path] = 0
newtree = start['member'].union(tree['member']).union(set(path))
newhead = max(newtree)
start['head'] = newhead
start['member'] = newtree
forest.remove(tree)
mwcds = start['member']
total_ws = np.sum(wts_0[list(mwcds)])
return mwcds, total_ws
def nwst_sph(adj, wts, mis):
# Shortest Path Heuristic, no sorting
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
nodes = np.array(range(wts_0.size))
mwcds = copy.deepcopy(mis)
mwcds_list = list(mwcds)
gray_set = set(nodes) - mwcds
nodes_forest = np.full_like(nodes, np.nan, dtype=np.double)
nodes_forest[mwcds_list] = mwcds_list
nodes_forest += 1.0 # forest id initialized as node id
cover_forest = np.full_like(nodes, np.nan, dtype=np.double)
# Merge any connected trees
for i in mwcds_list:
_, nb_set = np.nonzero(adj_0[i])
nb_tid = nodes_forest[nb_set]
nb_tids = nb_tid[~np.isnan(nb_tid)]
cover_forest[i] = len(set(nb_set).intersection(gray_set))
# nb_trees = nb_set[~np.isnan(nb_tid)]
if len(nb_tids) > 0:
fid_max = np.nanmax(nb_tid)
nodes_forest[i] = fid_max
for tid in nb_tids:
nodes_forest[nodes_forest == tid] = fid_max
# Creates a quotient graph
tids = np.unique(nodes_forest[mwcds_list])
trees = [set(np.argwhere(nodes_forest==tid).flatten()) for tid in tids]
forest = []
for tree in trees:
tree_dict = {'head': max(tree), 'member': tree, 'cover': np.sum(cover_forest[list(tree)])}
forest.append(tree_dict)
wts_st = copy.deepcopy(wts_0)
wts_st[mwcds_list] = 0
def edge_weight(s, d, attr):
return wts_st[s]+wts_st[d]
g = nx.from_scipy_sparse_matrix(adj_0)
# start = max(forest, key=lambda x: x['cover'])
start = forest[0]
forest.remove(start)
while len(forest) > 0:
dists = []
paths = []
for tree in forest:
src = start['head']
dst = tree['head']
path = nx.shortest_path(g, src, dst, weight=edge_weight)
dists.append(np.sum(wts_st[path]))
paths.append(path)
pid = np.argmin(dists)
path = paths[pid]
tree = forest[pid]
wts_st[path] = 0
newtree = start['member'].union(tree['member']).union(set(path))
newhead = max(newtree)
start['head'] = newhead
start['member'] = newtree
forest.remove(tree)
mwcds = start['member']
total_ws = np.sum(wts_0[list(mwcds)])
return mwcds, total_ws
def steiner_tree_mst(adj, wts, terminals):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
nodes = np.array(range(wts_0.size))
graph = nx.from_scipy_sparse_matrix(adj_0)
for u in graph:
graph.nodes[u]['weight'] = wts_0[u]
mwcds = copy.deepcopy(terminals)
mwcds_list = list(mwcds)
wts_st = copy.deepcopy(wts_0)
wts_st[mwcds_list] = 0
def edge_weight(s, d, attr):
return wts_st[s]+wts_st[d]
total_ws = 0.0
sg = nx.algorithms.approximation.steinertree.steiner_tree(graph, terminals, weight=edge_weight)
st = list(sg.nodes)
mwcds = set(st)
total_ws = np.sum(wts_0[st])
return mwcds, total_ws
def greedy_mwcds(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
mwds, gray_set, _ = mwds_greedy_mis(adj_0, wts_0)
# step 2: find node weighted steiner tree, with MWDS as the set of terminals
nodes = np.array(range(wts_0.size))
mwcds = copy.deepcopy(mwds)
mwds_list = list(mwds)
nodes_forest = np.full_like(nodes, np.nan, dtype=np.double)
nodes_forest[mwds_list] = mwds_list
nodes_forest += 1
gray_list = np.array(list(gray_set))
gray_wts = wts_0[gray_list]
idx = np.argsort(gray_wts)
gray_list = gray_list[idx]
degrees = adj_0.sum(axis=1)
degrees = np.asarray(degrees).flatten()
for i in gray_list:
if degrees[i] <= 1:
continue
_, nb_set = np.nonzero(adj_0[i])
nb_fid = nodes_forest[nb_set]
nb_fid_set = set(nb_fid[~np.isnan(nb_fid)])
if len(nb_fid_set) > 1:
mwcds.add(i)
fid_max = np.nanmax(nb_fid)
nodes_forest[i] = fid_max
for fid in nb_fid_set:
nodes_forest[nodes_forest==fid] = fid_max
total_ws = np.sum(wts_0[list(mwcds)])
return mwcds, mwds, total_ws
def greedy_mwcds2(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
# mwds, gray_set, _ = mwds_greedy_mis(adj_0, wts_0)
mwds, gray_set, _ = mwds_greedy(adj_0, wts_0)
# step 2: find node weighted steiner tree, with MWDS as the set of terminals
mwcds, total_ws = node_weight_steiner_set(adj_0, wts_0, mwds)
return mwcds, mwds, total_ws
def mwcds_vvv(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
graph = nx.from_scipy_sparse_matrix(adj_0)
for u in graph:
graph.nodes[u]['weight'] = wts_0[u]
mwds = nx.algorithms.approximation.min_weighted_dominating_set(graph, 'weight')
# step 2: find node weighted steiner tree, with MWDS as the set of terminals
mwcds, total_ws = node_weight_steiner_set(adj_0, wts_0, mwds)
return mwcds, mwds, total_ws
def mwds_vvv(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
graph = nx.from_scipy_sparse_matrix(adj_0)
for u in graph:
graph.nodes[u]['weight'] = wts_0[u]
mwds = nx.algorithms.approximation.min_weighted_dominating_set(graph, 'weight')
total_ws = np.sum(wts_0[list(mwds)])
gray_set = set(range(wts_0.size)) - mwds
return mwds, gray_set, total_ws
def dist_greedy_mwds(adj, wts):
'''
Ref: Ant Colony Optimization Applied to Minimum Weighted Dominating Set Problem (weight heuristic)
Color: White: Uncovered, Black: Dominating, Gray: Covered
Return MWDS set and the total weights of MWDS
:param adj: adjacency matrix (sparse)
:param wts: weights of vertices
:return: mwds, total_wt
'''
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
verts = np.array(range(wts_0.size))
mwds = set()
gray_set = set()
white_set = set(verts)
while len(white_set) > 0:
# weights = np.divide(1 + adj_0.dot(np.diag(np.eye(wts_0.size))), wts_0)
weights = np.divide(wts_0, 1 + adj_0.dot(wts_0))
weights[list(gray_set)] = 0
weights[list(mwds)] = 0
for i in white_set:
proc = False
_, nb_set = np.nonzero(adj_0[i])
if len(nb_set) > 0:
nb_min = np.amin(weights[nb_set])
nb_min_set = nb_set[weights[nb_set] == nb_min]
if weights[i] < nb_min or (weights[i] == nb_min and i < np.amax(nb_min_set)):
proc = True
else:
proc = True
if proc:
mwds.add(i)
nb_set = set(nb_set).intersection(white_set)
gray_set = gray_set.union(nb_set)
nb_set.add(i)
rm_set = gray_set.union(mwds)
white_set = white_set - rm_set
adj_0 = zero_rows(adj_0, list(rm_set))
adj_0 = zero_columns(adj_0, list(rm_set))
total_ws = np.sum(wts_0[list(mwds)])
return mwds, gray_set, total_ws
def dist_node_weight_steiner_set(adj, wts, mis):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
nodes = np.array(range(wts_0.size))
mwcds = copy.deepcopy(mis)
mwcds_list = list(mwcds)
gray_set = set(nodes) - mwcds
nodes_forest = np.full_like(nodes, np.nan, dtype=np.double)
nodes_forest[mwcds_list] = mwcds_list
nodes_forest += 1.0 # forest id initialized as node id
cover_forest = np.full_like(nodes, np.nan, dtype=np.double)
# Merge any connected trees
for i in mwcds_list:
_, nb_set = np.nonzero(adj_0[i])
nb_tid = nodes_forest[nb_set]
nb_tids = nb_tid[~np.isnan(nb_tid)]
cover_forest[i] = len(set(nb_set).intersection(gray_set))
# nb_trees = nb_set[~np.isnan(nb_tid)]
if len(nb_tids) > 0:
fid_max = np.nanmax(nb_tid)
nodes_forest[i] = fid_max
for tid in nb_tids:
nodes_forest[nodes_forest == tid] = fid_max
# Creates a quotient graph
tids = np.unique(nodes_forest[mwcds_list])
trees = [set(np.argwhere(nodes_forest==tid).flatten()) for tid in tids]
forest = []
for tree in trees:
tree_dict = {'head': max(tree), 'member': tree}
forest.append(tree_dict)
wts_st = copy.deepcopy(wts_0)
wts_st[mwcds_list] = 0
def edge_weight(s, d, attr):
return wts_st[s]+wts_st[d]
g = nx.from_scipy_sparse_matrix(adj_0)
while len(forest) > 1:
dst_paths = []
dst_trees = []
for start in forest:
dists = []
paths = []
for tree in forest:
if tree['head'] == start['head']:
continue
src = start['head']
dst = tree['head']
path = nx.shortest_path(g, src, dst, weight=edge_weight)
dists.append(np.sum(wts_st[path]))
paths.append(path)
pid = np.argmin(dists)
path = paths[pid]
# tree = forest[pid]
dst_trees.append(pid)
dst_paths.append(path)
for tid in range(len(forest)):
src_tree = forest[tid]
dst_tree = forest[dst_trees[tid]]
path = dst_paths[tid]
wts_st[path] = 0
new_tree = src_tree['member'].union(dst_tree['member']).union(set(path))
new_head = max(new_tree)
new_item = {'head': new_head, 'member': new_tree}
forest[tid] = new_item
forest[dst_trees[tid]] = new_item
# forest = list(set(forest))
exclude_list = []
del_list = []
for tid in range(len(forest)):
src_tree = forest[tid]
exclude_list.append(tid)
for ttid in range(len(forest)):
if ttid in exclude_list:
continue
else:
dst_tree = forest[ttid]
if len(src_tree['member'].intersection(dst_tree['member'])) > 0:
new_tree = src_tree['member'].union(dst_tree['member'])
new_head = max(new_tree)
new_item = {'head': new_head, 'member': new_tree}
src_tree = new_item
exclude_list.append(ttid)
del_list.append(ttid)
forest[tid] = src_tree
tid += 1
forest = [forest[idx] for idx in range(len(forest)) if idx not in del_list]
mwcds = forest[0]['member']
total_ws = np.sum(wts_0[list(mwcds)])
return mwcds, total_ws
def dist_greedy_mwcds(adj, wts):
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
mwds, gray_set, _ = dist_greedy_mwds(adj_0, wts_0)
mwcds, total_ws = node_weight_steiner_set(adj_0, wts_0, mwds)
return mwcds, mwds, total_ws
def dist_greedy_mwcds2(adj, wts):
'''
Return MWCDS set and the total weights of MWCDS
:param adj: adjacency matrix (sparse)
:param wts: weights of vertices
:param epislon: 0<epislon<1, to determin alpha and beta
:return: mwis, total_wt
'''
adj_0 = adj.copy()
wts_0 = np.array(wts).flatten()
# step 1: find MWDS, which is an independent set
mwds, gray_set, _ = dist_greedy_mwds(adj_0, wts_0)
# step 2: find node weighted steiner tree, with MWDS as the set of terminals
nodes = np.array(range(wts_0.size))
mwcds = copy.deepcopy(mwds)
mwcds_list = list(mwcds)
leafs = set()
nodes_forest = np.full_like(nodes, np.nan, dtype=np.double)
nodes_forest[mwcds_list] = mwcds_list
nodes_forest += 1.0
# Merge any connected trees
for i in mwcds_list:
_, nb_set = np.nonzero(adj_0[i])
nb_tid = nodes_forest[nb_set]
nb_tids = nb_tid[~np.isnan(nb_tid)]
# nb_trees = nb_set[~np.isnan(nb_tid)]
if len(nb_tids) > 0:
fid_max = np.nanmax(nb_tid)
nodes_forest[i] = fid_max
for tid in nb_tids:
nodes_forest[nodes_forest == tid] = fid_max
# adj_2 = adj_0.dot(adj_0)
while np.unique(nodes_forest[mwcds_list]).size > 1:
fid_mtx = adj_0.dot(np.diag(nodes_forest))
fid_mtx[fid_mtx == 0] = np.nan
fcover = nunique(fid_mtx)
# fid_mtx2 = adj_2.dot(np.diag(nodes_forest))
# fid_mtx2[fid_mtx2 == 0] = np.nan
# fcover2 = nunique(fid_mtx2)
weights = np.divide(wts_0, 1 + fcover)
weights[list(mwcds)] = np.Inf
weights[list(leafs)] = np.Inf
gray_set = gray_set - mwcds - leafs
for i in gray_set:
# if fcover[i] <= 1:
# leafs.add(i)
# continue
_, nb_set = np.nonzero(adj_0[i])
nb_min = np.amin(weights[nb_set])
nb_min_set = nb_set[weights[nb_set] == nb_min]
self_cover = np.unique(fid_mtx[i])
self_cover = self_cover[~np.isnan(self_cover)].tolist()
nb_weights = np.zeros_like(nb_set, dtype=np.float)
pair_cov_map = {}
for j in range(len(nb_set)):
nb_v = nb_set[j]
_, nb_pair_set = np.nonzero(adj_0[nb_v])
nb_fids = nodes_forest[nb_pair_set]
nb_cover = np.unique(nb_fids[~np.isnan(nb_fids)]).tolist()
pair_cover = set(self_cover).union(set(nb_cover))
p_cover = float(len(pair_cover))
wts_pair = (wts_0[i] + wts_0[nb_v])/(1+p_cover)
pair_cov_map[nb_v] = pair_cover
if p_cover > 1:
nb_weights[j] = wts_pair
else:
nb_weights[j] = np.Inf
nbp_min = np.amin(nb_weights)
nbvp_min = min(nb_min, nbp_min)
if (weights[i] < nbvp_min and fcover[i] > 1) or (weights[i] == nbvp_min and i < np.amax(nb_min_set)):
nb_fid = nodes_forest[nb_set]
nb_fid_set = set(nb_fid[~np.isnan(nb_fid)])
if len(nb_fid_set) > 1:
mwcds.add(i)
fid_max = np.nanmax(nb_fid)
nodes_forest[i] = fid_max
for fid in nb_fid_set:
nodes_forest[nodes_forest == fid] = fid_max
elif nbp_min <= nbvp_min:
# a pair is the smallest
nbp_set = nb_set[nb_weights == nbp_min]
nbp = np.amin(nbp_set)
nb_fid_set = pair_cov_map[nbp]
if len(nb_fid_set) > 1:
mwcds.add(i)
mwcds.add(nbp)
fid_max = np.nanmax(list(nb_fid_set))
nodes_forest[i] = fid_max
nodes_forest[nbp] = fid_max
for fid in nb_fid_set:
nodes_forest[nodes_forest == fid] = fid_max
elif fcover[i] <= 1:
leafs.add(i)
mwcds_list = list(mwcds)
total_ws = np.sum(wts_0[list(mwcds)])
return mwcds, mwds, total_ws
def test_heuristic():
# Create a random graph
t = time.time()
seed = np.random.randint(1, 10000)
# seed = 1844
# seed = 5251
# graph = nx.gaussian_random_partition_graph(200, 10, 10, 0.25, 0.1, seed=seed)
graph = nx.generators.random_graphs.barabasi_albert_graph(200, 10)
wts = np.random.uniform(0.1, 1.1, (200,))
N = len(graph.nodes)
for u in graph:
graph.nodes[u]['weight'] = wts[u] # np.square(np.random.randn())
# graph.nodes[u]['id'] = u
print("Time to create graph: {:.3f} s, graph seed: {}, connected: {}\n".format(time.time()-t, seed, nx.is_connected(graph)))
# Run Neighborhood Removal
adj = nx.adjacency_matrix(graph, nodelist=list(range(N)))
weights = np.array(wts)
vertices = np.array(range(N))
t = time.time()
# mwds = nx.algorithms.approximation.min_weighted_dominating_set(graph, 'weight')
# mwcds, total_wt = node_weight_steiner_set(adj, weights, set(mwds))
mwcds, mwds, total_wt = mwcds_vvv(adj, weights)
subgraph = graph.subgraph(mwcds)
print("min_weighted_dominating_set: {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds), len(mwcds), mwcds))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds), nx.is_connected(subgraph)))
t = time.time()
mwds = nx.algorithms.maximal_independent_set(graph)
mwds = set(mwds)
mwcds, total_wt = node_weight_steiner_set(adj, weights, mwds)
# mwcds, total_wt = steiner_tree_mst(adj, weights, mwds)
subgraph = graph.subgraph(mwcds)
print("maximal_independent_set + SPH: {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds), len(mwcds), mwcds))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds), nx.is_connected(subgraph)))
t = time.time()
mwds = nx.algorithms.maximal_independent_set(graph)
mwds = set(mwds)
# mwcds, total_wt = node_weight_steiner_set(adj, weights, mwds)
mwcds, total_wt = steiner_tree_mst(adj, weights, mwds)
subgraph = graph.subgraph(mwcds)
print("maximal_independent_set + MST: {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds), len(mwcds), mwcds))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds), nx.is_connected(subgraph)))
t = time.time()
mwcds1, mwds1, total_wt1 = greedy_mwcds2(adj, weights)
subgraph = graph.subgraph(mwcds1)
print("Time of greedy search 1: {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt1, len(mwds1), len(mwcds1), mwcds1))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds1), nx.is_connected(subgraph)))
# t = time.time()
# mwcds0, mwds0, total_wt = greedy_mwcds(adj, weights)
# subgraph = graph.subgraph(mwcds0)
# print("Time of greedy search 0: {:.3f} s".format(time.time()-t))
# print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
# print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds0), len(mwcds0), mwcds0))
# print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds0), nx.is_connected(subgraph)))
t = time.time()
mwcds, mwds, total_wt = dist_greedy_mwcds(adj, weights)
subgraph = graph.subgraph(mwcds)
print("Time of distributed greedy search (1-hop): {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds), len(mwcds), mwcds))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds), nx.is_connected(subgraph)))
t = time.time()
mwcds, mwds, total_wt = dist_greedy_mwcds2(adj, weights)
subgraph = graph.subgraph(mwcds)
print("Time of distributed greedy search (2-hop): {:.3f} s".format(time.time()-t))
print("Original Graph: {} nodes, {} edges.".format(graph.number_of_nodes(), graph.number_of_edges()))
print("Total Weights: {:.3f}, DS size: {}, CDS size: {}\n{}".format(total_wt, len(mwds), len(mwcds), mwcds))
print("Validation: dominate {}, connected {}\n".format(nx.is_dominating_set(graph, mwds), nx.is_connected(subgraph)))
if __name__ == "__main__":
test_heuristic()