Java 遍历Prim'的邻接矩阵;s-MST算法
我在用Java遍历加权邻接矩阵时遇到了一个问题。我要做的是使用Prim算法从矩阵中得到最小生成树的权重 到目前为止,我掌握的代码如下:Java 遍历Prim'的邻接矩阵;s-MST算法,java,algorithm,minimum-spanning-tree,adjacency-matrix,Java,Algorithm,Minimum Spanning Tree,Adjacency Matrix,我在用Java遍历加权邻接矩阵时遇到了一个问题。我要做的是使用Prim算法从矩阵中得到最小生成树的权重 到目前为止,我掌握的代码如下: public int findPrim(int[][] matrix) { ArrayList < Integer > checkThese = new ArrayList < > (); checkThese.add(0); //Starting vertex. boolean[] checked = new boolea
public int findPrim(int[][] matrix) {
ArrayList < Integer > checkThese = new ArrayList < > ();
checkThese.add(0); //Starting vertex.
boolean[] checked = new boolean[graph.vertexCount()];
int w = 0;
int column = 0;
int row = 0;
int smallest = 0;
for (Iterator < Integer > it = checkThese.Iterator(); it.hasNext();) {
smallest = Integer.MAX_VALUE;
for (int k = 0; k < graph.vertexCount(); k++) {
if ((matrix[r][k] < smallest) && matrix[r][k] != 0 && !checked[k - 1]) {
smallest = matrix[r][k];
column = k;
}
}
if (smallest != Integer.MAX_VALUE) {
w += smallest;
checkThese.add(column);
checked[column] = true;
}
}
return w;
}
计划从行
A
开始,并选择最小的边(2)。在此之后,我将排除列C
,然后检查行A
和C
等等,直到排除所有列,从而检查所有边。您需要另一个嵌套循环,以使其按照您指定的方式工作。这是更正后的伪代码
let n be the number of vertices
initialize cost <- 0
initialize checkThese <- [0]
initialize checked <- [true, false, ..., false] (length n)
repeat n - 1 times (alternatively, test for termination as indicated)
smallest <- infinity
argSmallest <- null
for v in checkThese
for w from 0 to n - 1
let cost = matrix[min(v, w)][max(v, w)]
if not checked[w] and cost < smallest then
smallest <- cost
argSmallest <- w
end if
end for
end for
(break here if argSmallest is null)
cost <- cost + smallest
add argSmallest to checkThese
checked[argSmallest] <- true
end repeat
如果所有边缘成本均为正值,则可以用
minCost[w]>0
替换测试checked
,并删除checked
数组。您还可以将这两个环路融合。谢谢您的回复。你能澄清一下这一行吗?let cost=matrix[min(v,w)][max(v,w)]
?我不确定我是否正确理解了那里的语法。为了补充我之前的评论,如果这意味着我要从v
和w
中为行选择最小值,为列选择最大值,那么我想我会遇到与如何获取v
属性值相同的问题,如果我用迭代器在checkthis中为v实现。@user1290164我的意思是,因为你已经将矩阵的条目存储在主对角线的上方,但不是对称的下方,如果它们的顺序错误,你需要交换索引。
let n be the number of vertices
initialize cost <- 0
initialize checkThese <- [0]
initialize checked <- [true, false, ..., false] (length n)
repeat n - 1 times (alternatively, test for termination as indicated)
smallest <- infinity
argSmallest <- null
for v in checkThese
for w from 0 to n - 1
let cost = matrix[min(v, w)][max(v, w)]
if not checked[w] and cost < smallest then
smallest <- cost
argSmallest <- w
end if
end for
end for
(break here if argSmallest is null)
cost <- cost + smallest
add argSmallest to checkThese
checked[argSmallest] <- true
end repeat
let n be the number of vertices
initialize cost <- 0
initialize checked <- [true, false, ..., false] (length n)
initialize minCost <- [0, infinity, ..., infinity] (length n)
repeat n - 1 times (alternatively, test for termination as indicated)
smallest <- infinity
argSmallest <- null
for w from 0 to n - 1
if not checked[w] and minCost[w] < smallest then
smallest <- minCost[w]
argSmallest <- w
end if
end for
(break here if argSmallest is null)
cost <- cost + smallest
checked[argSmallest] <- true
minCost[argSmallest] <- 0
for v from 0 to n - 1
let cost = matrix[min(argSmallest, v)][max(argSmallest, v)]
if not checked[v] and cost < minCost[v] then
minCost[v] <- cost
end if
end for
end repeat