gpt4 book ai didi

java - 所有对最短路径 Udaya Kumar Redd 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:10:15 25 4
gpt4 key购买 nike

使用以下论文第 4 页上的算法,我正在尝试计算所有对的最短路径矩阵。

http://www.waset.org/journals/ijcms/v3/v3-5-43.pdf

我对(算法的)第 7 行和第 8 行以及随后的第 11 和 12 行感到困惑,因为给 s1 赋值 j 并同时使用这两个第 8 行的比较对我来说似乎模棱两可。我是想知道我是否读错了缩进。我是算法新手,所以请耐心等待。

    while(flag != false){
for(int i=0; i<n;i++){
aMin = Integer.MAX_VALUE;
bMin = Integer.MAX_VALUE;

for(int j=0; j<n;j++){
// Line 7 of the algorithm
if((distanceMatrix[i][j] < aMin) && (j != i) && (BR[i][j] == false)){
aMin = distanceMatrix[i][j];
BR[i][j] = true;
a[i] = j;
int s1 = j; // End of line 7
// Line 8 of the algorithm
if(distanceMatrix[i][j] < distanceMatrix[i][s1])
BR[i][s1] = false; // End of line 8
}

}

for(int j=0; j<n; j++){
// Line 11 of the algorithm
if((distanceMatrix[i][j] < bMin) && (j != i) && (j != a[i]) && (BR[i][j] == false)){
bMin = distanceMatrix[i][j];
BR[i][j] = true;
b[i] = j;
int s2 = j; // end of line 11

// Line 12 of the algorithm
if(distanceMatrix[i][j] < distanceMatrix[i][s2])
BR[i][s2] = false; // end of line 12
}
}
}

for(int i=0; i <n; i++){
for(int j=0; j<n; j++){
int t1 = distanceMatrix[i][a[i]] + distanceMatrix[a[i]][j];
int t2 = distanceMatrix[i][b[i]] + distanceMatrix[b[i]][j];
distanceMatrix[i][j] = Math.min(distanceMatrix[i][j], Math.min(t1,t2));
distanceMatrix[j][i] = distanceMatrix[i][j];
}
}

flag = true;
for(int i=0; i<n;i++){
// Line 19 of the algorithm
for(int j=0; j<n; j++){
temp[i][j] = distanceMatrix[i][j]; // end of line 19
// Line 20 of the algorithm
if (distanceMatrix[i][j] == temp[i][j])
flag = false; // end of line 20
}
}
}

此外,通过查看算法中的第 19 行和第 20 行,我了解到它会陷入循环,因为 flag 始终为真。

最佳答案

我觉得应该是这样的:

4) for i ← 1 to n do amin← ∞ ; bmin ← ∞
5) for j ← 1 to n do
6) if D[i, j] < amin and j = i and BR[i, j] = 0 then
7) amin ← D[i, j] ; BR[i, j] ← 1 ; a[i] ← j ; s1 ← j
8) if D[i, j] < D[i, s1] then BR[i, s1] ← 0

如您所见,s1可能并不总是 j : 它永远是 <= j然而。

所以第二个if不在里面头。否则,它没有意义,因为 s1 == j总是,你不能拥有D[i, j] < D[i, j] .

关于java - 所有对最短路径 Udaya Kumar Redd 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com