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