-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
181 lines (145 loc) · 3.69 KB
/
Copy pathGraph.cpp
File metadata and controls
181 lines (145 loc) · 3.69 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <vector>
#include <list>
#include <queue>
using namespace std;
class Graph {
int V; // Number of vertices
list<int> *l; //int *arr
public:
Graph(int V) {
this->V = V;
//arr = new int[V];
l = new list<int>[V];
}
void addEdge(int u, int v) {
l[u].push_back(v);
l[v].push_back(u);
}
// void printAdjList() {
// for (int i = 0; i < V; i++) {
// cout << "Vertex " << i << ": ";
// for (int neigh : l[i]) {
// cout << neigh << " ";
// }
// cout << endl;
// }
// }
//BFS Traversal
void bfs() {
queue<int> Q;
vector<bool> vis(V, false);
Q.push(0);
vis[0] = true;
while(Q.size() > 0) {
int u = Q.front(); //u-v
Q.pop();
cout << u << " ";
for(int v : l[u]) { //v -> immediate neighbour
if(!vis[v]) {
Q.push(v);
vis[v] = true;
}
}
}
}
//DFS Traversal
void dfsHelper(int u, vector<bool> &vis) {
cout << u << " ";
vis[u] = true;
for(int v : l[u]) {
if(!vis[v]) {
dfsHelper(v, vis);
}
}
}
void dfs() {
int src = 0;
vector<bool> vis(V, false);
dfsHelper(src, vis);
}
//Cycle Detection in Undirected Graph using DFS
// bool isCycleUndirDFS(int src, int par, vector<bool> &vis) {
// vis[src] = true;
// list<int> neighbours = l[src];
// for(int v : neighbours) {
// if(!vis[v]) {
// if(isCycleUndirDFS(v, src, vis)) {
// return true;
// }
// } else if(v != par) {
// return true;
// }
// }
// return false;
// }
// bool isCycle() {
// vector<bool> &vis(V, false);
// for(int i = 0; i < V; i++) {
// if(!vis[i]) {
// if(isCycleUndirDFS(i, -1, vis)) {
// return true;
// }
// }
// }
// return false;
// }
//Cycle Detection in Undirected Graph using BFS
bool isCycleUndirBFS(int src, vector<bool> &vis) {
queue<pair<int, int>> q; //{node, parent}
q.push({src, -1});
vis[src] = true;
while(q.size() > 0) {
int u = q.front().first;
int parU = q.front().second;
q.pop();
list<int> neighbours = l[u];
for(int v : neighbours) {
if(!vis[v]) {
q.push({v, u});
vis[v] = true;
} else if(v != parU) {
return true;
}
}
}
return false;
}
bool isCycle() {
vector<bool> vis(V, false);
for(int i = 0; i < V; i++) {
if(!vis[i]) {
if(isCycleUndirBFS(i, vis)) {
return true;
}
}
}
return false;
}
};
int main() {
Graph g(5);
// g.addEdge(0, 1);
// g.addEdge(1, 2);
// g.addEdge(1, 3);
// g.addEdge(2, 3);
// g.addEdge(2, 4);
// g.printAdjList();
// g.bfs();
// g.addEdge(0, 1);
// g.addEdge(1, 2);
// g.addEdge(1, 3);
// g.addEdge(2, 4);
// cout << "DFS Traversal: ";
// g.dfs();
// cout << endl;
// cout << "BFS Traversal: ";
// g.bfs();
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 2);
g.addEdge(3, 4);
cout << g.isCycle() << endl;
return 0;
}