【问题标题】:Java out of Memory heap error when calculating shortest paths to all nodes through Dijkstra's algorithm from every node as source通过 Dijkstra 算法从每个节点作为源计算到所有节点的最短路径时出现 Java 内存堆错误
【发布时间】:2012-05-14 15:52:58
【问题描述】:

我有一个由 7 个节点和 8 个链接组成的网络。我从互联网上的示例中学习了以下课程。我想计算从每个节点到所有其他节点的最短路径。为此,我在 Solve (main) 中编写了所需的 for 循环。但是,我得到了显示的输出。从第一个节点 Harrisburg 出发的最短路径很好。从第二个节点开始,出现 java 内存不足。我需要做什么?谢谢你的帮助。

Vertex.java

    public class Vertex implements Comparable<Vertex> {

    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public double population, employment;
    public double targetPopulation, targetEmployment;

    public Vertex (String argName, double population, double employment, double targetPopulation, double targetEmployment) {
        this.name = argName;
        this.population = population;
        this.employment = employment;
        this.targetPopulation = targetPopulation;
        this.targetEmployment = targetEmployment;
    }

    public String toString() {
        return name;
    }

    //Vertex comparator
    @Override
    public int compareTo(Vertex other) {
        // TODO Auto-generated method stub
        return Double.compare(minDistance, other.minDistance);
    }

    }

Edge.java

public class Edge {

public final Vertex target;
public final double weight;

public Edge(Vertex argTarget, double argWeight) {
    this.target = argTarget;
    this.weight = argWeight;
}

}

Dijkstra.java

public class Dijkstra {

//simple compute paths function
public void computePaths(Vertex source) {
    source.minDistance = 0.;

    //Visit each vertex u, always visiting vertex with smallest minDistance first
    PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {
        Vertex u = vertexQueue.poll();

        //Visit each edge exiting u
        for (Edge e : u.adjacencies) {
            Vertex v = e.target;
            double weight = e.weight;

            //relax the edge (u,v)
            double distanceThroughU = u.minDistance + weight;
            if(distanceThroughU < v.minDistance) {
                //remove v from queue
                vertexQueue.remove(v);

                v.minDistance = distanceThroughU;
                v.previous = u;

                //re-add v to queue
                vertexQueue.add(v);
            }
        }
    }


}

//get shortest path function
public List<Vertex> getShortestPathTo(Vertex target) {
    List<Vertex> path = new ArrayList<Vertex>();
    for (Vertex vertex = target; vertex != null; vertex = vertex.previous) {
        path.add(vertex);
    }

    Collections.reverse(path);
    return path;
}

}

求解.java

Vertex v0 = new Vertex("Harrisburg", 5, 0.5, 9, 5);
Vertex v1 = new Vertex("Baltimore", 61, 21, 91, 32);
Vertex v2 = new Vertex("Washington", 99, 10, 10, 10);
Vertex v3 = new Vertex("Philadelphia", 159, 30, 100, 45);
Vertex v4 = new Vertex("Binghamton", 10, 10, 10, 10);
Vertex v5 = new Vertex("Allentown", 10, 10, 10, 10);
Vertex v6 = new Vertex("New York", 891, 200, 400, 220);

v0.adjacencies = new Edge[] { new Edge(v1, distances[0]),
                                new Edge(v5, distances[1]) };

    v1.adjacencies = new Edge[] { new Edge(v0, distances[0]),
                                new Edge(v2, distances[2]),
                                new Edge(v3, distances[3])};

    v2.adjacencies = new Edge[] { new Edge(v1, distances[2])};

    v3.adjacencies = new Edge[] { new Edge(v1, distances[3]),
                                new Edge(v5, distances[4]),
                                new Edge(v6, distances[5])};

    v4.adjacencies = new Edge[] { new Edge(v5, distances[6])};

    v5.adjacencies = new Edge[] { new Edge(v0, distances[1]),
                                new Edge(v3, distances[4]),
                                new Edge(v4, distances[6]),
                                new Edge(v6, distances[7]) };

    v6.adjacencies = new Edge[] { new Edge(v3, distances[5]),
                                new Edge(v5, distances[7]) };

