-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBellman.c
77 lines (68 loc) · 1.6 KB
/
Bellman.c
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
72
73
74
75
76
77
#include <stdio.h>
int bellman(int g[40][40], int V, int E, int edge[40][2], int source)
{
int distance[40], parent[40], flag = 1, i, k, j, u, v, source;
for (int i = 0; i < V; i++)
{
distance[i] = 1000;
parent[i] = -1;
}
distance[source - 1] = 0;
for (int i = 0; i < V - 1; i++)
{
for (int j = 0; j < E; j++)
{
u = edge[j][0];
v = edge[j][1];
if (distance[v] > distance[u] + g[u][v])
{
distance[v] = distance[u] + g[u][v];
parent[v] = u;
}
}
}
for (int j = 0; j < E; j++)
{
u = edge[j][0];
v = edge[j][1];
if (distance[v] > distance[u] + g[u][v])
flag = 0;
}
if (flag)
{
for (int i = 0; i < V; i++)
{
printf("Vertex->%d Cost ->%d Parent ->%d\n", i + 1, distance[i], parent[i] + 1);
}
}
return flag;
}
void main()
{
int g[40][40], edge[40][2], i, j, k = 0, v, source;
printf("Enter the number of vertices\n");
scanf("%d", &v);
printf("Enter the graph\n");
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j] != 0)
{
edge[k][0] = i;
edge[k++][1] = j;
}
}
}
printf("Enter the Source\n");
scanf("%d", &source);
if (bellman(g, v, k, edge, source))
{
printf("No negative weight cycle exist");
}
else
{
printf("Negative weight cycle exists");
}
}