Freud algorithm-----Shortest Path Algorithm (i) __ algorithm

Source: Internet
Author: User

Learn the reason for this algorithm: Yesterday afternoon stroll, met the girlfriend is looking at the algorithm, suddenly asked me will not Freud algorithm. I agreed to, and then spent half an hour to learn the algorithm, and 5 minutes to listen to her, and also to share with you in need of friends, so that you in the shortest possible time, a thorough grasp of the algorithm.


Robert W. Floyd (Robert Floyd) published the algorithm in the "Communication of the ACM" in 1962, which was published independently by Stephen Warshall (Steven Washel) in the same year. The Freudian algorithm can correctly deal with the shortest path problem of directed graph or directed graph or negative weight (but not the negative power loop), and is also used to compute transitive closures of directed graphs.


Since it is the shortest path algorithm, let us first look at an example.


There are 4 cities and 8 highways in the figure above, and the number on the highway indicates the length of the road. Please note that these highways are one-way. We need to find the shortest path between any two cities, that is, to find the shortest path between any two points. This problem is also referred to as the "multiple Source Shortest path" problem.


Now we store the information on the graph using a matrix (4*4, two-dimensional array e). For example, the route 1th to City 2nd is 2, and the value of e[1][2] is 2. City No. 2nd cannot reach City 4th, the value of setting e[2][4] is ∞. In addition here the agreement of a city itself to its own is also 0, for example E[1][1] for 0, as specified below.


Now back to the question: How to use this algorithm to find the shortest path between any two points.

Let's think about it, according to our past experience, if you want to shorten the path between any two points (for example, from vertex A to vertex B), you can only introduce a third point (vertex K) and pass the vertex k to the a->k->b, which may reduce the distance from vertex a to vertex b. So the vertex k of the relay is which point in the 1~n. Sometimes it's not even through a single point, but it goes through two or more points, which is shorter, a->k1->k2->b or a->k1->k2->...->k->i...->b. For example, the distance from the city of 4th to the city of 3rd (4->3) in the previous picture E[4][3] is 12. If only through the city of No. 1th Transit (4->1->3), the distance will be shortened to one (e[4][1]+e[1][3]=5+6=11). In fact, the city of No. 1th to the 3rd city can also be transit through the city of 2nd, making 1th to 3rd city's distance reduced to 5 (e[1][2]+e[2][3]=2+3=5). So if you pass the 1th and 2nd two cities, the distance from city 4th to City 3rd will be further shortened to 10. With this example, we find that each vertex is likely to make the distance between the other two vertices shorter. OK, let's generalize this question below.

When the third point is not allowed between any two points, the shortest distance between these cities is the initial distance, as follows


