-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPath.m
More file actions
235 lines (192 loc) · 8.32 KB
/
ShortestPath.m
File metadata and controls
235 lines (192 loc) · 8.32 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
//
// ShortestPath.m
// ShortestPath
//
// Created by Yoo Rinjae on 12. 11. 8..
// Copyright (c) 2012년 DearMai. All rights reserved.
//
#import "ShortestPath.h"
#import "Haversine.h"
#import "SignificantPoint.h"
#import "Airport.h"
#import "FlightRoute.h"
#define MAX_WEIGHT 99999999
@implementation ShortestPath {
NSArray *points, *routes, *airports;
}
- (id)initWithPoints:(NSArray *)aPoints andRoutes:(NSArray *)aRoutes andAirports:(NSArray *)aAirports {
if([self init]){
points = aPoints;
routes = aRoutes;
airports = aAirports;
}
return self;
}
- (SignificantPoint *)getNearestPointWithAirport:(Airport *)newAirport {
double min = 999999999999999;
int index = -1;
int i = 0;
Haversine *haversine;
for(SignificantPoint *point in points){
haversine = [[Haversine alloc] initWithLat1:newAirport.latitude
lon1:newAirport.longitude
lat2:point.latitude
lon2:point.longitude];
double rt = [haversine toKilometers];
if(min > rt) {
min = rt;
index = i;
}
i++;
}
return [points objectAtIndex:index];
}
- (int)significantPointIndexWithName:(NSString *)newName {
for (int i = 0; i < [points count]; i++){
SignificantPoint *point = [points objectAtIndex:i];
if([point.name isEqualToString:newName] == YES) {
return i;
}
}
return -1;
}
- (NSArray *)calcShortestPathWithDeparturePoint:(SignificantPoint *)newDeparturePoint
andArrivalPoint:(SignificantPoint *)newArrivalPoint {
NSUInteger countPoint = [points count];
double matrix[countPoint][countPoint];
BOOL debug = YES;
if(debug) printf("\n\n------------------------- Make matrix -------------------------\n\n");
for(int i = 0; i < countPoint; i++){
for(int j = 0; j < countPoint; j++){
matrix[i][j] = MAX_WEIGHT;
}
}
for(int i = 0; i < countPoint; i++) {
SignificantPoint *thisPoint = [points objectAtIndex:i];
matrix[i][i] = 0;
if(debug) printf("Check [%6s] point.\n", [thisPoint.name UTF8String]);
for(FlightRoute *route in routes) {
int j = 0;
for(NSString *pointName in route.pointNameArray) {
int pointIndex = [self significantPointIndexWithName:pointName];
// if(DEBUG && [route.name isEqualToString:@"Y64"]){
// printf(" %s(%d) ", [pointName UTF8String], pointIndex);
// }
j++;
if(pointIndex == i && j != [route.pointNameArray count]) {
NSString *strNextPoint = [route.pointNameArray objectAtIndex:j];
if(debug) printf("\t%s-%s", [route.name UTF8String], [strNextPoint UTF8String]);
int nextPointIndex = [self significantPointIndexWithName:strNextPoint];
SignificantPoint *nextPoint = [points objectAtIndex:nextPointIndex];
Haversine *haversign = [[Haversine alloc] initWithLat1:thisPoint.latitude
lon1:thisPoint.longitude
lat2:nextPoint.latitude
lon2:nextPoint.longitude];
double weight = [haversign toKilometers];
matrix[i][nextPointIndex] = weight;
}
}
//printf("\n");
// 뒤로도 가능하기 때문에 왕복
//j = (int)[route.pointNameArray count];
// for(int k = (int)[route.pointNameArray count] - 1; k >= 0; k--) {
// NSString *pointName = [route.pointNameArray objectAtIndex:k];
// int pointIndex = [self significantPointIndexWithName:pointName];
//
// if(pointIndex == i && k != 0) {
// NSString *strNextPoint = [route.pointNameArray objectAtIndex:k - 1];
// int nextPointIndex = [self significantPointIndexWithName:strNextPoint];
// SignificantPoint *nextPoint = [points objectAtIndex:nextPointIndex];
// Haversine *haversign = [[Haversine alloc] initWithLat1:thisPoint.latitude
// lon1:thisPoint.longitude
// lat2:nextPoint.latitude
// lon2:nextPoint.longitude];
// double weight = [haversign toKilometers];
//
// matrix[i][nextPointIndex] = weight;
// }
// }
}
if(debug) printf("\n");
}
printf(" ");
for(int i = 0; i < countPoint; i++){
printf("%2d", i % 100);
}
printf("\n");
for(int i = 0; i < countPoint; i++){
printf("%3d", i);
for(int j = 0; j < countPoint; j++){
if(matrix[i][j] == 0.0) printf(" 0");
else if(matrix[i][j] == MAX_WEIGHT) printf(" ");
else if(matrix[i][j] > 0.0) printf(" 1");
else printf(" ");
}
printf("\n");
}
double distance[countPoint];
BOOL visited[countPoint];
int path[countPoint];
const NSUInteger departureIndex = [self significantPointIndexWithName:newDeparturePoint.name];
const NSUInteger arrivalIndex = [self significantPointIndexWithName:newArrivalPoint.name];
for(int i = 0; i < countPoint; i++){
distance[i] = matrix[departureIndex][i];
visited[i] = NO;
if(distance[i] > 0 && distance[i] < MAX_WEIGHT) {
path[i] = (int)departureIndex;
} else {
path[i] = -1;
}
}
distance[departureIndex] = 0;
visited[departureIndex] = true;
for(int i = 0; i < countPoint; i++) {
double min = INT_MAX;
NSUInteger minIndex = -1;
for(int j = 0; j < countPoint; j++){
if(visited[j] == YES) continue;
if(distance[j] >= min) continue;
min = distance[j];
minIndex = j;
}
visited[minIndex] = true;
for(int j = 0; j < countPoint; j++){
if(visited[j] == YES) continue;
if(distance[minIndex] + matrix[minIndex][j] >= distance[j]) continue;
distance[j] = distance[minIndex] + matrix[minIndex][j];
path[j] = (int)minIndex;
}
}
NSLog(@"거리 : %fKm\n", distance[arrivalIndex]);
// 결과(전체 포인트에 대한 경로)
// for (int i = 0; i < countPoint; i++){
// int cv = i;
// SignificantPoint *point = [points objectAtIndex:cv];
// printf("To %s : %s", [point.name UTF8String], [point.name UTF8String]);
//
// while(path[cv] >= 0){
// SignificantPoint *point1 = [points objectAtIndex:path[cv]];
// printf("<-[%s]", [point1.name UTF8String]);
// cv = path[cv];
// }
//
// printf("\n");
// }
// 최종 결과 산출
NSMutableArray *shortestPath = [[NSMutableArray alloc] init];
int cv = arrivalIndex;
while(path[cv] >= 0) {
SignificantPoint *point = [points objectAtIndex:path[cv]];
[shortestPath addObject: point];
cv = path[cv];
}
return shortestPath;
}
- (NSArray *)main:(Airport *)newDepartureAirport andArrivalAirport:(Airport *)newArrivalAirport {
SignificantPoint *depaturePoint = [self getNearestPointWithAirport:newDepartureAirport];
SignificantPoint *arrivalPoint = [self getNearestPointWithAirport:newArrivalAirport];
NSLog(@"%@, %f, %f", depaturePoint.name, depaturePoint.latitude, depaturePoint.longitude);
NSLog(@"%@, %f, %f", arrivalPoint.name, arrivalPoint.latitude, arrivalPoint.longitude);
return [self calcShortestPathWithDeparturePoint:depaturePoint andArrivalPoint:arrivalPoint];
}
@end