-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFord.cpp
More file actions
144 lines (144 loc) · 2.59 KB
/
BellmanFord.cpp
File metadata and controls
144 lines (144 loc) · 2.59 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
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
#include<iostream>
#define temp 0
#define per 1
#define NIL -1
#define infinity 99999
using namespace std;
int v,**adj,ch;
int *queue,*state,*predecessor,*pathLength,front=-1,rear=-1;
void insertQueue(int data)
{
if(front==-1)
front=0;
rear++;
queue[rear]=data;
}
int deleteQueue()
{
int ans=queue[front];
front++;
return ans;
}
int isEmptyQueue()
{
if(front==-1||front>rear)
return 1;
else return 0;
}
void path(int s,int d)
{
if(d==s)
{
cout<<s;
return;
}
path(s,predecessor[d]);
cout<<"->"<<d;
}
int BellmanFord(int s)
{
int k=0;
state=new int[v];
predecessor=new int[v];
pathLength=new int[v];
queue=new int[v];
for(int i=0;i<v;i++)
{
state[i]=temp;
predecessor[i]=NIL;
pathLength[i]=infinity;
}
insertQueue(s);
pathLength[s]=0;
state[s]=per;
while(!isEmptyQueue())
{
int current = deleteQueue();
state[current]=temp;
if(s==current)
k++;
if(k>=v)
return -1;
for(int i=0;i<v;i++)
{
if(adj[current][i]!=0&&pathLength[current]+adj[current][i]<pathLength[i])
{
pathLength[i]=pathLength[current]+adj[current][i];
predecessor[i]=current;
if(state[i]==temp)
{
insertQueue(i);
state[i]=per;
}
}
}
}
return 1;
}
void createGraph()
{
int maxEdges,i,j,origin,destination,w;
cout<<"Enter 1 For Undirected Graph or 2 For Directed Graph : ";
cin>>ch;
cout<<"Enter The no of vertices : ";
cin>>v;
if(ch==1)
maxEdges=v*(v-1)/2;
else maxEdges=v*(v-1);
adj=new int*[v];
for(i=0;i<v;i++)
adj[i]=new int[v];
for(i=0;i<v;i++)
for(j=0;j<v;j++)
adj[i][j]=0;
for(i=1;i<maxEdges;i++)
{
//cout<<"Enter edge "<<i<<" (-1 -1 to quit) : ";
cin>>origin>>destination;
if(origin==-1&&destination==-1)
break;
if(origin<0||origin>=v||destination<0||destination>=v)
{
cout<<"Invalid Vertex! "<<endl;
i--;
}
else
{
// cout<<"Please Enter The Weight Of Edge : ";
cin>>w;
adj[origin][destination]=w;
if(ch==1)
adj[destination][origin]=w;
}
}
}
int main()
{
int s,d,check;
createGraph();
cout<<"Enter Source Vertex : ";
cin>>s;
check=BellmanFord(s);
if(check==-1)
{
cout<<"Negative Cycle Found!";
exit(0);
}
for(int i=0;i<v;i++)
cout<<pathLength[i]<<" "<<predecessor[i]<<" "<<state[i]<<" \n";
while(1)
{
cout<<"Enter Destination Vertex(-1 to Quit): ";
cin>>d;
if(d==-1)
break;
if(d>=v||d<0)
cout<<"This Vertex does not exist!\n";
else if(s==d)
cout<<"Source And Destination Are Same!\n";
else if(pathLength[d]==infinity)
cout<<"No path From Source to destination";
else path(s,d);
cout<<"\nShortest Distance is : "<<pathLength[d]<<"\n";
}
}