博客
关于我
Objective-C实现bfs 最短路径算法(附完整源码)
阅读量:792 次
发布时间:2023-02-17

本文共 3041 字,大约阅读时间需要 10 分钟。

Objective-C??BFS??????????????

Objective-C??????????????????iOS?macOS??????????????????Objective-C????????????BFS??????????????????????????????????????????????????????????Objective-C???BFS???

BFS???????????????????????????????????DFS????BFS????????????????????????????????????????

BFS????

  • ???????????BFS???????????????????????????????????Objective-C?????????????????????????????????

  • ?????????BFS??????????????????????????????????????????????

  • ???????BFS???????????????????????????????????????????????????????

  • ????????????????????????????????????????????????????????????????????

  • Objective-C????

    ?????BFS?????Objective-C?????

    #import 
    #import "Graph.h"@interface Graph : NSObject@property (nonatomic, assign) NSInteger nodeCount;@property (nonatomic, strong) NSMutableDictionary * adjacencyList;@end@implementation Graph- (void) initializeGraphWithNodes:(NSInteger)nodeCount { self.nodeCount = nodeCount; self.adjacencyList = [NSMutableDictionary new]; for (NSInteger i = 0; i < nodeCount; i++) { [self.adjacencyList setValue:[NSMutableArray new] forKey:[NSString stringWithFormat:@"%d", i]]; }}- (void) addEdgeBetweenNodes:(NSInteger)from to:(NSInteger)to { [[self.adjacencyList valueForKey:[NSString stringWithFormat:@"%d", from]] addObject:to]; [[self.adjacencyList valueForKey:[NSString stringWithFormat:@"%d", to]] addObject:from];}- (NSArray *) shortestPathFrom:(NSInteger)start to:(NSInteger)end { if (start == end) { return [NSArray arrayWithObject:@-1]; } BOOL *visited = [malloc sizeof(BOOL) * self.nodeCount]; visited[start] = true; NSInteger *distance = [malloc sizeof(NSInteger) * self.nodeCount]; distance[start] = 0; NSMutableArray *queue = [NSMutableArray new]; [queue addObject:start]; while (!queue.isEmpty) { NSInteger current = [queue.firstObject integerValue]; NSInteger currentIndex = current; for (NSInteger neighbor in [self.adjacencyList valueForKey:[NSString stringWithFormat:@"%d", currentIndex]]) { if (!visited[neighbor]) { visited[neighbor] = true; distance[neighbor] = distance[currentIndex] + 1; [queue addObject:neighbor]; } } [queue removeObjectAtIndex:0]; } if (!visited[end]) { return nil; } NSInteger current = end; NSMutableArray *path = [NSMutableArray new]; while (current != start) { [path addObject:[NSString stringWithFormat:@"%d", current]]; current = [distance objectAtIndex:current]; } [path reverse]; return path;}@end

    ????

  • Graph??????????Objective-C????????????????????????????????

  • initializeGraphWithNodes??????????????????????????

  • addEdgeBetweenNodes???????????????

  • shortestPathFrom???????????????????????????????????????

  • ????

    BFS?????????????????????????????????2D????????????????????BFS??????????????????????

    ??

    • ??????O(V + E)???V?????E????
    • ??????O(V + E)?
    • ???????????????????

    BFS????????????????????????????????Objective-C???????????

    转载地址:http://vdnfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现3n+1猜想(附完整源码)
    查看>>
    Objective-C实现9x9乘法表算法(附完整源码)
    查看>>
    Objective-C实现9×9二维数组数独算法(附完整源码)
    查看>>
    Objective-C实现A*(A-Star)算法(附完整源码)
    查看>>
    Objective-C实现A-Star算法(附完整源码)
    查看>>
    Objective-C实现abbreviation缩写算法(附完整源码)
    查看>>
    Objective-C实现ABC人工蜂群算法(附完整源码)
    查看>>
    Objective-C实现activity selection活动选择问题算法(附完整源码)
    查看>>
    Objective-C实现AC算法(Aho-Corasick) 算法(附完整源码)
    查看>>
    Objective-C实现adaboost算法(附完整源码)
    查看>>
    Objective-C实现Adler32算法(附完整源码)
    查看>>
    Objective-C实现AES算法(附完整源码)
    查看>>
    Objective-C实现AffineCipher仿射密码算法(附完整源码)
    查看>>
    Objective-C实现aliquot sum等分求和算法(附完整源码)
    查看>>
    Objective-C实现all combinations所有组合算法(附完整源码)
    查看>>
    Objective-C实现all permutations所有排列算法(附完整源码)
    查看>>
    Objective-C实现all subsequences所有子序列算法(附完整源码)
    查看>>
    Objective-C实现AlphaNumericalSort字母数字排序算法(附完整源码)
    查看>>
    Objective-C实现alternate disjoint set不相交集算法(附完整源码)
    查看>>
    Objective-C实现alternative list arrange备选列表排列算法(附完整源码)
    查看>>