forked from APGRoboCop/grave_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloyd.c
158 lines (131 loc) · 3.88 KB
/
floyd.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
#include <stdio.h>
/* here's what the graph looks like (numbers in parenthesis are distance)...
(50) (40)
0 ------> 1 ------> 2 ---\
| ^ | \
| | | \(50)
| | |(45) \
\ (45)| | \
(30)\ | | |
\ | V V
\--> 3 ------> 4 ------> 5
(55) (35)
Note that the only 2-way path is from 2 to 3 (and 3 to 2).
All other paths are one-way paths.
Here's the paths and distances:
0 -----> 1 (distance = 50 units)
1 -----> 2 (distance = 40 units)
1 -----> 3 (distance = 30 units)
2 -----> 3 (distance = 45 units)
2 -----> 4 (distance = 50 units)
3 -----> 2 (distance = 45 units)
3 -----> 4 (distance = 55 units)
4 -----> 5 (distance = 35 units)
All paths which aren't possible are indicated by -1 in the shortest_path table
The shortest_path array is a 2 dimensional array of all waypoints (i.e. if
there are 50 waypoints then the table will be 50 X 50 in size. The rows
(running down the table) indicate the starting index (0 to N) and the columns
(running across the table) indicate the ending index. So if you wanted to
know the distance between waypoint 37 and waypoint 42 you would look at
shortest_path[37][42]. If you wanted to know the distance between waypoints
42 and 37, you would look at shortest_path[42][37]. NOTE!, these 2 DO NOT
HAVE TO BE THE SAME!!! One way paths may mean that shortest_path[37][42]
is 150 units and shortest_path[42][37] is -1 (indicating you can't go that
way!!!)
*/
#define MAX 6
int shortest_path[MAX][MAX] = {
{ 0, 50, -1, -1, -1, -1},
{-1, 0, 40, 30, -1, -1},
{-1, -1, 0, 45, 50, -1},
{-1, -1, 45, 0, 55, -1},
{-1, -1, -1, -1, 0, 35},
{-1, -1, -1, -1, -1, 0}
};
int from_to[MAX][MAX];
void floyd(void)
{
int x, y, z;
int changed = 1;
int distance;
for (y=0; y < MAX; y++)
{
for (z=0; z < MAX; z++)
{
from_to[y][z] = z;
}
}
while (changed)
{
changed = 0;
for (x=0; x < MAX; x++)
{
for (y=0; y < MAX; y++)
{
for (z=0; z < MAX; z++)
{
if ((shortest_path[y][x] == -1) || (shortest_path[x][z] == -1))
continue;
distance = shortest_path[y][x] + shortest_path[x][z];
if ((distance < shortest_path[y][z]) ||
(shortest_path[y][z] == -1))
{
shortest_path[y][z] = distance;
from_to[y][z] = from_to[y][x];
changed = 1;
}
}
}
}
}
}
void main(void)
{
int a, b;
char buffer[20];
// run Floyd-Warshall algorithm on the shortest_path table...
floyd();
// initialize any unreachable paths based on shortest_path table...
for (a=0; a < MAX; a++)
{
for (b=0; b < MAX; b++)
{
if (shortest_path[a][b] == -1)
from_to[a][b] = -1;
}
}
printf("shortest_path:\n");
for (a=0; a < MAX; a++)
{
for (b=0; b < MAX; b++)
printf("%5d,", shortest_path[a][b]);
printf("\n");
}
printf("\n");
printf("from_to:\n");
for (a=0; a < MAX; a++)
{
for (b=0; b < MAX; b++)
printf("%2d,", from_to[a][b]);
printf("\n");
}
printf("\n");
printf("enter start node (0-5):");
gets(buffer);
a = atoi(buffer);
printf("\nenter end node (0-5):");
gets(buffer);
b = atoi(buffer);
if (shortest_path[a][b] != -1)
{
printf("\n\nShortest path from %d to %d is:\n", a, b);
while (a != b)
{
printf("from %d goto %d (total distance remaining=%d)\n",
a, from_to[a][b], shortest_path[a][b]);
a = from_to[a][b];
}
}
else
printf("You can't get from %d to %d!\n", a, b);
}