Detect Cycle in a Directed Graph

推断一个图是否有环,有环图例如以下:

这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致。

这里就是添加一个数组保存当前已经訪问过的路径信息 recStack[];

而visited[]数组是訪问过的点的信息,两者作用是不一样的。

知道这个知识点,这道题就非常easy了。

原文:

http://www.geeksforgeeks.org/detect-cycle-in-a-graph/

#include <stdio.h>
#include <list>
#include <limits.h>
#include <iostream>
using namespace std; class DetectCycleinaDirectedGraph
{
int size;
list<int> *adj; bool isCycleUtil(int v, bool visited[], bool *recStack)
{
if (!visited[v])
{
//本题巧妙之处:额外添加recStack,because it is directed, if it is undirected, then we don't really need recStack.
visited[v] = recStack[v] = true;
list<int>::iterator it = adj[v].begin();
for ( ; it != adj[v].end(); it++)
{
if (!visited[*it] && isCycleUtil(*it, visited, recStack))
return true;
else if (recStack[*it]) return true;
}
recStack[v] = false;
}
return false;
}
public:
DetectCycleinaDirectedGraph(int v) : size(v)
{
adj = new list<int>[size];
} void addEdge(int v, int w)
{
adj[v].push_back(w);
} bool isCyclic()
{
bool *visited = new bool[size];
bool *recStack = new bool[size];
fill(visited, visited+size, false);
fill(recStack, recStack+size, false); for (int i = 0; i < size; i++)
{
if (isCycleUtil(i, visited, recStack)) return true;
}
return false;
}
}; void DetectCycleinaDirectedGraph_RUN()
{
DetectCycleinaDirectedGraph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3); if(g.isCyclic())
cout << "Graph contains cycle\n";
else
cout << "Graph doesn't contain cycle\n";
}

Geeks - Detect Cycle in a Directed Graph 推断图是否有环的更多相关文章

  1. Detect cycle in a directed graph

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

  2. Data Structure Graph: cycle in a directed graph

    geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...

  3. 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), ...

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

  5. CodeChef Counting on a directed graph

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

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

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

  8. Directed Graph Loop detection and if not have, path to print all path.

    这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...

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

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

随机推荐

  1. python爬虫值requests模块

    - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...

  2. MVC系列学习(九)-DTO的使用

    本次学习用的数据库,如下 1.什么是DTO:DataTransferObject 即数据传输对象,服务端的客户端的通信,自动定义个小的实体类,里面只包含我们需要传输的属性 2.不用DTO会有什么问题 ...

  3. Java编程思想读书笔记_第二章

    java对于将一个较大作用域的变量“隐藏”的场景会有保护:编译告警.比如: int x = 5; { int x = 6; } 但是对于类中方法的局部变量和类成员变量确是可以重名的,比如 class ...

  4. Java&Xml教程(六)使用JDOM解析XML文件

    JDOM 提供了非常优秀的Java XML API来更方便的读取.修改.生成XML文档.JDOM还提供了包装类供用户从SAX.DOM.STAX事件解析.STAX流解析中选择具体的实现. 在本教程中,我 ...

  5. 移动web——bootstrap响应式轮播图

    基本介绍 1.bootstrap有轮播图的模板,我们只需要改动下就行. 2.这里我们将介绍桌面版本和移动版本最后是综合版本 桌面版本 1.这里的图片设置是有窍门的,不再去添加img标签,而是作为a标签 ...

  6. 易语言 打开exe可执行文件、打开网页

    打开文件--------按钮被单击事件 直接复制以下代码即可 .版本 2 .子程序 _按钮58_被单击 运行 (“exe文件路径”, 假, ) 打开网站--------按钮被单击事件 直接复制以下代码 ...

  7. Zip, Join, GroupJoin

    Zip 合并兩個序列,產生一個新的對象序列,但連接方式是一对一的(即序列1和第一项连接序列2的第一项),所以最终结果会在较短的序列处终止. Zip在這裏不是壓縮的意思,而是拉鏈,意爲連接兩個序列 Pe ...

  8. Centos7搭建lamp环境

    首先安装apache Centos7默认已经安装httpd服务,只是没有启动. 如果需要重新安装,输入 yum install -y httpd 启动服务: systemctl start httpd ...

  9. kickstart配置文件详解和system-config-kickstart (转载)

    kickstart是什么        许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart,系统管理员可以 ...

  10. 【sqli-labs】 less51 GET -Error based -Order By Clause -String -Stacked injection(GET型基于错误的字符型Order By从句堆叠注入)

    less50的字符型版本,闭合好引号就行 http://192.168.136.128/sqli-labs-master/Less-51/?sort=1';insert into users(id,u ...