最短路徑-迪傑斯特拉算法(Dijkstra) (簡單講解


 

 

現在給你一個深圳地鐵圖。小明從市民中心上車,計算他到深圳所有地鐵站所需時間(簡化每個站到下一個站只花2分鍾)。這就是迪傑斯特拉算法干的事。

歷史:Dijkstra thought about the shortest path problem when working at the Mathematical Center in Amsterdam in 1956 as a programmer to demonstrate capabilities of a new computer called ARMAC. His objective was to choose both a problem as well as an answer (that would be produced by computer) that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (64, so that 6 bits would be sufficient to encode the city number).[1] A year later, he came across another problem from hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the back panel of the machine. As a solution, he re-discovered the algorithm known as Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim).[5][6] Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.。

大概意思就是D這個人吶在MC工作,他在檢驗當時一個叫ARMAC機的能力。他想設計一個問題和算法讓普通人都能明白,於是拿起了荷蘭64座城市地圖。之后又做了一些事情,比如PRIM算法,以及利用這些算法解決了插接線的問題啥的。后來他在1959年就把這算法發表了。

代碼:

#include <iostream> 

using namespace std; 
const int maxnum = 100; 
const int maxint = 999999; 
// 各數組都從下標1開始 

int dist[maxnum]; // 表示當前點到源點的最短路徑長度 

int prev[maxnum]; // 記錄當前點的前一個結點 

int c[maxnum][maxnum]; // 記錄圖的兩點間路徑長度 

int n, line; // 圖的結點數和路徑數 

// n -- n nodes 

// v -- the source node 

// dist[] -- the distance from the ith node to the source node 

// prev[] -- the previous node of the ith node 

// c[][] -- every two nodes' distance 

void Dijkstra(int n, int v, int *dist, int *prev, int c[maxnum][maxnum]) 
{ 
    //步驟1------------初始化-------------

bool s[maxnum]; // 判斷是否已存入該點到S集合中 

   for(int i=1; i<=n; ++i) 
   { 

      dist[i] = c[v][i]; 
      s[i] = 0; // 初始都未用過該點 

       if(dist[i] == maxint) 
          prev[i] = 0; 
       else  
          prev[i] = v; 

   } 
   dist[v] = 0; 
   s[v] = 1; 

// 依次將未放入S集合的結點中,取dist[]最小值的結點,放入結合S中 
// 一旦S包含了所有V中頂點,dist就記錄了從源點到所有其他頂點之間的最短路徑長度 
// 注意是從第二個節點開始,第一個為源點 

   //-----------步驟2--找到最小的並納入-------------
         //maxint 是無窮
         //v是0點
         //tmp是 找的最小點u的dis
         
   for(i=2; i<=n; ++i) 
   { 

        int tmp = maxint; 
        int u = v; 

            
       for(int j=1; j<=n; ++j) 
          if((!s[j]) && dist[j]<tmp) 
          { 
                u = j; // u保存當前鄰接點中距離最小的點的號碼 
                tmp = dist[j]; 
          } 
          s[u] = 1; // 表示u點已存入S集合中 

    //----------步驟3--更新dist------------------ 
          //u 是剛剛找到最小點
          //s 是並入集合
          //newdist,dist[u]+c[u][j] 是0-u點距離+u到個點距離
          //dist[j]是個0-點的距離
          //prev[j]是個點的前一點

          for(j=1; j<=n; ++j) 
              if((!s[j]) && c[u][j]<maxint) 
              { 

                     int newdist = dist[u] + c[u][j]; 
                     if(newdist < dist[j]) 
                     { 
                         dist[j] = newdist; 
                         prev[j] = u; 
                     } 

              } 
   } //步驟2,3重復n-1

} 

// 查找從源點v到終點u的路徑,並輸出 

void searchPath(int *prev,int v, int u) 
{ 
    int que[maxnum]; 
    int tot = 1; 
    que[tot] = u; 
    tot++; 
    int tmp = prev[u]; 
    
    while(tmp != v) 
    { 
      que[tot] = tmp; 
       tot++; 
      tmp = prev[tmp]; 

    } 
    que[tot] = v; 
    
    for(int i=tot; i>=1; --i) 
        if(i != 1) 
            cout << que[i] << " -> "; 
        else 
            cout << que[i] << endl; 

} 

int main() 
{ 
    freopen("input.txt", "r", stdin); 
    // 各數組都從下標1開始 
    // 輸入結點數 
    cin >> n; 
    // 輸入路徑數 
    cin >> line; 
    
    int p, q, len; // 輸入p, q兩點及其路徑長度 
    // 初始化c[][]為maxint 
    
    for(int i=1; i<=n; ++i) 
        for(int j=1; j<=n; ++j) 
            c[i][j] = maxint; 
        
        
        for(i=1; i<=line; ++i) 
        { 

           cin >> p >> q >> len; 
           if(len < c[p][q]) // 有重邊 
           { 
               c[p][q] = len; // p指向q 
               c[q][p] = len; // q指向p,這樣表示無向圖 
           } 
        } 
        
        for(i=1; i<=n; ++i) 
            dist[i] = maxint; 
        
        for(i=1; i<=n; ++i) 
        { 
            for(int j=1; j<=n; ++j) 
                printf("%8d", c[i][j]); 
                 printf("\n"); 
        } 
        Dijkstra(n, 1, dist, prev, c); 
        
        for(int k=1;k<n;k++)
        {
          // 最短路徑長度 
          cout <<"源點到頂點"<<k+1<<" 的最短路徑長度:" << dist[k+1] <<"  ";
          // 路徑 
          cout <<"源點到頂點"<<k+1<<" 的路徑為: "; 
           searchPath(prev, 1, k+1); 
        }


} 

 

例子:

 

算法步驟如下:

 

  1. 初使時令 S={V0},T={其余頂點},T中頂點對應的距離值

    若存在<V0,Vi>,d(V0,Vi)為<V0,Vi>弧上的權值

  若不存在<V0,Vi>,d(V0,Vi)為∝

    2. 從T中選取一個其距離值為最小的頂點W且不在S中,加入S

    3. 對T中頂點的距離值進行修改:若加進W作中間頂點,從V0到Vi的
         距離值比不加W的路徑要短,則修改此距離值
 
     重復上述步驟2、3,直到S中包含所有頂點,即S=T為止

 

 

 初始化

S1納入2(10最小)

S1納入4(30最小)

S1納入3(50最小)

S1納入5(60最小)

2

10

10

10

10

10

3

無窮

10+50<無窮 60

30+20< 60  50

50

50

4

30

10+無窮>30  30

30

30

30

5

100

10+無窮>100 100

30+60<100  90

50+10<90  60

60

S1

{1}

{1,2}

{1,2,4}

{1,2,4,3}

{1,2,4,3,5}

S2

{2,3,4,5}

{3,4,5}

{3,5}

{5}

{}

dis

 

Dis2=10

Dis4=30

Dis3=50

Dis5=60

pre

Pre2=1

Pre4=1

Pre5=1

pre3=2

Pre3=4

Pre5=4

Pre5=3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM