Bellman Ford Algorithm Using Cpp

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Bellman Ford Algorithm Using Cpp as PDF for free.

More details

  • Words: 159
  • Pages: 2
//Bellman ford algo using cpp #include #include #define INFINITY 999999999 typedef struct { int source, destination, weight; } Edge; void BellmanFord(Edge edges[], int edgenum, int nodes, int source) { int distance[32]; if (distance == NULL) { std::cout <<"Failed, exit program \n"; exit(1); }//end if for (int i=0; i < nodes; ++i) distance[i] = INFINITY; distance[source] = 0; for (int i=0; i < nodes; ++i){ for (int j=0; j < edgenum; ++j){ if (distance[edges[j].source] != INFINITY) { if (distance[edges[j].source] + edges[j].weight < distance[edges[j].destination]){ int dummy = distance[edges[j].source] + edges[j].weight; if (dummy < distance[edges[j].destination]) distance[edges[j].destination] = dummy; }//end if }//end if }//end for }//end for for (int i=0; i < edgenum; ++i) { if (distance[edges[i].destination] > distance[edges[i].source] + edges[i].weight) { std::cout<<"Negative edge weight cycles detected!\n"; free(distance); break; }//end if }//end for for (int i=0; i < nodes; ++i) {

std::cout<<"The shortest distance between nodes " <<source<< " and " <<< " is " <

Related Documents

Corba Using Cpp
September 2019 51
Bellman
November 2019 8
Cpp
May 2020 23
Cpp
December 2019 37