If now only allowed to pass through the vertices of No. 1th, to find the shortest distance between any two points, how should be asked. Just judge if E[I][1]+E[1][J] is smaller than e[i][j. E[I][J] represents the distance from the vertex of I to the vertex of J. E[I][1]+E[1][J] Represents the sum of the distances from vertex i to 1th, then from Vertex 1th to the vertex of J. Where I is the 1~n loop, J is also the 1~n loop, the code is implemented as follows.


In cases where only 1th vertices are allowed, the shortest path between any two points is updated to:

From the above figure we found that: in the case of only the 1th vertex relay, the 3rd vertex to the 2nd vertex (e[3][2)), 4th Vertex to 2nd vertex (E[4][2]) and 4th Vertex to 3rd Vertex (e[4][3) the distance is shortened.

Then continue to ask for the shortest distance between any two points in the case where only 1 and 2nd two vertices are allowed. How to do it. We need to be able to determine whether the distance between vertex I and J can be shortened by 2nd vertices If the result of a minimum distance of two points is allowed to pass through the vertices of number 1th. That is, to determine if E[I][2]+E[2][J] is smaller than e[i][j, the code implementation is as follows.

Pass 1th Vertex 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    ]; Pass 2nd Vertex 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 cases where only 1 and 2nd vertices are allowed, the shortest distance between any two points is updated to:

It is shown in the diagram above that the distance between the e[1][3 and E[4][3] is made shorter by allowing the relay through the vertices 1 and 2nd, compared to only allowing the relay to pass through the vertices of number 1th.

In the same way, continue to be allowed to pass only 1, 2 and 3rd points of the relay, the shortest distance between any two point. The shortest distance between any two points is updated to:

Finally, all vertices are allowed to be used as relays, and the shortest distance between any two points is:

Although the whole algorithm process is cumbersome to say, but the code implementation is very simple, the core code only five elements:

for (k=1;k<=n;k++) to (i=1;i<=n;i++) for (j=1;j<=n;j++) if (e[i][j]>e[i][k]+e[k][j)) e[i ][J]=E[I][K]+E[K][J];

The basic idea of this code is that it is only allowed to go through the 1th vertex to the relay, and then only allow the 1 and 2nd vertices to relay ... Allow the 1~n number of all vertices to relay, for any two points between the shortest distance. In a nutshell is: from the I vertex to the J number of vertices only through the first k point of the shortest distance. In fact, this is a "dynamic planning" of the idea, about this idea we will be in the "aha." Algorithm 2--great thinking shines when the detailed discussion. The complete code for this algorithm is given below:

#include  <stdio.h>    Int main ()     {        int  e[10][10],k,i,j,n,m,t1,t2,t3;        int inf=99999999; //inf ( Infinity) stores a positive Infinity value         //reads N and m,n to denote the number of vertices, m represents the number of edges        &NBSP;&NBSP;SCANF ("%d %d", &n,&m);           //initialization         for (i=1;i<=n;i++)      for (j=1;j<=n;j++)           if (i==j)  e[i][j]=0;    else e[i][j]=inf;         //read in         for (i=1;i<=m;i++)         {     &NBSP;SCANF ("%d %d %d", &t1,&t2,&t3);     e[t1][t2]=t3;        }           //floyd-warshall algorithm core statement &nbsP       for (k=1;k<=n;k++)      for (i=1;i<=n;i++)           for (j=1;j<=n;j++)       if (e[i][j]>e[i][k]+e[k][j] )           e[i][j]=e[i][k]+e[k][j];            //output Final results         for (i=1;i<=n;i++)         {          for (j=1;j<=n;j++)      {          printf ("%10d", E[i][j]);     }     printf ("\ n");         }           return 0;   } 

One thing to note: How to represent positive infinity. We usually define positive infinity as 99999999, because this way even if two positive infinity is added, and 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, only need to set a little larger than it can. For example, if you have 100 edges, each side does not exceed 100, you can simply set the positive infinity to 10001. If you think that positive infinity and other values add up to a number that is greater than positive infinity is not allowed, we just need to add two criteria when we compare it, please note the underlined statement in the following code.

Floyd-warshall algorithm core statement for (k=1;k<=n;k++) to (i=1;i<=n;i++) for (j=1;j<=n;j++) if (e[i][k]<i NF && e[k][j]<inf && e[i][j]>e[i][k]+e[k][j]) e[i][j]=e[i][k]+e[k][j];

The input data style for the above code is:

4 8 1 2 2 1 3 6 1 4 4 2 3 3 3 1 7 3 4 1 4 1 5 4 the 3 12

The first row two numbers n and m,n represent the number of vertices, and m represents the number of edges.

Next to line m, each row has three numbers T1, T2, and T3, representing the T2 of the vertex t1 to the vertex T3.

The final results are as follows:

In this way, we can find the shortest path between any two points. Its time complexity is O (N3). It is very shocking that it has only five elements of code, it is very easy to achieve. Precisely because it is very easy to implement, if the time complexity requirements are not high, it is also possible to use Floyd-warshall to find the shortest path between the specified two points or to specify a point to the rest of the vertices. Of course there are faster algorithms, please see the next section: Dijkstra algorithm.

Also need to note is: Floyd-warshall algorithm can not solve with "negative power loop" (or "negative") of the graph, because there is a "negative-weight loop" of the figure does not have the shortest path. For example, the following figure does not have the shortest path between vertices 1th and 3rd. Because 1->2->3->1->2->3->...->1->2->3 such a path, every time around 1->-2>3 such a ring, the shortest road will be reduced by 1, never find the shortest way. In fact, if there is a "negative-weight loop" in the diagram, then there is no shortest path.

This algorithm was published by Robert W. Floyd (Robert Floyd) in 1962 on the "Communications of the ACM". In the same year, Stephen Warshall (Stephen Vauchers) also published the algorithm independently. Robert W. Floyd This cow is a wonderful flower, he was originally at the University of Chicago to read literature, but because the U.S. economy was not very good, looking for a job more difficult, but in the West House Electric Company when a computer operator, in the IBM650 room night shift, and thus began his computer career. In addition, he and J.w.j Williams (Williams) in 1964 jointly invented the famous heap sorting algorithm heapsort. Heap Sort algorithm We will study in the seventh chapter. Robert W. Floyd won the Turing Prize in 1987.



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.