我正在尝试从 CLRS 实现贝尔曼福特算法,它似乎随机溢出距离度量。我的代码如下:
private void initSingleSource(Vertice source) {
for(Vertice vertice:vertices) {
vertice.distance = Integer.MAX_VALUE;
vertice.predecessor = null;
}
source.distance = 0;
}
private void relax(Vertice u, Vertice v, int weight) {
if(v.distance > u.distance + weight) {
v.distance = u.distance + weight;
v.predecessor = u;
}
}
public boolean bellmanFord(Vertice source) {
initSingleSource(source);
for(int i = 0; i < vertices.size()-1; i++) {
for(Vertice u: edges.keySet()) {
Hashtable<Vertice, Integer> table = edges.get(u);
for(Vertice v: table.keySet()) {
relax(u, v, table.get(v));
}
}
}
for(Vertice u: edges.keySet()) {
Hashtable<Vertice, Integer> table = edges.get(u);
for(Vertice v: table.keySet()) {
if(v.distance > u.distance + table.get(v)) {
return false;
}
}
}
return true;
}
代码的输入是:
g.addAdjList(A, B, 6);
g.addAdjList(A, D, 7);
g.addAdjList(B, C, 5);
g.addAdjList(B, D, 8);
g.addAdjList(B, E, -4);
g.addAdjList(C, B, -2);
g.addAdjList(D, C, -3);
g.addAdjList(D, E, 9);
g.addAdjList(E, C, 7);
g.addAdjList(E, A, 2);
我随机得到的正确答案是:
B 2 C 4 D 7 E -2
否则我得到:
B -2147483636 C -2147483634 D -2147483631 E -2147483640
知道为什么会这样吗?