Connected components in a graph

조회 수: 2 (최근 30일)
Asaf McRock
Asaf McRock 2020년 12월 14일
댓글: Asaf McRock 2020년 12월 15일
M=[1 2;2 5;3 4;4 6;6 7;6 8;6 9;6 10];
G=graph(M(:,1),M(:,2))
[bins,binsizes]=conncomp(G);
When I use the built in function conncomp, MATLAB always assumes that first component of graph G is the one which node 1 belongs to.
The output bins in the given example is:
bins=[1 1 2 2 1 2 2 2 2 2] How can I make the first component of my graph is where node 6 (for example) belongs to?
Your help would be appreciated.
Thanks!

채택된 답변

Matt J
Matt J 2020년 12월 14일
편집: Matt J 2020년 12월 14일
M=[1 2;2 5;3 4;4 6;6 7;6 8;6 9;6 10];
G=graph(M(:,1),M(:,2));
[bins,binsizes]=conncomp(G);
idx=[2,1];
binsizes=binsizes(idx);
bins=idx(bins)
bins = 1×10
2 2 1 1 2 1 1 1 1 1
  댓글 수: 3
Matt J
Matt J 2020년 12월 15일
You would just need to change idx to an appropriate permutation of 1:N, e.g.,
idx=[3,1,2,4]
Asaf McRock
Asaf McRock 2020년 12월 15일
Awesome, got it.
Thank you again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Undirected Graphs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by