|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <cassert> |
| 4 | +#include <climits> |
| 5 | + |
| 6 | +/** |
| 7 | +* Bellman-ford algorithm is single source shortest distance algorithm that |
| 8 | +* works for positive and negative both types of edges. |
| 9 | +* 1. Initialize distance from source all the vertices as infinity, but source itself. |
| 10 | +* Distance to source will be zero. |
| 11 | +* 2. Select any edge u-v |
| 12 | +* 3. If dist[v] > dist[u] + weight(u-v), then dist[v] = dist[u] + weight(u-v) |
| 13 | +* 4. Execute step 2 and 3 for all the edges in the graph. |
| 14 | +* 5. Iterate steps 2 to 4 (V - 1) times, where V is number of vertices in the graph. |
| 15 | +* 6. For any edge u-v, if dist[v] > dist[u] + weight(u-v), then graph contains negative cycles. |
| 16 | +*/ |
| 17 | + |
| 18 | +class Edge |
| 19 | +{ |
| 20 | +public: |
| 21 | + Edge(long long src, long long dest, long long weight) : _src(src), _dest(dest), _weight(weight) |
| 22 | + { |
| 23 | + } |
| 24 | + int getSrc() const |
| 25 | + { |
| 26 | + return _src; |
| 27 | + } |
| 28 | + int getDest() const |
| 29 | + { |
| 30 | + return _dest; |
| 31 | + } |
| 32 | + long long getWeight() const |
| 33 | + { |
| 34 | + return _weight; |
| 35 | + } |
| 36 | +private: |
| 37 | + int _src, _dest; |
| 38 | + long long _weight;; |
| 39 | +}; |
| 40 | + |
| 41 | +class Graph |
| 42 | +{ |
| 43 | +public: |
| 44 | + Graph(int v) : _v(v) |
| 45 | + { |
| 46 | + } |
| 47 | + void addEdge(int a, int b, int wt) |
| 48 | + { |
| 49 | + assert(0 <= a && a < _v); |
| 50 | + assert(0 <= b && b < _v); |
| 51 | + _edges.push_back(Edge(a, b, wt)); |
| 52 | + } |
| 53 | + int getV() const |
| 54 | + { |
| 55 | + return _v; |
| 56 | + } |
| 57 | + const std::vector<Edge> getEdges() const |
| 58 | + { |
| 59 | + return _edges; |
| 60 | + } |
| 61 | +private: |
| 62 | + std::vector<Edge> _edges; |
| 63 | + int _v; |
| 64 | +}; |
| 65 | + |
| 66 | +// Stores shortest distance in the vector result, and returns true if no negative cycles present. |
| 67 | +// Returns false otherwise. |
| 68 | +bool bellmanFord(const Graph& g, int src, std::vector<long long>& result) |
| 69 | +{ |
| 70 | + result.clear(); |
| 71 | + result.reserve(g.getV()); |
| 72 | + for(int i = 0; i < g.getV(); i++) |
| 73 | + result.push_back(INT_MAX); |
| 74 | + result[src] = 0; |
| 75 | + |
| 76 | + auto edgeList = g.getEdges(); |
| 77 | + for(int i = 0; i < g.getV() - 1; i++) |
| 78 | + { |
| 79 | + for(const auto edge: edgeList) |
| 80 | + { |
| 81 | + if(result[edge.getDest()] > result[edge.getSrc()] + edge.getWeight()) |
| 82 | + result[edge.getDest()] = result[edge.getSrc()] + edge.getWeight(); |
| 83 | + } |
| 84 | + } |
| 85 | + for(const auto edge: edgeList) |
| 86 | + { |
| 87 | + if(result[edge.getDest()] > result[edge.getSrc()] + edge.getWeight()) |
| 88 | + { |
| 89 | + result.clear(); |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | +} |
| 95 | + |
| 96 | +const Graph& test1() |
| 97 | +{ |
| 98 | + const int vertices = 6; |
| 99 | + static Graph g(vertices); |
| 100 | + |
| 101 | + g.addEdge(0, 2, 2); |
| 102 | + g.addEdge(1, 0, 1); |
| 103 | + g.addEdge(2, 1, -2); |
| 104 | + g.addEdge(3, 0, -4); |
| 105 | + g.addEdge(3, 2, -1); |
| 106 | + g.addEdge(4, 3, 1); |
| 107 | + g.addEdge(5, 0, 10); |
| 108 | + g.addEdge(5, 4, 8); |
| 109 | + |
| 110 | + return g; |
| 111 | +} |
| 112 | + |
| 113 | +const Graph& test2() |
| 114 | +{ |
| 115 | + const int vertices = 3; |
| 116 | + static Graph g(vertices); |
| 117 | + |
| 118 | + g.addEdge(0, 2, -2); |
| 119 | + g.addEdge(1, 0, 1); |
| 120 | + g.addEdge(2, 1, -5); |
| 121 | + |
| 122 | + return g; |
| 123 | +} |
| 124 | + |
| 125 | +int main() |
| 126 | +{ |
| 127 | + Graph g1 = test1(); |
| 128 | + |
| 129 | + std::vector<long long> result; |
| 130 | + bool cycle = bellmanFord(g1, 5, result); |
| 131 | + |
| 132 | + std::cout << "Test 1: " << std:: endl |
| 133 | + << "Result for vertice 5 as source:" << std::endl; |
| 134 | + if(not cycle) |
| 135 | + { |
| 136 | + for(auto distance: result) |
| 137 | + std::cout << distance << ' '; |
| 138 | + } |
| 139 | + else |
| 140 | + std::cout << "Negative cycle is present in the graph"; |
| 141 | + std::cout << std::endl << std::endl; |
| 142 | + |
| 143 | + std::cout << "Test 2: " << std:: endl |
| 144 | + << "Result for vertice 1 as source:" << std::endl; |
| 145 | + |
| 146 | + |
| 147 | + g1 = test2(); |
| 148 | + cycle = bellmanFord(g1, 1, result); |
| 149 | + if(not cycle) |
| 150 | + { |
| 151 | + for(auto distance: result) |
| 152 | + std::cout << distance << ' '; |
| 153 | + } |
| 154 | + else |
| 155 | + std::cout << "Negative cycle is present in the graph"; |
| 156 | + std::cout << std::endl << std::endl; |
| 157 | + |
| 158 | + return 0; |
| 159 | +} |
0 commit comments