Get a shortest route based on Dijkstra Algorithm.
- (NSDictionary *)graphFromFile {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"plist"];
NSDictionary *graphFromFile = [[NSDictionary alloc] initWithContentsOfFile:filePath];
return graphFromFile;
}
- (NSDictionary *)graphFromCode {
return @{@"A":@{@"B":@(4),@"C":@(0.9)},
@"B":@{@"E":@(4)},
@"C":@{@"B":@(2),@"D":@(4)},
@"D":@{@"E":@(4)}
};
}
JLDijkstra.h
JLDijkstra.m
import header
#import "JLDijkstra.h"
create a map graph
NSDictionary *graph = [self graphFromFile];
OR
NSDictionary *graph = [self graphFromCode];
Execute Dijkstra
JLDijkstra *dijkstra = [[JLDijkstra alloc] init];
JLDijkstraResult *result = [dijkstra getRouteFromGraph:graph
startVertex:@"A"
endVertex:@"E"];
See the result
if(result) {
NSLog(@"route : %@", result.route);
NSLog(@"distance : %.1f", result.distance);
}
else {
NSLog(@"no result");
}
route : (
A,
C,
B,
E
)
distance : 6.9
A sample project is included. :-D
JLDijkstra is available under the MIT license. See the LICENSE file for more info.