Single source Shortest path Bellman-ford algorithm

Source: Internet
Author: User

The single source shortest path problem is fixed a starting point s, which asks it to the shortest path to all points.

The Bellman-ford algorithm can be used in cases where the edge weight is negative, unlike the Dijkstra only when the edge is positive (there is a negative loop return error), but its efficiency is lower.

The shortest distance from the starting point s to the vertex i is d[i] so
D[i] = min (d[j]+ (j->i) | where J->i belongs to e)

If the given graph is a dag then you can use topological order to number vertices, and use this recursive formula to calculate D (DP).
If there are loops in the diagram, it is not possible to rely on this order for calculation. In this case, the initial d[s]=0 D[i]=inf continues to use this recursive relationship to update the value of D. As long as there is no negative circle in the diagram, the update operation is limited. This is the Bellman-ford algorithm.

The Bellman-ford algorithm can be roughly divided into three parts
① initialize all D[i] to INF D[s] to 0
② cycle (n-1) times, in the loop inside all the edges, the relaxation calculation.
The ③ finally traverses all edges, checking for the presence of an I-to-J Edge, but d[i]+ (i->j) > D[j], if present, returns false, indicating that there is a negative circle in the diagram that can be reached from S.

Experiment Code
Enter each edge and start point to give the shortest path length from the starting point.

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intINF =1000000000;structEdge {//Side    intFrom,to,cost;};Const intMax_v =1010; Edge Eg[max_v];//SideintD[MAX_V];//d[i] Indicates the distance from the starting point to I is initialized to the INFintE,v;//number of edges, number of verticesintS//Starting pointintMain () {printf("Enter the number of sides, vertices and start \ n");scanf("%d%d%d", &e,&v,&s); for(inti =1; I <= e; i + +)scanf("%d%d%d", &eg[i].from,&eg[i].to,&eg[i].cost);//bellman-ford     for(inti =0; i < Max_v;    i + +) d[i] = INF; D[s] =0; while(1){BOOLUpdate =false; for(inti =1; I <= e; i + +) {if(Eg[i].from! = INF && D[eg[i].from] + eg[i].cost < d[eg[i].to])                {D[eg[i].to] = D[eg[i].from] + eg[i].cost; Update =true; }        }if(!update) Break; }//Output the shortest path for each point     for(inti =1; I <= v; i + +) {printf("point%d shortest path is%d\n", I,d[i]); }return 0;}

Single source Shortest path Bellman-ford 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: 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.