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 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...
随机推荐
- hud 6184
$n$ 点 $m$ 边的图求多少对三元环公用一条边变无向图为有向图 建图方法:对于每条无向边 度数小的端点向度数大的端点连边度数相同则编号小的点向编号大的点连边这样就构成 $DAG$遍历: 遍历每条边 ...
- linux系列(二十三):df命令
1.命令格式 df [选项] [文件] 2.命令功能 显示指定磁盘文件的可用空间.如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示.默认情况下,磁盘空间将以 1KB 为单位进行显示, ...
- linux下安装python3.6.6
1.到python的官网去下载python3.6.3安装包,必须是Linux版本的 2.在/usr/tmp下下载python安装包 wget https://www.python.org/ftp/py ...
- 1.OC类
一.基础语法 1.在OC中,一般用2个文件来描述一个类: 1> .h:类的声明文件,用于声明成员变量.方法.类的声明使用关键字@interface和@end. 注意:.h中的方法只是做一个声明, ...
- Python的is和==
is是对比地址:==是对比值
- c++ 珊格迷宫问题
#demo1 #include<iostream> #include<ctime> #include<cstdlib> #include<queue> ...
- Alpha项目冲刺! Day5-产出
各个成员今日完成的任务 林恩:任务分工,博客撰写,了解安卓环境搭建 杨长元:安卓本地数据库基本建立 李震:改了图标和背景 胡彤:完善服务端 寇永明:研究测试代码 王浩:研究测试代码 李杰:研究测试代码 ...
- arcgis python 一个mxd打包mpk
def onempk(fileName): if fileName: mxd = arcpy.mapping.MapDocument(fileName) else: mxd = arcpy.mappi ...
- 监控、日志、APM整个监控体系思考 我为峰2014 简书作者 4.6092018-11-19 11:39打开App 序言
监控.日志.APM整个监控体系思考 我为峰2014 简书作者 4.6092018-11-19 11:39打开App 序言
- linux性能监控 -CPU、Memory、IO、Network等指标的讲解
[操作系统-linux]linux性能监控 -CPU.Memory.IO.Network等指标的讲解(转) 一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,Syst ...