java - Dijkstra 算法 Java 添加邻接关系

标签 java

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Dijkstra {

public static void computePaths(Vertex source) {
    source.minDistance = 0.;
    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;
            double distanceThroughU = u.minDistance + weight;
            if (distanceThroughU < v.minDistance) {
                vertexQueue.remove(v);

                v.minDistance = distanceThroughU;
                v.previous = u;
                vertexQueue.add(v);
            }
        }
    }
}

public static 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;
}

    Vertex A = new Vertex("A");
    Vertex B = new Vertex("B");
    Vertex D = new Vertex("D");
    Vertex J = new Vertex("J");
    Vertex M = new Vertex("M");

    // set the edges and weight
    A.adjacencies = new Edge[]{new Edge(M, 8), new Edge(D, 11)};
    M.adjacencies = new Edge[]{new Edge(A, 8), new Edge(J, 10)};
    D.adjacencies = new Edge[]{new Edge(A, 11), new Edge(J, 5)};
    J.adjacencies = new Edge[]{new Edge(D, 5), new Edge(M, 10)};

    computePaths(J); // run Dijkstra
    System.out.println("Distance to " + A + ": " + A.minDistance);
    List<Vertex> path = getShortestPathTo(A);
    System.out.println("Path: " + path);
}
}

class Vertex implements Comparable<Vertex> {

public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;

public Vertex(String argName) {
    name = argName;
}

public String toString() {
    return name;
}

public int compareTo(Vertex other) {
    return Double.compare(minDistance, other.minDistance);
}

}

class Edge {

public final Vertex target;
public final double weight;

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

有没有办法添加邻接关系? 例如:

A.adjacencies = new Edge[]{new Edge(M, 8)};
A.adjacencies = new Edge[]{new Edge(D, 11)};

基本上我想检查 A.adjaccies 是否有任何边,以及它是否确实将第二行添加为新边而不覆盖第一行。

有办法吗?如果没有,有办法解决这个问题吗?

最佳答案

使用集合而不是数组。例如ArrayLisy:

A.adjacents = new ArrayList <Edge>();
A.add (new Edge (M, 8));
A.add (new Edge (D, 11));

关于java - Dijkstra 算法 Java 添加邻接关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243685/

相关文章:

java - 动态自动调整 JTable 列的宽度

java - 通过基于 Java Spring 框架构建的 RESTful API 服务器对客户端进行身份验证

java - 在 Java 项目中放置文本文件的文件夹以及如何引用文件夹

java - 什么是用于在多个文件中搜索搜索词列表的良好 Java 库?

java - 字符串 'ACED00057400'代表什么?

java.exe=jvm 和 javac.exe=编译器

java - JTable 中的重音在 Eclipse 中显示,但在 .jar 中不显示

java - 为什么在 jenkins 扩展点中,ConsoleLogFilter 使用 OutputStream 而不是 String?

java - 继承: Annotate @Id of Super class to child class having different sequences

java - 使用 j2html 的登录表单示例