-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathnumConnTriples.m
executable file
·63 lines (51 loc) · 1.52 KB
/
numConnTriples.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
% Count the number of connected triples in a graph.
% Note: works for undirected graphs only
%
% INPUTs: adjacency matrix, nxn
% OUTPUTs: integer - number of connected triples
%
% Other routines used: kneighbors.m, cycles3.m
% Last updated: October 4, 2012
function c = numConnTriples(adj)
c = 0; % initialize
for i = 1:length(adj)
neigh = kneighbors(adj, i, 1);
if length(neigh) < 2
continue;
end % handle leaves, no triple here
c = c + nchoosek(length(neigh), 2);
end
c = c - 2 * cycles3(adj); % due to the symmetry triangles repeat 3 times
% in the nchoosek count
% alternative
% def numConnTriples(L):
%
% % input: adjacency list
% % outputs: number of connected triples
%
% c=0; % initialize number of connected triples
%
% for i=1:length(L)
% neigh = L{i}
% if length(neigh)<2: continue; end
% c = c + nchoosek(length(neigh),2);
% end
%
% c = c - 2*cycles3(adjL2adj(L));
%!test
%!shared T
%! T = load_test_graphs();
%!assert(numConnTriples(T{4}{2}),6)
%!assert(numConnTriples(T{13}{2}),1)
%!assert(numConnTriples(edgeL2adj(T{10}{2})),1)
%!assert(numConnTriples(T{2}{2}),0)
%!assert(numConnTriples(T{18}{2}),4)
%!demo
%! cycle3 = [0 1 1; 1 0 1; 1 1 0];
%! numConnTriples(cycle3)
%! % ans = 1
%! adj = [0 1 0; 1 0 0; 0 0 0];
%! numConnTriples(adj)
%! % ans = 0
%! bowtie = [0 1 1 0 0 0; 1 0 1 0 0 0; 1 1 0 1 0 0; 0 0 1 0 1 1; 0 0 0 1 0 1; 0 0 0 1 1 0];
%! numConnTriples(bowtie)