-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.cpp
More file actions
72 lines (59 loc) · 1.86 KB
/
bellman_ford.cpp
File metadata and controls
72 lines (59 loc) · 1.86 KB
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
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
#define MAX_EDGES 100
#define INF 99999
// Structure to represent an edge
struct Edge {
int source, destination, weight;
};
int vertices, edges;
struct Edge edge[MAX_EDGES];
int distance[MAX_VERTICES];
// Function to perform Bellman-Ford algorithm
void bellmanFord(int source) {
// Initialize distances to all vertices as infinity
for (int i = 0; i < vertices; i++) {
distance[i] = INF;
}
distance[source] = 0; // Distance from source to source is 0
// Relax all edges |V| - 1 times
for (int i = 0; i < vertices - 1; i++) {
for (int j = 0; j < edges; j++) {
int u = edge[j].source;
int v = edge[j].destination;
int weight = edge[j].weight;
if (distance[u] != INF && distance[u] + weight < distance[v]) {
distance[v] = distance[u] + weight;
}
}
}
// Check for negative-weight cycles
for (int i = 0; i < edges; i++) {
int u = edge[i].source;
int v = edge[i].destination;
int weight = edge[i].weight;
if (distance[u] != INF && distance[u] + weight < distance[v]) {
printf("Graph contains negative-weight cycle\n");
return;
}
}
// Print the distances
printf("Vertex Distance from Source\n");
for (int i = 0; i < vertices; i++) {
printf("%d \t\t %d\n", i, distance[i]);
}
}
int main() {
printf("Enter number of vertices and edges: ");
scanf("%d %d", &vertices, &edges);
printf("Enter source, destination, and weight for each edge:\n");
for (int i = 0; i < edges; i++) {
scanf("%d %d %d", &edge[i].source, &edge[i].destination, &edge[i].weight);
}
int source;
printf("Enter source vertex: ");
scanf("%d", &source);
bellmanFord(source);
return 0;
}