博客
关于我
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实现BellmanFord贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现bezier curve贝塞尔曲线算法(附完整源码)
    查看>>
    Objective-C实现bfs 最短路径算法(附完整源码)
    查看>>
    Objective-C实现BF算法 (附完整源码)
    查看>>
    Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
    查看>>
    Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
    查看>>
    Objective-C实现binary search二分查找算法(附完整源码)
    查看>>
    Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
    查看>>
    Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
    查看>>
    Objective-C实现BinarySearchTreeNode树算法(附完整源码)
    查看>>
    Objective-C实现binarySearch二分查找算法(附完整源码)
    查看>>
    Objective-C实现binomial coefficient二项式系数算法(附完整源码)
    查看>>
    Objective-C实现binomial distribution二项分布算法(附完整源码)
    查看>>
    Objective-C实现bisection二分法算法(附完整源码)
    查看>>
    Objective-C实现bisection二等分算法(附完整源码)
    查看>>
    Objective-C实现BitMap算法(附完整源码)
    查看>>
    Objective-C实现bitmask位掩码算法(附完整源码)
    查看>>
    Objective-C实现bitonic sort双调排序算法(附完整源码)
    查看>>
    Objective-C实现BloomFilter布隆过滤器的算法(附完整源码)
    查看>>
    Objective-C实现BMP图像旋转180度(附完整源码)
    查看>>