-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectcycle.cpp
73 lines (65 loc) · 984 Bytes
/
detectcycle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<list>
#include<stack>
using namespace std;
class graph
{public:
int v;
list<int> *array;
stack <int> s;
bool *visited;
graph(int);
void add(int src,int des);
int isCycle(int);
};
graph::graph(int a)
{
v=a;
array=new list<int>[v];
visited=new bool[v+1];
for(int i=0;i<=v;i++)
visited[i]=false;
}
void graph::add(int src,int des)
{
array[src].push_back(des);
}
int graph::isCycle(int v)
{
int ans=0;
s.push(v);
visited[v]=true;
list<int>::iterator i;
while(!s.empty())
{
int now=s.top();
//cout<<now<<" ";
s.pop();
for(i=array[now].begin()+1;i!=array[now].end();i++)
{
if(visited[*i]==false)
{ans=isCycle(*i);
//visited[*i]=true;
}
else
{ans=1; cout<<now<<" ";
}
}
}
return ans;
}
int main()
{
graph g(4);
g.add(0,1);
g.add(0, 2);
g.add(1, 2);
g.add(2, 3);
//g.add(2, 3);
//g.add(3, 3);
if(g.isCycle(0))
cout<<"there is a cycle"<<endl;
else
cout<<"no cycle"<<endl;
return 0;
}