簡體   English   中英

Dijkstra算法的實現-陷入無限循環

[英]Implementation of Dijkstra`s Algorithm - Stuck in an infinite loop

有人可以幫我實施嗎? 我陷入無限循環,但我不知道為什么。 我認為問題出在我尋找具有最小距離的節點的步驟中……對此我將非常感謝。

import java.util.*;

public class Dijkstra {
private Map<String, Integer> dist;
private Set<Vertex> unvisited;
//private Set<Vertex> processed;
private Vertex source;
private Graph g;

public Dijkstra(Graph g, Vertex source) {
    this.g = g;
    this.source = source;
    //dist = new int[g.numOfVertices()];
    dist = new HashMap<String, Integer>();
    for(Vertex v: g.getVertices()) {
        if (v == this.source)
            dist.put(v.getId(), 0);
        else
            dist.put(v.getId(), Integer.MAX_VALUE);
    }
    unvisited = new HashSet<Vertex>(); 
    for(int i = 1; i < g.numOfVertices(); i++) {
        unvisited.add(g.getVertices().get(i));
    }       
}

public ArrayList<Integer> getShortestPaths() {
    while (!unvisited.isEmpty()) {
        Vertex current = this.getMinimum();
        System.out.println("Hello1");
        unvisited.remove(current);
        System.out.println("Hello2: "+ current.getId());
        if (dist.get(current.getId()) == Integer.MAX_VALUE)
            break;
        Map<Vertex,Integer > neighbors = new HashMap<Vertex,Integer>();
        for (Edge e : g.getEdges()) {
            if (e.getSource().getId() == current.getId() && unvisited.contains(e.getDestination())) {
                neighbors.put(e.getDestination(), e.getWeight());
            }
        }
        for (Vertex v : neighbors.keySet()) {
            int alt = dist.get(current.getId()) + neighbors.get(v);
            if (alt < dist.get(v.getId())) {
                dist.put(v.getId(), alt);
            }
        }           
    }





    return new ArrayList<Integer> (dist.values());//(ArrayList<Integer>) dist.values();
}

public Vertex getMinimum() {
    int indexOfMinimum = -1;
    //String indexOfMinimum = "";
    int minimum = Integer.MAX_VALUE;
    for (String i : dist.keySet() ) {
        if (dist.get(i) < minimum) {
            minimum = dist.get(i);
            System.out.println(minimum);
            indexOfMinimum = Integer.parseInt(i);
        }
    }
    return g.getVertices().get(indexOfMinimum);
}




public static void main(String[] args) {
    System.out.println("Hello World!!!");
    List<Vertex> v = new ArrayList<Vertex>();
    List<Edge> e = new ArrayList<Edge>();
    v.add(new Vertex("0"));
    v.add(new Vertex("1"));
    v.add(new Vertex("2"));
    v.add(new Vertex("3"));
    Graph g = new Graph(v ,e);
    g.addEdge(v.get(0), v.get(3), 1);
    g.addEdge(v.get(0), v.get(2), 4);
    g.addEdge(v.get(3), v.get(2), 2);
    g.addEdge(v.get(3), v.get(1), 6);
    g.addEdge(v.get(2), v.get(1), 3);
    Dijkstra sp = new Dijkstra(g, v.get(0));
    ArrayList<Integer> dist1 = sp.getShortestPaths();

    for (int i: dist1) { 
        System.out.println("Hello");
        System.out.println(dist1.get(i));
    }
    //v.add(new Vertex("5"));
    //v.add(new Vertex("6"));
    //v.add(new Vertex("7"));

}

}


public class Graph {
private final List<Vertex> vertices;
private final List<Edge> edges;

public Graph(List<Vertex> vertices, List<Edge> edges) {
    this.vertices = vertices;
    this.edges = edges;
}

public List<Vertex> getVertices() {
    return vertices;
}

public List<Edge> getEdges() {
    return edges;
}

public void addEdge(Vertex from, Vertex to, int weight) {
    edges.add(new Edge(from, to, weight));
}

public void addVertex(Vertex v) {
    vertices.add(v);
}

public int numOfVertices() {
    return this.vertices.size();
}

public int numOfEdges() {
    return this.edges.size();
}
}

class Vertex {
final private String id;

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

public String getId() {
    return this.id;
}
}

class Edge {
//private final String id;
private final Vertex source;
private final Vertex destination;
private final int weight;

public Edge(Vertex source, Vertex destination, int weight) {
    this.source = source;
    this.destination = destination;
    this.weight = weight;
}

public Vertex getSource() {
    return this.source;
}

public Vertex getDestination() {
    return this.destination;
}

public int getWeight() {
    return this.weight;
}

}

您的數據結構似乎有些混亂。 特別是,您似乎沒有任何結構可以按順序實際跟蹤需要考慮的節點。 dist包含起始節點,起始節點始終處於距離0,因此dist不能是該數據結構。

我建議從可靠來源的偽代碼版本開始,並要非常小心以使您的數據結構在名稱和含義上都與之完全匹配。 如果仍然遇到麻煩,請將對您所遵循的偽代碼的引用與代碼一起發布。 這將使您更容易理解您的意圖。

這可能不是問題,但很可疑:

for(int i = 1; i < g.numOfVertices(); i++) {
    unvisited.add(g.getVertices().get(i));
}      

為什么要初始化i = 1

您始終設置current = getMinimum()getMinimum()不在乎是否訪問了某個節點,因此它始終選擇0節點,這就是它循環的原因。 更改代碼,使getMinimum()必須選擇與current節點不同的節點。 實際上,您應該具有一個堆結構,該堆結構按距離排序,並且始終以最小的值poll()一個(並且我想通過poll()將其從容器中刪除)。 在getMinimum()上沒有log(n)復雜性的情況下實現它不是一個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM