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. 命令框中oracle dmp文件的导入和导出(仅做个人备忘)

    1.dmp文件导出 (全部)exp 用户名/密码 rows=y indexes=n compress=n buffer=65536 feedback=100000  file=F:\test.dmp ...

  2. 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化

    序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #.     _9_    /   \   3     2  ...

  3. Elasticsearch之CURL命令的mget查询

    我这里, 再,创建一个zhouls2的索引库. [hadoop@master elasticsearch-]$ curl -XPUT 'http://master:9200/zhouls2/' {]$ ...

  4. C#模拟百度登录并到指定网站评论回帖(二)

    序言: 回归正题:前面讲到的抓包分析的数据,是模拟登录要获得得必要信息(当然有些也不是必要的...我只是都列举出来这样有个对比)如果说,有哪个英文字母不知道什么意思的,可以问一下度娘,有不少前辈都发过 ...

  5. Hibernate 配置双向多对多关联

    本文解决问题:Hibernate 中配置项目(Project) 员工(Employee)   双向多对多关联 方案一:直接配置双向多对多 方案二:配置第三个关联类(xml)   将多对多查分开来(形成 ...

  6. 给网站添加运行时间的JavaScript完整代码

    function secondToDate(second) { if (!second) { return 0; } var time = new Array(0, 0, 0, 0, 0); if ( ...

  7. cordova科大讯飞语音识别

    cordova-plugin-IFlyspeech 科大讯飞的语音听说读写的cordova插件 Supported Platforms iOS android Installation 插件安装命令: ...

  8. PHP开发之旅-验证码功能实现

    验证码这样的功能可以说是无处不在了,接下来使用php来实现验证码这样的功能,这里我是将验证码实现抽取到一个类中独立开来,那么后面如果再使用到验证码功能,直接引入该类文件并创建该类的实例,就可以使用验证 ...

  9. 【PL/SQL】用星号拼出金字塔

    代码中首先声明了几个变量,然后使用嵌套循环去输出空格和星号,其中: 每层空格数=总层数-该层层数 每层星号数=当前层数*2-1 代码如下: declare v_number1 ); --外层循环控制金 ...

  10. CNN结构:色彩空间建模-色彩空间分析

    原文: 色彩空间基础 好一个NB的知乎专栏:色彩空间基础 第一章:色彩空间基础 关于色彩分析,引出了专门的数学基础.整个过程给出了完备的数学阐述,虽然没有试验数据,论述的相当精彩. 摘抄出一段:  上 ...