博客
关于我
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实现fischer yates shuffle洗牌算法(附完整源码)
    查看>>
    Objective-C实现FisherYates Shuffle洗牌算法(附完整源码)
    查看>>
    Objective-C实现fisherYates洗牌算法(附完整源码)
    查看>>
    Objective-C实现FloodFill洪水填充函数算法(附完整源码)
    查看>>
    Objective-C实现floor向下取整算法(附完整源码)
    查看>>
    Objective-C实现Floyd-Warshall算法(附完整源码)
    查看>>
    Objective-C实现FPmax算法(附完整源码)
    查看>>
    Objective-C实现frequency finder频率探测器算法(附完整源码)
    查看>>
    Objective-C实现FTP上传文件(附完整源码)
    查看>>
    Objective-C实现FTP文件上传(附完整源码)
    查看>>
    Objective-C实现FTP文件下载(附完整源码)
    查看>>
    Objective-C实现fuzzy operations模糊运算算法(附完整源码)
    查看>>
    Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
    查看>>
    Objective-C实现gamma recursive伽玛递归算法(附完整源码)
    查看>>
    Objective-C实现gamma 伽玛功能算法(附完整源码)
    查看>>
    Objective-C实现gauss easte高斯复活节日期算法(附完整源码)
    查看>>
    Objective-C实现gaussian filter高斯滤波器算法(附完整源码)
    查看>>
    Objective-C实现gaussian naive bayes高斯贝叶斯算法(附完整源码)
    查看>>
    Objective-C实现gaussian高斯算法(附完整源码)
    查看>>
    Objective-C实现geometric series几何系列算法(附完整源码)
    查看>>