Multi-source Shortest path--floyd-warshall algorithm

Source: Internet
Author: User

Any two-point shortest path is called a multi-source shortest path, that is, given any two points, a starting point, a point of arrival, to find the shortest path between the two points, is any two-point shortest path problem, multi-source shortest path, and the Floyd-warshall algorithm is the simplest, only 5 lines of code, you can solve the problem.


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 called the "multi-source Shortest path" problem.


Now we need an adjacency matrix to store the information of the graph, where we can store it with a 4*4 matrix (two-dimensional array e). 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:


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->k2->b or a->k1->k2...->ki->...->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. Well, this process requires only 5 lines of code to implement:

Floyd-warshall algorithm for (int k = 0, K < vertexnum; k++) for (int i = 0, i < vertexnum; i++) for (int j = 0; J < verte Xnum; J + +) if (Matrix[i][j]>matrix[i][k] + matrix[k][j]) matrix[i][j] = Matrix[i][k] + matrix[k][j];

If you learn single source shortest path, then the multi-source shortest path should be very simple. It is important to note that the k,i,j order of the above algorithms cannot be reversed. Because in this algorithm "matrix[i][j] = Matrix[i][k] + matrix[k][j]" A hidden premise is that matrix[i][k] and matrix[k][j] must be minimal, so k must be placed in the first loop.


Another point of note floyd-warshall algorithm can solve the negative weight graph, but can not solve the "negative weight loop" (or called "Negative weight Ring") diagram. Because there is no shortest path to a graph with a "negative weight loop". 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. See:


The complete code is affixed below:

#include <iostream> #include <iomanip> #define INF 10001//assumed weight is less than 100using namespace Std;int matrix[10][10] ;//Assuming no more than 10 vertices, the adjacency matrix stores int main () {/*4 1 2, 3 2, 0 3, 0, 2, 12*/for (int i = 0; i <; i++)//Initialize {for (i NT J = 0; J < 10; J + +) {if (i = = j) Matrix[i][j] = 0;elsematrix[i][j] = INF;}} int Vertexnum, sidenum;//vertex number and number of sides int x, y, w;//edge information and weights cout << "Please enter the number of vertices and sides:"; Cin >> Vertexnum >> Sidenum;co  UT << "\ n Please enter" << sidenum << "information and weights for the sidebar: \ n"; for (int i = 0; i < Sidenum; i++) {cin >> x >> y >> W;matrix[x][y] = w;//graph}//floyd-warshall algorithm for (int k = 0; k < vertexnum; k++) for (int i = 0; i < VERTEXNU M i++) for (int j = 0; J < Vertexnum; J + +) if (Matrix[i][j]>matrix[i][k] + matrix[k][j]) matrix[i][j] = matrix[i][k] + ma trix[k][j];//output cout << "\ nthe output is: \ n", for (int i = 0; i < Vertexnum; i++) {for (int j = 0; J < Vertexnum; J + +) C Out << left << SETW (4) << matrix[i][j];cout << Endl;} return 0;}

The output is:









Photo Resources from: http://blog.csdn.net/turingbooks/article/details/28635933


Multi-source Shortest path--floyd-warshall algorithm

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.