算法,以检查是否有向图是强连通

编程入门 行业动态 更新时间:2024-10-12 03:22:15
本文介绍了算法,以检查是否有向图是强连通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要检查是否有向图是强连接的,或者,换句话说,如果所有的节点可以由任何其它节点到达(不一定通过直接边缘)。

I need to check if a directed graph is strongly connected, or, in other words, if all nodes can be reached by any other node (not necessarily through direct edge).

这样做是运行DFS和BFS在每个节点上,看到所有的人依旧可达的一种方式。

One way of doing this is running a DFS and BFS on every node and see all others are still reachable.

有没有更好的方法来做到这一点?

Is there a better approach to do that?

推荐答案

Tarjan's强连通分量算法(或 Gabow 的变化),当然就足够了;如果只有一个强连通分量,那么,图形是强连接。

Tarjan's strongly connected components algorithm (or Gabow's variation) will of course suffice; if there's only one strongly connected component, then the graph is strongly connected.

两者都是线性的时间。

对于一个正常的深度优先搜索,跟踪每个节点的状态:新,观察,但仍处于打开状态(它在调用堆栈),并看到和成品。此外,您存储深度,当你第一次到达一个节点,最低等的深度,它是从节点的访问(你知道这一点后,你完成一个节点)。节点是一个强连接组件的根如果最低可达深度等于其自身的深度。这适用即使要使用从根到达一个节点的深度不可能的最小值。

As with a normal depth first search, you track the status of each node: new, seen but still open (it's in the call stack), and seen and finished. In addition, you store the depth when you first reached a node, and the lowest such depth that is reachable from the node (you know this after you finish a node). A node is the root of a strongly connected component if the lowest reachable depth is equal to its own depth. This works even if the depth by which you reach a node from the root isn't the minimum possible.

要检查只是整个图形无论是单SCC,启动DFS,从任何一个节点,而当你完成,如果最低可达深度为0,而每一个节点进行访问,那么整个图形强连接。

To check just for whether the whole graph is a single SCC, initiate the dfs from any single node, and when you've finished, if the lowest reachable depth is 0, and every node was visited, then the whole graph is strongly connected.

更多推荐

算法,以检查是否有向图是强连通

本文发布于:2023-11-29 22:47:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   图是强

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!