我有以下针对Union-Find数据结构的递归方法。但是,此实现给了我递归错误:
RecursionError: maximum recursion depth exceeded in comparison
我想不出任何方法将其转换为简单的迭代方法。我不擅长递归函数,我需要帮助以找到解决此问题的方法
def find(data, i):
if i != data[i]:
data[i] = find(data, data[i])
return data[i]
def union(data, i, j):
pi, pj = find(data, i), find(data, j)
if pi != pj:
data[pi] = pj
data = [i for i in range(15000)]
connections = [(1457,2978), (2453,9785), ....] # A huge list of tuples
for i, j in connections:
union(data, i, j)