-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman.cpp
95 lines (95 loc) · 1.79 KB
/
bellman.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
//to find shortest path
//to find negative cycles
//Bellman-Ford algorithm
#include<iostream>
using namespace std;
int vert[10];
struct edge
{ int weight; }edgeto[10][10];
int distto[10],e[10];
class stk
{ int top,arr[10];
public:
stk()
{ top=-1; }
void push(int n)
{ ++top;
arr[top]=n;
}
int pop()
{ if(top==-1)
return -1;
int temp=arr[top];
top--;
return temp;
}
}st;
void findshortestpath(int novert)
{ for(int i=0;i<novert;i++)
{ cout<<e[i]<<" "<<i<<endl; }
int c,s;
cout<<"Enter source and final vertex";
cin>>s>>c;
int k=c;
while(k!=s)
{ st.push(k);
k=e[k];
}
cout<<s<<" ";
k=st.pop();
while(k!=-1)
{ cout<<k<<" ";
k=st.pop();
}
}
void negcycle(int novert)
{ distto[0]=0;
for(int j=1;j<novert;j++)
{ for(int k=0;k<novert;k++)
{ for(int i=0;i<novert;i++)
{ if(edgeto[k][i].weight!=0)
{ if(distto[i]>distto[k]+edgeto[k][i].weight)
{ cout<<k;
distto[i]=distto[k]+edgeto[k][i].weight;
e[i]=k;
}
}
}
}
}
int flag=0;
for(int l=0;l<novert;l++)
for(int t=0;t<novert;t++)
{ if(edgeto[l][t].weight!=0)
if(distto[t]>distto[l]+edgeto[l][t].weight)
flag=1;
}
if(flag==0)
findshortestpath(novert);
else
cout<<"Negative cycle found";
}
int main()
{ cout<<"Enter numkber of vertices";
int novert,noedge;
cin>>novert;
cout<<"Enter vertices";
for(int i=0;i<novert;i++)
cin>>vert[i];
cout<<"Enter number of edges";
cin>>noedge;
for(int i=0;i<noedge;i++)
{ distto[i]=100;
//e[i]=0;
for(int j=0;j<noedge;j++)
edgeto[i][j].weight=0;
}
cout<<"Enter start,end point and weight of each edge";
for(int i=0;i<noedge;i++)
{ int a,b,c;
cin>>a>>b>>c;
edgeto[a][b].weight=c;
}
negcycle(novert);
return 0;
}