Analytics

2013年7月14日 星期日

[iOS]dijkstra in iOS

I use this algorithm to analyze Taipei MRT system.
https://github.com/dmitrikozlov/mj-dijkstra

And combine with a csv to NSDictionary code.




- (void) parseCSVFile
{

CSVParser *parser = [CSVParser new];
// get the path to the file in your xcode project's resource path
NSString *csvFilePath = [[NSBundle mainBundle] pathForResource:@"taipeiMRTstation" ofType:@"csv"];

[parser openFile:csvFilePath];

NSMutableArray *csvContent = [parser parseFile];

for (int i = 0; i < [csvContent count]; i++) {

NSLog(@"content of line %d: %@", i, [csvContent objectAtIndex:i]);
}

NSMutableDictionary *MRTmapDictionary;
NSArray *arrayOfFirstLine;
NSArray *lineArray;
NSMutableDictionary *stationDict = [[NSMutableDictionary alloc] init];

for (NSArray *contentArray in csvContent) {

NSMutableArray *relationArray = [[NSMutableArray alloc] init];

// the #0 contentArray is reference array
if ([csvContent indexOfObject:contentArray] != 0) {

NSMutableDictionary *relationDict = [[NSMutableDictionary alloc] init];

for (NSString *objectInLine in contentArray) {

// index >= 1
// means object for relation, and the value not nil

if ([contentArray indexOfObject:objectInLine] > 0 && (![objectInLine isEqualToString:@""])) {
// prepare relation dictionary
NSNumber *length = [NSNumber numberWithFloat:[objectInLine floatValue]];

[relationDict setObject:length forKey:[arrayOfFirstLine objectAtIndex:[contentArray indexOfObject:objectInLine]]];

}
}

[stationDict setObject:relationDict forKey:[contentArray objectAtIndex:0]];

} else {

// catch first line array
arrayOfFirstLine = [[NSArray alloc] initWithArray:contentArray];
}
}

MRTGraph = [[NSDictionary alloc] initWithDictionary:stationDict];

NSLog(@"the MRTGraph is %@", MRTGraph);

[parser closeFile];
}




沒有留言:

張貼留言