# The question asks to find the maximum and minimum spacing after clustering # the answer lies in the remaining edges in the heap. # First build a node dictionary for each node pointing to what cluster it is in # Also build a cluster dictionary that stores one value for each of N to N clusters # Then keep popping from the heap, and store the edge cost in the cluster dictionary according to which cluster each # of it's nodes are in # Once the cluster dictionary is full, the largest value is the maximum spacing, and the smallest is the minimum # Creating the node dictionary runs in O(n) # Creating the cluster dictionary runs in less then O(k^2). # Therefore if k is sufficiently smaller then n, this doesn't effect the overall running time of O(m|alpha(N)) node_to_cluster = {} c = 0 for cluster in union_find.get_components(): for node in cluster: node_to_cluster[node] = str(c) c+=1 cluster_dict = {} for i in range(k): for j in range(i, k): if j > i: cluster_1 = str(i) cluster_2 = str(j) cluster_dict[cluster_1+"_"+cluster_2] = None while None in cluster_dict.values(): (cost, (node_1, node_2)) = heapq.heappop(edge_heap)