forked from aeolianine/octave-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathloops4.m
executable file
·43 lines (33 loc) · 1.25 KB
/
loops4.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
%##################################################################
% Find cycles of length 4 in a graph;
% Note 1: Quite basic and slow.
% Note 2: Assumes undirected graph.
%
% INPUTs: adj - adjacency matrix of graph, nxn
% OUTPUTs: 4-tuples of nodes that form 4-cycles;
% format: {"n1-n2-n3-n4","n5-n6-n7-n8",...}
%
% Other functions used: adj2adjL.m
% GB: last updated, Oct 5 2012
%##################################################################
function l4 = loops4(adj)
n = size(adj,1); % number of nodes
L = adj2adjL(adj); % adjacency list or list of neighbors
l4 = {}; % initialize loops of size 4
for i=1:n-1
for j=i+1:n
int=intersect(L{i},L{j});
int=setdiff(int,[i j]);
if length(int)>=2
% enumerate pairs in the intersection
for ii=1:length(int)-1
for jj=ii+1:length(int)
loop4=sort([i,j,int(ii),int(jj)]);
loop4=strcat(num2str(loop4(1)),'-',num2str(loop4(2)),'-',num2str(loop4(3)),'-',num2str(loop4(4)));
if sum(ismember(l4,loop4))>0; continue; end
l4{length(l4)+1}=loop4;
end
end
end
end
end