Floyd algorithm of shortest circuit

Source: Internet
Author: User

1. Introduction

Floyd algorithm only five lines of code, the code is simple, three for loop can solve the problem, so its time complexity is O (n^3), can find multi-source shortest path problem.

2. Thought:

The basic idea of the Floyd algorithm is as follows: from any node A to any node B the shortest path is 2 possible, 1 is directly from a to b,2 is from a through a number of nodes X to B. Therefore, we assume that dis (AB) is the shortest path distance from Node A to Node B, for each node x we check whether dis (AX) + dis (XB) < DIS (AB) is established, and if so, prove that the path from a to x and then to B is shorter than the path of a directly to B, We set up dis (AB) = Dis (AX) + dis (XB) so that when we go through all the nodes X,dis (AB), the distance from the shortest path of A to B is recorded.

As an example: known,

If now only allowed through the 1th vertex, the shortest distance between any two points, just Judge 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 vertex J. Where I is the 1~n loop, and J is also the 1~n loop, the code is implemented as follows.

 for (i=1; i<=n; i++) {    for (j=1; j<=n; j + +)    {        if  (E[i][j] > e[i][1]+e[1][j])             = e[i][1]+e[1][j];}    }

Next, continue to ask for the shortest distance between any two points that only allow two vertices of 1 and 2nd. In the result of allowing only the shortest distance of any two points through the 1th vertex, it is also possible to determine whether the distance between vertex I and the J vertex can be shortened if the number 2nd vertex is passed. That is to judge E[i][2]+e[2][j] is smaller than e[i][j], the code is implemented as follows.

//after vertex number 1th 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];//after vertex number 2nd 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];

Finally, all vertices are allowed to be brokered, and the code is as follows:

 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];

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. Same as above

3. Code templates:

#include <stdio.h>#defineINF 0x3f3f3f3fintmap[ +][ +];intMain () {intk,i,j,n,m; //reads N and m,n to indicate the number of vertices, m represents the number of edgesscanf"%d%d",&n,&m); //Initialize     for(i=1; i<=n; i++)         for(j=1; j<=n; J + +)            if(i==j) Map[i][j]=0; ElseMap[i][j]=inf; intA,b,c; //Read in Edge     for(i=1; i<=m; i++) {scanf (" %d%d%d",&a,&b,&c); MAP[A][B]=c;//This is a graph with a direction    }    //Floyd-warshall algorithm Core statement     for(k=1; k<=n; k++)         for(i=1; i<=n; i++)             for(j=1; j<=n; J + +)                if(map[i][j]>map[i][k]+Map[k][j]) map[i][j]=map[i][k]+Map[k][j]; //outputs The final result, the final two-dimensional array, even if the shortest distance between two points     for(i=1; i<=n; i++)    {         for(j=1; j<=n; J + +) {printf ("%10d", Map[i][j]); } printf ("\ n"); }    return 0;}

Floyd algorithm of shortest circuit

Related Article

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.