To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / graph / mk_nbrs_of_digraph_broken.m @ 8:b5b38998ef3b

History | View | Annotate | Download (2.77 KB)

1
function [Gs, op, nodes] = mk_nbrs_of_digraph(G0)
2
% MK_NBRS_OF_DIGRAPH Make all digraphs that differ from G0 by a single edge deletion, addition or reversal
3
% [Gs, op, nodes] = mk_nbrs_of_digraph(G0)
4
%
5
% Gs(:,:,i) is the i'th neighbor
6
% op{i} = 'add', 'del', or 'rev' is the operation used to create the i'th neighbor. 
7
% nodes(i,1:2) are the head and tail of the operated-on arc.
8

    
9
debug = 0; % the vectorized version is about 3 to 10 times faster
10

    
11
n = length(G0);
12
[I,J] = find(G0); % I(k), J(k) is the k'th edge
13
E = length(I); % num edges present in G0
14

    
15
% SINGLE EDGE DELETIONS
16

    
17
Grep = repmat(G0(:), 1, E); % each column is a copy of G0
18
% edge_ndx(k) is the scalar location of the k'th edge 
19
edge_ndx = find(G0);
20
% edge_ndx = subv2ind([n n], [I J]); % equivalent
21
% We set (ndx(k), k) to 0 for k=1:E in Grep
22
ndx = subv2ind(size(Grep), [edge_ndx(:) (1:E)']);
23
G1 = Grep;
24
G1(ndx) = 0;
25
Gdel = reshape(G1, [n n E]);
26

    
27

    
28
% if debug
29
% % Non-vectorized version
30
% ctr = 1;
31
% for e=1:E
32
%   i = I(e); j = J(e);
33
%   Gdel2(:,:,ctr) = G0;
34
%   Gdel2(i,j,ctr) = 0;
35
%   ctr = ctr + 1;
36
% end
37
% assert(isequal(Gdel, Gdel2));
38
% end
39

    
40

    
41
% SINGLE EDGE REVERSALS
42

    
43
% rev_edge_ndx(k) is the scalar location of the k'th reversed edge
44
%rev_edge_ndx = find(G0'); % different order to edge_ndx, which is bad
45
rev_edge_ndx = subv2ind([n n], [J I]);
46
% We set (rev_edge_ndx(k), k) to 1 for k=1:E in G1
47
% We have already deleted i->j in the previous step
48
ndx = subv2ind(size(Grep), [rev_edge_ndx(:) (1:E)']);
49
G1(ndx) = 1;
50
Grev = reshape(G1, [n n E]);
51

    
52
% if debug
53
% % Non-vectorized version
54
% ctr = 1;
55
% for e=1:E
56
%   i = I(e); j = J(e);
57
%   Grev2(:,:,ctr) = G0;
58
%   Grev2(i,j,ctr) = 0;
59
%   Grev2(j,i,ctr) = 1;
60
%   ctr = ctr + 1;
61
% end
62
% assert(isequal(Grev, Grev2));
63
% end
64

    
65

    
66
% SINGLE EDGE ADDITIONS
67

    
68
Gbar = ~G0; % Gbar(i,j)=1 iff there is no i->j edge in G0
69
Gbar = setdiag(Gbar, 0); % turn off self loops
70
[Ibar,Jbar] = find(Gbar); 
71

    
72
bar_edge_ndx = find(Gbar);
73
Ebar = length(Ibar); % num edges present in Gbar
74
Grep = repmat(G0(:), 1, Ebar); % each column is a copy of G0
75
ndx = subv2ind(size(Grep), [bar_edge_ndx(:) (1:Ebar)']);
76
Grep(ndx) = 1;
77
Gadd = reshape(Grep, [n n Ebar]);
78

    
79
% if debug
80
% % Non-vectorized version
81
% ctr = 1;
82
% for e=1:length(Ibar)
83
%   i = Ibar(e); j = Jbar(e);
84
%   Gadd2(:,:,ctr) = G0;
85
%   Gadd2(i,j,ctr) = 1;
86
%   ctr = ctr + 1;
87
% end
88
% assert(isequal(Gadd, Gadd2));
89
% end
90

    
91

    
92
Gs = cat(3, Gdel, Grev, Gadd);
93

    
94
nodes = [I J;
95
	 I J;
96
	 Ibar Jbar];
97

    
98
op = cell(1, E+E+Ebar);
99
op(1:E) = {'del'};
100
op(E+1:2*E) = {'rev'};
101
op(2*E+1:end) = {'add'};
102

    
103

    
104
% numeric output:
105
% op(i) = 1, 2, or 3, if the i'th neighbor was created by adding, deleting or reversing an arc.
106

    
107
ADD = 1;
108
DEL = 2;
109
REV = 3;
110

    
111
%op = [repmat(DEL, 1, E) repmat(REV, 1, E) repmat(ADD, 1, Ebar)];