-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
283 lines (247 loc) · 5.81 KB
/
Copy pathdijkstra.cpp
File metadata and controls
283 lines (247 loc) · 5.81 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This file was first created on 4th April,2019 by Keshav Tangri
This is for educational purposes only.
This is for B.Tech Students who are or will be studying Data Structures in thier curriculum
For queries mail me @ : tangri57@gmail.com
*/
#include <iostream>
using namespace std;
class Node{
public:
Node * next;
int vertexName;
int distance;
int previousVertex;
};
class llist{
public:
Node * head = new Node;
llist(){
head = NULL;
}
Node * createnode(int vertexname,int distance,int previousvertex);
void addElement(int vertexname,int distance,int previousvertex);
void addatbeg(int vertexname,int distance,int previousvertex);
void deleteVertex(int pos);
int elementMinimum();
int searchele(int val);
void printingThePath(Node* vertex);
void display();
};
llist allVerticeLinkList,allDiscoveredVertices;
int main()
{
int v;
cout<<"Enter Number of vertices you have in your graph : ";
cin>>v;
int arr[v+1][v+1];
int j=0,i;
arr[0][0] = 0;
for(i=1;i<v+1;i++){
arr[j][i] = i;
arr[i][j] = i;
}
for(int i=1;i<v+1;i++){
for(int j=1;j<v+1;j++){
if(i==j){
arr[i][j] = 0;
}else{
cout<<"Enter weight of edge from "<<arr[i][0]<<" to "<<arr[0][j]<<" (if any) : ";
cin>>arr[i][j];
}
}
}
//PRINTING ADJACENCY MATRIX
cout<<"\n\nMATRIX IS : \n\n";
for(i=0;i<v+1;i++){
for(j=0;j<v+1;j++){
cout<<arr[i][j]<<" ";
}
cout<<"\n\n";
}
//SOURCE VERTEX INFO
int sourceVertex;
cout<<"Enter the source vertex from 1 to "<<v<<" : ";
cin>>sourceVertex;
while( !(sourceVertex>=1 && sourceVertex<=v) ){
cout<<"Source Vertex is Out of Range !\nEnter Again : ";
cin>>sourceVertex;
}
cout<<"\n\nSource Vertex is : "<<sourceVertex<<"\n\n";
//SOURCE VERTEX INFO ENDS
//DIJKSTRA'S ALGORITHM BEGIN !
//INITIALIZING THE LINK LIST HAVING ALL VERTICES AND SOURCE
//VERTEX INITIALIZED
for(int i=1;i<=v;i++){
if(i==sourceVertex)
allVerticeLinkList.addElement(i,0,NULL);
else
allVerticeLinkList.addElement(i,4294967294/2,NULL);
}
cout<<"\n\nPrinting the allVerticeLinkList Before deletion : \n\n";
allVerticeLinkList.display();
cout<<"\n\nPrinting the allDiscovered nodes LL Before deletion : \n\n";
allDiscoveredVertices.display();
while(allVerticeLinkList.head!=NULL){
//finding minimum value and adding the details to the discovered nodes list
int minimumValue = allVerticeLinkList.elementMinimum();
//Finding the position of the minimum value
int position_of_minimumValue = allVerticeLinkList.searchele(minimumValue);
//Deleting the vertex of miimum distance from all vertices list
//since it is already processed
allVerticeLinkList.deleteVertex(position_of_minimumValue);
//Next our goal will be relaxing an edge
Node * peakVertex = allDiscoveredVertices.head;
if(peakVertex==NULL){
break;
}else{
for(int i=1;i<=v;i++){
if(arr[peakVertex->vertexName][i] == 0){
continue;
}else{
Node* ptr = allVerticeLinkList.head;
while(ptr!=NULL){
if(ptr->vertexName==i && peakVertex->distance + arr[peakVertex->vertexName][i] < ptr->distance ){
ptr->distance = arr[peakVertex->vertexName][i] + peakVertex->distance;
ptr->previousVertex = peakVertex->vertexName;
break;
}
ptr =ptr->next;
}
}
}
}
}
for(int i=1;i<=v;i++){
Node * ptr = allDiscoveredVertices.head;
while(ptr->vertexName!=i){
ptr = ptr->next;
}
cout<<"\n Shortest Distance of vertex "<<i<<" is : "<<ptr->distance<<"\n";
cout<<"\n Path of the vertex "<<i<<" is : ";
allDiscoveredVertices.printingThePath(ptr);
cout<<" "<<i<<"\n";
}
return 0;
}
//CREATING THE NODE !
Node* llist::createnode(int vertexname,int distance,int previousvertex){
Node* temp =new Node;
temp->vertexName = vertexname;
temp->distance = distance;
temp->previousVertex = previousvertex;
return temp;
}
//ADDING ELEMENT IN THE LIST !
void llist::addElement(int vertexname,int distance,int previousvertex){
if(head == NULL){
Node* temp = createnode(vertexname,distance,previousvertex);
temp->next = head;
head = temp;
}else{
Node* temp = createnode(vertexname,distance,previousvertex);
Node* ptr =head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
temp->next=NULL;
ptr->next=temp;
}
}
//ADD ELEMENT AT BEGINNING !
void llist::addatbeg(int vertexname,int distance,int previousvertex){
Node * temp = createnode(vertexname,distance,previousvertex);
temp->next = head;
head = temp;
}
//PRINTING THE LINKED LIST !
void llist::display(){
Node* ptr = head;
if(head==NULL){
cout<<"\n List does not Exist !\n";
return;
}
while(ptr!=NULL){
cout<<"Vertex Name : "<<ptr->vertexName<<endl;
cout<<"Vertex Distance : "<<ptr->distance<<endl;
cout<<"Vertex Previous Vertex : "<<ptr->previousVertex<<endl<<"\n";
ptr = ptr->next;
}
cout<<"\n\n";
}
//DELETE AT POSITION !
void llist::deleteVertex(int pos){
if(head == NULL){
return;
}
if(pos==1){
Node* p = head;
head = p->next;
delete p;
}else{
int counter = 0;
Node* p = head;
while(p!=NULL){
p=p->next;
counter++;
}
if(pos>1&&pos<=counter){
p = head;
Node* prev;
for(int i=1;i<pos;i++){
prev =p;
p=p->next;
}
prev->next=p->next;
delete p;
}else{
cout<<"\nOUT OF RANGE EXCEPTION !\n\n";
}
}
}
//VALUE OF MINIMUM ELEMENT
int llist::elementMinimum(){
Node * minptr = head;
Node * ptrnext = head->next;
int min = head->distance;
while(ptrnext!=NULL){
if(ptrnext->distance<min){
min = ptrnext->distance;
minptr = ptrnext;
}
ptrnext = ptrnext->next;
}
allDiscoveredVertices.addatbeg(minptr->vertexName,minptr->distance,minptr->previousVertex);
return min;
}
//SEARCHING FOR POSITION OF MINIMUM ELEMENT
int llist::searchele(int val){
int counter=0;
Node* p = head;
if(head==NULL){
return counter;
}
while(p!=NULL){
counter++;
if(p->distance==val){
break;
}
p=p->next;
}
if(p==NULL){
counter = 0;
}
return counter;
}
//Printing the Node path and distance
void llist::printingThePath(Node* vertex){
if(vertex->previousVertex == 0){
return;
}else{
Node * ptr = head;
while(ptr->vertexName != vertex->previousVertex){
ptr = ptr->next;
}
printingThePath(ptr);
cout<<" "<<ptr->vertexName<<" -> ";
}
}