# once we have k clusters, # examine each cross-clusters edge and pick the minimum if min_spacing > w: min_spacing = w self.spacing = min_spacing def __sorted_edges(self): edges = [] for u in range(self.graph.V()): for e in self.graph.edges(u): # source vertex, dest vertex, edge weight edges.append((u, e[0], e[1])) def compare_edges(e1, e2): return cmp(e1[2], e2[2]) return sorted(edges, compare_edges) def max_spacing(self): return self.spacing if __name__ == "__main__": graph = graph_utils.load_weighted_graph('../data/clustering1.txt', False) slk = SingleLinkedClustering(4, graph) print slk.max_spacing()
self.w += w def __sorted_edges(self): edges = [] for u in range(self.graph.V()): for e in self.graph.edges(u): # source vertex, dest vertex, edge weight edges.append((u, e[0], e[1])) def compare_edges(e1, e2): return cmp(e1[2], e2[2]) return sorted(edges, compare_edges) def weight(self): return self.w if __name__ == "__main__": graph = graph_utils.load_weighted_graph("../data/tinyEWG.txt") mst = KruskalMST(graph) assert abs(1.81 - mst.weight()) < 0.0001 graph = graph_utils.load_weighted_graph("../data/mediumEWG.txt") mst = KruskalMST(graph) assert abs(10.46351 - mst.weight()) < 0.0001