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

Dijkstra(迪傑斯特拉)算法是典型的最短路徑路由算法,用於計算一個節點到其餘全部節點的最短路徑。主要特色是以起始點爲中心向外層層擴展,直到擴展到終點爲止。Dijkstra算法能得出最短路徑的最優解,但因爲它遍歷計算的節點不少,因此效率低。node

Dijkstra算法是頗有表明性的最短路算法,在不少專業課程中都做爲基本內容有詳細的介紹,如數據結構,圖論,運籌學等等。ios

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

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

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

Dijkstra算法的迭代過程:數據結構

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

#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' distance
void 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文件內容:code

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

輸出結果:ci

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

感謝原做者!

  歡迎關注微信公衆號「 **IT客**「 ,投稿郵箱 itkeyy@163.com

相關文章
相關標籤/搜索