-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectCycleDFS.cpp
More file actions
64 lines (59 loc) · 971 Bytes
/
DetectCycleDFS.cpp
File metadata and controls
64 lines (59 loc) · 971 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class Graph
{
int v;
list<int>* adj;
bool DFS(int u,bool* visited,int parent);
public:
Graph(int n);
void addEdge(int a,int b);
bool isCyclic();
};
Graph::Graph(int n)
{
v=n;
adj=new list<int>[v];
}
bool Graph::DFS(int u,bool* visited,int parent)
{
visited[u]=true;
list<int>::iterator i;
for(i=adj[u].begin();i!=adj[u].end();i++)
{
if(!visited[*i]&&DFS(*i,visited,u))
return true;
else if(*i!=parent)
return true;
}
return false;
}
bool Graph::isCyclic()
{
bool visited[v];
for(int i=0;i<v;i++)
visited[i]=false;
for(int i=0;i<v;i++)
{
if(!visited[i]&&DFS(i,visited,-1))
return true;
}
return false;
}
void Graph::addEdge(int a,int b)
{
adj[a].push_back(b);
cout<<"adding edge"<<endl;
adj[b].push_back(a);
}
int main()
{
Graph g1(5);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
g1.addEdge(2, 0);
g1.addEdge(0, 3);
g1.addEdge(3, 4);
cout<<g1.isCyclic();
return 0;
}