コード例 #1
0
        heapq.heappush(edge_heap,
                       # Add a tuple in the form of (cost, (node1, node2))
                        (
                           int(split[2]), (int(split[0]) - 1, int(split[1]) - 1)
                        )
                       )

        # creating a set of nodes solely to get the node count later to initialize the union find object
        set_of_nodes.add(split[0])
        set_of_nodes.add(split[1])

union_find = UF(len(set_of_nodes))

# Keep popping the smallest edge off the heap
# if the nodes are not connected then union them
while union_find.count() > k:
    (cost, (node_1, node_2)) = heapq.heappop(edge_heap)
    if not union_find.connected(node_1, node_2):
        union_find.union(node_1, node_2)


# 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).