posts - 241,  comments - 116,  trackbacks - 0
          暑假寫的Java實現Dijkstra單源最短路徑,主要特點是以起始點為中心向外層層擴展,直到擴展到終點為止。描述就不寫了,看相關書籍吧。 Dijkstra是一個貪心算法。
          package Section9;


          /*第九章  貪婪算法   Dijkstra單源最短路徑*/

          public class Dijkstra {

              /**
          墻頭草

               * @param args
               */
              public static void main(String[] args) {
                  // TODO Auto-generated method stub
                  int[][] weight = {
                          {0,3,2000,7,9999999},
                          {3,0,4,2,9999999},
                          {9999999,4,0,5,6},
                          {7,2,5,0,4},
                          {9999999,9999999,4,6,0}
                  };
                  
                  int[] path = Dijsktra(weight,0);
                  for(int i = 0;i < path.length;i++)
                      System.out.print(path[i] + "  ");
              }
              

              public static int[] Dijsktra(int[][] weight,int start){
                  //接受一個有向圖的權重矩陣,和一個起點編號start(從0編號,頂點存在數組中)
                  //返回一個int[] 數組,表示從start到它的最短路徑長度
                  int n = weight.length;        //頂點個數
                  int[] shortPath = new int[n];    //存放從start到其他各點的最短路徑
                  int[] visited = new int[n];        //標記當前該頂點的最短路徑是否已經求出,1表示已求出
                  
                  //初始化,第一個頂點求出
                  shortPath[start] = 0;
                  visited[start] = 1;
                  
                  for(int count = 1;count <= n - 1;count++)        //要加入n-1個頂點
                  {
                      int k = -1;    //選出一個距離初始頂點start最近的未標記頂點
                      int dmin = 1000;
                      for(int i = 0;i < n;i++)
                      {
                          if(visited[i] == 0 && weight[start][i] < dmin)
                          {
                              dmin = weight[start][i];
                              k = i;
                          }    
                      }
                      
                      //將新選出的頂點標記為已求出最短路徑,且到start的最短路徑就是dmin
                      shortPath[k] = dmin;
                      visited[k] = 1;
                      
                      //以k為中間點想,修正從start到未訪問各點的距離
                      for(int i = 0;i < n;i++)
                      {
                          if(visited[i] == 0 && weight[start][k] + weight[k][i] < weight[start][i])
                               weight[start][i] = weight[start][k] + weight[k][i];
                      }    
              
                  }
                  
                  return shortPath;
              }
          }
          posted on 2011-10-09 10:52 墻頭草 閱讀(3499) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 新野县| 广德县| 石河子市| 扬州市| 无棣县| 南皮县| 荥经县| 阿荣旗| 马关县| 谢通门县| 梨树县| 肃北| 深水埗区| 乌海市| 玛沁县| 文山县| 宁都县| 东乌| 磐石市| 定结县| 四子王旗| 阳新县| 浪卡子县| 迁安市| 潍坊市| 广安市| 尼勒克县| 女性| 奉化市| 常宁市| 惠州市| 龙州县| 正阳县| 谷城县| 泌阳县| 兴和县| 汉中市| 三门峡市| 介休市| 昂仁县| 乐安县|