Geeks - Detect Cycle in a Directed Graph 推断图是否有环

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";
}
时间: 2024-12-16 23:46:00

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

Geeks - Detect Cycle in a Directed Graph 判断图是否有环

Detect Cycle in a Directed Graph 判断一个图是否有环,有环图如下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,因为方向可以不一致. 这里就是增加一个数组保存当前已经访问过的路径信息 recStack[]: 而visited[]数组是访问过的点的信息,两者作用是不一样的. 知道这个知识点,这道题就很容易了. 原文: http://www.geeksforgeeks.org/detect-cycle-in-a-graph/ #include <st

Leetcode: Graph Valid Tree &amp;&amp; Summary: Detect cycle in directed graph and undirected graph

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return tru

Detect Cycle In Directed/Undirected Graph

Detect Cycle in a Directed Graph https://www.geeksforgeeks.org/detect-cycle-in-a-graph/ 有向图里的环必须是 a->b b->c c->a 类似这种的环(包括自环). 这学期刚上过算法,dfs遍历图会得到dfs tree(如果不是连通图就是forest),并提到了back edge的概念.如果dfs遍历图的时候,有back edge产生,那就说明一定有环. 写起来就按照标准的dfs写,需要一个visit

Geeks - Union-Find Algorithm - Detect Cycle in a an Undirected Graph算法

利用Union Find的方法查找图中是否有环. 在于构建一个图数据结构,和一般图的数据结构不同的是这个图是记录了边的图,并在查找过程中不断把边连接起来,形成一个回路. 原文地址: http://www.geeksforgeeks.org/union-find/ #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> class

[email&#160;protected] 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 vertex to second. For example, in the following graph, there is a path from vertex 1 to 3. As another example, there is no path from 3 to 0. We can eith

CodeChef Counting on a directed graph

Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. Read problems statements in Mandarin Chineseand Russian. Given an directed graph with N nodes (numbered from 1 to N) and M edges, calculate the number

[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 label and a list of its neighbors. (a connected set of a directed graph is a subgraph in which any two vertices are connected by direct edge path.) Exam

[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 nodes. LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制,Course Schedule 课程清单 和 Course Schedule II 课程清单之二.目前看来CareerCup中有关图的题在第四章中仅此一道,这是

leetcode-006 detect cycle

1 package leetcode; 2 3 public class DetectCycle { 4 public ListNode detectCycle(ListNode head) { 5 ListNode s=head; 6 ListNode f=head; 7 while(f!=null&&f.next!=null){ 8 s=s.next; 9 f=f.next.next; 10 if(s==f){ 11 break; 12 } 13 } 14 if(f==null||f.