-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbellman_ford.cpp
71 lines (66 loc) · 2.38 KB
/
bellman_ford.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// The Bellman–Ford algorithm is slower than Dijkstra's algorithm, but capable
// of handling graphs in which some of the edge weights are negative numbers.
// Bellman–Ford runs in {\displaystyle O(|V|\cdot |E|)} O(|V|\cdot |E|) time,
// where {\displaystyle |V|} |V| and {\displaystyle |E|} |E| are the number of
// vertices and edges respectively. Part of code is copied from
// https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/ a structure to
// represent a weighted edge in graph
#include <stdio.h>
#include <limits.h>
struct Edge {
int src;
int dest;
int weight;
};
// a structure to represent a connected, directed and
// weighted graph
struct Graph {
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges.
struct Edge* edges;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E) {
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edges = new Edge[E];
return graph;
}
bool BellmanFord(const struct Graph* graph, const int src, int* dist) {
int V = graph->V;
int E = graph->E;
// Step 1: Initialize distances from src to all other vertices
// as INFINITE
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
}
dist[src] = 0;
// Step 2: Relax all edges |V| - 1 times. A simple shortest
// path from src to any other vertex can have at-most |V| - 1
// edges
for (int i = 1; i < V; i++) {
for (int j = 0; j < E; j++) {
int u = graph->edges[j].src;
int v = graph->edges[j].dest;
int weight = graph->edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
// Step 3: check for negative-weight cycles. The above step
// guarantees shortest distances if graph doesn't contain
// negative weight cycle. If we get a shorter path, then there
// is a cycle.
for (int i = 0; i < E; i++) {
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int weight = graph->edge[i].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
fprintf(stderr, "Graph contains negative weight cycle...\n");
return false;
}
}
return true;
}