-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathfindConnCompI.m
executable file
·51 lines (41 loc) · 1.48 KB
/
findConnCompI.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
% Find the connected component to which node "i" belongs to
%
% INPUTS: adjacency matrix and index of the key node
% OUTPUTS: all node indices of the nodes in the same group
% to which "i" belongs to (including "i")
%
% Note: Only works for undirected graphs.
% Other functions used: kneighbors.m
% Last updated: Sep 22 2012
function comp = findConnCompI(adj, i)
neigh1 = kneighbors(adj, i, 1); % all neighbors of "i" 1 link away
neigh1 = unique([neigh1 i]); % add i to its own component
while 1
len0 = length(neigh1);
for j = 1:len0
neigh2 = kneighbors(adj, neigh1(j), 1);
neigh1 = unique([neigh1, neigh2]); % merge neigh1 and neigh2
end
if len0 == length(neigh1) % if no new neighbors found, return component
comp = neigh1;
return
end
end
end
%!test
%!shared T
%! T = load_test_graphs();
%!assert(findConnCompI(T{5}{2},1),[1,2,3])
%!assert(findConnCompI(T{5}{2},2),[1,2,3])
%!assert(findConnCompI(T{5}{2},3),[1,2,3])
%!assert(findConnCompI(T{5}{2},4),[4,5,6])
%!assert(findConnCompI(T{5}{2},5),[4,5,6])
%!assert(findConnCompI(T{5}{2},6),[4,5,6])
%!assert(findConnCompI([0 1 0; 1 0 0; 0 0 0],1),[1,2])
%!assert(findConnCompI([0 1 0; 1 0 0; 0 0 0],2),[1,2])
%!assert(findConnCompI([0 1 0; 1 0 0; 0 0 0],3),[3])
%!demo
%! % two disconnected three−node cycles
%! adj=[0 1 1 0 0 0; 1 0 1 0 0 0; 1 1 0 0 0 0; 0 0 0 0 1 1; 0 0 0 1 0 1; 0 0 0 1 1 0];
%! findConnCompI(adj, 1)
%! findConnCompI(adj, 5)