画最小生成树的两种方法:
一种是避圈法
function A = fun(W) [m, n] = size(W); e = 0;for i = 1 : n for j = i : n if W(i, j) ~= 0 e = e + 1; E(e, :) = [i, j, W(i, j)]; end endend % sort W's edge by weightfor i = 1 : e - 1 for j = i + 1 : e if E(i, 3) > E(j, 3) temp = E(j, :); E(j, :) = E(i, :); E(i, :) = temp; end endend A = zeros(1, 3);S = 1 : n; for i = 1 : e % if find-set(u) ~= find-set(v) if S(E(i, 1)) ~= S(E(i, 2)) % A = A + (u, v) A = cat(1, A, E(i,:)); %union(u, v) indicator = S(E(i, 1)); for j = 1 : n if S(j) == indicator S(j) = S(E(i, 2)); end end endend A(1, :) = [];
例子:W=xlsread('C:\Users\paul\Desktop\**.xls');fun(W)
破圈法
function T=tree()
A=[inf 50 14 40 13;50 inf 15 20 inf;14 15 inf 10 20;40 20 10 inf 10;13 inf 20 10 inf];n=5;k=1;for(i=1:n-1) for(j=i+1:n) ……