Plotting a complete multipartite graph

3 Ansichten (letzte 30 Tage)
Asha Sunilkumar
Asha Sunilkumar am 26 Aug. 2020
Bearbeitet: Lennart Sinjorgo am 9 Nov. 2020
How can we plot a complete multipartite graph K 2,2,3,4 and label its vertices in Matlab?

Antworten (1)

Lennart Sinjorgo
Lennart Sinjorgo am 9 Nov. 2020
Bearbeitet: Lennart Sinjorgo am 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

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by