这里总结针对一个并不一定所有点都连通的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.的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. CodeChef Counting on a directed graph

    Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...

  5. Geeks - Detect Cycle in a Directed Graph 推断图是否有环

    Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...

  6. Skeleton-Based Action Recognition with Directed Graph Neural Network

    Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...

  7. 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 ...

  8. Detect cycle in a directed graph

    Question: Detect cycle in a directed graph Answer: Depth First Traversal can be used to detect cycle ...

  9. Judge loop in directed graph

    1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...

随机推荐

  1. 【微服务系列】Spring SpringMVC SpringBoot SpringCloud概念、关系及区别

    一.正面解读 Spring主要是基于IOC反转Beans管理Bean类,主要依存于SSH框架(Struts+Spring+Hibernate)这个MVC框架,所以定位很明确,Struts主要负责表示层 ...

  2. css笔记 - 张鑫旭css课程笔记之 z-index 篇

    一.z-index语法.支持的属性值等 z-index: 在支持z-index的元素上, z-index规定了元素(包括子元素)的垂直z方向的层级顺序, z-index可以决定哪个元素覆盖在哪个元素上 ...

  3. 应该了解的Openstack命令

    整理一下Openstack的命令.下面的命令,我都是全部在机器验证过,主要是参考 redhat文档 查看rabbitmq 队列 rabbitmqctl list_queues 查看keystone的用 ...

  4. nginx 日志文件

    默认日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十六:IIC储存模块

    IIC储存器是笔者用来练习精密控时的经典例子.<整合篇>之际,IIC储存器的解释,笔者也自认变态.如今笔者回头望去,笔者也不知道自己当初到底发什么神经,既然将IIC的时序都解释一番.由于开 ...

  6. gradle项目,连同依赖一起打jar包

    在build里加入以下配置(如果不是一个可执行的jar包的话就不用配置Main-Class属性): def mainClassName = "你需要执行的main方法所在的的包名+类名&qu ...

  7. 总结一下最近用到的技术(2)--JsonSchema和JsonSchemaValidator

    我们最早接触xml的时候会使用一个dtd文件去定义xml里可以有哪些元素和属性等,后来发展到xml schama(是一个xsd文件,在dtd的基础上提供了命名空间等更强大的功能) 现在,RESTful ...

  8. 使用eclipse执行maven-release-plugin插件发布jar异常问题(.project)(Cannot prepare the release because you have local modifications )

    开发是用的eclipse,里面有工程文件.project这种文件,运行release:prepare的时候报异常: Cannot prepare the release because you hav ...

  9. Unity3D笔记 英保通十 射线碰撞器检测

    射线碰撞检测可以用来检测方向和距离: 通过Physics.RayCast光线投射来实现:常用于射击利用发射的射线来判断.还有对战中刀剑交战中.. 一.要涉及到RayCast和RayCastHit 1. ...

  10. wpgcms---流程控制

    在模板里面Twig标签语法的时候,很多时候会用到流程控制. if 判断: {% if true %} {% endif %} // 示例 {% if item.href %} href="{ ...