这里总结针对一个并不一定所有点都连通的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. sql management studio正则替换sql

    需要把create proc xxx替换为 drop proc xxx go create proc xxx 方法,使用正则查找替换 create procedure {\[dbo\]\.[^\(]+ ...

  2. 报错--"npm audit fix" or "npm audit"

    如图: 根据提示输入 npm audit fix --force 如图: 根据提示输入: npm audit

  3. Centos 安装 MySQL-python

    更新yum yum update yum install mysql-devel yum install gcc yum install python-devel pip install MySQL- ...

  4. [工具]Sublime 显示韩文

  5. 几种在Linux下查询外网IP的办法(转)

    Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo. ...

  6. [分布式系统学习] 6.824 LEC2 RPC和线程 笔记

    6.824的课程通常是在课前让你做一些准备.一般来说是先读一篇论文,然后请你提一个问题,再请你回答一个问题.然后上课,然后布置Lab. 第二课的准备-Crawler 第二课的准备不是论文,是让你实现G ...

  7. 170822、解决PLSQL记录被另一个用户锁住的问题

    1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b. ...

  8. python----并发编程之IO模型

    一:IO模型介绍  同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个 ...

  9. asm 32 /64

    我使用NASM编写的,运行在32位windows和linux主机上,但后来需求增加了,需要在64位windows和linux上运行,windows自身有个wow(windows on windows) ...

  10. related Field has invalid lookup: icontains 解决方法

    models.py 文件 # coding:utf8 from django.db import models class Book(models.Model):         name = mod ...