2013-12-15 71 views
1

這裏是我的問題(路徑圖)查找兩點之間的最短路徑,動態規劃

我想尋找到F從A shothest路徑,我讀,我應該使用Dijkstra算法,我試圖做這一點,但我有設置邊緣問題:

在79線我設置的邊緣http://pastebin.com/UAZiP7qb

這是很好的選擇,以解決這種情況下,通過這種算法?我該怎麼辦?

我不能設置邊正確:

public static void main(String[] args) 
    { 
     // mark all the vertices 
     Vertex A = new Vertex("A"); 
     Vertex B = new Vertex("B"); 
     Vertex C = new Vertex("C"); 
     Vertex D = new Vertex("D"); 
     Vertex E = new Vertex("E"); 
     Vertex F = new Vertex("F"); 


     // set the edges and weight 
     A.adjacencies = new Edge[]{ new Edge(B, 0) }; 
     B.adjacencies = new Edge[]{ new Edge(E, 2) }; 
     E.adjacencies = new Edge[]{ new Edge(F, 2) }; 
} 
+2

請,包括在問題本身的源代碼,而不是鏈接到一些討厭的廣告填充部位。此外,請指明您的問題到底是什麼,「有問題」不夠明確。 – chill

回答

1

您留下一些adjancency列表爲空。在嘗試迭代它們之前檢查。

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

接下來的兩行加

if (u.adjacencies == null) 
     continue; 

     // Visit each edge exiting u 
     for (Edge e : u.adjacencies) 
+0

如果我加這條線我應該如何設置邊緣? – user3104624

+0

只需現在設置它的方式,不要忘記添加兩個方向。 – chill

+0

但如果我添加A.adjacencies =新邊[] {新邊(C,0)};它取代我以前的行(A.adjacencies =新邊[] {新邊(B,0)};) 我應該如何設置它? – user3104624

相關問題