How to get connected component from adjacency matrix

The adjacency matrix is already known.
what I want to do is showed in the picture below.
I don't care about the order of the vertex.

 채택된 답변

Guillaume
Guillaume 2016년 8월 14일
A lot of graph functions have been added in R2015b. In particular, you have conncomp which gives you exactly what you want:
adjacencyMatrix =[0 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0
1 1 0 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1 0 0
1 1 1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 1
1 1 1 0 1 0 0 0 1 0
0 0 0 1 0 0 0 0 0 1
1 1 1 0 1 0 1 0 0 0
0 0 0 0 0 1 0 1 0 0]
G = graph(adjacencyMatrix);
plot(G); %view the graph
bins = conncomp(G);
binnodes = accumarray(bins', 1:numel(bins), [], @(v) {sort(v')});
fprintf('number of Regions = %d\n\n', numel(binnodes));
for binidx = 1:numel(binnodes)
fprintf('All these nodes are connected:%s\n', sprintf(' %d', binnodes{binidx}));
end
fprintf('\n');

댓글 수: 2

It's great and that's exactly what I want. I have never contact with this function before.Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 8월 13일
편집: Image Analyst 2016년 8월 13일
Use bwlabel() and regionprops:
[labeledMatrix, numberOfRegions] = bwlabel(A);
props = regionprops(labeledMatrix, 'PixelList');
labeledMatrix gives an ID number to each connected region. props has all the information on all the elements in each connected region. You can get indexes (rows and columns), values, areas, etc. depending on what you ask regionprops() for.

댓글 수: 7

