16-12-08 Freud Algorithm (Floyd)-Shortest path-фрейд__ algorithm

Source: Internet
Author: User

The shortest path problem with a single source point to all vertices in the weighted graph can be
Dijkstra algorithm http://blog.csdn.net/sodaoo/article/details/53495517

What if you ask for the shortest path between each vertex and the other vertices in the graph? The method that can be thought of is:
Each time with a vertex as the source point, repeated execution of the Jestra algorithm n times.
In this way, we can theoretically obtain the shortest path of each vertex and other vertices, and the total execution time is O (N3).
However, in order to achieve the beauty of the atmosphere of the needs of the alcoves, using: Freud algorithm

The basic idea of Floyd algorithm:
The shortest path from any node A to any node B is 2 possible, and 1 is directly from a to b,2 from a through several nodes X to B. So, let's assume that Dis (AB) is the shortest path from Node A to Node B, for each node x, we check whether dis (AX) + dis (XB) < dis (AB) is set up, and if so, prove that the path from a to X to B is shorter than the path of a direct to B. We then set up dis (AB) = Dis (AX) + dis (XB) so that when we go through all the nodes X,dis (AB), we record the distance from the shortest path of a to B.
But here we need to pay attention to the nesting sequence of loops, if you check all the nodes x in the most layer, then the result will be incorrect, why. Because of this it is premature to determine the shortest path I to J, and when there is a shorter path, it is no longer updated.

