Floyd algorithm template - Detailed

 

The map is not entitled to:

If from one vertex to another vertex exists a path, the path length is said that the number of edges on the path through which is equal to the number of vertices on the path is decremented.

Since from one vertex to another vertex there may be multiple paths, the number of edges through which each path may be different, i.e., different path lengths, (i.e., after the minimum number of edges) we have that path length of the shortest path is called shortest path, which is called the path length of the path length or the shortest distance.

 

For Figure weighted terms:

Consider all of the weight on the side of the path, the path length is usually the sum of weights for the path defined by the one side of said tape path or right path length.

 May be more than one path, the shortest path length with a right that path is called shortest path, the path length (sum of weights) is called a shortest distance or shortest path length from the source to the destination.

 

 

Floyd algorithm

Floyd algorithm (Floyd-Warshall  algorithm ) algorithm, also known as Floyd, interpolation point method is an algorithm to solve the shortest path between a given vertex in a weighted graph, we can correctly process the digraph or negative weight of the shortest routing problem , but is also used to calculate the closure of FIG transmitted. The algorithm name to one of the founders, in 1978, Turing Award winner, Professor of Computer Science at Stanford University Robert Floyd name.

Scope: no negative cycles can be, the right side can be positive or negative, can be obtained by running the algorithm once the shortest path between any two points.

 

Advantages and disadvantages:

Floyd algorithm is suitable for APSP (AllPairsShortestPaths), it is a dynamic programming algorithm , best, dense FIG right edge effect can be positive or negative. This algorithm is simple and effective, since the triple loop structure is compact, dense FIG respect, efficiency is higher than perform | V | times Dijkstra algorithm .

Advantages: readily appreciated, can calculate the shortest distance between any two nodes, simple coding

Disadvantages: time complexity is relatively high, not suitable for the large amounts of data.

时间复杂度:O(n^3);空间复杂度:O(n^2);

任意节点i到j的最短路径两种可能:

  1. 直接从i到j;
  2. 从i经过若干个节点k到j。

map(i,j)表示节点i到j最短路径的距离,对于每一个节点k,检查map(i,k)+map(k,j)小于map(i,j),如果成立,map(i,j) = map(i,k)+map(k,j);遍历每个k,每次更新的是除第k行和第k列的数。

步骤:

第1步:初始化map矩阵。
矩阵中map[i][j]的距离为顶点i到顶点j的权值;

如果i和j不相邻,则map[i][j]=∞。

如果i==j,则map[i][j]=0;                                          
第2步:以顶点A(假设是第1个顶点)为中介点,若a[i][j] > a[i][1]+a[1][j],则设置a[i][j]=a[i][1]+a[1][j]。

无向图构建最短路径长度邻接矩阵:

 

 

 

 

核心代码:

有向图构建最短路径长度邻接矩阵:

步骤:

 

 

 

 

 

 

 

 

 

 

核心代码:

Guess you like

Origin www.cnblogs.com/FengZeng666/p/11243714.html