-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCycle Detection I - Undirected Graph.cpp
63 lines (49 loc) · 1.58 KB
/
Cycle Detection I - Undirected Graph.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
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
// Function to perform DFS traversal
bool dfs(int v, vector<vector<int>>& graph, vector<bool>& visited, int parent) {
visited[v] = true;
// Traverse all adjacent vertices of v
for (int u : graph[v]) {
if (!visited[u]) {
if (dfs(u, graph, visited, v))
return true;
}
else if (u != parent) {
// If u is visited and not the parent of v, it forms a cycle
return true;
}
}
return false;
}
// Function to check if a graph contains a cycle
bool isCyclic(int V, vector<pair<int, int>>& edges) {
// Create an adjacency list representation of the graph
vector<vector<int>> graph(V);
// Add edges to the graph
for (auto edge : edges) {
int u = edge.first;
int v = edge.second;
graph[u].push_back(v);
graph[v].push_back(u);
}
// Create a visited array to keep track of visited vertices
vector<bool> visited(V, false);
// Perform DFS traversal starting from each unvisited vertex
for (int v = 0; v < V; v++) {
if (!visited[v]) {
if (dfs(v, graph, visited, -1))
return true;
}
}
return false;
}
int main() {
int V = 3; // Number of vertices
vector<pair<int, int>> edges = { {1, 2}, {0, 1}, {2, 0} }; // Edges of the graph
bool hasCycle = isCyclic(V, edges);
cout << (hasCycle ? "Cyclic" : "Not Cyclic") << endl;
return 0;
}