**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。
一年前关门了。
改进这个问题
我试图得到一个无向加权图的最小生成树,它应该返回最小生成树的权重,如果使用名为cits2200的包找不到最小生成树,则返回-1,下面是cits2200.jar的链接:
cits2200.jar文件
我想知道是否有人能理解为什么我在下面代码中的getminspanningtree方法没有通过测试,任何帮助都将不胜感激。干杯,本。c“,)
import CITS2200.*;
public class PathImp implements Path {
public int getMinSpanningTree(Graph g) {
int parent[] = new int[g.getNumberOfVertices()];
int key[] = new int [g.getNumberOfVertices()];
Boolean mstSet[] = new Boolean[g.getNumberOfVertices()];
for (int i = 0; i < g.getNumberOfVertices(); i++)
{
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
int sum = -1;
for (int count = 0; count < g.getNumberOfVertices()-1; count++)
{
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < g.getNumberOfVertices(); v++){
if (g.getWeight(u,v)!=0 && mstSet[v] == false &&
g.getWeight(u,v) < key[v])
{
parent[v] = u;
key[v] = g.getWeight(u,v);
sum += g.getWeight(u,v);
}
}
}
return sum;
}
public int minKey(int key[], Boolean mstSet[])
{
int min = Integer.MAX_VALUE, min_index=-1;
for (int v = 0; v < key.length; v++)
if (mstSet[v] == false && key[v] < min)
{
min = key[v];
min_index = v;
}
return min_index;
}
public int minDistance(int dist[], Boolean sptSet[])
{
int min = Integer.MAX_VALUE, min_index=-1;
for (int v = 0; v < dist.length; v++)
if (sptSet[v] == false && dist[v] <= min)
{
min = dist[v];
min_index = v;
}
return min_index;
}
public int[] getShortestPaths(Graph g, int src)
{
int dist[] = new int[g.getNumberOfVertices()];
int graph[][] = g.getEdgeMatrix();
Boolean sptSet[] = new Boolean[g.getNumberOfVertices()];
for (int i = 0; i < g.getNumberOfVertices(); i++)
{
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count < g.getNumberOfVertices()-1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < g.getNumberOfVertices(); v++)
if (!sptSet[v] && graph[u][v]!=0 &&
dist[u] != Integer.MAX_VALUE &&
dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
return dist;
}
}
getshortestpath(g,v)方法工作正常,但是,下面是针对测试类测试代码时收到的错误消息:
Checking files:
PathImp.java
Files exist and are readable.
Compiling Files:
PathImp
Compilation complete.
Loading Files:
PathImp
Loading complete.
Implements CITS2200.Path
Constructing instance...
Constructed PathImp
Analysing PathImp...
MST:
1
MST incorrect for 500 vertices at 0.03 density.
2
MST incorrect for 1000 vertices at 0.02 density.
3
MST incorrect for 1500 vertices at 0.01 density.
SSSP:
1
Shortest paths correct for 500 vertices at 0.03 density.
2
Shortest paths correct for 1000 vertices at 0.02 density.
3
Shortest paths correct for 1500 vertices at 0.01 density.
!Analysis halted: Your code has produced an incorrect output.
Your submission was not successful on this occasion.
Please try again when you've addressed this problem.
Execution stack trace (if any) follows.
java.lang.Exception: Your code has produced an incorrect output.
at Lab8.main(Lab8.java:86)
2条答案
按热度按时间btqmn9zl1#
我认为问题在于这一行:
每当您考虑向生成树添加边时,您都会将权重添加到总成本中。这将高估生成树的实际成本。
相反,您应该只在确定边正在添加到生成树时添加权重(即,紧随其后)
int u = minKey(key, mstSet);
)e、 g.类似于:
t3psigkw2#
直列:
for (int count = 0; count < g.getNumberOfVertices()-1; count++)
-去掉负1:for (int count = 0; count < g.getNumberOfVertices(); count++)
正如彼得所说的,你的问题在于总结。在mst查找过程中不需要求和。您需要将找到的mst的权重相加:删除
sum += g.getWeight(u,v);
从for循环。在for循环之后添加: