2009-06-01 124 views
6

這是一個學校項目;我遇到了大量麻煩,而且我似乎找不到一個可以理解的解決方案。用於Java中二維數組的Dijkstra算法

​​3210

這是二維數組。所以,如果你想找到最短路徑,它從a,b,e,d,z = 7,和(a,b)=(b,a) - 它將把你帶到新的行到鄰近行路徑

有沒有人可以幫助我實現這個例子的Dijkstra算法?我真的很感激它。 (我似乎最喜歡陣列,地圖和集合讓我困惑,列表是可管理的 - 雖然我願意在這一點上尋求任何解決方案)

[至少我不只是撕掉來自網絡的消息來源。其實,我想學習這些東西......這只是真的很難(>。<)

哦,起點爲A,終點爲Z


由於大多數人,我不覺得該算法的概念很難 - 我只能看到獲得編碼的權利...請幫助嗎?

示例代碼 - 一個朋友幫了我很多(雖然它充滿了我覺得難以遵循的數據結構)我也嘗試過修改來自dreamincode.net/forums/blog/martyr2/的C++代碼的index.php?showentry = 578到Java,但是這並沒有那麼好走......

import java.util.*; 

public class Pathy{ 

    private static class pathyNode{ 
     public final String name; 
     public Map<pathyNode, Integer> adjacentNodes; 

     public pathyNode(String n){ 
      name = n; 
      adjacentNodes = new HashMap<pathyNode, Integer>(); 
     } 

    } 

    //instance variables 

    //constructors 

    //accessors 

    //methods 
    public static ArrayList<pathyNode> convert(int[][] inMatrix){ 
     ArrayList<pathyNode> nodeList = new ArrayList<pathyNode>(); 
     for(int i = 0; i < inMatrix.length; i++){ 
      nodeList.add(new pathyNode("" + i)); 
     } 
     for(int i = 0; i < inMatrix.length; i++){ 
      for(int j = 0; j < inMatrix[i].length; j++){ 
       if(inMatrix[i][j] != -1){ 
        nodeList.get(i).adjacentNodes.put(nodeList.get(j), 
          new Integer(inMatrix[i][j])); 
       } 
      } 
     } 
     return nodeList; 
    } 

    public static Map<pathyNode, Integer> Dijkstra(ArrayList<pathyNode> inGraph){ 
     Set<pathyNode> visited = new HashSet<pathyNode>(); 
     visited.add(inGraph.get(0)); 
     pathyNode source = inGraph.get(0); 
     Map answer = new TreeMap<pathyNode, Integer>(); 
     for(pathyNode node : inGraph){ 
      dijkstraHelper(visited, 0, source, node); 
      answer.put(node, dijkstraHelper(visited, 0, source, node)); 
     } 
     return answer; 
    } 

    private static int dijkstraHelper(Set<pathyNode> visited, int sum, pathyNode start, pathyNode destination){ 
     Map<pathyNode, Integer> adjacent = new HashMap<pathyNode, Integer>(); 

     for(pathyNode n : visited){ 
      for(pathyNode m: n.adjacentNodes.keySet()){ 
       if(adjacent.containsKey(m)){ 
        Integer temp = n.adjacentNodes.get(m); 
        if(temp < adjacent.get(m)){ 
         adjacent.put(m, temp); 
        } 
       } 
       else{ 
        adjacent.put(m, n.adjacentNodes.get(m)); 
       } 
      } 
     } 

     Map<pathyNode, Integer> adjacent2 = new HashMap<pathyNode, Integer>(); 
     Set<pathyNode> tempSet = adjacent.keySet(); 
     tempSet.removeAll(visited); 
     for(pathyNode n: tempSet){ 
      adjacent2.put(n, adjacent.get(n)); 
     } 
     adjacent = adjacent2; 
     Integer min = new Integer(java.lang.Integer.MAX_VALUE); 
     pathyNode minNode = null; 

     for(pathyNode n: adjacent.keySet()){ 
      Integer temp = adjacent.get(n); 
      if(temp < min){ 
       min = temp; 
       minNode = n; 
      } 
     } 
     visited.add(minNode); 
     sum += min.intValue(); 
     sum = dijkstraHelper(visited, sum, start, destination); 
     return sum; 
    } 

    //main 
    public static void main(String[] args){ 

     int[][] input = new int[][] { {-1, 2, 3, -1, -1, -1}, 
          {2, -1, -1, 5, 2, -1}, 
          {3, -1, -1, -1, 5, -1}, 
          {-1, 5, -1, -1, 1, 2}, 
          {-1, 2, 5, 1, -1, 4}, 
          {-1, -1, -1, 2, 4, -1}, 
         }; 
         //-1 represents an non-existant path 

     System.out.println(Dijkstra(convert(input))); 
    } 
} 

回答

8

您呼叫的二維數組的表示,是圖的鄰接矩陣表示,你的問題試圖解決的是一個「單源最短路徑」問題的實例,Dijkstra的算法是爲了解決這類問題而設計的,這可能有幫助http://renaud.waldura.com/doc/java/dijkstra/。從站點下載代碼並閱讀文檔。編寫類似的代碼如下

RoutesMap map = map = new DenseRoutesMap(5); 
    map.addDirectRoute(City.A, City.B, 2); 
    map.addDirectRoute(City.A, City.C, 3); 
    map.addDirectRoute(City.B, City.A, 2); 
    map.addDirectRoute(City.B, City.D, 5); 
    map.addDirectRoute(City.B, City.D, 2); 
    ... 
    DijkstraEngine engine = new DijkstraEngine(map); 
    int distance = engine.getShortestDistance(City.F); 
+0

不幸的是,該網站並沒有多大幫助。它不處理像我也需要它的二維陣列... 其他人有沒有解決方案/建議? – Stan 2009-06-01 09:55:23

0

不知道是否有人仍有意Dijikstra的這個矩陣表示,但我一直在想,在ActionScript編碼Dijikstra所以首先要教自己Dijuikstra是如何工作的。完成了這一步並嘗試編碼之後,我只是在昨天才意識到,我可以用矩陣表示節點和距離,比如你在這篇文章的頂部。我的理論是,找到最短路徑將是一件非常簡單的事情。我仍在試驗以確保我的正確性。

在矩陣中;

abcdez 一個 - 2 3 - - - B 2 - - 5 2 - 的C 3 - - - 5 - d - 5 - - 1 2 ë - 2 5月1日至4日 ž - - - 2 4 -

我開始看行「a」(因爲這是起點),並選擇行中的最小數字是2(在b下)。因此,我以2的代價將路徑寫爲「a-b」。然後我到b行並再次找到b的右邊的最小數字(因爲我們已經在b節點了,這使得我有2 「e」,所以路徑現在是「a - b - e」,總共花費4美元。然後看看「e」行,規則應該找到出現在「e」列後面的最小數字。這會給我「z」,並給出完整的路徑爲「a - b - e - z」,總成本爲8.然而,記住我昨晚才明白這一點,方法學需要認識到, 「e」行另一種可能性是以1的成本去d列。然後看d行後的最低數字,這將給我「z」行的代價爲2.

因此,這是一個更好的解決方案,路徑是「a - b - e - d -z「,總共花費7美元,正如你所說的。

好的我仍然需要在ActionScript中編寫代碼,但我的直覺是,它將非常容易在ActionScript中編碼。害怕我在Java中沒有經驗,但如果您同意我的方法,那麼使用Java編碼應該很容易。

Paul