Bellman-Ford (Bellman-Ford)

Source: Internet
Author: User

Bellman-Ford (edge weight can be positive or negative)

The iterative relaxation operation of the Bellman-Ford algorithm is actually the process of generating the shortest path tree layer by layer based on the vertex distance S.

When each side is relaxed once, the branches starting from S and whose layers are mostly 1 are generated. That is to say, the shortest path of those vertices associated with at most one edge of S is found. When every side is relaxed for 2nd times, 2nd layers of branches are generated, that is to say, we have found the shortest path of those vertices connected by two edges ....... Because the shortest path can contain up to n-1 edges, you only need to loop for n-1 times.

If the shortest path exists, it must not contain loops:

1. If there is a zero or positive ring, the path of the removed ring will not become longer.

2. If a negative ring exists, the path will only get shorter and shorter as long as it goes around the ring. The lower bound will not converge and there will be no shortest path.


The shortest path only contains n-1 nodes (excluding the start point)

Procedure:

First, Initialize all vertices. Save a value for each vertex, indicating the distance from the origin to this vertex. Set the value of the origin to 0, and set the value of other vertices to infinity (indicating that the origin cannot be reached ).


Second, perform a loop. The subscripts of the loop are from 1 to n-1 (N is equal to the number of vertices in the graph ). In the loop, traverse all edges for relaxation calculation.


Third, traverse all the edges (u, v) on the way to determine whether such a situation exists:
D (v)> d (u) + W (u, v) indicates that downward convergence is not possible.



# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; # define INF 0x7ffffffstruct edge {int U, V, cost;} edge [2000]; int pre [200]; // The parent int dis [200]; // The number of edges from The Source Vertex to int n, m, Src; // The number of edges, source point bool relax (int u, int V, int cost) // side relaxation {If (DIS [v]> dis [u] + cost) {dis [v] = dis [u] + cost; return true; // successful relaxation} return false; // no relaxation successful} int bellman_ford () {int I, j, flag; for (I = 1; I <= N; I ++) dis [I] = (I = SRC? 0: INF); // Step 1: Initialize dis for (I = 1; I <n; I ++) // Step 2: Perform n-1 iterations, every time all edges are relaxed {flag = 0; For (j = 0; j <m; j ++) {If (relax (edge [J]. u, edge [J]. v, edge [J]. cost) {pre [v] = u; // record path flag = 1 ;}} if (! Flag) break; // all edges are not relaxed successfully} for (I = 0; I <m; I ++) // Step 3: If you can continue to relax, it indicates that the lower bound does not converge and there is a negative ring {If (relax (edge [I]. u, edge [I]. v, edge [I]. cost) {return 0 ;}} return 1 ;}int main () {int I, j; while (~ Scanf ("% d", & N, & M, & SRC) {for (I = 0; I <m; I ++) {scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. cost); // enter the Left and Right endpoints of each edge, weight} If (bellman_ford () {for (I = 1; I <= N; I ++) {printf ("% d \ n", DIS [I]); // distance from each output point to the source point} else printf ("have negative circle \ n ");} return 0 ;}
Input case 1.
4 6 1
1 2 20
1 3 5
4 1-200
2 4 4
4 2 4
3 4 2



Input case 2.
4 6 1
1 2 2
1 3 5
4 1 10
2 4 4
4 2 4
3 4 2

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.