Data structure-understanding and implementation of the Freud FLOYD algorithm of the shortest path of the graph

The understanding of the Floyd FLOYD algorithm
It can realize the shortest path from all vertices to all vertices

But its time complexity is high, but the code is relatively concise (less)

First define an array D to record the weights between vertices,
and then define an array P to record the subsequent vertex values ​​of the vertices

for (v = 0; v < G.NumVer; ++v)
	{
    
    
		for (w = 0; w < G.NumVer; ++w)
		{
    
    
			(D)[v][w] = G.Arc[v][w];//记录权值
			(P)[v][w] = w;
		}
	}

Find the shortest path part

for (k = 0; k < G.NumVer; ++k)
	{
    
    
		for (v = 0; v < G.NumVer; ++v)
		{
    
    
			for (w = 0; w < G.NumVer; ++w)
			{
    
    
				if ((D)[v][w] > ((D)[v][k] + (D)[k][w]))
				{
    
    //k是一个中间过程  中间路径判断一下经过k时 会不会更新
					//否则将权值改为经过k时的路径
					//P保存该结点的后继  根据权值大小和终点选择
					(D)[v][w] = ((D)[v][k] + (D)[k][w]);
					(P)[v][w] = (P)[v][k];
				}
			}
		}
	}

k is an intermediate path to determine whether it will be updated when passing k

The entire code
is similar to
what was written before, but one unsolved problem is how to initialize it if you define an array of pointers? ? ?

#include<iostream>
#include <iomanip>
using namespace std;

typedef char Vertextype;//顶点类型
typedef int Edgetype;//边缘权值
typedef int Status;
#define MAXVER 9//最大顶点数
#define INF 65535//代表无穷
#define NULL 0

typedef int Pathmatirx[MAXVER][MAXVER];
typedef int ShortPathTable[MAXVER][MAXVER];

typedef struct Graph
{
    
    
	Vertextype Ver[MAXVER];
	Edgetype Arc[MAXVER][MAXVER];
	int NumVer, NumEdg;
}MGraph;

//生成图——邻接矩阵
Status CreatGraph(MGraph &G)
{
    
    
	int i, j, w;
	cout << "Please enter the number of verticesof the graph : " << endl;
	cin >> G.NumVer;
	cout << "Please enter the number of edges the graph : " << endl;
	cin >> G.NumEdg;
	cout << "Please enter the name of vex : " << endl;
	for (i = 0; i < G.NumVer; i++){
    
    
		//cout << "Please enter the NO." << i + 1 << "%d name of vex : " << endl;
		cin >> G.Ver[i];
	}

	cout << "Diagonal infinity ..." << endl;
	for (i = 0; i < G.NumVer; i++)
		for (j = 0; j < G.NumVer; j++)
		{
    
    
		G.Arc[i][j] = INF;//简单图  不循环
		}
	cout << "...Diagonal infinity" << endl;

	for (int k = 0; k < G.NumEdg; k++){
    
    //因为具体哪条边存在不一定 所以选择性输入边
		cout << "Enter the subscripts and weights from vertex vi to vertex vj : " << endl;
		cin >> i >> j >> w;
		/*cout << "Please enter the subscript j of the edge : " << endl;
		cin >> j;
		cout << "Please enter the weight from vertex "<<i<<" to vertex "<<j<<" : " << endl;
		cin >> w;*/

		G.Arc[i][j] = w;
		G.Arc[j][i] = G.Arc[i][j];//无向图  边的信息  是对称的  //有向图的话 无需设置

	}
	return 0;
}

Status ShortestPath_Floyd(MGraph &G, Pathmatirx &P,ShortPathTable &D)
{
    
    
	int v, w, k;
	
	//初始化
	for (v = 0; v < G.NumVer; ++v)
	{
    
    
		for (w = 0; w < G.NumVer; ++w)
		{
    
    
			(D)[v][w] = G.Arc[v][w];//记录权值
			(P)[v][w] = w;
		}
	}

	
	for (k = 0; k < G.NumVer; ++k)
	{
    
    
		for (v = 0; v < G.NumVer; ++v)
		{
    
    
			for (w = 0; w < G.NumVer; ++w)
			{
    
    
				if ((D)[v][w] > ((D)[v][k] + (D)[k][w]))
				{
    
    //k是一个中间过程  中间路径判断一下经过k时 会不会更小
					//否则将权值改为经过k时的路径
					//P保存该结点的前驱或者后继  根据权值大小和终点选择
					(D)[v][w] = ((D)[v][k] + (D)[k][w]);
					(P)[v][w] = (P)[v][k];
				}
			}
		}
	}
	return 0;
}

int main()
{
    
    
	MGraph G;
	Pathmatirx P;
	ShortPathTable D;
	CreatGraph(G);
	ShortestPath_Floyd(G,P,D);

	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/113835717