LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
graph_connected_components.m
1function [labels rts] = graph_connected_components(C)
2% C - connection matrix
3% labels =[1 1 1 2 2 3 3 ...] lenght(labels)=L, label for each vertex
4% labels(i) is order number of connected component, i is vertex number
5% rts - roots, numbers of started vertex in each component
6
7L=size(C,1); % number of vertex
8
9% Breadth-first search:
10labels=zeros(1,L); % all vertex unexplored at the beginning
11rts=[];
12ccc=0; % connected components counter
13while true
14 ind=find(labels==0);
15 if ~isempty(ind)
16 fue=ind(1); % first unexplored vertex
17 rts=[rts fue];
18 list=[fue];
19 ccc=ccc+1;
20 labels(fue)=ccc;
21 while true
22 list_new=[];
23 for lc=1:length(list)
24 p=list(lc); % point
25 cp=find(C(p,:)); % points connected to p
26 cp1=cp(labels(cp)==0); % get only unexplored vertices
27 labels(cp1)=ccc;
28 list_new=[list_new cp1];
29 end
30 list=list_new;
31 if isempty(list)
32 break;
33 end
34 end
35 else
36 break;
37 end
38end