Plotting a complete multipartite graph

8 次查看(过去 30 天)
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 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by