Skip to content

Instantly share code, notes, and snippets.

@devils-ey3
Created September 21, 2016 10:57
Show Gist options
  • Save devils-ey3/3899032740332569ac6e8bfdc54fa08b to your computer and use it in GitHub Desktop.
Save devils-ey3/3899032740332569ac6e8bfdc54fa08b to your computer and use it in GitHub Desktop.
quick find connectivity check algorithm demo
"""
Is a quick find connectivity check algorithm demo code.
I try to write it simply, hope you can understand it
"""
def give_index_value(a):
count = 0
id = {}
a = set(a) # for removing duplicates'
for i in a:
id[i]=count
count+=1
return id
def union(p,q,id):
if id[q]==id[p]:
return id
else:
value_of_index_p = id[p]
id[p] = id[q]
for k,v in id.items():
if v == value_of_index_p:
id[k]=id[q]
return id
def check_connection(p,q,id):
try:
return ('Not connected','connected') [id[p]==id[q]]
except TypeError:
return 'Not Connected'
def show(id):
for k in id.keys():
print(k,end=' ')
print()
for v in id.values():
print(v,end=' ')
a = [0,1,2,3,4,5,6,7,8,9]
a = give_index_value(a)
x = int(input('Press 1 for Union\nPress 2 for check connection\n'))
return_id = None
while(1):
if x==1:
i = input('Enter two number for union : ')
i = i.split()
return_id = union(int(i[0]),int(i[1]),a)
show(return_id)
elif x==2:
i = input('Enter two number for check connection : ')
i = i.split()
print(check_connection(int(i[0]),int(i[1]),return_id))
x = int(input('\nPress 1 for Union\nPress 2 for check connection\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment