-
Notifications
You must be signed in to change notification settings - Fork 5
/
DetectCycleInUndirectedGraph.java
143 lines (126 loc) · 4.19 KB
/
DetectCycleInUndirectedGraph.java
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
/*https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1*/
class Solution
{
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj)
{
boolean[] visited = new boolean[V];
boolean[] currentPath = new boolean[V];
int[] currString = new int[V*2];
//check cycle starting from every node
for (int i = 0; i < V; ++i)
{
currString[0] = i;
if (checkCycle(i,adj,currString,0,visited,currentPath))
return true;
}
return false;
}
public boolean checkCycle(int currNode, ArrayList<ArrayList<Integer>> graph, int[] currString, int currInd, boolean[] visited, boolean[] currentPath)
{
//if the current node is already visited in the current recursion call and it is not the parent of the node, cycle exists
if (currentPath[currNode] && currString[currInd-2] != currNode) return true;
//if the current node is checked for cycle and program isn't terminated, cycle doesn't exists
if (visited[currNode]) return false;
//mark the current node visited
visited[currNode] = true;
currentPath[currNode] = true;
//get the adjacent nodes
ArrayList<Integer> adjacentNodes = graph.get(currNode);
//recur for every adjacent node
for (Integer adjacentNode : adjacentNodes)
{
currString[++currInd] = adjacentNode;
if (checkCycle(adjacentNode,graph,currString,currInd,visited,currentPath))
return true;
--currInd;
}
//backtrack
currentPath[currNode] = false;
return false;
}
}
class Solution {
int[] color;
boolean result;
// Function to detect cycle in an undirected graph.
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj)
{
// Code here
color = new int[V];
result = false;
for (int i = 0; i < V; ++i) if (color[i] == 0) dfs(adj, V, i, -1);
return result;
}
public void dfs(ArrayList<ArrayList<Integer>> graph, int V, int src, int parent)
{
if (result) return;
color[src] = 1; // entry color is 1
for (int adjacentNode : graph.get(src)) // for each neighbour of the current node
{
if (color[adjacentNode] == 2) // if color is 2
continue; // continue
if (adjacentNode != parent) // if color is 1 or dfs from neighbour is false, return false
{
if (color[adjacentNode] == 1)
{
result = true;
break;
}
dfs(graph, V, adjacentNode, src); //recursion call
}
}
if (result) return;
color[src] = 2; // exit color is 2
}
}
class Solution {
int[] parent;
int[] size;
boolean[] visited;
// Function to detect cycle in an undirected graph.
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
// Code here
parent = new int[V];
size = new int[V];
visited = new boolean[V];
int i;
//initialize disjoint set
for (i = 0; i < V; ++i)
{
parent[i] = i;
size[i] = 1;
}
//for each edge, call union and find
for (i = 0; i < V; ++i)
{
if (visited[i]) continue;
visited[i] = true;
for (Integer adjNode : adj.get(i))
{
if (!visited[adjNode] && union(i,adjNode)) return true;
}
}
return false;
}
private boolean union(int i, int j)
{
int parentI = find(i);
int parentJ = find(j);
if (parentI == parentJ) return true;
if (size[parentI] > size[parentJ])
{
int temp = parentI;
parentI = parentJ;
parentJ = temp;
}
parent[parentI] = parentJ;
size[parentJ] += size[parentI];
return false;
}
private int find(int i)
{
if (parent[i] == i) return i;
parent[i] = find(parent[i]);
return parent[i];
}
}