Detect cycle in a directed graph
Question: Detect cycle in a directed graph
Answer:
Depth First Traversal can be used to detect cycle in a Graph. DFS for a connected graph produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is from a node to itself (selfloop) or one of its ancestor in the tree produced by DFS.
The idea is to do DFS of given graph and while doing traversal, assign one of the below three colors to every vertex.
WHITE : Vertex is not processed yet. Initially all vertices are WHITE. GRAY : Vertex is being processed (DFS for this vertex has started, but not finished which means that all descendants (ind DFS tree) of this vertex are not processed yet (or this vertex is in function call stack) BLACK : Vertex and all its descendants are processed. While doing DFS, if we encounter an edge from current vertex to a GRAY vertex, then this edge is back edge and hence there is a cycle. https://www.geeksforgeeks.org/detect-cycle-direct-graph-using-colors/
Detect cycle in a directed graph的更多相关文章
- Geeks - Detect Cycle in a Directed Graph 推断图是否有环
Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...
- Data Structure Graph: cycle in a directed graph
geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 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 ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...
- [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 ...
- Directed Graph Loop detection and if not have, path to print all path.
这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...
- Skeleton-Based Action Recognition with Directed Graph Neural Network
Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...
随机推荐
- BZOJ 4570: [Scoi2016]妖怪
二次联通门 : BZOJ 4570: [Scoi2016]妖怪 二次联通门 : luogu P3291 [SCOI2016]妖怪 LibreOJ : LibreOJ #2015. 「SCOI2016 ...
- 容器网络启用RDMA高速通讯-Freeflow
容器网络启用RDMA高速通讯-Freeflow 容器网络启用RDMA高速通讯-Freeflow 本文编译自: Freeflow,https://github.com/openthings/Freefl ...
- nginx的跨域设置
官方文档 中说,只有当响应状态码为以下几种类型中之一时,add_header 才会生效.如果需要 add_header 在所有情况下都生效,可以在后面加上 always 参数即可解决. Adds th ...
- 函数第二部分:为什么说动态参数是没有计划好的参数-Python基础前传(11)
动态参数1-一个星号变元组 动态参数存在的意义? 函数的作者有时候也不知道这个函数到底需要多少个参数,这时候动态参数就有存在的意义了 动态参数创建-加* 底层原理是:把数值型或其他数据类型变成了元组类 ...
- 如何判断Linux下 网卡是虚拟还是物理网卡?
ifconfig命令可以查看Linux系统中正在使用的网卡,包括物理网卡和虚拟网卡,如果想要查看Linux系统中全部的网卡,可以查看/proc/net/dev文件,那如何区分网卡是虚拟还是物理的呢? ...
- idea 2019激活码
9MWZD5CC4E-eyJsaWNlbnNlSWQiOiI5TVdaRDVDQzRFIiwibGljZW5zZWVOYW1lIjoiMjAxNzY1MDYxQGNxdS5lZHUuY24gLiIsI ...
- train loss和test loss
train loss 不断下降,test loss不断下降,说明网络仍在学习;(最好的)train loss 不断下降,test loss趋于不变,说明网络过拟合;(max pool或者正则化)tra ...
- Android绘图通用第三方控件
https://github.com/ddanny/achartengine CodePull requests 2SecurityInsights Charting library fo ...
- 【转载】 第四范式首席科学家杨强:AlphaGo的弱点及迁移学习的应对(附视频)
原文地址: https://www.jiqizhixin.com/articles/2017-06-02-2 ============================================= ...
- 34 Flutter仿京东商城项目 用户注册 注册流程 POST发送验证码 倒计时功能 验证验证码
加群452892873 下载对应34课文件,运行方法,建好项目,直接替换lib目录 以下列出的是本课涉及的文件. RegisterFirst.dart import 'package:flutter/ ...