已知图的邻接矩阵a,试给出该图的最小生成树.matlab编程邻接矩阵a:a(1,2)=50; a(1,3)=60; a(2,4)=65; a(2,5)=40;a(3,4)=52;a(3,7)=45; a(4,5)=50; a(4,6)=30;a(4,7)=42; a(5,6)=70;
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 08:04:22
已知图的邻接矩阵a,试给出该图的最小生成树.matlab编程邻接矩阵a:a(1,2)=50; a(1,3)=60; a(2,4)=65; a(2,5)=40;a(3,4)=52;a(3,7)=45; a(4,5)=50; a(4,6)=30;a(4,7)=42; a(5,6)=70;
已知图的邻接矩阵a,试给出该图的最小生成树.matlab编程
邻接矩阵a:a(1,2)=50; a(1,3)=60; a(2,4)=65; a(2,5)=40;
a(3,4)=52;a(3,7)=45; a(4,5)=50; a(4,6)=30;
a(4,7)=42; a(5,6)=70;
已知图的邻接矩阵a,试给出该图的最小生成树.matlab编程邻接矩阵a:a(1,2)=50; a(1,3)=60; a(2,4)=65; a(2,5)=40;a(3,4)=52;a(3,7)=45; a(4,5)=50; a(4,6)=30;a(4,7)=42; a(5,6)=70;
给你个最小生成树的调用函数吧.将你的顶点数复制给n,邻接矩阵为W.然后调用mintree(n,W)
function [Wt,Pp]=mintree(n,W)
%求最小生成树,n为顶点个数,W是权值邻接矩阵,不相邻的用inf表示
%Wt是最小生成树的权,Pp(:,1:2)表示最小生成树的两顶点
%Pp(:,4)表示最小生成树的序号
tmpa=find(W~=inf);
[tmpb,tmpc]=find(W~=inf);
w=W(tmpa);
e=[tmpb,tmpc];
[wa,wb]=sort(w);
E=[e(wb,:),wa,wb];
[nE,mE]=size(E);
temp=find(E(:,1)-E(:,2));
E=E(temp,:);
P=E(1,:);
k=length(E(:,1));
while rank(E)>0
temp1=max(E(1,2),E(1,1));
temp2=min(E(1,2),E(1,1));
for i=1:k
if E(i,1)==temp1
E(i,1)=temp2;
end
if E(i,2)==temp1
E(i,2)=temp2;
end
end
a=find(E(:,1)-E(:,2));
E=E(a,:);
if rank(E)>0
P=[P;E(1,:)];
k=length(E(:,1));
end
end
Wt=sum(P(:,3))
Pp=[e(P(:,4),:),P(:,3:4)];
for i=1:length(P(:,3))
disp(['','e',num2str(P(i,4)),'',...
'(v',num2str(P(i,1)),'','v',num2str(P(i,2)),')']);
end
axis equal;%画最小生成树
hold on
[x,y]=cylinder(1,n);
xm=min(x(1,:));
ym=min(y(1,:));
xx=max(x(1,:));
yy=max(y(1,:));
axis([xm-abs(xm)*0.15,xx+abs(xx)*0.15,ym-abs(ym)*0.15,yy+abs(yy)*0.15]);
plot(x(1,:),y(1,:),'ko');
for i=1:n
temp=['v',int2str(i)];
text(x(1,i),y(1,i),temp);
end
for i=1:nE
plot(x(1,e(i,:)),y(1,e(i,:)),'b');
end
for i=1:length(P(:,4))
plot(x(1,Pp(i,1:2)),y(1,Pp(i,1:2)),'r');
end
text(-0.35,-1.2,['最小生成树的权为','',num2str(Wt)]);
title('红色连线为最小生成树');
axis off;
hold off;