Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union Find Algorithm in Python #92

Open
abhaysinghr516 opened this issue Oct 23, 2023 · 4 comments
Open

Union Find Algorithm in Python #92

abhaysinghr516 opened this issue Oct 23, 2023 · 4 comments

Comments

@abhaysinghr516
Copy link
Owner

No description provided.

@alok5050
Copy link

Hey, I would like to work on this issue.

@alok5050
Copy link

def initialize(n):
global Arr, size
for i in range(n + 1):
Arr[i] = i
size[i] = 1

def find(i):
global Arr, size

while (Arr[i] != i):
    Arr[i] = Arr[Arr[i]] # Skip one level 
    i = Arr[i] # Move to the new level
return i

def _union(xr, yr):
global Arr, size
if (size[xr] < size[yr]): # Make yr parent of xr
Arr[xr] = Arr[yr]
size[yr] += size[xr]
else: # Make xr parent of yr
Arr[yr] = Arr[xr]
size[xr] += size[yr]

def isCycle(adj, V):
global Arr, size

for i in range(V):
    for j in range(len(adj[i])):
        x = find(i) # find root of i 
        y = find(adj[i][j]) # find root of adj[i][j] 

        if (x == y):
            return 1 # If same parent 
        _union(x, y) # Make them connect
return 0

MAX_VERTEX = 101

Arr = [None] * MAX_VERTEX

size = [None] * MAX_VERTEX

V = 3

adj = [[] for i in range(V)]

adj[0].append(1)
adj[0].append(2)
adj[1].append(2)

if (isCycle(adj, V)):
print("Graph contains Cycle.")
else:
print("Graph does not contain Cycle.")

@abhaysinghr516
Copy link
Owner Author

Create Pull Request

@nazishkn67
Copy link
Contributor

Hey Abhay, I would like to work on this issue if currently, no one is working on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants