Plotting a complete multipartite graph

조회 수: 6 (최근 30일)
Asha Sunilkumar
Asha Sunilkumar 2020년 8월 26일
편집: Lennart Sinjorgo 2020년 11월 9일
How can we plot a complete multipartite graph K 2,2,3,4 and label its vertices in Matlab?

답변 (1개)

Lennart Sinjorgo
Lennart Sinjorgo 2020년 11월 9일
편집: Lennart Sinjorgo 2020년 11월 9일
The adjacency matrix of the complete multipartite graph is mostly a matrix of all ones. Except on the diagonal there are blocks of zero matrices. The zero matrices blocks have sizes of the partitions. I added a function below which does the job. Not really optimzed but no problem for such a question.
function [Adj] = CompleteMultipartite(Partitions)
% Enter the Partitions as row or column vector
% Adjacency of K_{2,3,4} would be CompleteMultipartite([2 3 4])
Rows = size(Partitions,1);
if Rows == 1
Partitions = Partitions' ;
end
SumPartitions = sum(Partitions,1);
Adj = ones(SumPartitions, SumPartitions);
n = length(Partitions);
Adj(1:Partitions(1),1:Partitions(1)) = zeros(Partitions(1),Partitions(1));
for i = 2:n
IntervalEnd = sum(Partitions(1:i),1);
IntervalStart = sum(Partitions(1:(i-1),1))+1;
IntervalSize = IntervalEnd - IntervalStart +1;
Adj(IntervalStart:IntervalEnd,IntervalStart:IntervalEnd) = zeros(IntervalSize,IntervalSize);
end
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by