-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bellman Ford.cpp
62 lines (57 loc) · 1.63 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
/*
Bellman Ford Algorithm By Rafat
Used to find single source shortest path with negative edge
Time Complexity: O(|V|*|E|)
Drawback: Won't work if there is a negative cycle
*/
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f
int dist[100005];
struct graph{
int from,to,cost;
graph(int a,int b,int c){
from=a;
to=b;
cost=c;
}
};
vector<graph>ListOfEdges;
int nodes,edges;
void Bellman_Ford()
{
for(int i=1;i<=nodes-1;i++) // The main idea of bellman ford is to relax edges n-1 times. that's what this loop for! //
{
int changed=0;
for(int j=0;j<ListOfEdges.size();j++)
{
// Relaxation Starts
int src=ListOfEdges[j].from;
int destination=ListOfEdges[j].to;
int weight=ListOfEdges[j].cost;
if(dist[src]+weight<dist[destination]) // Relaxation condition
{
dist[destination]=dist[src]+weight;
changed++; // it indicates we found a new shortest path in this iteration
}
// Relaxation Ends
}
if(changed==0) // indicates that our graph is now stable
break;
}
}
int main()
{
int u,v,c;
memset(dist,inf,sizeof(dist)); // Assigning distance array with a large value (inf)
scanf("%d%d",&nodes,&edges);
dist[1]=0; // let 1 be the source that's why distance of 1 is 0.
for(int i=1;i<=edges;i++)
{
scanf("%d%d%d",&u,&v,&c);
ListOfEdges.push_back(graph(u,v,c));
}
Bellman_Ford();
for(int i=1;i<=nodes;i++)
printf("Destination of Node %d from 1 is %d\n",i,dist[i]);
}