2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <limits.h>
|
2017-10-12 19:50:34 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//Wrapper class for storing an edge
|
2019-08-21 10:10:08 +08:00
|
|
|
class Edge
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int src, dst, weight;
|
2017-10-12 19:50:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//Wrapper class for storing a graph
|
2019-08-21 10:10:08 +08:00
|
|
|
class Graph
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int vertexNum, edgeNum;
|
|
|
|
Edge *edges;
|
2017-10-12 19:50:34 +08:00
|
|
|
|
|
|
|
//Constructs a graph with V vertices and E edges
|
2019-08-21 10:10:08 +08:00
|
|
|
Graph(int V, int E)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
this->vertexNum = V;
|
|
|
|
this->edgeNum = E;
|
2019-08-21 10:10:08 +08:00
|
|
|
this->edges = (Edge *)malloc(E * sizeof(Edge));
|
2017-10-12 19:50:34 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
//Adds the given edge to the graph
|
|
|
|
void addEdge(int src, int dst, int weight)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
static int edgeInd = 0;
|
2019-08-21 10:10:08 +08:00
|
|
|
if (edgeInd < this->edgeNum)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
Edge newEdge;
|
|
|
|
newEdge.src = src;
|
|
|
|
newEdge.dst = dst;
|
|
|
|
newEdge.weight = weight;
|
|
|
|
this->edges[edgeInd++] = newEdge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Utility function to print distances
|
2019-08-21 10:10:08 +08:00
|
|
|
void print(int dist[], int V)
|
|
|
|
{
|
|
|
|
cout << "\nVertex Distance" << endl;
|
|
|
|
for (int i = 0; i < V; i++)
|
|
|
|
{
|
|
|
|
if (dist[i] != INT_MAX)
|
|
|
|
cout << i << "\t" << dist[i] << endl;
|
2017-10-13 14:24:05 +08:00
|
|
|
else
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << i << "\tINF" << endl;
|
2017-10-13 14:24:05 +08:00
|
|
|
}
|
2017-10-12 19:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//The main function that finds the shortest path from given source
|
|
|
|
//to all other vertices using Bellman-Ford.It also detects negative
|
|
|
|
//weight cycle
|
2019-08-21 10:10:08 +08:00
|
|
|
void BellmanFord(Graph graph, int src)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
int V = graph.vertexNum;
|
|
|
|
int E = graph.edgeNum;
|
|
|
|
int dist[V];
|
|
|
|
|
|
|
|
//Initialize distances array as INF for all except source
|
|
|
|
//Intialize source as zero
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 0; i < V; i++)
|
2017-10-12 19:50:34 +08:00
|
|
|
dist[i] = INT_MAX;
|
|
|
|
dist[src] = 0;
|
|
|
|
|
|
|
|
//Calculate shortest path distance from source to all edges
|
|
|
|
//A path can contain maximum (|V|-1) edges
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 0; i <= V - 1; i++)
|
|
|
|
for (int j = 0; j < E; j++)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
int u = graph.edges[j].src;
|
|
|
|
int v = graph.edges[j].dst;
|
|
|
|
int w = graph.edges[j].weight;
|
2019-08-21 10:10:08 +08:00
|
|
|
|
|
|
|
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
2017-10-12 19:50:34 +08:00
|
|
|
dist[v] = dist[u] + w;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2017-10-12 19:50:34 +08:00
|
|
|
//Iterate inner loop once more to check for negative cycle
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int j = 0; j < E; j++)
|
|
|
|
{
|
2017-10-12 19:50:34 +08:00
|
|
|
int u = graph.edges[j].src;
|
|
|
|
int v = graph.edges[j].dst;
|
|
|
|
int w = graph.edges[j].weight;
|
2019-08-21 10:10:08 +08:00
|
|
|
|
|
|
|
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
|
|
|
{
|
|
|
|
cout << "Graph contains negative weight cycle. Hence, shortest distance not guaranteed." << endl;
|
2017-10-12 19:50:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print(dist, V);
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2017-10-12 19:50:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Driver Function
|
2019-08-21 10:10:08 +08:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int V, E, gsrc;
|
|
|
|
int src, dst, weight;
|
|
|
|
cout << "Enter number of vertices: ";
|
|
|
|
cin >> V;
|
|
|
|
cout << "Enter number of edges: ";
|
|
|
|
cin >> E;
|
|
|
|
Graph G(V, E);
|
|
|
|
for (int i = 0; i < E; i++)
|
|
|
|
{
|
|
|
|
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
|
|
|
cin >> src;
|
|
|
|
cout << "Enter destination: ";
|
|
|
|
cin >> dst;
|
|
|
|
cout << "Enter weight: ";
|
|
|
|
cin >> weight;
|
2017-10-12 19:50:34 +08:00
|
|
|
G.addEdge(src, dst, weight);
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nEnter source: ";
|
|
|
|
cin >> gsrc;
|
|
|
|
BellmanFord(G, gsrc);
|
2017-10-12 19:50:34 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|