Directed Graph Loop detection and if not have, path to print all path.
这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Schedule_Medium tag: BFS, DFS, 这个题实际上就是监测directed graph里面是否有loop存在. 我在网上看了比较经典的做法为DFS, 并且用三个set去标志not visited点,(0), 正在visiting的点(-1), 已经visited过的点(1), 我结合这个思路, 用dictionary去"模拟"这个的三个set, 分别用0, -1, 1 表示not visited, visiting, and visited.
1) Check whether have loop in directed graph
实际上就是找backedge, 如果判断edge是backedge呢, 就是说从node出发的指针指向了node自己或者它的祖先node, 那么表明是backedge, 同时也表明了有loop存在. 所以实际上就是用DFS去依次访问每个node, 如果node是1, 表明visited过了(并且从node出发的所有path都被visited过了), 就continue, 如果是-1, 表明visiting但是再次被visited, 所以直接返回False, 否则没有visited过, 将其标志为-1, visiting, 然后直到把所有从node出发的path的node都监测一遍没问题, 再将node tag为1, 表明visited过了, 并且返回continue. 直到所有的点都没返回False, 那么返回True.
参考视频虽然是老印口音,但是有字幕, 讲的还是很清楚的.
code 如下:
class Solution:
def checkLoopInDirectedGraph(self, graph, n): # n = number of nodes
d = collections.Counter() #default value is 0, not visited
def dfs(d, graph, i):
if d[i] == -1: return False
if d[i] == 1: return True
d[i] = -1
for neig in graph[i]:
if not dfs(d, graph, neig):
return False
d[i] = 1
return True
for i in range(n):
if not dfs(d, graph, i):
return False
return True
2) Check whether have loop in directed graph, if not loop, print path from start node to end, order does not matter for not connected, else return []
所以这个path的返回, 因为对不是联通的graph part的order无所谓, 所以我们只需要将以上的code加一行即可, 就是在visited node return True之前, 将其append进入到ans里面, 这样的话ans的顺序就是从尾巴print到node head, 所以返回的时候将ans reverse即可.
这个思路可以运用在[LeetCode] 210. Course Schedule II.
code如下:
class Solution:
def pathInDirectedGraph:(self, graph, n):
d, ans = collections.Counter(), []
def dfs(d, graph, i):
if d[i] == 1: return True
if d[i] == -1: return False
d[i] = -1
for neig in graph[i]:
if not dfs(d, graph, neig):
return False
d[i] = 1
ans.append(i)
return True
for i in range(n):
if not dfs(d, graph, i):
return []
return ans[::-1] # remember to reverse the ans
Directed Graph Loop detection and if not have, path to print all path.的更多相关文章
- dataStructure@ Find if there is a path between two vertices in a directed graph
Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...
- [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nod ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...
- Geeks - Detect Cycle in a Directed Graph 推断图是否有环
Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...
- Skeleton-Based Action Recognition with Directed Graph Neural Network
Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...
- Find the Weak Connected Component in the Directed Graph
Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...
- Detect cycle in a directed graph
Question: Detect cycle in a directed graph Answer: Depth First Traversal can be used to detect cycle ...
- Judge loop in directed graph
1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...
随机推荐
- Java 使用单例模式的注意事项
某个类使用单例模式实现,如果该类里面含有List或Map等集合,使用时,请注意两点 1. List或Map 等集合使用前,需要判断是否已经数据,调用clear()方法先清除掉 2. List或Map ...
- VS自动添加头部注释
让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio 14.0\ ...
- 开发常见错误之 : IMP-00058: 遇到 ORACLE 错误 1691
IMP-00058: 遇到 Oracle 错误 1691ORA-01691: Lob 段YQPRO.SYS_LOB0000031467C00006$$无法通过128(在表空间YQPRO中)扩展这种情况 ...
- jquery validator
jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目中早已用过,但看到这篇文章写得还是不错的,转载下与大家共同分享. 一.用前必备 官方网站: ...
- Linux应急处理操作手册
基础准备--命令防篡改与命令记录 很多黑客入侵到操作系统后,会做两个常见的操作unset history和替换命令文件(或者对应的链接库文件),针对这两点要做好记录shelllog并且检查链接库类文件 ...
- 让google.com不跳转到google.com.hk
自从google的服务器搬离中国大陆后,大陆地区用户用google服务时会自动跳转到香港的http://google.com.hk,,有关键字过滤而且偶尔不是很稳定,这对我们的生活工作都造成了困扰. ...
- Windows任务计划的设置
需求:因为要定时跑一些exe的小程序,来定时执行项目内的某段代码.所以需要建一个任务计划 1. 2. 3.
- java try catch 异常后还会继续执行吗
catch 中如果你没有再抛出异常 , 那么catch之后的代码是可以继续执行的 , 但是try中 , 报错的那一行代码之后 一直到try结束为止的这一段代码 , 是不会再执行的. ========= ...
- iOS - 网址、链接、网页地址、下载链接等正则表达式匹配(解决url包含中文不能编码的问题)
DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母.标号中除连字符(-)外不能使用其他的标点符号.级别最低的域名写在最左边,而级别最高的域名写在最右边.由多 ...
- Spark2 生存分析Survival regression
在spark.ml中,实现了加速失效时间(AFT)模型,这是一个用于检查数据的参数生存回归模型. 它描述了生存时间对数的模型,因此它通常被称为生存分析的对数线性模型. 不同于为相同目的设计的比例风险模 ...