-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBellman Ford.cpp
124 lines (98 loc) · 2.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<cassert>
using namespace std;
struct edge
{
int src;
int dest;
int weight;
/* edge(int a,int b,int c)
{
src=a;
dest=b;
weight=c;
}*/
};
/*
struct edge* create_edge(int a,int b,int c)
{
struct edge *node=new struct edge(a,b,c);
return node;
}*/
struct graph
{
int E;
int V;
struct edge* edge;
};
struct graph* create_graph(int E,int V)
{
struct graph *g=new struct graph;
g->E=E;
g->V=V;
g->edge=new edge[E];
}
void bellman_ford(graph *g)
{
int *arr = new int[g->V];
for(int i=0;i<g->V;i++)
arr[i]=INT_MAX-1000;
arr[0]=0;
for(int i=0;i<g->V;i++)
{
for(int j=0;j<g->E;j++)
{
if(arr[g->edge[j].dest]>arr[g->edge[j].src]+g->edge[j].weight)
arr[g->edge[j].dest]=arr[g->edge[j].src]+g->edge[j].weight;
}
}
for(int i=0;i<g->V;i++)
{
for(int j=0;j<g->E;j++)
{
if(arr[g->edge[j].dest]>arr[g->edge[j].src]+g->edge[j].weight)
{
cout<<"Negetive cycle present";
}
}
}
for(int i=0;i<g->V;i++)
cout<<arr[i]<<" ";
}
int main()
{
graph *graph=create_graph(8,5);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = -1;
// add edge 0-2 (or A-C in above figure)
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;
// add edge 1-2 (or B-C in above figure)
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
// add edge 1-3 (or B-D in above figure)
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;
// add edge 1-4 (or A-E in above figure)
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
// add edge 3-2 (or D-C in above figure)
graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;
// add edge 3-1 (or D-B in above figure)
graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;
// add edge 4-3 (or E-D in above figure)
graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;
bellman_ford(graph);
return 0;
}