Floyd Shortest path algorithm with only five elements

Source: Internet
Author: User

Summer vacation, Xiao Hum prepares to go to some city to travel. Some cities have highways, and some cities do not, as shown below. To save money and facilitate planned journeys, Xiao Hum hopes to know the shortest distance before any two cities before leaving.

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 now need to find the shortest distance 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 need a data structure to store the information of the graph, we can still store it with a 4*4 matrix (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 is to own is also 0, for example E[1][1] for 0, as specified below.

Now back to the question: how do you find the shortest path between any two points? Through previous studies we know that the shortest path between two points can be found by depth or breadth-first search. Therefore, N2 depth or breadth-first search, that is, every two points for a depth or breadth first search, you can get any two points between the shortest path. But is there any other way?

Let's think about it, according to our past 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 the vertex k to the a->k->b, which may reduce the distance from vertex a to vertex b. So where is the vertex k of the relay in 1~n? Sometimes it's not even through a single point, but it goes through two or more points, which is shorter, a->k1->k2b-> 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] was originally 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 any two points are not allowed to pass through a third point, the shortest distance between these cities is the initial distance, as follows.

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.