forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBellmanford.dart
248 lines (229 loc) · 5.87 KB
/
Bellmanford.dart
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Bellman Ford Algorithm
* ----------------------------------------------
* This algorithm is used to find shortest distances of all vertices from
* a source vertex in a graph. It is a DP algorithm unlike Dijkstra which is a greedy one.
* This algorithm has O(m*n) time complexity where Dijkstra has O(n*log(n)).
* Bellman ford is suitable for distributed systems and works well on negative edges unline
* Dijkstra.
*
*/
// Importing required libraries
import 'dart:io';
// Class to define an edge
class Edge{
int source;
int destination;
int weight;
// Constructor
Edge(int source, int destination, int weight){
this.source = source;
this.destination = destination;
this.weight = weight;
}
}
// Class to define a graph
class Graph{
int numVertices;
int numEdges;
var edges;
// Constructor
Graph(int n, int m){
this.numVertices = n;
this.numEdges = m;
edges = List<Edge>();
}
// Method to add an edge to the graph
void add_edge(int src, int dest, int wt){
Edge edge = Edge(src, dest, wt);
this.edges.add(edge);
}
/* Method to find shortest distances of all vertices from a source vertex
using Bellman Ford algorithm */
void bellmanFordShortestDistances(int src){
// Using maximum int value as infinity
const int int64MaxValue = 9223372036854775807;
var distances = List(this.numVertices);
// Initializing the distances array
for(int i = 0; i < this.numVertices; i ++){
distances[i] = int64MaxValue;
}
distances[src] = 0;
// Finding shortest distances
for(int i = 0; i < this.numVertices - 1; i ++){
for(int j = 0; j < this.numEdges; j ++){
int srce = this.edges[j].source;
int desti = this.edges[j].destination;
int wt = this.edges[j].weight;
if(distances[srce] != int64MaxValue && distances[desti] > distances[srce] + wt){
distances[desti] = distances[srce] + wt;
}
}
}
// Checking for a negative cycle.
int flag = 0;
for(int j = 0; j < this.numEdges; j ++){
int srce = this.edges[j].source;
int desti = this.edges[j].destination;
int wt = this.edges[j].weight;
if(distances[srce] != int64MaxValue && distances[desti] > distances[srce] + wt){
print("Negative cycle found!!");
flag = 1;
break;
}
}
// Printing shortest distances if negative cycle is not found
if(flag == 0){
print("The shortest distances of all vertices from src vertex are in the following order:");
print(distances.join(" "));
}
}
}
// Driver method of the program
void main(){
// Input of number of vertices
print("Enter number of vertices:");
var input = stdin.readLineSync();
int n = int.parse(input);
// Input of number of edges
print("Enter number of edges:");
input = stdin.readLineSync();
int m = int.parse(input);
// Creating the graph
Graph g = Graph(n, m);
// Input of edges
print("Enter edges:");
for(int i = 0; i < m; i ++){
print("Enter edge ${i + 1}");
print("Enter source vertex:");
int src = int.parse(stdin.readLineSync());
print("Enter destination vertex:");
int dest = int.parse(stdin.readLineSync());
print("Enter weight of the edge:");
int wt = int.parse(stdin.readLineSync());
g.add_edge(src, dest, wt);
}
// Input of source vertex for Bellman Ford
print("Enter the source vertex to find shortest distances:");
int src = int.parse(stdin.readLineSync());
// Printing output of Bellman Ford
g.bellmanFordShortestDistances(src);
}
/**
* Sample Input and Output
* -----------------------------
* Sample 1
* -----------------------------
* Enter number of vertices:
* 5
* Enter number of edges:
* 8
* Enter edges:
* Enter edge 1
* Enter source vertex:
* 0
* Enter destination vertex:
* 1
* Enter weight of the edge:
* -1
* Enter edge 2
* Enter source vertex:
* 0
* Enter destination vertex:
* 2
* Enter weight of the edge:
* 4
* Enter edge 3
* Enter source vertex:
* 1
* Enter destination vertex:
* 2
* Enter weight of the edge:
* 3
* Enter edge 4
* Enter source vertex:
* 3
* Enter destination vertex:
* 1
* Enter weight of the edge:
* 1
* Enter edge 5
* Enter source vertex:
* 1
* Enter destination vertex:
* 3
* Enter weight of the edge:
* 2
* Enter edge 6
* Enter source vertex:
* 3
* Enter destination vertex:
* 2
* Enter weight of the edge:
* 5
* Enter edge 7
* Enter source vertex:
* 1
* Enter destination vertex:
* 4
* Enter weight of the edge:
* 2
* Enter edge 8
* Enter source vertex:
* 4
* Enter destination vertex:
* 3
* Enter weight of the edge:
* -3
* Enter the source vertex to find shortest distances:
* 0
* The shortest distances of all vertices from src vertex are in the following order:
* 0 -1 2 -2 1
* ------------------------
* Sample 2
* ------------------------
* Enter number of vertices:
* 4
* Enter number of edges:
* 5
* Enter edges:
* Enter edge 1
* Enter source vertex:
* 0
* Enter destination vertex:
* 1
* Enter weight of the edge:
* 5
* Enter edge 2
* Enter source vertex:
* 0
* Enter destination vertex:
* 2
* Enter weight of the edge:
* 4
* Enter edge 3
* Enter source vertex:
* 2
* Enter destination vertex:
* 1
* Enter weight of the edge:
* -6
* Enter edge 4
* Enter source vertex:
* 1
* Enter destination vertex:
* 3
* Enter weight of the edge:
* 3
* Enter edge 5
* Enter source vertex:
* 3
* Enter destination vertex:
* 2
* Enter weight of the edge:
* 2
* Enter the source vertex to find shortest distances:
* 0
* Negative cycle found!!
*
*/