gpt4 book ai didi

java - 我需要帮助按照我的老师想要的方式制作 prims 算法

转载 作者:行者123 更新时间:2023-12-01 19:34:36 25 4
gpt4 key购买 nike

所以我检查了之前发布的 prims 算法帖子。我找不到一个能满足老师要求的。我和他一起编写了这段代码,并且大部分工作正常。然而,由于某种原因,当它到达某个点时,它会破裂并走向错误的边缘。

'''public int prims(T startVertex) {
int tempWeight = 0;
int championWeight = 0;
int totalWeight = 0;
int i = 0;
boolean firstOne = false;
T championVertex = null;
T currentVertex = null;
T checkVertex = null;
T championMarked = null;
UnboundedQueueInterface<T> vertexQueue = new LinkedUnbndQueue<T>();

clearMarks();
markVertex(startVertex);
currentVertex = startVertex;

do {
for (int y = 0; y < numVertices; y++) {
currentVertex = vertices[y];
if (isMarked(currentVertex)) {
championWeight = 0;
championVertex = null;
checkVertex = null;
firstOne = true;
vertexQueue = getToVertices(currentVertex);

while (!vertexQueue.isEmpty()) {
checkVertex = vertexQueue.dequeue();
if ((!(isMarked(checkVertex)))) {
tempWeight = weightIs(currentVertex, checkVertex);
if (championWeight > tempWeight || firstOne == true) {
championWeight = tempWeight;
championVertex = checkVertex;
championMarked = currentVertex;
firstOne = false;
}
}
}
}

}

System.out.println((String) championMarked + (String) championVertex + championWeight);
markVertex(championVertex);

totalWeight += championWeight;
} while (!(getUnmarked() == null));
System.out.println("Total cost is " + totalWeight);
return totalWeight; '''

当我运行它时,我得到以下输出图1AD1DF4FC3FE12FZ17清空0

直到 FE12 行为止,图表的输出都是正确的。应该是CE4。当我运行调试时,我看到代码找到了答案,但随后跳转到 for 循环并失去了正确的答案。我知道我的逻辑有问题,但我无法弄清楚。感谢您的意见。谢谢

最佳答案

所以我已经解决了我的问题,我需要在代码输出解决方案后对代码进行重置,否则它们在哪里,如果还有任何顶点需要检查尚未使用,则代码将放宽当前值。

他们需要去这里

'''System.out.println((String) championMarked + (String) championVertex + 
championWeight);
markVertex(championVertex);
totalWeight += championWeight;
championWeight = 0;
championVertex = null;
checkVertex = null;
firstOne = true;

} while (!(getUnmarked() == null));'''

关于java - 我需要帮助按照我的老师想要的方式制作 prims 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231751/

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