forked from aeolianine/octave-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinc2adj.m
executable file
·53 lines (39 loc) · 1.24 KB
/
inc2adj.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
%##################################################################
% Convert an incidence matrix representation to an adjacency matrix representation for an arbitrary graph.
%
% INPUTs: incidence matrix, nxm (num nodes x num edges)
% OUTPUTs: adjacency matrix, nxn
%
% GB: last updated, Sep 25, 2012
%##################################################################
function adj = inc2adj(inc)
m = size(inc,2); % number of edges
adj = zeros(size(inc,1)); % initialize adjacency matrix
if isempty(find(inc==-1)) % undirected graph
for e=1:m
ind=find(inc(:,e)==1);
if length(ind)==2
adj(ind(1),ind(2))=1;
adj(ind(2),ind(1))=1;
elseif length(ind)==1 % selfloop
adj(ind,ind)=1;
else
'invalid incidence matrix'
return
end
end
else % directed graph (there are "-1"
% entries in the incidence matrix)
for e=1:m
ind1=find(inc(:,e)==1);
indm1=find(inc(:,e)==-1);
if isempty(indm1) & length(ind1)==1 % selfloop
adj(ind1,ind1)=1;
elseif length(indm1)==1 & length(ind1)==1
adj(indm1,ind1)=1;
else
'invalid incidence matrix'
return
end
end
end