Dynamic Connectivity
This commit is contained in:
17
dynamic-connectivity/index.py
Normal file
17
dynamic-connectivity/index.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# https://www.codewars.com/kata/dynamic-connectivity/python
|
||||||
|
|
||||||
|
class DynamicConnectivity(object):
|
||||||
|
def __init__(self, n):
|
||||||
|
self.__map = list(range(0, n))
|
||||||
|
|
||||||
|
def __root(self, p):
|
||||||
|
while p != self.__map[p]:
|
||||||
|
self.__map[p] = self.__map[self.__map[p]]
|
||||||
|
p = self.__map[p]
|
||||||
|
return p
|
||||||
|
|
||||||
|
def union(self, p, q):
|
||||||
|
self.__map[self.__root(p)] = self.__root(q)
|
||||||
|
|
||||||
|
def connected(self, p, q):
|
||||||
|
return self.__root(p) == self.__root(q)
|
||||||
Reference in New Issue
Block a user