Floyd算法(多源最短路径问题)

简介: Floyd算法(多源最短路径问题)

Floyd算法


弗洛伊德算法,常用于多源最短路径问题,即求每对顶点间的最短路径



/*  
有权图的多源最短路径问题
*/
#include <iostream>
#include <queue>
using namespace std;
#define MaxVertexNum 10
#define infinity 1e9
struct Graph
{
    int edgenum;
    int vertexnum;
    int edgeList[MaxVertexNum][MaxVertexNum];
    int path[MaxVertexNum][MaxVertexNum]; // 前驱结点
    int dist[MaxVertexNum][MaxVertexNum]; // 从源点到其他各顶点当前最短路径长度
};
void BuildGraph(Graph *G)
{
    int start, end;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的权重初始化
    for (int i = 1; i <= G->vertexnum; i++)
    {
        for (int j = 1; j <= G->vertexnum; j++)
        {
            if (i == j)
            {
                G->edgeList[i][j] = 0;
            }
            else
            {
                G->edgeList[i][j] = infinity;
            }
            G->dist[i][j] = G->edgeList[i][j];
            G->path[i][j] = -1;
        }
    }
    // 输入权重信息
    for (int i = 1; i <= G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end;
        cin >> G->edgeList[start][end];
        G->dist[start][end] = G->edgeList[start][end];
    }
}
void Floyd(Graph *G)
{
    for (int k = 1; k <= G->vertexnum; k++)
    {
        for (int i = 1; i <= G->vertexnum; i++)
        {
            for (int j = 1; j <= G->vertexnum; j++)
            {
                if (G->dist[i][k] + G->dist[k][j] < G->dist[i][j])
                {
                    G->dist[i][j] = G->dist[i][k] + G->dist[k][j];
                    G->path[i][j] = k;
                }
            }
        }
    }
}
int main()
{
    Graph G;
    BuildGraph(&G);
    Floyd(&G);
    cout << "edge" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            if (G.edgeList[i][j] != infinity)
            {
                printf("%10d", G.edgeList[i][j]);
            }
            else
            {
                printf("%10s", "infinity");
            }
        }
        cout << endl;
    }
    cout << "distance" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            cout << G.dist[i][j] << " ";
        }
        cout << endl;
    }
    cout << "path" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            cout << G.path[i][j] << " ";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}
/*  
4 8
1 4 4
4 1 5
4 3 12
3 4 1
1 3 6
3 1 7
1 2 2
2 3 3
*/

可以这么理解,将第一层循环的 k 看成是中转点,嵌套的2层循环里得到的 i,j 一对顶点,求的就是 i 到 j 的距离。判断从 i 出发经由 k 再到 j 的路径是否比当前储存的路径短,然后更新。

目录
相关文章
|
3月前
|
算法
最短路之Floyd算法
最短路之Floyd算法
32 1
|
4月前
|
算法
Floyd 最短路径【学习算法】
Floyd 最短路径【学习算法】
30 0
|
5月前
|
算法
Floyd算法的应用
Floyd算法的应用
32 0
|
6月前
|
算法 C# C++
C++算法:多源最短路径的原理及实现
C++算法:多源最短路径的原理及实现
|
8天前
|
算法 定位技术 Windows
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
13 4
|
1月前
|
人工智能 算法 Java
【算法设计与分析】— —单源最短路径的贪心算法
【算法设计与分析】— —单源最短路径的贪心算法
32 0
|
8月前
|
存储 算法 定位技术
探索最短路径问题:寻找优化路线的算法解决方案
在现实生活中,我们常常面临需要找到最短路径的情况,如地图导航、网络路由等。最短路径问题是一个关键的优化问题,涉及在图中寻找两个顶点之间的最短路径,以便在有限时间或资源内找到最快的方式。本文将深入探讨最短路径问题的定义、经典算法以及实际应用,为您揭示一种重要的算法解决方案。
159 0
|
4月前
|
算法
floyd算法
floyd算法
|
6月前
|
算法 测试技术 C#
C++算法:存在负权边的单源最短路径的原理和实现
C++算法:存在负权边的单源最短路径的原理和实现
|
7月前
|
机器学习/深度学习 传感器 算法
【WSN】基于蚁群算法的WSN路由协议(最短路径)消耗节点能量研究(Matlab代码实现)
【WSN】基于蚁群算法的WSN路由协议(最短路径)消耗节点能量研究(Matlab代码实现)