Plotting a complete multipartite graph
3 views (last 30 days)
Show older comments
How can we plot a complete multipartite graph K 2,2,3,4 and label its vertices in Matlab?
0 Comments
Answers (1)
Lennart Sinjorgo
on 9 Nov 2020
Edited: Lennart Sinjorgo
on 9 Nov 2020
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!