Repository navigation
Expand file tree
/
Copy pathcycleDetectionUndirected.cpp
More file actions
112 lines (105 loc) · 2.61 KB
/
Copy pathcycleDetectionUndirected.cpp
File metadata and controls
112 lines (105 loc) · 2.61 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
BFS
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl '\n'
bool isCycle(int node, vector<int> adj[], vector<int> &visited, vector<int> &parent)
{
queue<int> q;
q.push(node);
visited[node] = 1;
while (!q.empty())
{
int temp = q.front();
q.pop();
for (auto adjNode: adj[temp])
{
if (visited[adjNode] != 1)
{
q.push(adjNode);
parent[adjNode] = temp;
visited[adjNode] = 1;
}
else if (visited[adjNode] == 1 && parent[adjNode] != temp)
{
return true;
}
}
}
return false;
}
int main()
{
int n;
cin >> n;
vector<vector<int>> adjMatrix(n, vector<int>(n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> adjMatrix[i][j];
}
}
// create adjList
vector<int> adjList[n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (adjMatrix[i][j] == 1 && i != j)
{
adjList[i].push_back(j);
adjList[j].push_back(i);
}
}
}
vector<int> visited(n, 0);
vector<int> parent(n);
for (int i = 0; i < n; i++)
{
parent[i] = i;
}
for (int i = 0; i < n; i++)
{
if (visited[i] == 0)
{
cout << isCycle(i, adjList, visited, parent) << endl;
}
}
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
DFS
class Solution {
private:
bool isCyclic(int v, int parent, unordered_map<int, bool>& visited, vector<int> adj[]) {
visited[v] = true;
for (auto neighbour : adj[v]) {
if (!visited[neighbour]) {
bool ans = isCyclic(neighbour, v, visited, adj);
if (ans) {
return true;
}
}
else if (neighbour != parent) {
return true;
}
}
return false;
}
public:
// Function to detect cycle in an undirected graph.
bool isCycle(int V, vector<int> adj[]) {
// to handle all componenets
unordered_map<int, bool> visited;
for (int i = 0; i < V; i++) {
if (!visited[i]) {
bool ans = isCyclic(i, -1, visited, adj);
if (ans == true) {
return true;
}
}
}
return false;
}
};