-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathDisjointSetUnion.py
47 lines (38 loc) · 1.17 KB
/
DisjointSetUnion.py
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
from collections import defaultdict
# Take input from the user
N = int(input("Enter the number of vertices: "))
edges = []
num_edges = int(input("Enter the number of edges: "))
print("Enter the edges in the format 'x y':")
for _ in range(num_edges):
x, y = map(int, input().split())
edges.append([x, y])
# Stores the parent of each vertex
parent = list(range(N))
# Function to find the topmost parent of vertex x
def find(x):
if x != parent[x]:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
parent_x = find(x)
parent_y = find(y)
if parent_x != parent_y:
parent[parent_y] = parent_x
# Perform the union-find algorithm on the given edges
for x, y in edges:
union(x, y)
# Create a dictionary to store the groups of vertices
dict_pair = defaultdict(list)
# Group vertices based on their parent
for idx, val in enumerate(parent):
dict_pair[find(val)].append(idx)
# Print the input and output
print("\nInput:")
print("Number of vertices:", N)
print("Edges:", edges)
print("\nOutput:")
print("Number of disjoint groups:", len(dict_pair.keys()))
print("Groups of vertices:")
for group in dict_pair.values():
print(group)