class DisjointSet_Test_Iter_After_Union_Towards_Multiple_Final_Sets( unittest.TestCase): def setUp(self): self._disjointset = DisjointSetWithUnion() self._positive_items_1 = [1, 2, 3, 4, 5, 6, 7, 8] self._negative_items_1 = [-1, -2, -3, -4, -5] self._all_items = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8] for item in self._all_items: self._disjointset.make_set(item) negative_items_count = len(self._negative_items_1) for index in range(0, negative_items_count - 1): set = self._disjointset.union(self._negative_items_1[index], self._negative_items_1[index + 1]) positive_items_count = len(self._positive_items_1) for index in range(0, positive_items_count - 1): set = self._disjointset.union(self._positive_items_1[index], self._positive_items_1[index + 1]) #Now there should be 3 sets with representations by -1, 0 and 1 def test_iter_after_union_towards_multiple_final_set(self): positive_representative_item = 1 negative_representative_item = -1 zero = 0 for item, set_of_item in self._disjointset: if item == 0: self.assertEqual( set_of_item, zero, 'Iterating over (item, set) yields wrong set membership') elif item > 0: self.assertEqual( set_of_item, positive_representative_item, 'Iterating over (item, set) yields wrong set membership') else: self.assertEqual( set_of_item, negative_representative_item, 'Iterating over (item, set) yields wrong set membership') def tearDown(self): self._disjointset = None
class DisjointSet_Test_Union_Towards_Multiple_Final_Sets(unittest.TestCase): def setUp(self): self._disjointset = DisjointSetWithUnion() self._positive_items_1 = [1, 2, 3, 4, 5, 6, 7, 8] self._negative_items_1 = [-1, -2, -3, -4, -5] self._all_items = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8] for item in self._all_items: self._disjointset.make_set(item) def test_union_towards_multiple_final_set(self): negative_items_count = len(self._negative_items_1) for index in range(0, negative_items_count - 1): set = self._disjointset.union(self._negative_items_1[index], self._negative_items_1[index + 1]) positive_items_count = len(self._positive_items_1) for index in range(0, positive_items_count - 1): set = self._disjointset.union(self._positive_items_1[index], self._positive_items_1[index + 1]) #Now there should be 3 sets with representations by -1, 0 and 1 positive_representative_item = 1 negative_representative_item = -1 zero = 0 for item in self._positive_items_1: self.assertEqual( self._disjointset.find(item), positive_representative_item, 'Find failed after union towards multiple final sets.') for item in self._negative_items_1: self.assertEqual( self._disjointset.find(item), negative_representative_item, 'Find failed after union towards multiple final sets.') self.assertEqual( self._disjointset.find(0), zero, 'Find failed after union towards multiple final sets.') def tearDown(self): self._disjointset = None
class DisjointSet_Test_Union_With_None(unittest.TestCase): def setUp(self): self._disjointset = DisjointSetWithUnion() def test_union_with_none(self): self.assertEqual(self._disjointset.union(item_1=None, item_2=None), None, 'disjoint union with none,none failed') def test_union_with_none_and_value(self): self.assertEqual( self._disjointset.union(item_1=None, item_2='Some Value'), None, 'disjoint union(None, some value) failed') def test_union_with_value_and_none(self): self.assertEqual( self._disjointset.union(item_1='Some value', item_2=None), None, 'disjoint union(some value, None) failed') def tearDown(self): self._disjointset = None
class DisjointSet_Test_Union_Towards_Single_Final_Set(unittest.TestCase): def setUp(self): self._disjointset = DisjointSetWithUnion() self._items = [1, 2, 3, 4, 5, 6, 7] for item in self._items: self._disjointset.make_set(item) def test_union_towards_single_final_set(self): items_count = len(self._items) set = None for index in range(0, items_count - 1): set = self._disjointset.union(self._items[index], self._items[index + 1]) self.assertEqual( self._disjointset.find(self._items[index]), set, 'Disjoint union-find failed while progressing to a single final set' ) self.assertEqual( self._disjointset.find(self._items[index + 1]), set, 'Disjoint union-find failed while progressing to a single final set' ) def tearDown(self): self._disjointset = None
from builtins import str from pycoils.sets.disjoint_sets_with_union import DisjointSetWithUnion if __name__ == '__main__': integers = [-5, -3, 0, 2, 10] disjoint_set = DisjointSetWithUnion() for integer in integers: disjoint_set.make_set(integer) item_to_find = -3 set_rep = disjoint_set.find(item_to_find) print('Item %d belongs to set represented by %d' % (item_to_find, set_rep)) #join set represented by -5 to the set represented by -3 print('Joining sets represented by -5 and -3') disjoint_set.union(-5, -3) print('Joining sets represented by 2 and 10') disjoint_set.union(2, 10) item_to_find = -3 set_rep = disjoint_set.find(item_to_find) print('Item %d belongs to set represented by %d' % (item_to_find, set_rep)) item_to_find = 10 set_rep = disjoint_set.find(item_to_find) print('Item %d belongs to set represented by %d' % (item_to_find, set_rep)) print('Iterating over the Disjoint set to get (item, set) pairs') for item, set_to_which_item_belongs in disjoint_set: print('Item %s belongs to set %s' %