Bellman-ford algorithm of Shortest path algorithm for graphs

Source: Internet
Author: User

The Bellman-ford algorithm was invented by American mathematician Richard Bermain (Richard Bellman, the author of Dynamic planning) and small Lester Ford (Lester Ford).

Scope of application:
-direction graph, no direction diagram (need to repeat edge 2 times);
That is, for the side w (U, v), stored 2 times: w (u,v), W (v,u);
-Suitable for a fixed point, the shortest path to other points to solve;

The algorithm roughly describes:
1. Initialize all points, using the Dis[i] array to store the root point to the shortest path of I point;
2. Dis[root] is 0, the other dis[i] is initialized with the maximum value;
3. Traverse all sides W (u,v), or replace if dis[u]+w (UV) is less than the current DIS[V];
4. Pre[i] Save the access path;

#include <iostream>#include<cstdio>using namespacestd;#defineMAX 0x3f3f3f3f#defineN 1010intNodenum, Edgenum, original;//point, side, beginningtypedefstructEdge//side{    intu, v; intCost ;} Edge; Edge Edge[n];intDis[n], pre[n];BOOLBellman_ford () { for(inti =1; I <= nodenum; ++i)//InitializeDis[i] = (i = = original?)0: MAX); //for (int i = 1; I <= nodenum-1; ++i)     for(intj =1; J <= Edgenum *2; ++j) {if(DIS[EDGE[J].V] > dis[edge[j].u] + edge[j].cost)//relaxation (Order must not reverse ~){DIS[EDGE[J].V]= Dis[edge[j].u] +Edge[j].cost; PRE[EDGE[J].V]=edge[j].u; }    }    BOOLFlag =1;//determine if a negative power loop is included     for(inti =1; I <= edgenum*2; ++i)if(DIS[EDGE[I].V] > dis[edge[i].u] +edge[i].cost) {Flag=0;  Break; }    returnFlag;}voidPrint_path (intRoot//Print the shortest path (reverse){     while(Root! = Pre[root])//precursor{printf ("%d-->", Root); Root=Pre[root]; }    if(Root = =Pre[root]) printf ("%d\n", root);}intMain () {scanf ("%d%d%d", &nodenum, &edgenum, &original); Pre[original]=original;  for(inti =1; I <= edgenum; ++i) {scanf ("%d%d%d", &edge[i].u, &AMP;EDGE[I].V, &edge[i].cost); Edge[edgenum+ i].u =edge[i].v; Edge[edgenum+ I].V =edge[i].u; Edge[edgenum+ I].cost =Edge[i].cost; }    if(Bellman_ford ()) for(inti =1; I <= nodenum; ++i)//Shortest path per point{printf ("path from%d to%d are:%d\n", original, I, Dis[i]); printf ("Path:");        Print_path (i); }    Elseprintf ("Have negative circle\n"); System ("Pause"); return 0;}

A test case:

6 8 6 6 5 1006 4 306 2 104 5 604 3 203 5 102 3 501 2 5

Bellman-ford algorithm of Shortest path algorithm for graphs

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.