-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijstra_shortest_path.c
660 lines (640 loc) · 31.5 KB
/
Dijstra_shortest_path.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*Using the Dijkstra algorithm to find out the shortest way from original to the
destination.And out put the direction in each intersection.*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <string.h>
#include"read_map.h"
struct shorter_path{//this struct is to store a group of lenthes and choose the optional one
float distance;
int unique_from;//lenth's start node
int unique_to;//lenth's end node
};
struct search_path{//after each has his father in the shortest pathes,we need to find all the shortest path to the destination,
int pre_label;
int unique;
};
struct each_path{//later I will create a struct array to store each shortest path,each path is a list.
int unique_num;
int x;
int y;
float distance;
float section_distance;//from the previous turning intersection to the present turning intersection
struct each_path *next_node;
};
typedef struct each_path *path_link;
int Index(int );//use the unique number to find the poisition in the adjance list
void Insert_start_destination(int ,int ,int ,char *,char *);//insert the start and destination node in the adjance list
void get_BFS_list(struct search_path *,int ,int );//I will use BFS search method to find out all the shortest pathes
void Dijkstra();//main algorithm
void get_the_ways(struct each_path *,struct search_path *);//store all the shortest pathes in a struct array
void turn_by_turn(struct each_path *,int );//output each path in turn by turn method
main()
{
FILE *f;
char filename[50];
printf("input the name of the file:");
scanf("%s",filename);//the file's name can be absolute path,include the .txt example: D:\\file1\\file2\\test.txt
f=fopen(filename,"r");
if (f==NULL)//if can not open the file,stop the program
{
printf("OMG,the file is not existed!\n");
system("pause");
return;
}
get_the_map(f);//get the information of map
getchar();
printf("input the start street name:");
gets(start_street);
printf("input the start house number:");
scanf("%d",&start_house);
getchar();
printf("input the destination street name:");
gets(destination_street);
printf("input the destination house number:");
scanf("%d",&destination_house);
Insert_start_destination(street_num,start_house,destination_house,start_street,destination_street);
if (start_unique=='\0' || destination_unique=='\0')//if we don't get the unique number for start and destination nodes,we stop.
{
system("pause");
return 0;
}
if (start_unique == destination_unique)//if the start node has the same coordinate as the destination node.stop.
{
printf("the start point is the same as destination!\n");
system("pause");
return 0;
}
int k=1;
int i,j;
Dijkstra();//Dijkstra is to store each node's father unique numbers
struct search_path BFS_array[50];
/*the destination node has his father nodes,and these father nodes also have their own father nodes.
So it's a tree,and I use the BFS method to find every shortest path.
*/
int label;
label=Index(destination_unique);
BFS_array[1].unique=destination_unique;
i=1;
int from,present;//"from" and "present" are labels in the array
from=2;
j=from;
present=from+adj_list[label].father_num;
while (i<=adj_list[label].father_num)//begin at the destination,so that store the destination's father firstly
{
BFS_array[j].unique=adj_list[label].father_unique_num[i];
BFS_array[j].pre_label=1;
j++;
i++;
}
get_BFS_list(BFS_array,from,present);//"from" to store the present first father's position,and "present" to store the array's present position
i=1;
int counter=0;
while(BFS_array[i].unique!='\0')//"counter" is to get the total number of the shortest pathes
{
if (BFS_array[i].unique==start_unique)
counter++;
i++;
}
struct each_path shortest_ways[counter+1];
get_the_pathes(shortest_ways,BFS_array);
int ways=counter;
for (i=1;i<=ways;i++)//for each shortest path,store each node's distance between he and his father
{
path_link present;
present=&shortest_ways[i];
float pre_x,pre_y;
if (present->unique_num==1)//if the unique number is 1,do some special operations
{
pre_x=start_x;
pre_y=start_y;
}
else
{
present->x=present->unique_num/100;
present->y=present->unique_num%100;
pre_x=present->x;
pre_y=present->y;
}
present=present->next_node;
while(present!=NULL)
{
float X,Y;
if (present->unique_num==2)//if the next node is destination node,also do some special operations
{
X=pre_x-destination_x;
Y=pre_y-destination_y;
present->distance=sqrt(pow(X,2)+pow(Y,2))*mile;
}
else
{
present->x=present->unique_num/100;
present->y=present->unique_num%100;
X=pre_x-present->x;
Y=pre_y-present->y;
present->distance=sqrt(pow(X,2)+pow(Y,2))*mile;
pre_x=present->x;
pre_y=present->y;
}
present=present->next_node;
}
}
turn_by_turn(shortest_ways,ways);//turn by turn output each shortest path
system("pause");
}
void turn_by_turn(struct each_path *shortest_ways,int ways)//"ways" save the number of the shorest ways,shortest_ways[] save all the shortest pathes
{
int i;
printf("/---------------------Next Path-------------------/\n");
for (i=1;i<=ways;i++)
{
path_link present,next,future;
present=&shortest_ways[i];
next=present->next_node;
future=next->next_node;
next->section_distance=next->distance;
float x1,yl,x2,y2,x3,y3;
if (present->unique_num==1)//if the presetn node is start node,take the x and y value in special ways
{
x1=start_x;
yl=start_y;
}
else if (present->unique_num!=1)
{
x1=present->x;
yl=present->y;
}
if (next->unique_num==2)//if the next node is destination node,take the x and y value in special ways
{
x2=destination_x;
y2=destination_y;
}
else if (next->unique_num!=2)
{
x2=next->x;
y2=next->y;
}
if (x1>x2 && yl>y2)//diceide the first direction
printf("Head SouthWest ");
else if (x1<x2 && yl>y2)
printf("Head SouthEast ");
else if (x1>x2 && yl<y2)
printf("Head NorthWest ");
else if (x1<x2 && yl<y2)
printf("Head NorthEest ");
else if (x1>x2 && yl==y2)
printf("Head West ");
else if (x1<x2 && yl==y2)
printf("Head Eest ");
else if (x1==x2 && yl<y2)
printf("Head North ");
else if (x1==x2 && yl>y2)
printf("Head South ");
int present_label,future_label,next_label;
int j,k;
char start_street[20];
present_label=Index(present->unique_num);
next_label=Index(next->unique_num);
for (j=1;j<=adj_list[present_label].present_num;j++)//find the start_street name
{
int get_name=0;
for (k=1;k<=adj_list[next_label].present_num;k++)
if (strcmp(adj_list[present_label].street_name[j],adj_list[next_label].street_name[k])==0)
{
get_name=1;
strcpy(start_street,adj_list[present_label].street_name[j]);
break;
}
if (get_name==1)
break;
}
printf("on %s.\n",start_street);//start_street must be the same street for both first node and the second node
if (future==NULL)//if future==NULL means they are very closed.
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("stop on street %s,house number is %d,in %d foot.\n",destination_street,destination_house,feet);
else if (feet==0)
printf("stop on street %s,house number is %d,in %.1f miles.\n",destination_street,destination_house,miles);
else if (miles>0 && feet>0)
printf("stop on street %s,house number is %d,in %.1f miles and %d foot.\n",destination_street,destination_house,miles,feet);
}
while (future!=NULL)
{
present_label=Index(present->unique_num);
future_label=Index(future->unique_num);
next_label=Index(next->unique_num);
int same_street=0;//to check whether the future node has the same as the previous two nodes
char same_street_name[20];
for (j=1;j<=adj_list[present_label].present_num;j++)
{
int get_name=0;
for (k=1;k<=adj_list[next_label].present_num;k++)
if (strcmp(adj_list[present_label].street_name[j],adj_list[next_label].street_name[k])==0)
{
get_name=1;
strcpy(same_street_name,adj_list[present_label].street_name[j]);
break;
}
if (get_name==1)
break;
}
for (k=1;k<=adj_list[future_label].present_num;k++)
if (strcmp(same_street_name,adj_list[future_label].street_name[k])==0)
{
same_street=1;
break;
}
if (same_street==1)//if they are in the same street
{
future->section_distance=future->distance+next->section_distance;
future=future->next_node;
next=next->next_node;
present=present->next_node;
if (future==NULL)
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("stop on street %s,house number is %d,in %d foot.\n",destination_street,destination_house,feet);
else if (feet==0)
printf("stop on street %s,house number is %d,in %.1f miles.\n",destination_street,destination_house,miles);
else if (miles>0 && feet>0)
printf("stop on street %s,house number is %d,in %.1f miles and %d foot.\n",destination_street,destination_house,miles,feet);
}
while (future!=NULL)
{
int go_ahead=0;
future_label=Index(future->unique_num);
for (k=1;k<=adj_list[future_label].present_num;k++)//if they are still in a same street,continue to find.Until emerge a node in a different street.
if (strcmp(same_street_name,adj_list[future_label].street_name[k])==0)
{
go_ahead=1;
future->section_distance=future->distance+next->section_distance;
future=future->next_node;
next=next->next_node;
present=present->next_node;
break;
}
if (future==NULL)
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("stop on street %s,house number is %d,in %d foot.\n",destination_street,destination_house,feet);
else if (feet==0)
printf("stop on street %s,house number is %d,in %.1f miles.\n",destination_street,destination_house,miles);
else if (miles>0 && feet>0)
printf("stop on street %s,house number is %d,in %.1f miles and %d foot.\n",destination_street,destination_house,miles,feet);
}
if (go_ahead==0)//has to stop and turn a direction
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("go to the vertex %d %d in %d foot.And then ",next->x,next->y,feet);
else if (feet==0)
printf("go to the vertex %d %d in %.1f miles.And then ",next->x,next->y,miles);
else if (miles>0 && feet>0)
printf("go to the vertex %d %d in %.1f miles and %d foot.And then ",next->x,next->y,miles,feet);
if (present->unique_num==1 && future->unique_num==2)
{
x1=start_x;
yl=start_y;
x2=next->x;
y2=next->y;
x3=destination_x;
y3=destination_y;
}
else if (present->unique_num==1 && future->unique_num!=2)
{
x1=start_x;
yl=start_y;
x2=next->x;
y2=next->y;
x3=future->x;
y3=future->y;
}
else if (present->unique_num!=1 && future->unique_num==2)
{
x1=present->x;
yl=present->y;
x2=next->x;
y2=next->y;
x3=destination_x;
y3=destination_y;
}
else if (present->unique_num!=1 && future->unique_num!=2)
{
x1=present->x;
yl=present->y;
x2=next->x;
y2=next->y;
x3=future->x;
y3=future->y;
}
direction(x1,yl,x2,y2,x3,y3);//output the direction by using three pairs of coordinates
next_label=Index(next->unique_num);
char next_street[10];
for (j=1;j<=adj_list[next_label].present_num;j++)//find the street name after turn a direction
{
int get_name=0;
for (k=1;k<=adj_list[future_label].present_num;k++)
if (strcmp(adj_list[next_label].street_name[j],adj_list[future_label].street_name[k])==0)
{
get_name=1;
strcpy(next_street,adj_list[next_label].street_name[j]);
break;
}
if (get_name==1)
break;
}
printf("onto %s\n",next_street);//out put the next street name
future=future->next_node;
next=next->next_node;
present=present->next_node;
next->section_distance=next->distance;
if (future==NULL)
{
float distance,miles,decimal;
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("stop on street %s,house number is %d,in %d foot.\n",destination_street,destination_house,feet);
else if (feet==0)
printf("stop on street %s,house number is %d,in %.1f miles.\n",destination_street,destination_house,miles);
else if (miles>0 && feet>0)
printf("stop on street %s,house number is %d,in %.1f miles and %d foot.\n",destination_street,destination_house,miles,feet);
}
break;
}
}
}
if (same_street==0)
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("go to the vertex %d %d in %d foot.And then ",next->x,next->y,feet);
else if (feet==0)
printf("go to the vertex %d %d in %.1f miles.And then ",next->x,next->y,miles);
else if (miles>0 && feet>0)
printf("go to the vertex %d %d in %.1f miles and %d foot.And then ",next->x,next->y,miles,feet);
if (present->unique_num==1 && future->unique_num==2)
{
x1=start_x;
yl=start_y;
x2=next->x;
y2=next->y;
x3=destination_x;
y3=destination_y;
}
else if (present->unique_num==1 && future->unique_num!=2)
{
x1=start_x;
yl=start_y;
x2=next->x;
y2=next->y;
x3=future->x;
y3=future->y;
}
else if (present->unique_num!=1 && future->unique_num==2)
{
x1=present->x;
yl=present->y;
x2=next->x;
y2=next->y;
x3=destination_x;
y3=destination_y;
}
else if (present->unique_num!=1 && future->unique_num!=2)
{
x1=present->x;
yl=present->y;
x2=next->x;
y2=next->y;
x3=future->x;
y3=future->y;
}
direction(x1,yl,x2,y2,x3,y3);
char next_street[10];
for (j=1;j<=adj_list[next_label].present_num;j++)//find the street name after turn a direction
{
int get_name=0;
for (k=1;k<=adj_list[future_label].present_num;k++)
if (strcmp(adj_list[next_label].street_name[j],adj_list[future_label].street_name[k])==0)
{
get_name=1;
strcpy(next_street,adj_list[next_label].street_name[j]);
break;
}
if (get_name==1)
break;
}
printf("onto %s\n",next_street);//output the next street name
future=future->next_node;
next=next->next_node;
present=present->next_node;
next->section_distance=next->distance;
if (future==NULL)
{
float distance,miles,decimal;//to find out the miles in 1 decimal place and the foot in integer
int integer,feet;
distance=next->section_distance;
distance=distance*10;
integer=distance/1;
decimal=distance-integer;
miles=(float)integer/10;
feet=5280*decimal/10;
if (miles==0)
printf("stop on street %s,house number is %d,in %d foot.\n",destination_street,destination_house,feet);
else if (feet==0)
printf("stop on street %s,house number is %d,in %.1f miles.\n",destination_street,destination_house,miles);
else if (miles>0 && feet>0)
printf("stop on street %s,house number is %d,in %.1f miles and %d foot.\n",destination_street,destination_house,miles,feet);
}
}
}
if (i<ways)//if the path is over
printf("/---------------------Next Path-------------------/\n");
}
}
int Index(int unique)//get the label from the adjance list
{
int i=1;
while (unique!=adj_list[i].unique_num)
i++;
return i;
}
get_the_pathes(struct each_path *shortest_ways,struct search_path *BFS_array)//after having a BFS array,then we will bulid a struct array to store all the shortest path
{
int j=1;
int i;
for (i=2;BFS_array[i].unique!='\0';i++)
{
if (BFS_array[i].unique == start_unique)//if we find the start node,then go back to find his child,and use his child to find the next child
{//in this time,all the data in the array will have only one child
int label;
path_link current,newptr;
current=&shortest_ways[j];
shortest_ways[j].unique_num=start_unique;
//current=&shortest_ways[j];
label=BFS_array[i].pre_label;
while (label!=1)//label==1 means find the start node.
{
newptr=malloc(sizeof(struct each_path));
newptr->unique_num=BFS_array[label].unique;
newptr->next_node=NULL;
current->next_node=newptr;
current=newptr;
label=BFS_array[label].pre_label;
}
newptr=malloc(sizeof(struct each_path));
newptr->unique_num=destination_unique;
newptr->next_node=NULL;
current->next_node=newptr;
current=newptr;
j++; //j is to record how many shortest path we have
}
}
}
void Dijkstra()
{
int visited_set[50];
int i,j;
visited_set[1]=start_unique;
int set_num=1;//set_num is to store how many visited nodes already have
while(visited_set[set_num]!=destination_unique)//when the last visited node in the set is not the destination,continue
{
i=1;
j=1;
int label;
struct shorter_path lenth_compare[20]={0};//lenth_compare[] is to store all the protential link
while (i<=set_num)
{
label=Index(visited_set[i]);//visited_set[i] is an unique number,label is to find this unique number's position in the adjance list
adj_link temp;
temp=&adj_list[label];
temp=temp->next_node;
while (temp!=NULL)//temp is the child of the current node in the set
{
int n;
for (n=1;n<=set_num;n++)
if (visited_set[n]==temp->unique_num)
break;
if (n>set_num)//if this node has never been visited,then store its lenth
{
float a;
a=temp->distance+adj_list[label].distance_to_start_node;//"a" is to store the distance from start node to the father node plus distance from father to his child
lenth_compare[j].distance=a;
lenth_compare[j].unique_from=adj_list[label].unique_num;//unique_from is to record the father's unique number
lenth_compare[j].unique_to=temp->unique_num;//unique_to is to record the current node's unique number
j++;
}
temp=temp->next_node;
}
i++;
}
int k=1;
float min_distance;
min_distance=lenth_compare[k].distance;
k++;
while (k<j)//j repersents the number of the protential links we are going to compare
{
if (min_distance>lenth_compare[k].distance)//get the shortest one
min_distance=lenth_compare[k].distance;
k++;
}
for (k=1;k<j;k++)
{
if (min_distance==lenth_compare[k].distance)//if this is the shortest one
{
int num_of_father=1;
int temp;
temp=lenth_compare[k].unique_to;
label=Index(temp);
while (k<j)//this loop is to find out how many different fathers the "unique_to" has
{
if (lenth_compare[k].distance==min_distance && lenth_compare[k].unique_to==temp)
{
adj_list[label].distance_to_start_node=min_distance;
adj_list[label].father_unique_num[num_of_father]=lenth_compare[k].unique_from;
num_of_father++;//increase the number of his father
}
k++;
}
adj_list[label].father_num=num_of_father-1;
set_num++;//the number of the visited set increases
visited_set[set_num]=temp;//take this node's unique into the visited set
break;
}
}
}
}
void get_BFS_list(struct search_path *BFS_array,int from,int present)
{
int stop_label;
int flag=0;
stop_label=present;//Every time stop_label is equal to the present.
while (from<stop_label)//when the present father is over,the loop is finished.
{
int unique_num;//is the current father's unique number
int label;
int times;
int i;
unique_num=BFS_array[from].unique;
if (unique_num==start_unique)
{
from++;
continue;
}
label=Index(unique_num);
times=adj_list[label].father_num;
for (i=1;i<=times;i++)
{
BFS_array[present].unique=adj_list[label].father_unique_num[i];
BFS_array[present].pre_label=from;
present++;
}
flag=1;//if the "from" is not small than stop_label,then means there is no children come out.then the function terminates.Once the loop is executed,means has at least one child
from++;
}
if (flag==1)
return get_BFS_list(BFS_array,from,present);
}