|
| 1 | +// A C++ program for Bellman-Ford's single source |
| 2 | +// shortest path algorithm. |
| 3 | +#include <bits/stdc++.h> |
| 4 | + |
| 5 | +// a structure to represent a weighted edge in graph |
| 6 | +struct Edge { |
| 7 | +int src, dest, weight; |
| 8 | +}; |
| 9 | + |
| 10 | +// a structure to represent a connected, directed and |
| 11 | +// weighted graph |
| 12 | +struct Graph { |
| 13 | +// V-> Number of vertices, E-> Number of edges |
| 14 | +int V, E; |
| 15 | + |
| 16 | +// graph is represented as an array of edges. |
| 17 | +struct Edge* edge; |
| 18 | +}; |
| 19 | + |
| 20 | +// Creates a graph with V vertices and E edges |
| 21 | +struct Graph* createGraph(int V, int E) |
| 22 | +{ |
| 23 | +struct Graph* graph = new Graph; |
| 24 | +graph->V = V; |
| 25 | +graph->E = E; |
| 26 | +graph->edge = new Edge[E]; |
| 27 | +return graph; |
| 28 | +} |
| 29 | + |
| 30 | +// A utility function used to print the solution |
| 31 | +void printArr(int dist[], int n) |
| 32 | +{ |
| 33 | +printf("Vertex Distance from Source\n"); |
| 34 | +for (int i = 0; i < n; ++i) |
| 35 | +printf("%d \t\t %d\n", i, dist[i]); |
| 36 | +} |
| 37 | + |
| 38 | +// The main function that finds shortest distances from src to |
| 39 | +// all other vertices using Bellman-Ford algorithm. The function |
| 40 | +// also detects negative weight cycle |
| 41 | +void BellmanFord(struct Graph* graph, int src) |
| 42 | +{ |
| 43 | +int V = graph->V; |
| 44 | +int E = graph->E; |
| 45 | +int dist[V]; |
| 46 | + |
| 47 | +// Step 1: Initialize distances from src to all other vertices |
| 48 | +// as INFINITE |
| 49 | +for (int i = 0; i < V; i++) |
| 50 | +dist[i] = INT_MAX; |
| 51 | +dist[src] = 0; |
| 52 | + |
| 53 | +// Step 2: Relax all edges |V| - 1 times. A simple shortest |
| 54 | +// path from src to any other vertex can have at-most |V| - 1 |
| 55 | +// edges |
| 56 | +for (int i = 1; i <= V - 1; i++) { |
| 57 | +for (int j = 0; j < E; j++) { |
| 58 | +int u = graph->edge[j].src; |
| 59 | +int v = graph->edge[j].dest; |
| 60 | +int weight = graph->edge[j].weight; |
| 61 | +if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) |
| 62 | +dist[v] = dist[u] + weight; |
| 63 | +} |
| 64 | +} |
| 65 | + |
| 66 | +// Step 3: check for negative-weight cycles. The above step |
| 67 | +// guarantees shortest distances if graph doesn't contain |
| 68 | +// negative weight cycle. If we get a shorter path, then there |
| 69 | +// is a cycle. |
| 70 | +for (int i = 0; i < E; i++) { |
| 71 | +int u = graph->edge[i].src; |
| 72 | +int v = graph->edge[i].dest; |
| 73 | +int weight = graph->edge[i].weight; |
| 74 | +if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) { |
| 75 | +printf("Graph contains negative weight cycle"); |
| 76 | +return; // If negative cycle is detected, simply return |
| 77 | +} |
| 78 | +} |
| 79 | + |
| 80 | +printArr(dist, V); |
| 81 | + |
| 82 | +return; |
| 83 | +} |
| 84 | + |
| 85 | +// Driver program to test above functions |
| 86 | +int main() |
| 87 | +{ |
| 88 | +/* Let us create the graph given in above example */ |
| 89 | +int V = 5; // Number of vertices in graph |
| 90 | +int E = 8; // Number of edges in graph |
| 91 | +struct Graph* graph = createGraph(V, E); |
| 92 | + |
| 93 | +// add edge 0-1 (or A-B in above figure) |
| 94 | +graph->edge[0].src = 0; |
| 95 | +graph->edge[0].dest = 1; |
| 96 | +graph->edge[0].weight = -1; |
| 97 | + |
| 98 | +// add edge 0-2 (or A-C in above figure) |
| 99 | +graph->edge[1].src = 0; |
| 100 | +graph->edge[1].dest = 2; |
| 101 | +graph->edge[1].weight = 4; |
| 102 | + |
| 103 | +// add edge 1-2 (or B-C in above figure) |
| 104 | +graph->edge[2].src = 1; |
| 105 | +graph->edge[2].dest = 2; |
| 106 | +graph->edge[2].weight = 3; |
| 107 | + |
| 108 | +// add edge 1-3 (or B-D in above figure) |
| 109 | +graph->edge[3].src = 1; |
| 110 | +graph->edge[3].dest = 3; |
| 111 | +graph->edge[3].weight = 2; |
| 112 | + |
| 113 | +// add edge 1-4 (or A-E in above figure) |
| 114 | +graph->edge[4].src = 1; |
| 115 | +graph->edge[4].dest = 4; |
| 116 | +graph->edge[4].weight = 2; |
| 117 | + |
| 118 | +// add edge 3-2 (or D-C in above figure) |
| 119 | +graph->edge[5].src = 3; |
| 120 | +graph->edge[5].dest = 2; |
| 121 | +graph->edge[5].weight = 5; |
| 122 | + |
| 123 | +// add edge 3-1 (or D-B in above figure) |
| 124 | +graph->edge[6].src = 3; |
| 125 | +graph->edge[6].dest = 1; |
| 126 | +graph->edge[6].weight = 1; |
| 127 | + |
| 128 | +// add edge 4-3 (or E-D in above figure) |
| 129 | +graph->edge[7].src = 4; |
| 130 | +graph->edge[7].dest = 3; |
| 131 | +graph->edge[7].weight = -3; |
| 132 | + |
| 133 | +BellmanFord(graph, 0); |
| 134 | + |
| 135 | +return 0; |
| 136 | +} |
0 commit comments