bellman.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3140/fall2026/notes/SP/code/bellman.cpp
 
#include <iostream>
#include <vector>
#include <map>
#include <fstream>


using namespace std;

struct EdgeT {
   char dest;
   int weight;
};

struct NodeT {
    vector<EdgeT> adj;
    int weight{0};
    bool isInfin{true};
    char parent{' '};
};

map<char,NodeT> G;
char start;

void ReadGraph(string fileName);
void PrintGraphRep();
void PrintGraphView();
void PrintTree();
void PrintLabels();
void Continue();

int main(int argc, char * argv[]) {
   string fileName;
   if (argc == 2) {
      fileName = argv[1];
   } else {
      cout << "Usage: " << argv[0] << " filename" << endl;
      return 0;
   }

   ReadGraph(fileName);

   if (start == ' ') {
      return 0;
   }

   G[start].weight = 0;
   G[start].isInfin = false;

   PrintGraphRep();
   cout << endl;
   PrintGraphView();
   Continue();

   cout << endl << endl;
   for(int i = 1; i < G.size()-1; ++i) {
      cout << "Iteration " << i << endl;
      for(auto & vertex: G){
         char u = vertex.first;
         for(auto & edge: G[u].adj) {
            char v = edge.dest;
            cout << "\tChecking " << u <<  " -> " << v << endl;

               if (G[v].isInfin or G[v].weight > G[u].weight + edge.weight) {
                  G[v].isInfin = false;
                  G[v].parent = u;
                  G[v].weight = G[u].weight + edge.weight;
                  cout <<  "\t\tRelax:" << v
                       << " has new weight " << G[v].weight << endl;

               }
         }

      }

      cout << endl << endl;
      PrintTree();
      PrintLabels();
      cout << endl << endl;
      cout << " The Graph" << endl;
      PrintGraphRep();

      Continue();
   }

   bool good{true};
   for (auto vertex: G) {
       char u = vertex.first;
       for(auto edge : G[u].adj) {
           char v = edge.dest;
           if (G[v].weight > G[u].weight + edge.weight) {
              cout << "There is a negative cycle" << endl;
              good = false;
           }
       }
   }
   if (good) {
      cout << "All is well " << endl;  
   }

   return 0;
}

void Continue() {
      char go;
      cout << endl << endl;
      cout << "Press Enter to continue: ";
      cin.get(go);
      cout << endl << endl;
}

void WeightLabel(char v){
      cout << " [label=<" << v << " <br/> " ;
      if (G[v].isInfin) {
           cout << "&infin;";
      } else { 
           cout << G[v].weight  ;
      }
      cout << " >]" << endl;
}

void PrintTree(){
   for(auto vertex: G) {
      char v = vertex.first ;
      if (G[v].parent != ' ' and v != start) {
         cout << v << " -> " << G[v].parent << " [color=\"red\"]" << endl;

      }
   }
}

void PrintLabels(){
   for (auto vertex:G) {
      char v = vertex.first;
      cout << v;
      WeightLabel(v);
   }
}


void PrintGraphView() {
   PrintLabels();

   for(auto vertex: G) {
      char v = vertex.first ;
      for( auto edge: G[v].adj) {
         cout << v << " -> " << edge.dest  
              << " [label = " << edge.weight << " ]" << endl;
      }
   }


}

void PrintGraphRep() {
   for(auto v: G) {
      cout << v.first << endl;
      cout << "\t";
      for (auto e: v.second.adj){
         cout << e.dest << ":"<< e.weight << " ";
      }
      cout << endl;
      cout << "\tParent: " << v.second.parent << endl;
      cout << "\tCost:";
      if (!v.second.isInfin) {
        cout << v.second.weight;
      }
      cout << endl;
   }
}

void ReadGraph(string fileName){

   ifstream inFile(fileName);
   if (!inFile) {
       cout << "Invalid File" << endl;
       start = ' ';
   }

   inFile >> start;
   char src;
   inFile >> src;
   while (inFile) {
        char  dest;
        int weight;
        inFile >> dest >> weight;
        G[src].adj.push_back(EdgeT{dest, weight});
        inFile >> src;
   }
}