这意味着什么
最小生成的边树在数组mst(大小为n-1乘2(中返回
?
当我运行程序时,在某个时刻显示
这两个阵列
然而,我不知道如何将其解释为最小生成树的边。
我该如何处理边缘?有没有办法画出这个答案?
有人能帮忙吗?
这就是代码。
function [mst, cost] = prim(A)
[n,n] = size(A);
A, n, pause,
if norm(A-A','fro') ~= 0 ,
disp(' Error: Adjacency matrix must be symmetric ')
return,
end;
intree = [1]; number_in_tree = 1;
number_of_edges = 0;
notintree = [2:n]'; number_notin_tree= n-1;
in = intree(1:number_in_tree),
out = notintree(1:number_notin_tree),
pause,
while number_in_tree < n,
mincost = Inf;
for i=1:number_in_tree,
for j=1:number_notin_tree,
ii = intree(i); jj =
notintree(j);
if A(ii,jj) < mincost,
mincost = A(ii,jj); jsave = j;
iisave = ii; jjsave = jj;
end;
end;
end;
number_of_edges = number_of_edges +1;
mst(number_of_edges,1) = iisave;
mst(number_of_edges,2) = jjsave;
costs(number_of_edges,1) = mincost;
number_in_tree = number_in_tree + 1;
intree = [intree; jjsave];
for j=jsave+1:number_notin_tree,
notintree(j-1) = notintree(j);
end;
number_notin_tree = number_notin_tree - 1;
in = intree(1:number_in_tree),
out = notintree(1:number_notin_tree),
pause,
end;
disp(' Edges in minimum spanning tree and their costs: ')
[mst costs]
cost = sum(costs)
边可以通过其连接的两个顶点唯一标识。mst
的每一行包含到跨越边的两个顶点的两个索引。
输入图由一组顶点和连接它们的边组成,表示为邻接矩阵A
。如果A(i,j)
为真,则顶点i和j是相邻的(即共享边(。在输出矩阵mst
中,该边缘将由mst(index,:) = [i,j]
表示。