"Ah ha! Algorithm "algorithm 6: Floyd shortest path algorithm with only five elements

Source: Internet
Author: User

summer vacation, Xiao Hum ready to go to some city tour. Some cities have highways, and some cities do not, such as. To save money and make it easier to plan your trip, Xiao Hum hopes to know the shortest distance before departing any two cities before departure.

There are 8 highways in 4 cities, and the numbers on the highways indicate the length of the road. Please note that these highways are one-way. We now need to find the shortest distance between any two cities, that is, the shortest path between any two points. This problem is also known as the "multi-source Shortest path" problem. Now we need a data structure to store the information of the graph, we can still use a 4*4 matrix (two-dimensional array e) to store. For example, City 1th to City 2nd is 2, then set e[1][2] to a value of 2. City No. 2nd cannot reach City 4th, the value of setting e[2][4] is ∞. In addition here the Convention a city itself is to own also 0, for example E[1][1] for 0, specifically as follows. <ignore_js_op>now back to the question: how to find the shortest path between any two points? Through previous studies we know that the shortest path between two points can be found through depth or breadth-first search. So n2 the depth or breadth of the first search, that is, every two points of a depth or breadth first search, you can find any two points between the shortest path. But is there any other way?

Let's think about it , according to our previous experience, if you want to shorten the distance between any two points (for example, from vertex A to vertex B), you can only introduce a third point (vertex K) and pass through this vertex K relay, which is a->k->b, which may reduce the original distance from vertex A to vertex b. So what is the point in the 1~n that this relay vertex k is? Sometimes it is even shorter to pass through two or more points, not just through a point, a->k1->k2b-> or a->k1->k2...->k->i...->b. For example, the distance from city 4th to City 3rd (4->3) e[4][3] was originally 12. If you only pass through City 1th (4->1->3), the journey will be shortened to one (e[4][1]+e[1][3]=5+6=11). In fact, City 1th to 3rd City can also transit through the city of 2nd, making the 1th to 3rd city journey shortened to 5 (e[1][2]+e[2][3]=2+3=5). So if you go through two cities at 1th and 2nd, the distance from city 4th to City 3rd will be shortened to 10. With this example, we find that each vertex has the potential to shorten the distance between the other two vertices. OK, let's generalize this question. when a third point is not allowed between any two points, the shortest distance between these cities is the initial distance, as follows.

<ignore_js_op>What if the shortest distance between any two points is allowed to go through only 1th vertices now? Just determine if e[i][1]+e[1][j] is smaller than e[I][j]. e[I][j] represents the distance from the I vertex to the J number vertex. e[I][1]+e[1][j] represents the sum of the distances from vertex i to number 1th, and from vertex number 1th to the vertex of J. Where I is the 1~n loop, and J is also the 1~n loop, the code is implemented as follows.
    1. for (i=1;i<=n;i++)
    2. {
    3. for (j=1;j<=n;j++)
    4. {
    5. if (E[i][j] > E[i][1]+e[1][j])
    6. E[I][J] = E[i][1]+e[1][j];
    7. }
    8. }
Copy Codein cases where only 1th vertices are allowed, the shortest distance between any two points is updated to:

We found that the distances from vertex 3rd to vertex 2nd (e[3][2]), vertex 4th to vertex 2nd ( e[4][2]), and 4th vertices to 3rd vertices (e[4][3]) were shortened when only the 1th vertices were brokered.

Next, continue to ask for the shortest distance between any two points that only allow two vertices of 1 and 2nd. How to do it? We need to be able to take the shortest distance of any two points when only the 1th vertex is allowed, and then determine if passing the vertex number 2nd will make the distance between the I vertex and the J vertex shorter. That is to judge e[I][2]+e[2][j] is smaller than e[I][j], the code is implemented as follows.
    1. After vertex number 1th
    2. for (i=1;i<=n;i++)
    3. for (j=1;j<=n;j++)
    4. if (E[i][j] > E[i][1]+e[1][j]) e[i][j]=e[i][1]+e[1][j];
    5. After vertex number 2nd
    6. for (i=1;i<=n;i++)
    7. for (j=1;j<=n;j++)
    8. if (E[i][j] > E[i][2]+e[2][j]) e[i][j]=e[i][2]+e[2][j];
Copy Code

in cases where only 1 and 2nd vertices are allowed, the shortest distance between any two points is updated to:by knowing that, when compared to only allowing transit through the number 1th vertices, this allows the transfer through vertices 1 and 2nd, making e[1][3] and e[4][3] shorter distances. in the same vein, the shortest distance between any two points is continued in cases where only 1, 2, and 3rd vertices are allowed to be brokered. The shortest distance between any two points is updated as follows:Finally, all vertices are allowed as a transit, and the final shortest distance between any two points is:the entire algorithm process, though cumbersome, but the code implementation is very simple, the core code only five elements:
    1. for (k=1;k<=n;k++)
    2. for (i=1;i<=n;i++)
    3. for (j=1;j<=n;j++)
    4. if (E[i][j]>e[i][k]+e[k][j])
    5. E[I][J]=E[I][K]+E[K][J];
