Personal insights on Floyd's algorithm

Personal insights on Floyd's algorithm

  Because there is an algorithm homework to be handed in recently, I looked at the title and it was roughly the shortest path with weights. So I thought of Freud's algorithm, I went to look at Baidu, and then make a summary.
  If we go to travel, we definitely want to avoid the wrong way, then we have to know the shortest distance between any two places.
Insert picture description here
  There are eight roads to reach these four places. Please note that these roads are one-way. We now need to ask for the shortest distance between any two cities, that is, find the shortest path between any two points. This problem is also known as the " multi-source shortest path " problem.
  Now we need a two-dimensional array to store it, and we can create a 4*4 matrix. For example, the distance from city 1 to city 2 is 2, then e[1][2] is set to 2. City 2 cannot To reach city No. 4, set the value of e[2][4] to ∞. In addition, it is agreed here that a city is to its own and is also 0, for example, e[1][1] is 0, as follows. Insert picture description here
  We know that if you want to shorten the distance between two points, you only have to introduce a transit point (vertex k) to make the distance shorter (ie a->k->b). Sometimes it may take two transit points or more. To make the route shorter, that is, to introduce k1, k2, k3, etc. (ie a->k1->k2...->k->i...->b), for example, the original 4->3 route It is 12. If it becomes 4->1->3 after 1 passes, then the distance becomes 11 (ie e[4][1]+e[1][3]=5+6=11). So now turn the problem into a general situation.

  • Without passing through another transit point
    Insert picture description here

  •   What should I do if I pass through transit point 1 and only pass through transit point 1? Just judge whether e[i][1]+e[1][j] is smaller than e[i][j], e[i][j] means from vertex i to vertex j The distance between. e[i][1]+e[1][j] represents the sum of the distance from vertex i to vertex 1, and then from vertex 1 to vertex j. The code is implemented as follows.
for (i = 1; i <= n; i++)
{
    
    
     for (j = 1; j <= n; j++)
      {
    
    
      	  //循环是遍历整个矩阵,判断每个点经过1是否会变小
          if (e[i][j] > e[i][1] + e[1][j])
              e[i][j] = e[i][1] + e[1][j];
      }
}

After passing 1, the route table is updated as follows:
Insert picture description here
  Through the above figure, we can find that the distance from the three places marked in white to other points after passing 1 becomes shorter.


  •   What happens if we only pass 1 and 2 in sequence after passing the transit point 1 and 2? We need to determine whether the distance between vertex i and vertex j can be made shorter if we pass through vertex No. 2 under the result of allowing the shortest distance between any two points when passing vertex No. 1. That is, to determine whether e[i][2]+e[2][j] is smaller than e[i][j], the code is implemented as follows.
//经过1号顶点
for(i=1;i<=n;i++)
{
    
    
	for(j=1;j<=n;j++)
	{
    
    
		if (e[i][j] > e[i][1]+e[1][j])  e[i][j]=e[i][1]+e[1][j];
	}
}

//经过2号顶点
for(i=1;i<=n;i++)
{
    
    
	for(j=1;j<=n;j++)
	{
    
    
		if (e[i][j] > e[i][2]+e[2][j])  e[i][j]=e[i][2]+e[2][j];
	}
}

  In the case that only vertices 1 and 2 are allowed, the shortest distance between any two points is updated to:
Insert picture description here
  We can find that the previous 4->3 has changed to 11 after 1 (4->1->3), and now After 2 (4->1->2->3), it becomes 10,

  • The
      same applies to the transit points 1, 2, and 3, and the shortest distance between any two points is found under the condition that only vertices 1, 2, and 3 are allowed to transit. The shortest distance between any two points is updated to:
    Insert picture description here
  • After all the transfer points,
      all vertices are allowed to pass as a transfer. The final shortest distance between any two points is: the
    Insert picture description here
    whole process can actually be simplified into a few lines of code:
for(k=0; k<n ; k++){
    
     //简单的说就是更新二维矩阵,将任何一个点经过任何一个中间点之后的路程更新为最短的距离
                            //一句话概括就是:从i号顶点到j号顶点只经过前k号点的最短路程。
        for(i =0; i<n; i++){
    
    
        for(j=0; j<n; j++){
    
    
            if(e[i][j] > (e[i][k] + e[k][j])){
    
     
                e[i][j] = e[i][k] + e[k][j];
            }
        }
    }
    }

  The above is the shortest distance to any point after passing all the transit points. Simply put, it is the shortest distance from vertex i to vertex j through only the first k points.

Guess you like

Origin blog.csdn.net/qq_45125250/article/details/109791793