單源最短路徑分支限界法之java實現

來源:互聯網
上載者:User

public class Short {
 public static void main(String args[]){
 
  int n = 5;
  int[] prev = new int[n+1];
  int[] dist = {0,0,5000,5000,5000,5000};
  int[][] c = {{0,0,0,0,0,0},{0,0,2,3,5000,5000},
    {0,5000,0,1,2,5000},{0,5000,5000,0,9,2},
    {0,5000,5000,5000,0,2},{0,5000,5000,5000,5000,0}};
  /*
  int n = 4;
  int[] dist = {0,0,5000,5000,5000};
  int[][] c = {{0,0,0,0,0},{0,0,2,3,5000},{0,5000,0,1,2},{0,5000,5000,0,9},{0,5000,5000,5000,0}};
  
  */

  Graph g = new Graph(n,prev,c,dist);
  g.shortestPath(1);
  System.out.println(dist[n]);
 }
 
}

class MinHeapNode{

 int i;    //頂點編號
 int length;   //當前路長

}
//迴圈隊列
class CirQueue{
  int size = 5000;    //定義隊列長度為1000
  int front,rear;
  MinHeapNode data[] = new MinHeapNode[size];

  public CirQueue(){
   front = rear = 0;
  }
  
  //元素入隊操作
  void queryIn(MinHeapNode e){
   
   if((rear +1)%size == front){
    System.out.println("隊列已滿");
   }else{
    rear = (rear+1)%size; //隊尾指標在迴圈意義下加1
    data[rear] = e;   //在隊尾插入元素
    
   }
  }
  
  
  //元素出隊操作
  MinHeapNode queryOut(){
   if(rear == front){
    System.out.println("隊列已空");
    return null;
   }else{
    front = (front+1)%size;  //隊列在迴圈意義下加1
    return data[front];
   }
  }
  //讀取隊頭元素,但不出隊
  MinHeapNode getQuery(){
   if(rear == front){
    System.out.println("隊列已空");
    return null;
   }else{
    return data[(front+1)%size];
   }
  }
  
  
  //判斷隊列是否為空白
  boolean empty(){
   return front == rear;
   
  }
 }

//圖的表示
class Graph{
 static int inf = 5000;   //兩點距離上界
 int n;       //圖的頂點個數
 int prev[];      //圖的前驅頂點
 int c[][];      //圖的鄰接矩陣
 int dist[];      //最短距離數組
 public Graph(int n,int[] prev,int[][] c,int[] dist){
  
  this.n = n;
  this.prev = prev;
  this.c = c;
  this.dist  = dist;
 }
 
 /**
  * 單源最短路徑問題的優先隊列式分支限界法
  */
 
 public void shortestPath(int v){
  //建立隊列
  CirQueue qq = new CirQueue();
  
  //定義源為初始擴充結點
  MinHeapNode e = new MinHeapNode();
  e.i = v;
  e.length = 0;
  dist[v] = 0;
  qq.queryIn(e);
  //搜尋問題的解空間
  while(true){
   for(int j = 1;j<=n;j++){
    
    if(j>n){
     break;
    }
    MinHeapNode m = qq.getQuery();
    if((c[m.i][j]<inf)&&(m.length + c[m.i][j] < dist[j])){
     
     //頂點i到頂點j可達,且滿足控制約束
     dist[j] = m.length + c[m.i][j];
     prev[j] = m.i;
    
    //加入活結點優先隊列
    MinHeapNode mi = new MinHeapNode();
    mi.i = j;
    mi.length  = dist[j];
    
    qq.queryIn(mi);   //元素入隊
    }
   }//for迴圈結束
   
   
   qq.queryOut();    //當該結點的孩子結點全部入隊後,刪除該結點
   
   if(qq.empty()){    //當隊列為空白時,演算法結束
    break;
   }
  }//while迴圈結束
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.