Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要帮助按照老师的要求制作prims算法_Java_Prims Algorithm - Fatal编程技术网

Java 我需要帮助按照老师的要求制作prims算法

Java 我需要帮助按照老师的要求制作prims算法,java,prims-algorithm,Java,Prims Algorithm,所以我检查了之前发布的prims算法帖子。我找不到一个能满足老师要求的。我和他一起编写了这段代码,并且基本上都能正常工作。然而,由于某种原因,当它到达某个特定点时,它会断裂并到达错误的边缘 '''public int prims(T startVertex) { int tempWeight = 0; int championWeight = 0; int totalWeight = 0; int i = 0; bo

所以我检查了之前发布的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; '''
''public int prims(T startVertex){
int tempWeight=0;
int championWeight=0;
整数总权重=0;
int i=0;
布尔值firstOne=false;
T championVertex=null;
T currentVertex=null;
T checkVertex=null;
T championMarked=null;
UnbounddQueueInterface vertexQueue=新建LinkedUnbndQueue();
清晰标记();
标记顶点(startVertex);
currentVertex=startVertex;
做{
对于(int y=0;ytempWeight | | firstOne==true){
championWeight=tempWeight;
championVertex=检查顶点;
championMarked=当前顶点;
第一个=假;
}
}
}
}
}
System.out.println((字符串)championMarked+(字符串)championVertex+championWeight);
标记顶点(championVertex);
总重量+=总重量;
}而(!(getUnmarked()==null));
系统输出打印项次(“总成本为”+总重量);
返回总重量;'''
当我运行它时,我得到以下输出 图1 AD1 DF4 FC3 二月十二日 FZ17 最后


在FE12行之前,图形的输出是正确的。它应该是CE4。当我运行调试时,我看着代码找到答案,但随后跳到for循环并失去了正确的答案。我知道我的逻辑中有一个错误,但我不能完全理解。非常感谢你的输入。谢谢你,我已经解决了我的问题,我需要t在代码输出解决方案后重置代码,否则,如果有任何顶点未使用,代码将丢失当前值

他们需要到这里来

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

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