破圈法
% For undirected graph only, but can be changed easily to
directed graph.
% input: W - weight matrix. Since the graph is undirected, the
matrix is symmetric. So we just use the
% upper triangular sub matrix of W. In directed graph, use them
all.
% output: A - minimum span tree edge matrix, each row is a
vector including vertices of an edge and its
% weight in the minimum span tree.
fun.m
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
end
end
% sort W's edge by weight
for i = 1 : e - 1
for j = i
+ 1 : e
if E(i, 3) > E(j, 3)
temp = E(j, :);
E(j, :) =