-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion-find.py
More file actions
58 lines (43 loc) · 1.24 KB
/
Copy pathunion-find.py
File metadata and controls
58 lines (43 loc) · 1.24 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
from collections import defaultdict
# The structure to represent a graph
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = defaultdict(list)
def add_edge(self, u, v):
self.graph[u].append(v)
# Find the parent of the vertices
def find_parent(self, i, parent):
if parent[i] == -1:
return i
if parent[i] != -1:
return self.find_parent(parent[i], parent)
# Sets the parent of each vertex
def union(self, parent, x, y):
parent[x] = y
# Check if there is a cycle
# Return True if there is one and False if isn't
def check_cycle(self):
parent = [-1] * self.V
for i in self.graph:
for j in self.graph[i]:
x = self.find_parent(i, parent)
y = self.find_parent(j, parent)
if x == y:
return True
self.union(parent, x, y)
def main():
g = Graph(6)
g.add_edge(0, 1)
g.add_edge(0, 4)
g.add_edge(1, 2)
g.add_edge(1, 4)
g.add_edge(2, 3)
g.add_edge(3, 4)
g.add_edge(3, 5)
if g.check_cycle():
print('Cycle')
else:
print('No cycles')
if __name__ == '__main__':
main()