<<<<– code June –>>>>

 typedef int PATHMATRIX[MAXVEX][MAXVEX]; typedef int SHORTPATHTABLE[MAXVEX][MATRIX]; void
    Shortestpath_floyd (mgraph g,pathmatrix *p,shortpathtable *d) {int v,w,k; for (v. = 0; v < g.numvertexes; ++v) {for (w = 0; w < numvertexes; ++w) {/*matrix Some also do arc[][]  
                     , which means the adjacency matrix */(*D) [v][w] = g.matrix[v][w];         /* (*D) [V][w] is the value of the corresponding points between the weights * * (*P) [v][w] = w;
        /* Initialize p[][]*/}} for (k = 0; k < numvertexes; ++k) {for (v = 0; v < numvertexes; ++v) {for (w = 0; w < numvertexes ++w) {if (*d) [v][w] > (*D) [v][k] + (*d) K [W]) {/* If the vertex path with subscript k is shorter than the previous two-point path/* Sets the current two-point weight to a smaller one/* (*d) [V
                    ][W] = (*d) [v][k] + (*D) [k][w]; (*p) [V]  [W] = (*p) [v][k]; /* path is set to the vertex with subscript k//,}}} 


You may be a little confused, (0,1) + (1,2) > (0,2) You can understand that
But as ↑ 0-> 3 vertices of the graph, the middle spans several vertices, and how to ensure that the path to find is the shortest path.
Haha of course don't worry about that we found K at the most outer layer of circulation,
D[x][y] > d[x][0]+ d[0][y] will remain until the vertex associated with all d[x][0]+ d[0][y is traversed, at which point the shortest path close to Vertex 0 has been determined, (0,2) = 4; (0,4) = 5; it's natural to find (0 , 3) = 7;

for (k = 0; k < numvertexes; ++k)
    {for
        (v = 0; v < numvertexes; ++v)
        {for
            (w = 0; W < Numvertex Es ++w)
            {
                if (*d) [v][w] > (*D) [v][k] + (*D) [k][w]) {(*d) [v][w]  
                    = (*d) [v][k] + (*D) [k][w];
/* Then look at the placement of K V W and the corresponding relationship of the IF () statement, K must be placed on the most outer layer, and then v,w can be exchanged. (。 Not tested. )*/

So how do we get a specific shortest path from the path array of p? Take V0 to V8 as an example, trace the P array to find, p[0][8] = 1, get to go through the vertex V1, and then replace 1 0 get p[1][8] = 2, explain to go through the V2, and then 2 to replace 1 get p[2][8] = 4 ...
<<< Code >>>

printf ("The shortest path between vertices is as follows: \ n"); 
    For (v=0 v<g.numvertexes; ++v) 
    {for 
        (w=v+1; w<g.numvertexes; w++) 
        {
            printf (v%d-v%d Weight:%d ", V,w,d[v][w]);
            K=P[V][W];                /* Get the first path vertex subscript
            /printf ("Path:%d", v);    /* Print source point
            /while (K!=W)/                * If the path vertex subscript is not the endpoint *
                /{printf ("->%d", k);    * * Print path vertex
                /k=p[k][w];            /* Get the next path vertex subscript
            *
            /} printf ("->%d\n", W);    /* Print end
        /} printf ("\ n");
    }

All code
<<< all code June >>>

#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1 #define ERROR    0 #define TRUE 1 #define FALSE 0 #define Maxedge #define MAXVEX #define INFINITY 65535 typedef Status;
    /* Status is the type of function whose value is the function result status code, such as OK/typedef struct {int Vexs[maxvex];
    int Arc[maxvex][maxvex];
int numvertexes, numedges;

}mgraph;
typedef int PATHARC[MAXVEX][MAXVEX];

typedef int SHORTPATHTABLE[MAXVEX][MAXVEX];

    /* Widget Graph/void createmgraph (Mgraph *g) {int I, J; /* printf ("Please enter number of edges and vertices:");
    * * g->numedges=16;

    g->numvertexes=9;
    for (i = 0; i < g->numvertexes; i++)/* Initialization diagram/{g->vexs[i]=i;
        for (i = 0; i < g->numvertexes; i++)/* Initialize Graph/{for (j = 0; J < g->numvertexes; J +)
            {if (i==j) g->arc[i][j]=0;
        else g->arc[i][j] = g->arc[j][i] = INFINITY; }} g->arc[0][1]=1;
    g->arc[0][2]=5; 
    g->arc[1][2]=3; 
    g->arc[1][3]=7; 

    g->arc[1][4]=5; 
    g->arc[2][4]=1; 
    g->arc[2][5]=7; 
    g->arc[3][4]=2; 
    g->arc[3][6]=3;

    g->arc[4][5]=3;
    g->arc[4][6]=6; 
    g->arc[4][7]=9; 
    g->arc[5][7]=5; 
    g->arc[6][7]=2;

    g->arc[6][8]=7;


    g->arc[7][8]=4; for (i = 0, i < g->numvertexes; i++) {for (j = i; J < g->numvertexes; J + +) {g
        >arc[j][i] =g->arc[i][j]; }}/* Floyd algorithm to find the shortest path p[v][w and weighted length d[v][w] of each vertex v to the rest of the vertices in the Network Diagram G. 
    */void Shortestpath_floyd (Mgraph G, Patharc *p, shortpathtable *d) {int v,w,k; For (v=0 v<g.numvertexes; ++v)/* Initialize D and p */{for (w=0; w<g.numvertexes; ++w) {(*    D) [v][w]=g.arc[v][w];                /* D[v][w] value is the right value between the corresponding points * * (*p) [V][w]=w; /* Initialize P */}} for (K=0 k<g.numvertexes; ++k) {for (w=0; w<g.numverteXes; ++W) {for (v=0 v<g.numvertexes; ++v) {if (*d) [v][w]> (*d) [V][k] + (*D) [k][w]) {/* If the subscript to K vertex path is shorter than the original two-point path (*D) [v][w]= (*d) [v][k]+ (*d) [k][w];/*] the right value between the current two points
Set to smaller one/(*P) [v][w]= (*p) [v][k];/* Path is set to the vertex with subscript K]}} 
    int main (void) {int v,w,k; 

    Mgraph G; 
    Patharc P; Shortpathtable D;

    * * To find the shortest path to the rest of the point * * * createmgraph (&AMP;G); 

    Shortestpath_floyd (G,&AMP;P,&AMP;D); 
    printf ("The shortest path between vertices is as follows: \ n"); For (v=0, v<g.numvertexes; ++v) {for (w=v+1; w<g.numvertexes; w++) {printf ("v%d-v
            %d weight:%d ", v,w,d[v][w]);                K=P[V][W];    /* Get the first path vertex subscript/printf ("Path:%d", V);    /* Print source point/while (K!=W)/* If the path vertex subscript is not the endpoint */{printf ("->%d", k);  * * Print path vertex/k=p[k][w];          /* Get the next path vertex subscript */} printf ("->%d\n", W);
    /* Print end/} printf ("\ n");
    printf ("Shortest path d\n"); For (v=0 v<g.numvertexes; ++v) {for (w=0; w<g.numvertexes; ++w) {printf ("%d\t", D [V]
        [W]);
    printf ("\ n");
    printf ("Shortest path p\n"); For (v=0 v<g.numvertexes; ++v) {for (w=0; w<g.numvertexes; ++w) {printf ("%d", p[
        V][W]);
    printf ("\ n");
return 0; }

Acknowledgements:
htp://blog.sina.com.cn/s/blog_66d94d130101l8i5.html
HP://WWW.CNBLOGS.COM/BRAVELIU/ARCHIVE/2013/12 /05/3459768.html
htp://blog.csdn.net/wwj_ff/article/details/46560559
htp://blog.chinaunix.net/ uid-26548237-id-3834873.html

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.