Exemplo n.º 1
0
    def clustering(self, groups, minheap):
        #maxheap is a max heap of edges (one direction only)
        unionfind = UnionFind(list(self.graph.keys()))
        while unionfind.size() > groups:
            #keep merging until number of desired groups reached
            curr = minheap.pop()
            curr_edge = curr.get_data()
            if unionfind.find(curr_edge[0]) == unionfind.find(curr_edge[1]):
                #same group
                continue
            unionfind.union(curr_edge[0], curr_edge[1], True)
        while unionfind.find(curr_edge[0]) == unionfind.find(curr_edge[1]):
            #pop until different groups to get max distance, because next edge might be within a group
            minheap.pop().get_data()
            curr_edge = minheap.peek().get_data()

        #smallest distance is the edge at the top of max heap since they are in different groups
        return unionfind, minheap.peek().get_key()