the key to union find is actaully two functions, find and union. union is too simple to mentioned. find has two implementations, the following ones also have path compressions:

the recursive one:

def find(x):
    if x != father[x]:
        father[x] = find(father[father[x]])
    return father[x]

the procedure one:

def find(x):
    while x != father[x]:
        x = father[x]
        father[x] = father[father[x]]
    return father[x]

results matching ""

    No results matching ""