-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellman_ford.cpp
More file actions
85 lines (73 loc) · 1.77 KB
/
Copy pathBellman_ford.cpp
File metadata and controls
85 lines (73 loc) · 1.77 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<limits>
using namespace std;
int vertices,edges;
void display(vector<list<pair<int,int> > > adjacency_list){
list<pair<int,int> >::iterator itr;
for(int i=1;i<=vertices;i++){
cout<<i;
for(itr=adjacency_list[i].begin();itr!=adjacency_list[i].end();itr++){
cout<<itr->first<<" "<<itr->second<<"\t" ;
}
cout<<endl;
}
}
int *dist;
int *parent;
void initialise(int source){
dist=new int[vertices+1];
parent=new int[vertices+1];
for(int i=0;i<=vertices;i++){
dist[i]=numeric_limits<int>::max();
parent[i]=i;
}
dist[source]=0;
}
void Prim(vector<list<pair<int,int> > > adjacency_list){
cout<<"\nEnter the source\n";
int source;
bool flag=true;
cin>>source;
list<pair<int,int> >::iterator itr;
initialise(source);
for(int i=1;i<=vertices-1;i++){
for(itr=adjacency_list[i].begin();itr!=adjacency_list[i].end();itr++){
if((dist[itr->first])>(dist[i]+itr->second)){
parent[itr->first]=i;
dist[itr->first]=dist[i]+itr->second;
}
}
}
list<pair<int,int> >::iterator itr1;
for(int i=1;i<=vertices;i++){
for(itr1=adjacency_list[i].begin();itr1!=adjacency_list[i].end();itr1++){
if((dist[itr1->first])>(dist[i]+itr1->second)){
cout<<"Presence of negative weight cycle\n";
flag=false;
}
}
}
if(flag==true){
for(int i=1;i<=vertices;i++){
cout<<"dist of "<<i<<dist[i]<<endl;
cout<<"Parent of i "<<parent[i]<<endl;
}
}
}
int main(){
cout<<"\nEnter vertices and edges\n";
cin>>vertices>>edges;
int v1,v2,weight;
vector<list<pair<int,int> > > adjacency_list(vertices+1);
for(int i=0;i<edges;i++){
cin>>v1>>v2>>weight;
adjacency_list[v1].push_back(make_pair(v2,weight));
}
display(adjacency_list);
Prim(adjacency_list);
return 0;
}