将给出的答案解释为两个数组
Interpreting an answer given as two arrays
这是什么意思
The edges of the minimum spanning
tree are returned in array mst (of size n-1 by 2)
?
当我运行程序时,在某些时候显示
this two arrays
但是我不知道如何将其解释为最小生成树的边。
如何取边?有没有办法绘制这个答案?
有人可以帮忙吗?
这是代码。
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]
.
表示
这是什么意思
The edges of the minimum spanning tree are returned in array mst (of size n-1 by 2)
?
当我运行程序时,在某些时候显示
this two arrays
但是我不知道如何将其解释为最小生成树的边。
如何取边?有没有办法绘制这个答案?
有人可以帮忙吗?
这是代码。
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]
.