Thanks for your answer!
But maybe I didn't introduce my question very clear.
What I have is "adjacency matrix", which means the relation between vertex and edge. And maybe bwlabel() is more suitable for a Binary Image problem rather than my problem.
Here is another example to introduce my question.
There are 6 vertex and 3 identity connected regions.
If I use bwlabel(), the vertex "1" maybe ignored.
Yeah, I got it. I'm not sure you understand so I'll do the full blown demo with both of your cases so that you can see it tells you all the nodes that are connected:
% Example 1
adjacencyMatrix = false(6);
adjacencyMatrix(2, 3:4) = true;
adjacencyMatrix(3:4, 2) = true;
adjacencyMatrix(5, 6) = true;
adjacencyMatrix(6, 5) = true
[labeledMatrix, numberOfRegions] = bwlabel(adjacencyMatrix)
props = regionprops(labeledMatrix, 'PixelList');
for regions = 1 : numberOfRegions
fprintf('All these nodes are connected: ');
fprintf('%d ', unique(props(regions).PixelList));
fprintf('\n');
end
% Example 2
adjacencyMatrix = false(6);
adjacencyMatrix(1, 3) = true;
adjacencyMatrix(2, 3:4) = true;
adjacencyMatrix(3, [1,2,4]) = true;
adjacencyMatrix(4, 2:3) = true;
adjacencyMatrix(5, 6) = true;
adjacencyMatrix(6, 5) = true
[labeledMatrix, numberOfRegions] = bwlabel(adjacencyMatrix)
props = regionprops(labeledMatrix, 'PixelList');
for regions = 1 : numberOfRegions
fprintf('All these nodes are connected: ');
fprintf('%d ', unique(props(regions).PixelList));
fprintf('\n');
end
In the command window you'll see this for example 1:
adjacencyMatrix =
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
labeledMatrix =
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 2
0 0 0 0 2 0
numberOfRegions =
2
All these nodes are connected: 2 3 4
All these nodes are connected: 5 6
and this for example 2:
adjacencyMatrix =
0 0 1 0 0 0
0 0 1 1 0 0
1 1 0 1 0 0
0 1 1 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
labeledMatrix =
0 0 1 0 0 0
0 0 1 1 0 0
1 1 0 1 0 0
0 1 1 0 0 0
0 0 0 0 0 2
0 0 0 0 2 0
numberOfRegions =
2
All these nodes are connected: 1 2 3 4
All these nodes are connected: 5 6
Isn't that what you want to know? Which nodes are all part of the same group? I thought it was. Correct me if I'm wrong.
By the way, bwlabel does not ignore single isolated nodes, but you can remove those in advance if you want with the bwmorph()'s "Clean" option.
Well, thanks for your patience. I know the meaning of your method now, but there seems to be a little different with what I want to know.
For example, the answer I want is:
numberOfRegions =
3
All these nodes are connected: 1
All these nodes are connected: 2 3 4
All these nodes are connected: 5 6
Here,I want to give a more complex example,
% example 01
adjacencyMatrix =[0 1 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 0 1 1 1 1
0 0 0 0 1 1 0 1 1 1
0 0 0 0 1 1 1 0 1 1
0 0 0 0 1 1 1 1 0 1
0 0 0 0 1 1 1 1 1 0]
[labeledMatrix, numberOfRegions] = bwlabel(adjacencyMatrix)
props = regionprops(labeledMatrix, 'PixelList');
for regions = 1 : numberOfRegions
fprintf('All these nodes are connected: ');
fprintf('%d ', unique(props(regions).PixelList));
fprintf('\n');
end
matlab answer is:
adjacencyMatrix =
0 1 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 0 1 1 1 1
0 0 0 0 1 1 0 1 1 1
0 0 0 0 1 1 1 0 1 1
0 0 0 0 1 1 1 1 0 1
0 0 0 0 1 1 1 1 1 0
labeledMatrix =
0 1 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 2 2 2 2 2
0 0 0 0 2 0 2 2 2 2
0 0 0 0 2 2 0 2 2 2
0 0 0 0 2 2 2 0 2 2
0 0 0 0 2 2 2 2 0 2
0 0 0 0 2 2 2 2 2 0
numberOfRegions =
2
All these nodes are connected: 1 2 3 4
All these nodes are connected: 5 6 7 8 9 10
Another example
% Example 2
adjacencyMatrix =[0 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0
1 1 0 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1 0 0
1 1 1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 1
1 1 1 0 1 0 0 0 1 0
0 0 0 1 0 0 0 0 0 1
1 1 1 0 1 0 1 0 0 0
0 0 0 0 0 1 0 1 0 0]
[labeledMatrix, numberOfRegions] = bwlabel(adjacencyMatrix)
props = regionprops(labeledMatrix, 'PixelList');
for regions = 1 : numberOfRegions
fprintf('All these nodes are connected: ');
fprintf('%d ', unique(props(regions).PixelList));
fprintf('\n');
end
matlab answer is:
adjacencyMatrix =
0 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0
1 1 0 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1 0 0
1 1 1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 1
1 1 1 0 1 0 0 0 1 0
0 0 0 1 0 0 0 0 0 1
1 1 1 0 1 0 1 0 0 0
0 0 0 0 0 1 0 1 0 0
labeledMatrix =
0 1 1 0 4 0 5 0 5 0
1 0 1 0 4 0 5 0 5 0
1 1 0 0 4 0 5 0 5 0
0 0 0 0 0 0 0 5 0 0
2 2 2 0 0 0 5 0 5 0
0 0 0 0 0 0 0 0 0 5
3 3 3 0 3 0 0 0 5 0
0 0 0 3 0 0 0 0 0 5
3 3 3 0 3 0 3 0 0 0
0 0 0 0 0 3 0 3 0 0
numberOfRegions =
5
All these nodes are connected: 1 2 3
All these nodes are connected: 1 2 3 5
All these nodes are connected: 1 2 3 4 5 6 7 8 9 10
All these nodes are connected: 1 2 3 5
All these nodes are connected: 1 2 3 4 5 6 7 8 9 10
From the above two examples we can find that the answer will change if we only change the node order.
And the answer I want is :
numberOfRegions = 2
All these nodes are connected: 1 2 3 5 7 9
All these nodes are connected: 4 6 8 10
You can't get that with regionprops. You'll have to look at graph/node/network functions. I'm not familiar with those.
Thank you anyway!
Unaccept my Answer and see if Guillaume's gives you the answer and if it does, accept his answer to give him credit.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

질문:

2016년 8월 13일

댓글:

2020년 12월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by