Floyd-warshall Algorithm

Source: Internet
Author: User

The Floyd-warshall algorithm is used to locate the shortest distance between each point. It needs to store edges using an adjacent matrix. This algorithm obtains the optimal path by considering the best sub-path.

Note that the path of an edge is not necessarily the best path.

  • Start with any single-side path. The distance between all two points is the edge weight, or an infinite number. If there is no EDGE connection between the two points.
  • For each pair of vertices u and v, check whether there is a vertex W which makes the path from u to W and then to V shorter than the known path. Update it.
  • What is incredible is that you can get the result as long as you select the appropriate order.
// Dist (I, j) is the shortest path from node I to node J. For I do 1 to n do for J do 1 to n do dist (I, j) = weight (I, j) for k then 1 to n do // K is the "Media node" for I then 1 to n do for J then 1 to n do if (Dist (I, K) + dist (K, j) <dist (I, j) Then // is it a shorter path? Dist (I, j) = dist (I, K) + dist (K, J)

The efficiency of this algorithm is O (V3 ). It needs an adjacent matrix to store graphs.
This algorithm is easy to implement with just a few lines.

Even if the problem is to find the single-source shortest path, we recommend that you use this algorithm. If the time and space are allowed (as long as there is space in the bottom adjacent matrix, there is no problem in time ).

Sample Code:

# Include <stdio. h>
# Define NV 5
# Define Ma 9999999

Int main (void)
{
Int graph [NV] [NV] =
{
0, 1, 7, 2, 8,
1, 0, 3, 2, 2,
7, 3, 0, 4, 4,
2, 2, 4, 0, 3,
8, 2, 4, 3, 0
};
Int num = NV;
Int path [NV] [NV];
Int I, J, K;
For (I = 0; I <num; I ++ ){
For (j = 0; j <num; j ++)
Printf ("% d", graph [I] [J]);
Printf ("");
}
For (I = 0; I <num; I ++ ){
For (j = 0; j <num; j ++ ){
Path [I] [J] = graph [I] [J];
}
}

For (k = 0; k <num; k ++)
For (I = 0; I <num; I ++)
For (j = 0; j <num; j ++)
If (path [I] [J]> graph [I] [k] + graph [k] [J])
Path [I] [J] = graph [I] [k] + graph [k] [J];
For (I = 0; I <num; I ++)
{For (j = 0; j <num; j ++)
Printf ("shortest distance between <% d, % d>: % d", I + 1, J + 1, path [I] [J]);
}
Return 0;
}

 

 

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.