-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathmk_nbrs_of_dag.m
64 lines (52 loc) · 1.15 KB
/
mk_nbrs_of_dag.m
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
function [Gs, op, nodes] = mk_nbrs_of_dag(G0)
% MK_NBRS_OF_DAG Make all DAGs that differ from G0 by a single edge deletion, addition or reversal
% [Gs, op, nodes] = mk_nbrs_of_dag(G0)
%
% Gs{i} is the i'th neighbor.
% op{i} = 'add', 'del', or 'rev' is the operation used to create the i'th neighbor.
% nodes(i,1:2) are the head and tail of the operated-on arc.
Gs = {};
op = {};
nodes = [];
[I,J] = find(G0);
nnbrs = 1;
% all single edge deletions
for e=1:length(I)
i = I(e); j = J(e);
G = G0;
G(i,j) = 0;
Gs{nnbrs} = G;
op{nnbrs} = 'del';
nodes(nnbrs, :) = [i j];
nnbrs = nnbrs + 1;
end
% all single edge reversals
for e=1:length(I)
i = I(e); j = J(e);
G = G0;
G(i,j) = 0;
G(j,i) = 1;
if acyclic(G)
Gs{nnbrs} = G;
op{nnbrs} = 'rev';
nodes(nnbrs, :) = [i j];
nnbrs = nnbrs + 1;
end
end
[I,J] = find(~G0);
% all single edge additions
for e=1:length(I)
i = I(e); j = J(e);
if i ~= j % don't add self arcs
G = G0;
G(i,j) = 1;
if G(j,i)==0 % don't add i->j if j->i exists already
if acyclic(G)
Gs{nnbrs} = G;
op{nnbrs} = 'add';
nodes(nnbrs, :) = [i j];
nnbrs = nnbrs + 1;
end
end
end
end