Graph theory (iii) (i) Bellman-ford algorithm for Shortest path problem

Source: Internet
Author: User

Brief: The Bellman-ford algorithm still calculates the shortest path algorithm from one point to all other points , its time complexity is O (NE), n is the number of points, and E is the amount of edges, it is not difficult to see, when a graph is slightly denser, The number of edges will be more than points then the efficiency is actually lower than the Dijkstra algorithm. However, this algorithm can calculate the existence of negative right side (no negative circuit), so it can be used in a wider range of situations, but in fact, in the daily application of the problem we do not use the algorithm, because there is a more efficient algorithm is SPFA algorithm, the next chapter will be introduced. SPFA algorithm is actually evolved from the Bellman-ford algorithm, then from the beginning of the foundation, we first to understand the Bellman-ford algorithm.

The algorithm describes:S is the starting point, dis[v] is the shortest distance from S to V, Pre[v] is the precursor node of V, W[j] is the length of the Edge J, and the start and end points of J Edge are u,v respectively.

1, initialization: dis[s]=0, dis[v]=∞ (v≠s), pre[s]=0

2. for (i=1;i<=n-1;i++)

for (j=1;j<=e;j++)

if (Dis[u]+w[j]<dis[v]) {

DIS[V]=DIS[U]+W[J];

Pre[v]=u;

}

algorithm Understanding: at the beginning of the marked point only the starting point, each enumeration of all the edges, there will always be some edges connected to the marked points and unmarked points, that is, the shortest distance has been calculated and the shortest distance to calculate the point. Therefore, each enumeration updates some unmarked points as marked points. And the n-1 times ensure that the worst case of all points can be marked, that is, the picture is a "string" situation.

In the case of negative-weight loops, because each enumeration passes through a loop of negative weights, the shortest distance between the points on either side of the loop will be infinitely reduced, resulting in errors. In this respect, we give a solution is not a solution, that is, if the two loops, after the end of the enumeration of each edge, if the distance between a two point is reduced, then return "error", has expressed a negative power loop, can not calculate the answer.

Of course, for the negative power loop also has a solution, after the introduction of the SPFA algorithm will be added.

The code is as follows:

#include <stdio.h>#include<stdlib.h>#include<string.h>intn,e,s;structnode{intx; inty; intVal;} m[ the];intdis[ the],pre[ the],a,b,w[ the][ the];intBellmanford (ints) {     inti,j;  for(i=1; i<=n;i++) Dis[i]=W[s][i]; Dis[s]=0;p re[s]=0;  for(i=1; i<=n-1; i++)       for(j=1; j<=e;j++)           if(dis[m[j].x]+m[j].val<Dis[m[j].y]) {DIS[M[J].Y]=dis[m[j].x]+M[j].val; PRE[M[J].Y]=m[j].x; }      for(j=1; j<=e;j++){         if(Dis[m[j].x]+m[j].val<dis[m[j].y])return 0; }     returndis[5]; }intMain () {inti,j; scanf ("%d%d",&n,&e); memset (DIS, -,sizeof(DIS)); Memset (W, -,sizeof(w));  for(i=1; i<=e;i++) {scanf ("%d%d%d",&m[i].x,&m[i].y,&m[i].val); A=m[i].x; b=m[i].y; W[A][B]=M[i].val; }     for(i=1; i<=5; i++)      for(j=1; j<=5; j + +) printf ("%d", W[i][j]); scanf ("%d",&s); printf ("%d", Bellmanford (s)); return 0;} 

Graph theory (iii) (i) Bellman-ford algorithm for Shortest path problem

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.