Vertex[] vertices = {v0, v1, v2, v3, v4, v5, v6};

Dijkstra dijkstra = new Dijkstra();

........

for(int i = 0; i < vertices.length; i++) {

                    for(Vertex v : vertices) {
            v.setMinDistance(Double.POSITIVE_INFINITY);
        }
        dijkstra.computePaths(vertices[i]);
        //print out shortest paths and distance

        System.out.println("Shortest paths from "+ vertices[i].name);
        for (Vertex v: vertices) {
            System.out.println("Distance to " + v + ": " + v.minDistance);
            List<Vertex> shortestPath = dijkstra.getShortestPathTo(v);
            System.out.println("Path: " + shortestPath);

            currentAccE[i] = currentAccE[i] + (v.employment)*impedance(v.minDistance);
            currentAccP[i] = currentAccP[i] + (v.population)*impedance(v.minDistance);

        }
    }

........

输出:

Solve started..........
Shortest paths from Harrisburg
Distance to Harrisburg: 0.0
Path: [Harrisburg]
Distance to Baltimore: 79.0
Path: [Harrisburg, Baltimore]
Distance to Washington: 118.0
Path: [Harrisburg, Baltimore, Washington]
Distance to Philadelphia: 142.0
Path: [Harrisburg, Allentown, Philadelphia]
Distance to Binghamton: 214.0
Path: [Harrisburg, Allentown, Binghamton]
Distance to Allentown: 81.0
Path: [Harrisburg, Allentown]
Distance to New York: 172.0
Path: [Harrisburg, Allentown, New York]
Shortest paths from Baltimore
Distance to Harrisburg: 79.0
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at umd.sapeksha.shortestpath.Dijkstra.getShortestPathTo(Dijkstra.java:48)
at umd.sapeksha.shortestpath.Solve.main(Solve.java:109)

【问题讨论】:

  • 代码有什么问题?
  • 正如您在输出中看到的,在哈里斯堡的第一组最短路径值之后,对于巴尔的摩的第二组最短路径是错误的。巴尔的摩到哈里斯堡不是 0,而且路径也打印错了。我想最短路径不会再次计算。我想知道为什么?
  • 我知道你想使用 Dijkstra 算法,但作为建议,Floyd-Warshall 算法会更有效,如果你可以使用的话。
  • 我已经在main中初始化了这些变量。

标签: java algorithm out-of-memory shortest-path dijkstra


【解决方案1】:

每次更改源时,您都需要将顶点的minDistance 重新初始化为Double.POSITIVE_INFINITY,否则算法将使用先前计算的最小距离,这显然会根据您从哪里开始而有所不同。

【讨论】:

  • 谢谢。我应该在循环的哪个位置重新初始化 minDistance 的值?我在循环开始时使用 v.minDistance = Double.POSITIVE_INFINITY;它仍然没有给出正确的最短路径。
  • 在 Solve.java 中的 for(int i = 0; i &lt; vertices.length; i++) 之后立即尝试
  • 我猜它现在可以工作了。但是当我再次将其设置为 POSITIVE_INFINITY 时,会发生 java out of memory 错误,如下所示。线程“主”java.lang.OutOfMemoryError 中的异常:Java 堆空间。如果您可以提供指向讨论处理此问题的页面的链接,那就太好了。谢谢。
  • 可能你的while 循环没有终止(堆栈溢出错误)。您可以从其他每个位置到达每个位置吗?尝试调试它发生的位置。
  • 在循环期间重置minDistance 时,您还应该将previous 设置为null。 for 循环没有终止,因为previous 顶点图中有一个循环。
猜你喜欢
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 2019-02-20
  • 1970-01-01
  • 2020-08-02
相关资源
最近更新 更多