iOS深度优先算法
来源:互联网 发布:洗面奶推荐知乎 编辑:程序博客网 时间:2024/05/17 22:27
#import <Foundation/Foundation.h>/**************************************** 无向图 对称矩阵 V0--------V1 | /| | / | | V2 | | / \ | | / \ | V3 V4 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 ******************************************//****************************************** 有向图 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 V0->V1 V0->V2 V2->V3 V3->V0 ******************************************//********************************************* 深度优先遍历:从初始点出发,不断向前走,如果碰到死路了,就往回走一步,尝试另一条路,直到发现了目标位置。这种不撞南墙不回头的方法,即使成功也不一定找到一条好路,但好处是需要记住的位置比较少。 广度优先:从初始点出发,把所有可能的路径都走一遍,如果里面没有目标位置,则尝试把所有两步能够到的位置都走一遍,看有没有目标位置;如果还不行,则尝试所有三步可以到的位置。这种方法,一定可以找到一条最短路径,但需要记忆的内容实在很多,要量力而行。 **********************************************/@interface AMWGraph : NSObject@property (nonatomic, strong) NSMutableArray *edges;//邻接矩阵,用来存储边//初始化矩阵,一维数组,和边的数目- (instancetype)initWithN:(NSInteger)n;//得到结点的个数- (NSInteger)getNumOfVertex;//得到边的数目- (NSInteger)getNumOfEdges;// 返回结点i的数据- (id)getValueByIndex:(NSInteger)i;//返回v1,v2的权值- (NSInteger)getWeight:(NSInteger)v1 withV2:(NSInteger)v2;//插入结点- (void)insertVertex:(id)vertex;- (void)insertEdge:(NSInteger)v1 withV2:(NSInteger)v2 withWeight:(NSInteger)weight;//删除结点- (void)deleteEdge:(NSInteger)v1 withV2:(NSInteger)v2;//得到第一个邻接结点的下标- (NSInteger)getFirstNeighbor:(NSInteger)index;//根据前一个邻接结点的下标来取得下一个邻接结点- (NSInteger)getNextNeighbor:(NSInteger)v1 withV2:(NSInteger)v2;//对外公开函数,深度优先遍历,与其同名私有函数属于方法重载- (void)depthFirstSearch:(NSMutableArray *)isVisited;//对外公开函数,广度优先遍历- (void)broadFirstSearch:(NSMutableArray *)isVisited;@end
#import "AMWGraph.h"@interface AMWGraph()@property (nonatomic, strong) NSMutableArray *vertexList;//存储点的链表@property (nonatomic, assign) NSInteger numOfEdges;//边的数目@end@implementation AMWGraph- (instancetype)initWithN:(NSInteger)n{ if (self == [super init]) { _edges = [NSMutableArray array]; _vertexList = [NSMutableArray arrayWithCapacity:n]; _numOfEdges = 0; } return self;}- (NSInteger)getNumOfVertex{ return self.vertexList.count;}- (NSInteger)getNumOfEdges{ return self.numOfEdges;}- (id)getValueByIndex:(NSInteger)i{ return self.vertexList[i];}- (NSInteger)getWeight:(NSInteger)v1 withV2:(NSInteger)v2{ return [self.edges[v1][v2] integerValue];;}- (void)insertVertex:(id)vertex{ [self.vertexList addObject:vertex];}- (void)insertEdge:(NSInteger)v1 withV2:(NSInteger)v2 withWeight:(NSInteger)weight{ NSMutableArray *arr = [self.edges objectAtIndex:v1]; arr[v2] = [NSNumber numberWithInteger:weight]; self.numOfEdges ++;}- (void)deleteEdge:(NSInteger)v1 withV2:(NSInteger)v2{ self.edges[v1][v2] = [NSNumber numberWithInt:0]; self.numOfEdges --;}- (NSInteger)getFirstNeighbor:(NSInteger)index{ for (NSInteger j = 0; j<[_vertexList count]; j++) { if ([_edges[index][j] intValue] >0 ) { return j; } } return -1;}- (NSInteger)getNextNeighbor:(NSInteger)v1 withV2:(NSInteger)v2{ for (NSInteger j = v2+1; j<[_vertexList count]; j++) { if ([_edges[v1][j] intValue]>0) { return j; } } return -1;}- (void)depthFirstSearch:(NSMutableArray *)isVisited withI:(NSInteger)i{ isVisited[i] = [NSNumber numberWithBool:YES]; NSInteger w = [self getFirstNeighbor:i]; while (w!=-1) { if ([isVisited[w] compare:@(0)] == NSOrderedSame) { [self depthFirstSearch:isVisited withI:w]; } w = [self getNextNeighbor:i withV2:w]; }}- (void)depthFirstSearch:(NSMutableArray *)isVisited{ for (NSInteger i =0; i<[self getNumOfVertex]; i++) { if ([isVisited[i] compare:@(0)] == NSOrderedSame) { [self depthFirstSearch:isVisited withI:i]; } }}- (void)broadFirstSearch:(NSMutableArray *)isVisited withI:(NSInteger)i{ NSInteger u,w; NSMutableArray *queue = [NSMutableArray array]; isVisited[i] = [NSNumber numberWithBool:YES]; //结点入队列 [queue addObject:[NSNumber numberWithInteger:i]]; while ([queue count]>0) { u = [queue[0]integerValue]; [queue removeObjectAtIndex:0]; w = [self getFirstNeighbor:u]; while (w!=-1) { if ([isVisited[w] compare:@(0)] == NSOrderedSame) { isVisited[w] = [NSNumber numberWithBool:YES]; [queue addObject:[NSNumber numberWithInteger:w]]; } w = [self getNextNeighbor:u withV2:w]; } }}- (void)broadFirstSearch:(NSMutableArray *)isVisited{ for (NSInteger i =0; i<[self getNumOfVertex]; i++) { if ([isVisited[i] compare:@(0)] == NSOrderedSame) { [self broadFirstSearch:isVisited withI:i]; } }}@end
0 0
- iOS深度优先算法
- 深度优先搜索算法
- 深度优先搜索算法
- 深度优先算法
- 深度优先搜索算法
- 深度优先搜索算法
- 深度优先搜索算法
- dfs深度优先算法
- 【深度优先搜索算法】
- C# 深度优先算法
- 深度优先搜索算法
- 深度优先算法 -- 冰壶
- 深度优先搜索算法
- 深度优先算法
- 深度优先算法
- 深度优先算法思想
- 深度优先算法DFS
- 深度优先搜索算法
- vue transition-group 列表过渡
- 更快学习 JS 的 6 个简单思维技巧
- Spring文件上传
- ListView+CheckBox实现单选列表
- linux vim 编辑器
- iOS深度优先算法
- android studio配置Terminal命令窗口
- 怎么解决java.lang.NoClassDefFoundError错误
- Jsp自定义标签
- 15自由度机器人课程日志(八)直立行走
- Linux的wait函数
- perl面向对象
- 高德地图API开发应用-----地图显示+定位+marker
- initWithStyle: reuseIdentifier: 与 registerClass: forCellReuseIdentifier: 冲突问题 cell.detailTextLabel.