Given figure G (V, E) (where V, E is the vertex set and edge set of Figure g, respectively), the source point S,
- Array Distant[i] records the path length from the source point s to the vertex I, initializes the array distant[n] to, Distant[s] is 0;
The following operations loop through a maximum of n-1 times and N is the number of vertices:
For each Edge e (U, v), if Distant[u] + w (u, v) < Distant[v], then another distant[v] = distant[u]+w (U, v). W (u, v) is the weighted value of the Edge e (u,v);
If the above operation does not update the distant, the shortest path has been found, or some points are unreachable, jump out of the loop. Otherwise the next cycle is executed;
- In order to detect whether there is a negative loop in the graph, the sum of the weights is less than 0 of the loop. For each Edge e (U, v), if there is an edge of Distant[u] + w (u, v) < Distant[v], then there is a negative loop in the diagram, that is to say the map cannot find the shortest path single source. Otherwise, the shortest path length of the source point s to each vertex is recorded in the array distant[n].
It is found that the time complexity of finding the shortest path of single source Bellman-ford algorithm is O (v*e).
First, let's introduce the relaxation calculation. Such as:
Before the relaxation calculation, the value of point B is 8, but the value of point a plus the weight on the Edge 2, gets 5, is smaller than the value of point B (8), so the value of point B is reduced to 5. The point of this process is to find a shorter route to point B, which goes through point A, then passes through the edge of the weight of 2 and reaches point B.
Of course, if there's a situation
The value of point B is not modified because 3+4>6.
The Bellman-ford algorithm can be roughly divided into three parts
First, initialize all of the points. Each point holds a value that represents the distance from the origin to the point, sets the value of the origin to 0, and the value of the other points to infinity (which means that it is not reachable).
Second, loop, loop subscript from 1 to n-1 (n equals the number of midpoints in the graph). Inside the loop, all edges are traversed and the slack is calculated.
Third, traverse all sides (Edge (U,V)) to determine if there is such a situation:
D (v) > D (U) + w (u,v)
Returns false, indicating that there is a negative loop in the way from which the source point can be reached.
The reason why the third part is needed is that if there is a negative loop from the source point. It is not possible to find the shortest path due to the inability to converge.
Consider the following diagram:
After the first traversal, the value of point B becomes 5, the value of point C becomes 8, and attention is paid to the edge with a weight of-10, which causes the value of point A to become-2. (8+ -10=-2)
After the second traversal, the value of point B becomes 3, point C becomes 6, and point a becomes-4. It is because there is a negative edge in the loop that causes the values of each point to become smaller after each traversal.
In return, take a look at the third part of the Bellman-ford algorithm, traverse all edges, and check for the presence of D (v) > D (U) + w (u,v). Because the number of cycles in the second part is fixed, if there is an inability to converge, it can certainly be checked in the third part. Like what
At this point A has a value of-2, point B has a value of 5, and Edge AB weighs 5,5 >-2 + 5. Check out that this side does not converge.
Therefore, the Bellman-ford algorithm can solve the problem of the single-source shortest path in the graph with the right negative side.
Consider: why cycle V-1 times?
Answer: Because the shortest path must be a simple path, it is not possible to include loops,
If the loop is included and the weight of the loop is positive, then the circuit is removed and a shorter path can be obtained.
If the weight of the loop is negative, then there must be no solution.
The graph has n points, and there is no loop.
So the shortest path is up to n-1 edge
And because each cycle, at least relax one side
So you can do it at most n-1 times.
1#include <iostream>2 using namespacestd;3 Const intMaxnum = -;4 Const intMaxint =99999;5 6 //side,7typedefstructedge{8 intU, v;//starting point, emphasis9 intWeight//weighted value of the edgeTen }edge; One AEdge Edge[maxnum];//Save the value of an edge - intDist[maxnum];//minimum distance from node to source point - the intNodenum, Edgenum, source;//node count, number of edges, source point - - //initialization Diagram - voidInit () + { - //input node, number of sides, source point +CIN >> Nodenum >> edgenum >>Source; A for(intI=1; i<=nodenum; ++i) atDist[i] =maxint; -Dist[source] =0; - for(intI=1; i<=edgenum; ++i) - { -CIN >> edge[i].u >> edge[i].v >>Edge[i].weight; - if(edge[i].u = = Source)//Note that the initial situation is set here inDIST[EDGE[I].V] =Edge[i].weight; - } to } + - //Relaxation Calculation the voidRelaxintUintVintweight) * { $ if(Dist[v] > Dist[u] +weight)Panax NotoginsengDIST[V] = Dist[u] +weight; - } the + BOOLBellman_ford () A { the for(intI=1; i<=nodenum-1; ++i) + for(intj=1; j<=edgenum; ++j) - Relax (edge[j].u, EDGE[J].V, edge[j].weight); $ BOOLFlag =1; $ //determine if there is a negative loop - for(intI=1; i<=edgenum; ++i) - if(DIST[EDGE[I].V] > dist[edge[i].u] +edge[i].weight) the { -Flag =0;Wuyi Break; the } - returnFlag; Wu } - intMain () About { $ //freopen ("Input3.txt", "R", stdin); - init (); - if(Bellman_ford ()) - for(inti =1; I <= nodenum; i++) Acout << Dist[i] <<Endl; + return 0; the}
View Code
Bellman-ford algorithm