Dijkstra算法无限循环

时间:2016-05-19 02:14:31

标签: graph-algorithm

我正在学习最短路径算法,我正在尝试实现一个Dijkstra算法,该算法从这样的文件中获取输入:

    7
    A
    B
    C
    D
    E
    F
    G
    A B 21
    A C 14
    B E 5
    B D 7
    D F 3
    E C 44
    E G 53
    E D 123
    G F 51

问题是当我为D B 12添加额外的边缘时,:

DIJKSTRA ALGORITHM:

public Set<Vertex> dijkstraAlgo(Graph G, int s) {
    initializeSingleSource(G, s);
    Set<Vertex> set = new HashSet<Vertex>(); // intitially empty set of
                                                // vertexes

    Queue<Vertex> Q = new PriorityQueue<Vertex>(10, new VertexComparator()); // min
                                                                                // priority
                                                                                // queue

    for (Vertex v : G.vertices) { // add source to priority queue
        Q.add(G.vertices[s]);
    }

    while (Q.size() != 0) {
        Vertex u = Q.poll(); // extract vertex which have min distance in
                                // priority queue
        set.add(u); // add that vertex to set
        for (String vertexId : u.neighbours.keySet()) { // see neighbours of
            // vertex extracted
            int vertexNum = indexForName(G, vertexId);

            Vertex v = G.vertices[vertexNum];
            int w = weightFunc(G, u, v);

            relax(u, v, w);
            Q.add(v);
        }
    }

    return set;

}

阅读文件:

public class Graph {
 Vertex[] vertices;


public Graph(String file) throws FileNotFoundException{
  Scanner sc = new Scanner(new File(file));
  vertices=new Vertex[sc.nextInt()];

  for (int v = 0; v < vertices.length; v++){
    vertices[v] = new Vertex(sc.next());
  }

    while (sc.hasNext()) {
    int v1= indexForName(sc.next());     //read source vertex
    String destination=sc.next();        //read destination vertex 

    int w=sc.nextInt();                  //read weight of the edge


    vertices[v1].neighbours.put(destination, w);   //put the edge adjacent to source vertex
    }
    sc.close();

}

MAIN:

public static void main(String[] args) throws FileNotFoundException {
    String fileName = "Dijikstra.txt";
    Dijkstra dijkstra = new Dijkstra(fileName);

    Set<Vertex> vertexInfo = dijkstra.dijkstraAlgo(dijkstra.graph, 0);
    System.out
            .println("Printing min distance of all vertexes from source vertex A ");
    for (Vertex v : vertexInfo) {
        System.out.println("Id: " + v.id + " distance: " + v.d + " Comming From " + v.p);
    }
}

VERTEX:

class Vertex{
   String id;    
    int d;        //to store min distance from source
    Vertex p;     //to store last vertex from which min distance is reached
Map<String,Integer> neighbours;   //to store edges of adjacent to the vertex            

    public Vertex(String id){
    this.id=id;
    neighbours=new HashMap<String,Integer>();
   }
 }

1 个答案:

答案 0 :(得分:0)

for (Vertex v : G.vertices) { // add source to priority queue
        Q.add(G.vertices[s]);
    }

为什么要将每个顶点添加到优先级队列而不是仅添加初始端点?您的Vertex类是什么样的?