Ejemplo n.º 1
0
 def test_initialization(self):
     """Test public methods on an empty union."""
     # Create { }.
     an_empty_graph = UnionFind()
     self.assertEqual(an_empty_graph.n_vertices(), 0)
     self.assertEqual(an_empty_graph.connected(0, 0), False)
     self.assertEqual(an_empty_graph.connected(0, 1), False)
Ejemplo n.º 2
0
 def test_non_consecutive_adding(self):
     """Test adding vertices non-consecutively."""
     # Create { }.
     a_graph = UnionFind()
     # Add 1 to { }, which becomes { {0}, {1} }.
     # Here 0 is added implicitly.
     a_graph.add(1)
     self.assertEqual(a_graph.n_vertices(), 2)
     self.assertEqual(a_graph.connected(0, 0), True)
     self.assertEqual(a_graph.connected(0, 1), False)
     self.assertEqual(a_graph.connected(1, 1), True)
Ejemplo n.º 3
0
 def test_consecutive_adding(self):
     """Test adding vertices consecutively."""
     # Create { }.
     a_graph = UnionFind()
     # Add 0 to { }, which becomes { {0} }.
     a_graph.add(0)
     self.assertEqual(a_graph.n_vertices(), 1)
     self.assertEqual(a_graph.connected(0, 0), True)
     self.assertEqual(a_graph.connected(0, 1), False)
     self.assertEqual(a_graph.connected(1, 1), False)
     # Add 0 to { {0} }, nothing changes.
     a_graph.add(0)
     self.assertEqual(a_graph.n_vertices(), 1)
     self.assertEqual(a_graph.connected(0, 0), True)
     self.assertEqual(a_graph.connected(0, 1), False)
     self.assertEqual(a_graph.connected(1, 1), False)
     # Add 1 to { {0} }, which becomes { {0}, {1} }.
     a_graph.add(1)
     self.assertEqual(a_graph.n_vertices(), 2)
     self.assertEqual(a_graph.connected(0, 0), True)
     self.assertEqual(a_graph.connected(0, 1), False)
     self.assertEqual(a_graph.connected(1, 1), True)
Ejemplo n.º 4
0
 def test_implicit_adding_by_connecting(self):
     """Test connect(), which implicitly calls add()."""
     # Create { }.
     a_graph = UnionFind()
     # Implicitly add 0, 1, 2 to { }, then connect 1 with 2,
     # which makes { {0}, {1, 2} }.
     a_graph.connect(1, 2)
     self.assertEqual(a_graph.n_vertices(), 3)
     # Trivially connected components.
     self.assertEqual(a_graph.connected(0, 0), True)
     self.assertEqual(a_graph.connected(1, 1), True)
     self.assertEqual(a_graph.connected(2, 2), True)
     # Connected components created by connect().
     self.assertEqual(a_graph.connected(1, 2), True)
     self.assertEqual(a_graph.connected(2, 1), True)
     # Disconnected components.
     self.assertEqual(a_graph.connected(0, 1), False)
     self.assertEqual(a_graph.connected(1, 0), False)
     self.assertEqual(a_graph.connected(0, 2), False)
     self.assertEqual(a_graph.connected(2, 0), False)