I have an adjacency matrix adj and a cellarray nodeManes that contains names that will be given to the graph G that will be constructed from adj
So I use
G = digraph(adj,nodeNames);
And I get the following graph :
Now , I want to find the strongly connected components in G and do a graph condensation so I use the following :
C = condensation(G);
p2 = plot(C);
So I get the following graph :
So I have 6 strongly connected components , but my problem is that I lost the node names , I want to get something like :
Is that any way to get the nodes names in the result of the condentation ?
Thanks.

 채택된 답변

Christine Tobler
Christine Tobler 2017년 12월 4일

4 개 추천

Here is some code that does this:
bins = conncomp(G);
compNames = cell(max(bins), 1);
for ii=1:length(bins)
if isempty(compNames{bins(ii)})
compNames{bins(ii)} = G.Nodes.Name{ii};
else
compNames{bins(ii)} = [compNames{bins(ii)} ', ' G.Nodes.Name{ii}];
end
end
C.Nodes.Name = compNames;
plot(C);
There's no one-line command to combine several node names based on the vector of bins used in condensation, so I wrote a for-loop to do this instead.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 4일

3 개 추천

Stealing basic technique from Christine, and reworking it without a loop:
bins = conncomp(G);
temp = accumarray(bins.', (1:length(bins)).', [], @(IDX) {G.Nodes.Name(IDX)});
C.Nodes.Name = cellfun(@(CS) strjoin(CS, ', '), temp, 'uniform', 0);

댓글 수: 10

Very nice!
stam dadi
stam dadi 2017년 12월 4일
Thank's
I wouldn't have known the calls to make without Christine's post.
stam dadi
stam dadi 2017년 12월 5일
Solved..
stam dadi
stam dadi 2017년 12월 5일
편집: stam dadi 2017년 12월 5일
Walter Robinson ....How can I plot the resulted graph C of type struct ? I used the plot fonction but got :
Not enough input arguments
Steven Lord
Steven Lord 2017년 12월 5일
It shouldn't be a struct. In your original code it was the output of a call to condensation, which would return a digraph if called with a digraph. Walter and Christine's codes modified that digraph, specifically the names of the digraph's nodes.
stam dadi
stam dadi 2017년 12월 5일
편집: stam dadi 2017년 12월 5일
I get it as a struct . The problem is it was working and I got the plot , after that I start getting the error !
You would get a struct as an output if you had assigned the output
C = condensation(G);
to some other variable name than C
yes.. I just checked , I accidently deleted the line :
C = condensation(G);
Thank's
Waseem AL Aqqad
Waseem AL Aqqad 2021년 11월 27일
Wow! Thanks.

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

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

질문:

2017년 12월 4일

댓글:

2021년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by