function [wSum StartPos EndPos] = MST_JunH(W_Matrix, headPos)
% W_Matrix' = W_Matrix : 表示权值矩阵
% headPos : 表示树头在W_Matrix的位置
% wSum : 表示得到的最小生成树的权值总和
% CopyRight : JunH 20130411
[row col] = size(W_Matrix);
if row ~= col
error(' W_Matrix 不是权值矩阵, 因为不是对称矩阵');
end
wSum = 0;
StartPos = [];
EndPos = [];
TEset =[];
TEset = [TEset headPos];
TVMinQuene = [];
TEEndMinQuene = [];
TEStartMinQuene = [];
for R=1:row
if 0 == isValueInSet(TEset, R)
[TVMinQuene TEStartMinQuene TEEndMinQuene] = insertMinQuene(TVMinQuene, TEStartMinQuene, TEEndMinQuene, W_Matrix(headPos, R), headPos, R);
end
end
for R=2:row
wSum = wSum + TVMinQuene(1, 1);
StartPos = [StartPos TEStartMinQuene(1, 1)];
EndPos = [EndPos TEEndMinQuene(1, 1)];
TEset = [TEset TEEndMinQuene(1, 1)];
[TVMinQuene TEStartMinQuene TEEndMinQuene] = clearMinQuene(TVMinQuene, TEStartMinQuene, TEEndMinQuene, TEset(1, size(TEset, 2)));
for RR=1:row
if 0 == isValueInSet(TEset, RR)
[TVMinQuene TEStartMinQuene TEEndMinQuene] = insertMinQuene(TVMinQuene, TEStartMinQuene, TEEndMinQuene, W_Matrix(TEset(1, size(TEset, 2)), RR), TEset(1, size(TEset, 2)), RR);
end
end
end
end
function [boolean] = isValueInSet(set, value)
boolean = 0;
len = size(set, 2);
for R=1:len
if value == set(1, R)
boolean = 1;
break;
end
end
end
function [TTVMinQuene TTEStartMinQuene TTEEndMinQuene] = clearMinQuene(TVMinQuene, TEStartMinQuene, TEEndMinQuene, Pos)
len = size(TEEndMinQuene, 2);
TTVMinQuene = [];
TTEStartMinQuene = [];
TTEEndMinQuene = [];
for R=1:len
if TEEndMinQuene(1, R) ~= Pos
TTVMinQuene = [TTVMinQuene TVMinQuene(1, R)];
TTEStartMinQuene = [TTEStartMinQuene TEStartMinQuene(1, R)];
TTEEndMinQuene = [TTEEndMinQuene TEEndMinQuene(1, R)];
end
end
end
function [TVMinQuene TEStartMinQuene TEEndMinQuene] = insertMinQuene(TVMinQuene, TEStartMinQuene, TEEndMinQuene, value, startPos, endPos)
% 保持从小到大
len = size(TVMinQuene, 2);
for R=1:len
if value < TVMinQuene(1, R)
prior = TVMinQuene(1, 1:R-1);
after = TVMinQuene(1, R:len);
TVMinQuene = [prior value after];
priorES = TEStartMinQuene(1, 1:R-1);
afterES = TEStartMinQuene(1, R:len);
TEStartMinQuene = [priorES startPos afterES];
priorEE = TEEndMinQuene(1, 1:R-1);
afterEE = TEEndMinQuene(1, R:len);
TEEndMinQuene = [priorEE endPos afterEE];
return;
end
end
TVMinQuene = [TVMinQuene value];
TEStartMinQuene = [TEStartMinQuene startPos];
TEEndMinQuene = [TEEndMinQuene endPos];
end
--------------------------------------------------------------------------------------------------------------------------------------------
%最小生成树kruskal源程序
%c:原图的邻接矩阵
%v0:根节点
%c1:最小生成树的邻接矩阵
function c1=Krusk(c,v0)
[X,Y]=size(c);
if X~=Y
error('输入必须为方阵')
end
if v0>length(c(1,:))
error('不存在该顶点')
end
N=length(c(:,1));
con=0;
c(find(c==0))=inf;
c1=zeros(N,N);
comp=zeros(N,N);
comp(:,1)=[1:N]';
while con<N-1
clear min0;
min0=min(min(c));
[x,y]=find(c==min0);
X=x(1);
Y=y(1);
c(X,Y)=inf;
[i1,j1]=find(comp==X);
[i2,j2]=find(comp==Y);
if i1==i2
continue
else
l1=length(find(comp(i1,:)~=0));
l2=length(find(comp(i2,:)~=0));
comp(i1,[l1+1:l1+l2])=comp(i2,[1:l2]);
comp(i2,:)=0;
c1(X,Y)=min0;
con=con+1;
end
end
c1=c1';
----------------------------------------------------------------------------------------------------------------------------------------
function [T,WT]=prim(A)
%Prim法求最小生成树
%A为无向图G={V,E}的权矩阵;
%T为A的最小生成树的权矩阵,WT为其权值。
n=length(A);
A(find(A==0))=inf;
T=zeros(n);WT=0;
V=2:n;%未通过点的集合
V1=1;%通过点的初始集合
p=1;%记录通过点的个数.
i=1;[y,k]=min(A(i,:));%{从以1为一个端点,以V中的点为另一个端点的所有边中,找出权值最小的边%}
p=p+1;V1(p)=k;%将找到的点加到V1中
V(k-1)=[];T(i,k)=y;T(k,i)=y;%将找到的点从V中删去,并把相应边加到T中。
WT=WT+y;A(i,k)=inf;A(k,i)=inf;
while p<n
s=1;
for i=V1 %找出临跨V1和V的权值最小的边
y(s)=min(A(i,V));s=s+1;
end
y=min(y);%y为找出的最小权值
for i=V1 %找出与y相关联的两个点
for j=V
if(A(i,j)==y)T(i,j)=y;T(j,i)=y;A(i,j)=inf;A(j,i)=inf;WT=WT+y;
p=p+1;V1(p)=j;%将点加到V1中
V(find(V==j))=[];%将通过点从V中删掉
end
break;end;end;end
end
可以在MATLAB中输入以下命令进行检验:
A=[inf 4 15 inf 7 inf 28;4 inf 9 inf infinf inf;15 9 inf 25 5 inf inf;inf inf 25 inf 32 16 12;7 inf 5 32 inf inf 30;infinf inf 16 inf inf 20;28 inf inf 12 30 20 inf];
[T,WT]=prim(A)