Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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中的边不相交最短对算法?_Java_Algorithm_Graph_Dijkstra - Fatal编程技术网

Java中的边不相交最短对算法?

Java中的边不相交最短对算法?,java,algorithm,graph,dijkstra,Java,Algorithm,Graph,Dijkstra,我试图在给定的一对顶点之间找到最短的边不相交路径对,我一直在遵循这一点,我想这通常是Suurballe的算法 以下是算法: 对给定的一对顶点运行最短路径算法(我使用的是Dijkstra算法) 将最短路径(相当于两条方向相反的弧)的每条边替换为指向源顶点的单个弧 使上述每个弧的长度为负值 运行最短路径算法(注意:算法应接受负成本) 擦除找到的两条路径的重叠边,并反转第一条最短路径上剩余圆弧的方向,使其上的每个圆弧现在都指向汇顶点。结果显示所需的路径对 在wikipedia中,第一步是找到源节点

我试图在给定的一对顶点之间找到最短的边不相交路径对,我一直在遵循这一点,我想这通常是
Suurballe的算法

以下是算法:

  • 对给定的一对顶点运行最短路径算法(我使用的是Dijkstra算法)
  • 将最短路径(相当于两条方向相反的弧)的每条边替换为指向源顶点的单个弧
  • 使上述每个弧的长度为负值
  • 运行最短路径算法(注意:算法应接受负成本)
  • 擦除找到的两条路径的重叠边,并反转第一条最短路径上剩余圆弧的方向,使其上的每个圆弧现在都指向汇顶点。结果显示所需的路径对
在wikipedia中,第一步是找到源节点和目标节点之间的最短路径,我可以使用
Dijkstra算法正确地找到它,如下代码所示-

public class DijkstraAlgorithm {

    private static final Graph.Edge[] GRAPH = { 
        new Graph.Edge("A", "G", 8), 
        new Graph.Edge("A", "B", 1), 
        new Graph.Edge("A", "E", 1), 
        new Graph.Edge("B", "C", 1), 
        new Graph.Edge("B", "E", 1),
        new Graph.Edge("B", "F", 2),
        new Graph.Edge("C", "G", 1),
        new Graph.Edge("C", "D", 1),
        new Graph.Edge("D", "F", 1),
        new Graph.Edge("D", "Z", 1),
        new Graph.Edge("E", "F", 4),
        new Graph.Edge("F", "Z", 4),
        new Graph.Edge("G", "Z", 2),
    };

    private static final String START = "A";
    private static final String END = "Z";

    public static void main(String[] args) {
        Graph g = new Graph(GRAPH);
        g.dijkstra(START);
        //  print the shortest path using Dijkstra algorithm
        g.printPath(END);
        //        g.printAllPaths();
    }
}


class Graph {
    private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges

    /** One edge of the graph (only used by Graph constructor) */
    public static class Edge {
        public final String v1, v2;
        public final int dist;

        public Edge(String v1, String v2, int dist) {
            this.v1 = v1;
            this.v2 = v2;
            this.dist = dist;
        }
    }

    /** One vertex of the graph, complete with mappings to neighbouring vertices */
    public static class Vertex implements Comparable<Vertex> {
        public final String name;
        public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
        public Vertex previous = null;
        public final Map<Vertex, Integer> neighbours = new HashMap<Vertex, Integer>();

        public Vertex(String name) {
            this.name = name;
        }

        private void printPath() {
            if (this == this.previous) {
                System.out.printf("%s", this.name);
            } else if (this.previous == null) {
                System.out.printf("%s(unreached)", this.name);
            } else {
                this.previous.printPath();
                System.out.printf(" -> %s(%d)", this.name, this.dist);
            }
        }

        public int compareTo(Vertex other) {
            if (dist==other.dist)
                return name.compareTo(other.name);
            return Integer.compare(dist, other.dist);
        }
    }

    /** Builds a graph from a set of edges */
    public Graph(Edge[] edges) {
        graph = new HashMap<String, Vertex>(edges.length);

        //one pass to find all vertices
        for (Edge e : edges) {
            if (!graph.containsKey(e.v1))
                graph.put(e.v1, new Vertex(e.v1));
            if (!graph.containsKey(e.v2))
                graph.put(e.v2, new Vertex(e.v2));
        }

        //another pass to set neighbouring vertices
        for (Edge e : edges) {
            graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
            graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also for an undirected graph
        }
    }

    /** Runs dijkstra using a specified source vertex */
    public void dijkstra(String startName) {
        if (!graph.containsKey(startName)) {
            System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
            return;
        }
        final Vertex source = graph.get(startName);
        NavigableSet<Vertex> q = new TreeSet<Vertex>();

        // set-up vertices
        for (Vertex v : graph.values()) {
            v.previous = v == source ? source : null;
            v.dist = v == source ? 0 : Integer.MAX_VALUE;
            q.add(v);
        }

        dijkstra(q);
    }

