annotate toolboxes/FullBNT-1.0.7/graph/mk_nbrs_of_digraph_broken.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [Gs, op, nodes] = mk_nbrs_of_digraph(G0)
Daniel@0 2 % MK_NBRS_OF_DIGRAPH Make all digraphs that differ from G0 by a single edge deletion, addition or reversal
Daniel@0 3 % [Gs, op, nodes] = mk_nbrs_of_digraph(G0)
Daniel@0 4 %
Daniel@0 5 % Gs(:,:,i) is the i'th neighbor
Daniel@0 6 % op{i} = 'add', 'del', or 'rev' is the operation used to create the i'th neighbor.
Daniel@0 7 % nodes(i,1:2) are the head and tail of the operated-on arc.
Daniel@0 8
Daniel@0 9 debug = 0; % the vectorized version is about 3 to 10 times faster
Daniel@0 10
Daniel@0 11 n = length(G0);
Daniel@0 12 [I,J] = find(G0); % I(k), J(k) is the k'th edge
Daniel@0 13 E = length(I); % num edges present in G0
Daniel@0 14
Daniel@0 15 % SINGLE EDGE DELETIONS
Daniel@0 16
Daniel@0 17 Grep = repmat(G0(:), 1, E); % each column is a copy of G0
Daniel@0 18 % edge_ndx(k) is the scalar location of the k'th edge
Daniel@0 19 edge_ndx = find(G0);
Daniel@0 20 % edge_ndx = subv2ind([n n], [I J]); % equivalent
Daniel@0 21 % We set (ndx(k), k) to 0 for k=1:E in Grep
Daniel@0 22 ndx = subv2ind(size(Grep), [edge_ndx(:) (1:E)']);
Daniel@0 23 G1 = Grep;
Daniel@0 24 G1(ndx) = 0;
Daniel@0 25 Gdel = reshape(G1, [n n E]);
Daniel@0 26
Daniel@0 27
Daniel@0 28 % if debug
Daniel@0 29 % % Non-vectorized version
Daniel@0 30 % ctr = 1;
Daniel@0 31 % for e=1:E
Daniel@0 32 % i = I(e); j = J(e);
Daniel@0 33 % Gdel2(:,:,ctr) = G0;
Daniel@0 34 % Gdel2(i,j,ctr) = 0;
Daniel@0 35 % ctr = ctr + 1;
Daniel@0 36 % end
Daniel@0 37 % assert(isequal(Gdel, Gdel2));
Daniel@0 38 % end
Daniel@0 39
Daniel@0 40
Daniel@0 41 % SINGLE EDGE REVERSALS
Daniel@0 42
Daniel@0 43 % rev_edge_ndx(k) is the scalar location of the k'th reversed edge
Daniel@0 44 %rev_edge_ndx = find(G0'); % different order to edge_ndx, which is bad
Daniel@0 45 rev_edge_ndx = subv2ind([n n], [J I]);
Daniel@0 46 % We set (rev_edge_ndx(k), k) to 1 for k=1:E in G1
Daniel@0 47 % We have already deleted i->j in the previous step
Daniel@0 48 ndx = subv2ind(size(Grep), [rev_edge_ndx(:) (1:E)']);
Daniel@0 49 G1(ndx) = 1;
Daniel@0 50 Grev = reshape(G1, [n n E]);
Daniel@0 51
Daniel@0 52 % if debug
Daniel@0 53 % % Non-vectorized version
Daniel@0 54 % ctr = 1;
Daniel@0 55 % for e=1:E
Daniel@0 56 % i = I(e); j = J(e);
Daniel@0 57 % Grev2(:,:,ctr) = G0;
Daniel@0 58 % Grev2(i,j,ctr) = 0;
Daniel@0 59 % Grev2(j,i,ctr) = 1;
Daniel@0 60 % ctr = ctr + 1;
Daniel@0 61 % end
Daniel@0 62 % assert(isequal(Grev, Grev2));
Daniel@0 63 % end
Daniel@0 64
Daniel@0 65
Daniel@0 66 % SINGLE EDGE ADDITIONS
Daniel@0 67
Daniel@0 68 Gbar = ~G0; % Gbar(i,j)=1 iff there is no i->j edge in G0
Daniel@0 69 Gbar = setdiag(Gbar, 0); % turn off self loops
Daniel@0 70 [Ibar,Jbar] = find(Gbar);
Daniel@0 71
Daniel@0 72 bar_edge_ndx = find(Gbar);
Daniel@0 73 Ebar = length(Ibar); % num edges present in Gbar
Daniel@0 74 Grep = repmat(G0(:), 1, Ebar); % each column is a copy of G0
Daniel@0 75 ndx = subv2ind(size(Grep), [bar_edge_ndx(:) (1:Ebar)']);
Daniel@0 76 Grep(ndx) = 1;
Daniel@0 77 Gadd = reshape(Grep, [n n Ebar]);
Daniel@0 78
Daniel@0 79 % if debug
Daniel@0 80 % % Non-vectorized version
Daniel@0 81 % ctr = 1;
Daniel@0 82 % for e=1:length(Ibar)
Daniel@0 83 % i = Ibar(e); j = Jbar(e);
Daniel@0 84 % Gadd2(:,:,ctr) = G0;
Daniel@0 85 % Gadd2(i,j,ctr) = 1;
Daniel@0 86 % ctr = ctr + 1;
Daniel@0 87 % end
Daniel@0 88 % assert(isequal(Gadd, Gadd2));
Daniel@0 89 % end
Daniel@0 90
Daniel@0 91
Daniel@0 92 Gs = cat(3, Gdel, Grev, Gadd);
Daniel@0 93
Daniel@0 94 nodes = [I J;
Daniel@0 95 I J;
Daniel@0 96 Ibar Jbar];
Daniel@0 97
Daniel@0 98 op = cell(1, E+E+Ebar);
Daniel@0 99 op(1:E) = {'del'};
Daniel@0 100 op(E+1:2*E) = {'rev'};
Daniel@0 101 op(2*E+1:end) = {'add'};
Daniel@0 102
Daniel@0 103
Daniel@0 104 % numeric output:
Daniel@0 105 % op(i) = 1, 2, or 3, if the i'th neighbor was created by adding, deleting or reversing an arc.
Daniel@0 106
Daniel@0 107 ADD = 1;
Daniel@0 108 DEL = 2;
Daniel@0 109 REV = 3;
Daniel@0 110
Daniel@0 111 %op = [repmat(DEL, 1, E) repmat(REV, 1, E) repmat(ADD, 1, Ebar)];