zoukankan      html  css  js  c++  java
  • 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)

    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' 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("
    ");
        }
    
        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

    感谢原作者!

      欢迎关注微信公众号“ **IT客**“ ,投稿邮箱 itkeyy@163.com

  • 相关阅读:
    rand()和srand()关系很简单——一看就明白(通过一个可移植的源码)
    opencart配置mail服务
    dedecms mysql连接错误:#1040
    自动获取访客QQ
    apache虚拟目录设置
    在XAMPP上建立多个域名的站点
    QQ互联不能使用的通用解决方法
    织梦系统与discuz论坛整合方法
    DEDECMS整站复制
    DEDECMS模板中dede标签使用php和if判断语句的方法
  • 原文地址:https://www.cnblogs.com/simuhunluo/p/7469495.html
Copyright © 2011-2022 走看看