(Floyd) entry Floyd algorithm

This algorithm is in fact carried out based on the vision of a dynamic programming algorithm (similar to 01 backpack)
part of the code reference and ideas from the shortest path algorithm

Set Di, j, k from i to j only to (1 ... k) is the set of nodes in the shortest path length of the intermediate node, then in accordance with the thinking that the backpack 01 1. J from the vertex to the vertex I is not after the K, it is Dijk-1,2. If that after k, then put the two vertices split into Dikk-1 + Djkk-1 two portions
values and then use the three cycles, to find the corresponding.
Finally, three-dimensional array can be turned into a two-dimensional array

Three-dimensional array form:

void floyd()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			dis[i][j][0]=map[i][j];
	for(int k=1;k<=n;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				dis[i][j][k]=dis[i][j][k-1];
				if(dis[i][j][k]>dis[i][k][k-1]+dis[k][j][k-1])
					dis[i][j][k]=dis[i][k][k-1]+dis[k][j][k-1];
			}
} 

Two-dimensional array form:

  for(int k =1 ;  k <= n ; k ++ ){
      for(int i =1 ; i<= n ; i++){
          for(int j =1 ;j<=n;j++){
                 dist[ i ][ j ]= min( dist[ i ][ j ],dist[ i ][ k ]+dist[ k ][ j ] );      
            }
       }
  }

To conclude, Floyd algorithm is suitable for multiple origins and multiple end, but Dykstra method is more suitable only start and end points.

Published 72 original articles · won praise 5 · Views 2821

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/104878874