-2

JAVA中的MST算法有问题吗?

我正在尝试在 java 中为 MST 编写代码

在这里,图已经给出,我正在尝试编写 addCheapest 方法来添加节点(不在路径上),当添加到路径中时,在某个位置,最小化路径在图中所有节点和所有位置的成本可以添加它们;将其添加到该位置。

private void addCheapest(List<String> path)

Here's what I wrote so far....


private void addCheapest(List<String> path){


g.getAllEdges();


int minEdge = Integer.MAX_VALUE;

int edgeValue = g.getEdgeValue(edge);

for (Edge e : g.getAllEdges())
  {
      if ( edgeValue < minEdge)
          g.getAllEdges() = minEdge;
  }

  while ( g != null)
  {
      g.removeNode(nodeName);
      for ( int i = 0; i < path.size(); i ++)
      {
          if (!path.contains(nodeName))
              path.add(nodeName);
      }
  }

}*

4

1 回答 1

1

如果不深入研究这个问题和图形算法的理论,这将行不通:

int minEdge = Integer.MIN_VALUE;接着if ( edgeValue < minEdge)

因为minEdge已经尽可能小了。你应该设置minEdgeInteger.MAX_VALUE

于 2012-03-14T07:51:51.127 回答