matlab最小生成树举例,Matlab最小生成树示例

利用graphminspantree可以求最小生成树,具体参考MATLAB帮助

% Create an undirected graph with 6 nodes

S=[1 1 2 2 3 4 4 5 5 6 6];%起始节点向量

E=[2 6 3 5 4 1 6 3 4 2 5]; %终止节点向量

W = [.41 .29 .51 .32 .50 .45 .38 .32 .36 .29 .21];%边权值向量

DG = sparse(S,E,W);

view(biograph(DG,[],ShowArrows,off,ShowWeights,on)) %DG

% Find the minimum spanning tree of DG

[ST,pred] = graphminspantree(DG) %DG

view(biograph(ST,[],ShowArrows,off,ShowWeights,on))

V=sum(sum(ST)); % 最小生成树的权

disp([最小生成树的权为:,num2str(V)]);

%运行结果为

ST =

(4,1) 0.4500

(6,2) 0.2900

(5,3) 0.3200

(5,4) 0.3600

(6,5) 0.2100

pred =

0 6 5 1 4 5

最小生成树的权为:1.63

利用graphminspantree可以求最小生成树,具体参考MATLAB帮助 % Create an undirected graph with 6 nodes S=[1 1 2 2 3 4 4 5 5 6 6];%起始节点向量 E=[2 6 3 5 4 1 6 3 4 2 5]; %终止节点向量 W = [.41 .29 .51 .32 .50 .45 .38 .32 .36 .29 .21];%边权值向量 DG = sparse(S,E,W); view(biograph(DG,[],ShowArrows,off,ShowWeights,on)) %DG % Find the minimum spanning tree of DG [ST,pred] = graphminspantree(DG) %DG view(biograph(ST,[],ShowArrows,off,ShowWeights,on)) V=sum(sum(ST)); % 最小生成树的权 disp([最小生成树的权为:,num2str(V)]); %运行结果为 ST = (4,1) 0.4500 (6,2) 0.2900 (5,3) 0.3200 (5,4) 0.3600 (6,5) 0.2100 pred = 0 6 5 1 4 5 最小生成树的权为:1.63
经验分享 程序员 微信小程序 职场和发展