TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
bellman_ford.cpp
1#include <climits>
2#include <iostream>
3#include <vector>
4
5using namespace std;
6
7// Wrapper class for storing an edge
8class Edge {
9 public:
10 int src, dst, weight;
11};
12
13// Wrapper class for storing a graph
14class Graph {
15 public:
16 int vertexNum, edgeNum;
17 std::vector<Edge> edges;
18
19 // Constructs a graph with V vertices and E edges
20 Graph(int V, int E) {
21 this->vertexNum = V;
22 this->edgeNum = E;
23 this->edges.reserve(E);
24 }
25
26 // Adds the given edge to the graph
27 void addEdge(int src, int dst, int weight) {
28 static int edgeInd = 0;
29 if (edgeInd < this->edgeNum) {
30 Edge newEdge;
31 newEdge.src = src;
32 newEdge.dst = dst;
33 newEdge.weight = weight;
34 this->edges[edgeInd++] = newEdge;
35 }
36 }
37};
38
39// Utility function to print distances
40void print(const std::vector<int>& dist, int V) {
41 cout << "\nVertex Distance" << endl;
42 for (int i = 0; i < V; i++) {
43 if (dist[i] != INT_MAX)
44 cout << i << "\t" << dist[i] << endl;
45 else
46 cout << i << "\tINF" << endl;
47 }
48}
49
50// The main function that finds the shortest path from given source
51// to all other vertices using Bellman-Ford.It also detects negative
52// weight cycle
53void BellmanFord(Graph graph, int src) {
54 int V = graph.vertexNum;
55 int E = graph.edgeNum;
56 std::vector<int> dist;
57 dist.reserve(E);
58
59 // Initialize distances array as INF for all except source
60 // Intialize source as zero
61 for (int i = 0; i < V; i++) dist[i] = INT_MAX;
62 dist[src] = 0;
63
64 // Calculate shortest path distance from source to all edges
65 // A path can contain maximum (|V|-1) edges
66 for (int i = 0; i <= V - 1; i++)
67 for (int j = 0; j < E; j++) {
68 int u = graph.edges[j].src;
69 int v = graph.edges[j].dst;
70 int w = graph.edges[j].weight;
71
72 if (dist[u] != INT_MAX && dist[u] + w < dist[v])
73 dist[v] = dist[u] + w;
74 }
75
76 // Iterate inner loop once more to check for negative cycle
77 for (int j = 0; j < E; j++) {
78 int u = graph.edges[j].src;
79 int v = graph.edges[j].dst;
80 int w = graph.edges[j].weight;
81
82 if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
83 cout << "Graph contains negative weight cycle. Hence, shortest "
84 "distance not guaranteed."
85 << endl;
86 return;
87 }
88 }
89
90 print(dist, V);
91
92 return;
93}
94
95// Driver Function
96int main() {
97 int V, E, gsrc;
98 int src, dst, weight;
99 cout << "Enter number of vertices: ";
100 cin >> V;
101 cout << "Enter number of edges: ";
102 cin >> E;
103 Graph G(V, E);
104 for (int i = 0; i < E; i++) {
105 cout << "\nEdge " << i + 1 << "\nEnter source: ";
106 cin >> src;
107 cout << "Enter destination: ";
108 cin >> dst;
109 cout << "Enter weight: ";
110 cin >> weight;
111 G.addEdge(src, dst, weight);
112 }
113 cout << "\nEnter source: ";
114 cin >> gsrc;
115 BellmanFord(G, gsrc);
116
117 return 0;
118}
int main()
Main function.
#define endl
Graph Algorithms.