forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellmanFord.cpp
More file actions
68 lines (63 loc) · 1.66 KB
/
bellmanFord.cpp
File metadata and controls
68 lines (63 loc) · 1.66 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
#include <iostream>
#include <vector>
using namespace std;
void printArr(int *A, int n)
{
for(int i = 0; i < n; i++)
cout<<i<<"->"<<A[i]<<endl;
}
void bellmanFord(vector<vector<int> > adjMatrix, int n, int e, int s)
{
int dist[n];
for (int i = 0; i < n; i++)
dist[i] = INT_MAX;
dist[s] = 0;
for (int i = 1; i <= n - 1; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
if (adjMatrix[j][k] != 0)
{
int weight = adjMatrix[j][k];
if (dist[j] != INT_MAX && dist[j] + weight < dist[k])
dist[k] = dist[j] + weight;
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (adjMatrix[i][j] != 0)
{
int weight = adjMatrix[i][j];
if (dist[i] != INT_MAX && dist[i] + weight < dist[j])
{
cout << "Graph contains negative weight cycle" << endl;
return;
}
}
}
}
cout<<"Shortest Distance:"<<endl;
printArr(dist, n);
return;
}
int main()
{
int v, e;
int e1, e2, w;
cout << "Enter the number of vertices and edges:"<<endl;
cin >> v >> e;
vector<vector<int> > adjMatrix( v , vector<int> (v, 0));
cout << "Enter edges with weight" << endl;
for (int i = 0; i < e; i++)
{
cin >> e1 >> e2 >> w;
adjMatrix[e1][e2] = w;
}
bellmanFord(adjMatrix, v, e, 0);
}