最短路徑演算法—Dijkstra(迪傑斯特拉)演算法分析與實現(C/C++)

來源:互聯網
上載者:User

標籤:printf   using   previous   void   代表性   有向圖   roc   路徑   尋找   

Dijkstra(迪傑斯特拉)演算法是典型的最短路徑路由演算法,用於計算一個節點到其他所有節點的最短路徑。主要特點是以起始點為中心向外層層擴充,直到擴充到終點為止。Dijkstra演算法能得出最短路徑的最優解,但由於它遍曆計算的節點很多,所以效率低。

Dijkstra演算法是很有代表性的最短路演算法,在很多專業課程中都作為基本內容有詳細的介紹,如資料結構,圖論,運籌學等等。

其基本思想是,設定頂點集合S並不斷地作貪心選擇來擴充這個集合。一個頂點屬於集合S若且唯若從源到該頂點的最短路徑長度已知。

初始時,S中僅含有源。設u是G的某一個頂點,把從源到u且中間只經過S中頂點的路稱為從源到u的特殊路徑,並用數組dist記錄當前每個頂點所對應的最短特殊路徑長度。Dijkstra演算法每次從V-S中取出具有最短特殊路長度的頂點u,將u添加到S中,同時對數組dist作必要的修改。一旦S包含了所有V中頂點,dist就記錄了從源到所有其它頂點之間的最短路徑長度。

例如,對中的有向圖,應用Dijkstra演算法計算從源頂點1到其它頂點間最短路徑的過程列在下表中。

Dijkstra演算法的迭代過程:

以下是具體的實現(C/C++):

#include <iostream>#include<stdio.h>#include<stdlib.h>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‘ distancevoid Dijkstra(int n, int v, int *dist, int *prev, int c[maxnum][maxnum]){    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就記錄了從源點到所有其他頂點之間的最短路徑長度         // 注意是從第二個節點開始,第一個為源點    for(int i=2; i<=n; ++i)    {        int tmp = maxint;        int u = v;        // 找出當前未使用的點j的dist[j]最小值        for(int j=1; j<=n; ++j)            if((!s[j]) && dist[j]<tmp)            {                u = j;              // u儲存當前鄰接點中距離最小的點的號碼                tmp = dist[j];            }        s[u] = 1;    // 表示u點已存入S集合中        // 更新dist        for(int 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;                }            }    }}// 尋找從源點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(int 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(int i=1; i<=n; ++i)        dist[i] = maxint;    for(int 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);    // 最短路徑長度    cout << "源點到最後一個頂點的最短路徑長度: " << dist[n] << endl;    // 路徑    cout << "源點到最後一個頂點的路徑為: ";    searchPath(prev, 1, n);}

input.txt檔案內容:

5
7
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60

輸出結果:

999999 10 999999 30 100
10 999999 50 999999 999999
999999 50 999999 20 10
30 999999 20 999999 60
100 999999 10 60 999999
源點到最後一個頂點的最短路徑長度: 60
源點到最後一個頂點的路徑為: 1 -> 4 -> 3 -> 5

Process returned 0 (0x0) execution time : 0.024 s
Press any key to continue.

 

原文連結:http://www.wutianqi.com/?p=1890

感謝原作者!

 

最短路徑演算法—Dijkstra(迪傑斯特拉)演算法分析與實現(C/C++)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.