-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraProject.c
347 lines (318 loc) · 9.97 KB
/
DijkstraProject.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include<unistd.h>
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
//struct to make the graph from
struct graphNode
{
int weight;
char town [100];
};
typedef struct graphNode* edge;
//variable that holds the size of graph (maximum number of nodes in the file)
int numOfNodes = 0;
edge **graph;
//function to check if a string is a number
int isNumber(char* s)
{
for (int i = 0; i < strlen(s); i++)
if (!isdigit(s[i]))
return 0;
return 1;
}
//function to read from the file and save the result to the graph array
void ReadFromFile()
{
//Read the first time to calculate the maximum nodes number
FILE *fp;
fp = fopen("segments.txt", "r");
while(!feof(fp))
{
char line[500];
int v1, v2;
fgets(line, 500, fp);
char* token;
token = strtok(line, " ");
v1 = atoi(token);
token = strtok(NULL, " ");
v2 = atoi(token);
numOfNodes = MAX(MAX(numOfNodes, v1), v2);
}
fclose(fp);
numOfNodes++;
//create graph and reserve space for all the nodes
graph = (edge**)malloc(numOfNodes*sizeof(struct graphNode));
for(int i=0; i<numOfNodes; i++)
{
graph[i] = (edge*)malloc(numOfNodes*sizeof(struct graphNode));
for(int j=0; j<numOfNodes; j++)
{
graph[i][j] = (edge)malloc(sizeof(struct graphNode));
graph[i][j]->weight=0;
strcpy(graph[i][j]->town, "");
}
}
//Second time reading to fill the graph
fp = fopen("segments.txt", "r");
while(!feof(fp))
{
char line[500];
int v1, v2, cost;
char town[300] = "";
fgets(line, 500, fp);
char* token;
token = strtok(line, " ");
v1 = atoi(token);
token = strtok(NULL, " ");
v2 = atoi(token);
token = strtok(NULL, " ");
while(token != NULL)
{
if(!isNumber(token))
{
if(strcmp(town, "")!=0)
strcat(town," ");
strcat(town, token);
}
else
{
cost = atoi(token);
}
token = strtok(NULL, " \n");
}
graph[v1][v2]->weight=cost;
graph[v2][v1]->weight=cost;
strcpy(graph[v1][v2]->town, town);
strcpy(graph[v2][v1]->town, town);
}
fclose(fp);
}
//find the minimum distance
int least(int dist[], int visited[])
{
int min = 99999;
int indexOfMin;
for (int v=0; v<numOfNodes; v++)
{
if (visited[v] == 0 && dist[v] <= min)
min = dist[v], indexOfMin = v;
}
return indexOfMin;
}
//print the result of the operation by backtracking the process (to the console)
void printResult(int j, int prev[])
{
if(prev[j]==-1)
{
return;
}
printResult(prev[j], prev);
printf("\t%d ->\t%-20s -> %d\n",prev[j],graph[j][prev[j]]->town, j);
}
//print the result of the operation by backtracking the process (to the file)
void fPrintResult(int j, int prev[], FILE* fp)
{
if(prev[j]==-1)
{
return;
}
fPrintResult(prev[j], prev, fp);
fprintf(fp, "\t%d ->\t%-20s -> %d\n",prev[j],graph[j][prev[j]]->town, j);
}
//Dijkstra algorithm that calculates the minimum distance from a vertex to another
void Dijkstra(int from, int to)
{
int visited[numOfNodes];
int dist[numOfNodes];
int prev[numOfNodes];
for(int i=0; i<numOfNodes; i++)
{
visited[i]=0, dist[i]=99999, prev[i]=-1;
}
dist[from]=0;
for(int i=0; i<numOfNodes-1; i++)
{
int u = least(dist, visited);
if(dist[u]==99999)
{
break;
}
visited[u]=1;
for(int j=0; j<numOfNodes; j++)
{
if (visited[j]==0 && graph[u][j]->weight !=0 && dist[u] != 99999 && dist[u] + graph[u][j]->weight < dist[j])
{
dist[j] = dist[u] + graph[u][j]->weight;
prev[j]=u;
}
}
}
//print the cost and the towns past either to the console or to the file
if(visited[to]!=0)
{
printf("\n\tThe cost needed to go from vertex %d to vertex %d is: %d\n", from, to, dist[to]);
printf("\n\tThe following path is followed:\n");
}
else
{
printf("\n\tThere is no way from vertex %d to vertex %d\n", from, to);
}
printResult( to, prev);
printf("\n\t Enter 1 if you want to save the data in the file: ");
int save;
scanf("%d", &save);
if(save==1)
{
FILE*fp;
fp = fopen("route.txt", "a");
if(visited[to]!=0)
{
fprintf(fp, "The cost needed to go from vertex %d to vertex %d is: %d\n", from, to, dist[to]);
}
else
{
fprintf(fp, "There is no way from vertex %d to vertex %d\n", from, to);
}
fprintf(fp, "The following path is followed:\n");
fPrintResult( to, prev, fp);
fprintf(fp, "\n\n");
fclose(fp);
printf("\n\tData is Saved to the file successfully\n");
}
}
//Menu to be shown to the user
void Menu()
{
printf("\t----------------------------------------Menu------------------------------------------\n");
printf("\n\t Choose one of the following.\n\n") ;
printf("\t (1): Read the file and Fill the graph.\n\n") ;
printf("\t (2): Find the shortest path between two vertices using Dijkstra Algorithm.\n\n") ;
printf("\t (3): Exit from the program.\n\n");
printf("\t--------------------------------------------------------------------------------------\n");
}
//This functions clears the screen for easier using of the program
void ClearScreen()
{
system("cls");
printf("\n\t\t_______________________________________________________________________________\n\n");
}
//Function that empties the file to start writing data with each run
void EmptyFile()
{
if( access( "route.txt", F_OK ) == 0 )
{
if (remove("route.txt") == 0)
printf("Deleted successfully");
else
printf("Unable to delete the file");
}
}
int main()
{
system("color f0");
EmptyFile();
char select;
int selection;
int didReading=0;
//loop for selections
while(1)
{
Menu();
printf("\tEnter Your Selection:\n\t");
scanf("%s", &select);
if(strncmp(&(select),"0",1)>=0 && strncmp(&(select),"3",1)<=0)
{
selection = atoi(&(select));
}
else
{
ClearScreen();
printf("\tInvalid choice. You can only enter digits from 1 to 3\n");
printf("\t\t_______________________________________________________________________________\n\n");
continue;
}
switch(selection)
{
case 1:
ClearScreen();
printf("\n\t...........You have chosen option 1: Read the file and Fill the graph...........\n\n");
ReadFromFile();
didReading=1;
printf("\t The data is read and filled in the graph successfully.\n");
printf("\t\t_______________________________________________________________________________\n\n");
break;
case 2:
ClearScreen();
printf("\n\t...........You have chosen option 2: Find the shortest path between two vertices using Dijkstra Algorithm...........\n\n");
//chack if the user want to read the file
if(!didReading)
{
printf("\tYou need to read the file and fill the graph before choosing this option\n");
printf("\tEnter 1 if you want to read the file: ");
char doReading;
scanf("%s", &doReading);
if(strncmp(&(doReading),"1",1)==0)
{
ReadFromFile();
didReading=1;
printf("\n\tFile is successfully read\n");
}
else
{
printf("\tOperation was not complete. Please read the file first\n");
continue;
}
}
//loop to read two vertices each time and calculate the shortest path
while(1)
{
int from,to;
int fromFlag=0, toFlag=0;
printf("\n\tPlease Enter the starting vertex: ");
scanf("%d", &from);
printf("\tPlease Enter the vertex you want to arrive to: ");
scanf("%d", &to);
if(from>=0 && from<=numOfNodes)
{
fromFlag=1;
}
if(to>=0 && to<=numOfNodes)
{
toFlag=1;
}
if(!fromFlag || !toFlag)
{
printf("\n\tThe vertices you entered are not in the right format\n");
}
else
{
Dijkstra(from, to);
}
printf("\n\tEnter -1 to stop searching: ");
int stopReading;
scanf("%d", &stopReading);
if(stopReading==-1)
{
printf("\n\tSearching for path operation stopped\n");
break;
}
}
printf("\t\t_______________________________________________________________________________\n\n");
break;
case 3:
ClearScreen();
printf("\n\t...........You have chosen option 3: Exit from the program...........\n\n");
printf("\tThank you For using my program\n");
printf("\t\t_______________________________________________________________________________\n\n");
exit(0);
break;
default:
ClearScreen();
printf("\tWrong selection. Try again Please.\n");
printf("\t\t_______________________________________________________________________________\n\n");
}
}
return 0;
}