Skip to content

Commit 181b875

Browse files
Create Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
1 parent 64d7d96 commit 181b875

1 file changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// Java Program for union-find algorithm to detect cycle in a graph
2+
3+
import java.util.*;
4+
5+
import java.lang.*;
6+
7+
import java.io.*;
8+
9+
10+
11+
class Graph
12+
{
13+
14+
int V, E; // V-> no. of vertices & E->no.of edges
15+
16+
Edge edge[]; // /collection of all edges
17+
18+
19+
20+
class Edge
21+
22+
{
23+
24+
int src, dest;
25+
26+
};
27+
28+
29+
30+
// Creates a graph with V vertices and E edges
31+
32+
Graph(int v,int e)
33+
34+
{
35+
36+
V = v;
37+
38+
E = e;
39+
40+
edge = new Edge[E];
41+
42+
for (int i=0; i<e; ++i)
43+
44+
edge[i] = new Edge();
45+
46+
}
47+
48+
49+
50+
// A utility function to find the subset of an element i
51+
52+
int find(int parent[], int i)
53+
54+
{
55+
56+
if (parent[i] == -1)
57+
58+
return i;
59+
60+
return find(parent, parent[i]);
61+
62+
}
63+
64+
65+
66+
// A utility function to do union of two subsets
67+
68+
void Union(int parent[], int x, int y)
69+
70+
{
71+
72+
int xset = find(parent, x);
73+
74+
int yset = find(parent, y);
75+
76+
parent[xset] = yset;
77+
78+
}
79+
80+
81+
82+
83+
84+
// The main function to check whether a given graph
85+
86+
// contains cycle or not
87+
88+
int isCycle( Graph graph)
89+
90+
{
91+
92+
// Allocate memory for creating V subsets
93+
94+
int parent[] = new int[graph.V];
95+
96+
97+
98+
// Initialize all subsets as single element sets
99+
100+
for (int i=0; i<graph.V; ++i)
101+
102+
parent[i]=-1;
103+
104+
105+
106+
// Iterate through all edges of graph, find subset of both
107+
108+
// vertices of every edge, if both subsets are same, then
109+
110+
// there is cycle in graph.
111+
112+
for (int i = 0; i < graph.E; ++i)
113+
114+
{
115+
116+
int x = graph.find(parent, graph.edge[i].src);
117+
118+
int y = graph.find(parent, graph.edge[i].dest);
119+
120+
121+
122+
if (x == y)
123+
124+
return 1;
125+
126+
127+
128+
graph.Union(parent, x, y);
129+
130+
}
131+
132+
return 0;
133+
134+
}
135+
136+
137+
138+
// Driver Method
139+
140+
public static void main (String[] args)
141+
142+
{
143+
144+
/* Let us create the following graph
145+
146+
0
147+
148+
| \
149+
150+
| \
151+
152+
1---2 */
153+
154+
int V = 3, E = 3;
155+
156+
Graph graph = new Graph(V, E);
157+
158+
159+
160+
// add edge 0-1
161+
162+
graph.edge[0].src = 0;
163+
164+
graph.edge[0].dest = 1;
165+
166+
167+
168+
// add edge 1-2
169+
170+
graph.edge[1].src = 1;
171+
172+
graph.edge[1].dest = 2;
173+
174+
175+
176+
// add edge 0-2
177+
178+
graph.edge[2].src = 0;
179+
180+
graph.edge[2].dest = 2;
181+
182+
183+
184+
if (graph.isCycle(graph)==1)
185+
186+
System.out.println( "graph contains cycle" );
187+
188+
else
189+
190+
System.out.println( "graph doesn't contain cycle" );
191+
192+
}
193+
}

0 commit comments

Comments
 (0)