Copy Code The basic idea of this code is that it is only allowed to go through the number 1th vertices at first, and then only the 1 and 2nd vertices are allowed to relay ... The shortest distance between any two points is allowed through all vertices of the 1~n number. In a nutshell: From the I vertex to the J vertex only passes the shortest distance from the front K points. In fact, this is a "dynamic planning" of the idea, about this idea we will be in the "Aha!" The algorithm 2--the great thinking shines in a detailed discussion. The complete code for this algorithm is given below:
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. int e[10][10],k,i,j,n,m,t1,t2,t3;
  5. int inf=99999999; Use INF (infinity abbreviation) to store a positive infinity value we think
  6. Reads N and m,n to indicate the number of vertices, m represents the number of edges
  7. scanf ("%d%d", &n,&m);
  8. Initialization
  9. for (i=1;i<=n;i++)
  10. for (j=1;j<=n;j++)
  11. if (i==j) e[i][j]=0;
  12. else E[i][j]=inf;
  13. Read in Edge
  14. for (i=1;i<=m;i++)
  15. {
  16. scanf ("%d%d%d", &T1,&T2,&T3);
  17. E[T1][T2]=T3;
  18. }
  19. Floyd-warshall Algorithm Core statement
  20. for (k=1;k<=n;k++)
  21. for (i=1;i<=n;i++)
  22. for (j=1;j<=n;j++)
  23. if (E[i][j]>e[i][k]+e[k][j])
  24. E[I][J]=E[I][K]+E[K][J];
  25. Output the final result
  26. for (i=1;i<=n;i++)
  27. {
  28. for (j=1;j<=n;j++)
  29. {
  30. printf ("%10d", E[i][j]);
  31. }
  32. printf ("\ n");
  33. }
  34. return 0;
  35. }
Copy Code

One thing to note is: How to represent positive infinity. We typically define positive infinity as 99999999, because so even if two positive infinity is added, its sum still does not exceed the range of type int (the maximum positive integer that the C language int type can store is 2147483647). In practical applications it is best to estimate the upper limit of the shortest path, just set a bit larger than it can be. For example, with 100 edges and no more than 100 on each side, just set the positive infinity to 10001. If you think positive infinity and other values add up to a number greater than the positive infinity is not allowed, we only need to add two judgment conditions when comparing, please note the underlined statement in the following code.
    1. Floyd-warshall Algorithm Core statement
    2. for (k=1;k<=n;k++)
    3. for (i=1;i<=n;i++)
    4. for (j=1;j<=n;j++)
    5. if (e[i][k]<inf && e[k][j]<inf && e[i][j]>e[i][k]+e[k][j])
    6. E[I][J]=E[I][K]+E[K][J];
Copy Code

the input data style for the above code is:
    1. 4 8
    2. 1 2 2
    3. 1 3 6
    4. 1 4 4
    5. 2 3 3
    6. 3 1 7
    7. 3 4 1
    8. 4 1 5
    9. 4 3 12
Copy CodeThe first row of two numbers is N and M,n represents the number of vertices, and m represents the number of edges. The next m line, each row has three numbers T1, T2, and T3, indicating that the vertex t1 to the vertex t2 the distance is T3. get the final result as follows:

by this method we can find the shortest path between any two points. Its time complexity is O (N3). What is shocking is that it has only five lines of code, it is very easy to achieve. Precisely because it is very easy to implement, if time complexity is not required, it is possible to use Floyd-warshall to specify the shortest path between two points or to specify a point to the rest of each vertex. Of course there are faster algorithms, see the next section: Dijkstra algorithm. It is also important to note that the Floyd-warshall algorithm cannot solve a graph with a "negative weight loop" (or "negative weight loop") because there is no shortest path with a "negative weight loop" diagram. For example, the following figure does not have the shortest path from vertex number 1th to vertex 3rd. Because 1->2->3->1->2->3->...->1->2->3 such a path, each time around a 1->-2>3 such a ring, the shortest path will be reduced by 1, never find the shortest way. In fact, if a figure with a "negative weight loop" then this diagram is not the shortest way. <ignore_js_op>This algorithm was published by Robert W. Floyd (Robert Floyd) in 1962 on "Communications of the ACM". That same year Stephen Warshall (Stephen Vauchers) also published the algorithm independently. Robert W.. Floyd This cow man is a wonderful, he originally read the literature at the University of Chicago, but because the American economy is not very prosperous, find a job more difficult, helpless to Westinghouse Electric Company when a computer operator, in the IBM650 room night, and thus began his computer career. In addition, he and J.W.J Williams in 1964 jointly invented the famous heap sorting algorithm heapsort. Heap Sorting algorithm We will study in the seventh chapter. Robert W.. Floyd won the Turing Award in 1987.



code word is not easy ah, reprint trouble Annotated Source
"One-week algorithm" algorithm 6: Floyd shortest path algorithm with only five elements
http://bbs.ahalei.com/thread-4554-1-1.html

"Ah ha! Algorithm "algorithm 6: Floyd shortest path algorithm with only five elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.