Algorithm learning-Bellman-Ford (Bellman Ford) algorithm (C ++ implementation)

Source: Internet
Author: User

Algorithm learning-Bellman-Ford (Bellman Ford) algorithm (C ++ implementation)

Advantages and disadvantages of the BellmanFord Algorithm

BellmanFord Algorithm

The Bellman-Ford algorithm is a single-source shortest path algorithm. The algorithm aims to find the shortest path and path length from the entire graph to the Start Node (set by yourself.

Advantages/disadvantages

Advantages

The advantage of this algorithm is relative to the Dijkstra algorithm.There can be a negative weight pathIt can also detect whether there is a negative weight loop in the graph.

Disadvantages

The disadvantage is that although the negative weight loop can be detected, the shortest path problem with the negative weight loop cannot be solved. In addition, the time complexity is relatively high. O (| V | * | E | ).

Implementation

In fact, the principle is to perform a relaxation operation on the current graph every time. Total | V | times. Every relaxation operation is|V|-1Times of relaxation judgment.

The following is code implementation:

/// Main. cpp // BellmanFord /// Created by Alps on 15/3/26. // Copyright (c) 2015 chen. All rights reserved. // # include
  
   
Using namespace std; # ifndef NumVertex # define NumVertex 4 # endif # ifndef Infinity # define Infinity 1000 # endiftypedef int Vertex; struct LinkList {int val; int weight; LinkList * next; linkList (int v, int w): val (v), weight (w), next (NULL) {}}; typedef LinkList * VList; struct TableEntry {VList Header; vertex Dist; Vertex Path;}; typedef TableEntry Table [NumVertex + 1]; void InitTable (Vertex start, Table T) {int OutDegree = 0; VList temp = NULL; for (int I = 1; I <= NumVertex; I ++) {T [I]. header = NULL; // init the vertex T [I]. dist = Infinity; T [I]. path =-1; scanf ("% d", & OutDegree); for (int j = 0; j <OutDegree; j ++) {// init the link vertex temp = (VList) malloc (sizeof (struct LinkList); scanf ("% d", & temp-> val, & temp-> weight); temp-> next = T [I]. header; T [I]. header = temp ;}t [start]. dist = 0;} void P RintPath (Vertex V, Table T) {if (T [V]. Path! =-1) {PrintPath (T [V]. path, T); printf ("to");} printf ("% d", V);} bool BellFord (Vertex start, Table T) {bool Update = false; VList temp; for (int I = 1; I <= NumVertex; I ++) {// cycle the num of vertex Update = false; for (int j = 1; j <= NumVertex; j ++) {// traversal all the vertex if (T [j]. dist! = Infinity) {// if the current vertex distance is not Inf temp = T [j]. Header; while (temp! = NULL) {// if it have traversaled the link vertex if (T [j]. dist + temp-> weight <T [temp-> val]. dist) {// if need to relax T [temp-> val]. dist = T [j]. dist + temp-> weight; // relax operation T [temp-> val]. path = j; // mark the path Update = true; // mark the vertex update is true} temp = temp-> next; // find the next node }}}if (Update = true) {return false; // if the Graph have a negative cycle} Else {return true; // no negative cycle} int main (int argc, const char * argv []) {Table T; InitTable (1, T); if (! BellFord (1, T) {printf ("There is a cycle! \ N "); return 0;} PrintPath (3, T); return 0 ;}
  
Test case: 2 // outbound degree of the first node 2-1 // node id and the length to node 2 3 1 2 // outbound degree of the second node 3-2 // likewise 4 1 1 4-1 0

The figure above is:
Node 1-> 2 (-1)-> 3 (1)
Node 2-> 3 (-2)-> 4 (1)
Node 3-> 4 (-1)
Node 4

Related Article

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.