-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathadj2inc.m
executable file
·72 lines (49 loc) · 2.08 KB
/
adj2inc.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% Convert adjacency matrix to an incidence matrix
% Note: Valid for directed/undirected, simple/not simple graphs
%
% INPUTs: adjacency matrix, nxn
% OUTPUTs: incidence matrix: n x m (number of edges)
%
% Other routines used: isDirected.m
% Last updated: May 20 2023
function inc = adj2inc(adj)
n = length(adj); % number of nodes
inc = []; % initialize incidence matrix
if isDirected(adj)
for i = 1:n
for j = 1:n
% handle self-loops
if i == j; for x = 1:adj(i, j); inc = [inc; zeros(1, length(1:i - 1)), 1, zeros(1, length(i + 1:n))]; end; continue; end
for x = 1:adj(i, j)% add multiple edges if any
if i < j
inc = [inc; zeros(1, length(1:i - 1)), -1, zeros(1, length(i + 1:j - 1)), 1, zeros(1, length(j + 1:n))];
else
inc = [inc; zeros(1, length(1:j - 1)), 1, zeros(1, length(j + 1:i - 1)), -1, zeros(1, length(i + 1:n))];
end
end
end
end
else % undirected
for i = 1:n
for j = i:n
% handle self-loops
if i == j; for x = 1:adj(i, j); inc = [inc; zeros(1, length(1:i - 1)), 1, zeros(1, length(i + 1:n))]; end; continue; end
% add multiple edges if any
for x = 1:adj(i, j); inc = [inc; zeros(1, length(1:i - 1)), 1, zeros(1, length(i + 1:j - 1)), 1, zeros(1, length(j + 1:n))]; end
end
end
end
inc = inc';
%!test
%!shared T, randint
%! T = load_test_graphs();
%! randint = randi(10)+1;
%!assert(adj2inc(eye(randint)),eye(randint))
%!assert(adj2inc(T{13}{2}), T{15}{2}) % directed 3-cycle
%!assert(adj2inc([0 1 0; 0 1 0; 1 0 0 ]),[-1 0 1; 1 1 0; 0 0 -1])
%!assert(adj2inc([0 2; 0 0]),[-1 -1; 1 1]) % directed double edge
%!assert(adj2inc(T{1}{2}), [-1 1]') % one directed edge
%!demo
%! adj2inc([0 1 0; 1 0 0; 0 0 0]) % one undirected edge
%! adj2inc([0 1 1; 1 0 0; 1 0 0]) % two undirected edges
%! adj2inc([0 1 1; 0 0 0; 0 0 0]) % two directed edges