博客
关于我
Objective-C实现articulation-points(关键点)(割点)算法(附完整源码)
阅读量:796 次
发布时间:2023-02-17

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

Objective-C 实现割点算法

在图论中,割点(Articulation Point)是描述在连通图中,移除某个顶点及其相关边后,图的连通性发生变化的点。以下是 Objective-C 中实现割点算法的完整代码示例。

#import   @interface Graph : NSObject @property (nonatomic, strong) NSMutableArray *vertices; @property (nonatomic, strong) NSMutableArray *edges; @property (nonatomic, strong) NSMutableArray *adjacencyList;
// 初始化图的顶点和边
-(void)initializeGraph
{
self.vertices = [NSMutableArray new];
self.edges = [NSMutableArray new];
self.adjacencyList = [NSMutableArray new];
}

// 添加顶点    -(void)addVertex:(id)vertex    {        [self.vertices addObject:vertex];    }      // 添加边    -(void)addEdge:(id)edge fromVertex:(id)from toVertex:(id)to    {        [self.edges addObject:edge];        [self.adjacencyList[fromVertex] addObject:toVertex];        [self.adjacencyList[toVertex] addObject:fromVertex];    }      //DFS 遍历并找出割点    -(NSArray *)findArticulationPoints    {        NSMutableArray *points = [NSMutableArray new];        [self dfs:0 visited:[NSMutableArray new] disc:[NSMutableArray new] low:[NSMutableArray new]];        return points;    }      //DFS 记录    -(void)dfs:(int)u visited:(NSMutableArray *)visited disc:(NSMutableArray *)disc low:(NSMutableArray *)low    {        visited[u] = YES;        disc[u] = [NSDate timeIntervalSinceReferenceDate];        low[u] = disc[u];              for (id v in self.adjacencyList[u])        {            if (!visited[v])            {                [self dfs:v visited:visited disc:disc low:low];                low[u] = [low[u] < low[v]] ? low[u] : low[v];            }            else if (v != parent[u])            {                low[u] = [low[u] < disc[v]] ? low[u] : disc[v];            }        }              if (low[u] > disc[u])        {            [points addObject:u];        }    }    

该代码实现了通过深度优先搜索(DFS)来识别图中的割点。通过记录每个顶点的访问时间(disc)和低值(low),算法可以判断移除某个顶点后对图的连通性是否有影响。

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

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
查看>>