def test_Find_OnNotConnectedNodes_ReturnsFalse(self): uf = UnionFind(5) uf.union(1, 2) uf.union(2, 3) uf.union(3, 4) self.assertEqual(uf.find(1), uf.find(3)) self.assertEqual(uf.find(2), uf.find(4)) self.assertNotEqual(uf.find(1), uf.find(5)) self.assertNotEqual(uf.find(4), uf.find(5))
def find_clusters(self, count): sorted_edges = sorted(self._edges, key=attrgetter("cost")) uf = UnionFind(self._num_vertices) for index, edge in enumerate(sorted_edges): if count != uf.count and not uf.connected(edge.from_vertex, edge.to_vertex): uf.union(edge.from_vertex, edge.to_vertex) if uf.count == count and not uf.connected(edge.from_vertex, edge.to_vertex): return edge.cost
def test_GetCount_OnNonEffectUnions_RemainsTheSame(self): uf = UnionFind(10) uf.union(1, 2) self.assertEqual(uf.count, 9) uf.union(1, 2) self.assertEqual(uf.count, 9) uf.union(2, 3) self.assertEqual(uf.count, 8) uf.union(1, 3) self.assertEqual(uf.count, 8)
def test_Union_ThreeNodesInTwoPairs_AllHaveTheSameParent(self): uf = UnionFind(3) uf.union(1, 2) uf.union(2, 3) self.assertEqual(uf.find(1), uf.find(3))
def test_Union_TwoNodes_HaveTheSameParent(self): uf = UnionFind(2) uf.union(1, 2) self.assertEqual(uf.find(1), uf.find(2))