这是什么意思
最小跨度的边缘 树以数组mst返回(大小为n-1 x 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)
答案 0 :(得分:2)
一条边可以由其连接的两个顶点唯一标识。
mst
的每一行都包含指向跨越边缘的两个顶点的两个索引。
输入图由一组顶点和连接它们的边组成,表示为邻接矩阵A
。如果A(i,j)
为真,则顶点i和j相邻(即共享一条边)。在输出矩阵mst
中,此边缘将由mst(index,:) = [i,j]
表示。