    /** Implementation of dijkstra's algorithm using a binary heap. */
    private void dijkstra(final NavigableSet<Vertex> q) {
        Vertex u, v;
        while (!q.isEmpty()) {

            u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
            if (u.dist == Integer.MAX_VALUE)
                break; // we can ignore u (and any other remaining vertices) since they are unreachable

            //look at distances to each neighbour
            for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
                v = a.getKey(); //the neighbour in this iteration

                final int alternateDist = u.dist + a.getValue();
                if (alternateDist < v.dist) { // shorter path to neighbour found
                    q.remove(v);
                    v.dist = alternateDist;
                    v.previous = u;
                    q.add(v);
                }
            }
        }
    }

    /** Prints a path from the source to the specified vertex */
    public void printPath(String endName) {
        if (!graph.containsKey(endName)) {
            System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
            return;
        }

        graph.get(endName).printPath();
        System.out.println();
    }

    /** Prints the path from the source to every vertex (output order is not guaranteed) */
    public void printAllPaths() {
        for (Vertex v : graph.values()) {
            v.printPath();
            System.out.println();
        }
    }
}
公共类dijkstra算法{
私有静态最终图。边[]图={
新的图边(“A”,“G”,8),
新的图边(“A”,“B”,1),
新的图边(“A”,“E”,1),
新的图边(“B”、“C”和“1”),
新的边图(“B”、“E”、1),
新的图。边(“B”,“F”,2),
新的图边(“C”,“G”,1),
新的图边(“C”、“D”、1),
新的图边(“D”、“F”、1),
新的图边(“D”、“Z”、1),
新的边图(“E”、“F”、4),
新的图。边(“F”,“Z”,4),
新的图。边(“G”,“Z”,2),
};
私有静态最终字符串START=“A”;
私有静态最终字符串END=“Z”;
公共静态void main(字符串[]args){
图g=新图(图);
g、 迪克斯特拉(起点);
//使用Dijkstra算法打印最短路径
g、 打印路径(结束);
//g.printalpath();
}
}
类图{
private final Map graph;//从一组边构建顶点名称到顶点对象的映射
/**图的一条边(仅由图构造函数使用)*/
公共静态类边缘{
公共最终字符串v1、v2;
公共终审法院;
公共边缘(字符串v1、字符串v2、int-dist){
这1.v1=v1;
这1.v2=v2;
this.dist=dist;
}
}
/**图的一个顶点,完成到相邻顶点的映射*/
公共静态类{
公共最终字符串名;
public int dist=Integer.MAX_VALUE;//假定MAX_值为无穷大
公共顶点上一个=空;
公共最终映射邻居=新HashMap();
公共顶点(字符串名称){
this.name=名称;
}
私有void printPath(){
if(this==this.previous){
System.out.printf(“%s”,此.name);
}else if(this.previous==null){
System.out.printf(“%s(未访问)”,此.name);
}否则{
this.previous.printPath();
System.out.printf(“->%s(%d)”,this.name,this.dist);
}
}
公共整数比较(顶点其他){
if(dist==其他.dist)
返回name.compareTo(其他.name);
返回整数.compare(dist,other.dist);
}
}
/**从一组边生成图形*/
公共图(边[]边){
图形=新的HashMap(edges.length);
//一次查找所有顶点
用于(边e:边){
如果(!graph.containsKey(e.v1))
放置(e.v1,新顶点(e.v1));
如果(!graph.containsKey(e.v2))
图.put(e.v2,新顶点(e.v2));
}
//另一个过程用于设置相邻顶点
用于(边e:边){
graph.get(e.v1)、neights.put(graph.get(e.v2)、e.dist);
graph.get(e.v2).neights.put(graph.get(e.v1),e.dist);//也适用于无向图
}
}
/**使用指定的源顶点运行dijkstra*/
公共无效dijkstra(字符串起始名){
if(!graph.containsKey(startName)){
System.err.printf(“图形不包含起始顶点\%s\“\n”,起始名称);
返回;
}
最终顶点源=graph.get(startName);
NavigableSet q=新树集();
//设置顶点
对于(顶点v:graph.values()){
v、 previous=v==source?source:null;
v、 dist=v==source?0:Integer.MAX_值;
q、 添加(v);
}
迪克斯特拉(q);
}
/**使用二进制堆实现dijkstra算法*/
私人void dijkstra(最终NavigableSet q){
顶点u,v;
而(!q.isEmpty()){
u=q.pollFirst();//距离最短的顶点(第一次迭代将返回源)
if(u.dist==整数.MAX_值)
break;//我们可以忽略u(和任何其他剩余顶点),因为它们是不可访问的
//看看每个邻居的距离
对于(Map.Entry a:u.neights.entrySet()){
v=a.getKey();//此迭代中的邻居
最终int alternateDist=u.dist+a.getValue();
if(alternativeist