コード例 #1
0
    def test_isomorphic_trees_05(self):
        g = Graph(10)
        g.add_undirected_edge(0, 2, 0)
        g.add_undirected_edge(0, 1, 0)
        g.add_undirected_edge(0, 3, 0)
        g.add_undirected_edge(2, 6, 0)
        g.add_undirected_edge(2, 7, 0)
        g.add_undirected_edge(1, 4, 0)
        g.add_undirected_edge(1, 5, 0)
        g.add_undirected_edge(5, 9, 0)
        g.add_undirected_edge(3, 8, 0)

        root0 = IsomorphicTrees.root_tree(g, 0)
        self.assertEqual(IsomorphicTrees._encode(root0),
                         "(((())())(()())(()))")
コード例 #2
0
 def test_isomorphic_trees_04(self):
     g1 = Graph(5)
     g2 = Graph(5)
     g1.add_undirected_edge(2, 0, 0)
     g1.add_undirected_edge(3, 4, 0)
     g1.add_undirected_edge(2, 1, 0)
     g1.add_undirected_edge(2, 3, 0)
     g2.add_undirected_edge(1, 0, 0)
     g2.add_undirected_edge(2, 4, 0)
     g2.add_undirected_edge(1, 3, 0)
     g2.add_undirected_edge(1, 2, 0)
     self.assertEqual(IsomorphicTrees.isomorphic_equal(g1, g2), True)
コード例 #3
0
 def test_isomorphic_trees_00(self):
     g = Graph(9)
     g.add_undirected_edge(0, 1, 0)
     g.add_undirected_edge(2, 1, 0)
     g.add_undirected_edge(2, 3, 0)
     g.add_undirected_edge(3, 4, 0)
     g.add_undirected_edge(5, 3, 0)
     g.add_undirected_edge(2, 6, 0)
     g.add_undirected_edge(6, 7, 0)
     g.add_undirected_edge(6, 8, 0)
     centers = IsomorphicTrees.get_tree_centers(g)
     self.assertCountEqual(centers, [2])
コード例 #4
0
 def test_isomorphic_trees_03(self):
     g = Graph(9)
     g.add_undirected_edge(0, 1, 0)
     g.add_undirected_edge(2, 1, 0)
     g.add_undirected_edge(2, 3, 0)
     g.add_undirected_edge(3, 4, 0)
     g.add_undirected_edge(5, 3, 0)
     g.add_undirected_edge(2, 6, 0)
     g.add_undirected_edge(6, 7, 0)
     g.add_undirected_edge(6, 8, 0)
     root_tree = IsomorphicTrees.root_tree(g, 6)
     keys = [c.key for c in root_tree.get_children()]
     self.assertEqual(root_tree.key, 6)
     self.assertIsNone(root_tree.parent)
     self.assertCountEqual(keys, [2, 7, 8])
コード例 #5
0
 def test_isomorphic_trees_02(self):
     g = Graph(7)
     g.add_undirected_edge(0, 1, 0)
     g.add_undirected_edge(1, 2, 0)
     g.add_undirected_edge(2, 3, 0)
     g.add_undirected_edge(3, 4, 0)
     g.add_undirected_edge(4, 5, 0)
     g.add_undirected_edge(4, 6, 0)
     centers = IsomorphicTrees.get_tree_centers(g)
     self.assertCountEqual(centers, [2, 3])
コード例 #6
0
class BFSGraphTest(unittest.TestCase):

  def setUp(self):
    self.g = Graph(13)
    self.g.add_undirected_edge(0, 7, 1)
    self.g.add_undirected_edge(0, 9, 1)
    self.g.add_undirected_edge(0, 11, 1)
    self.g.add_undirected_edge(7, 11, 1)
    self.g.add_undirected_edge(7, 6, 1)
    self.g.add_undirected_edge(7, 3, 1)
    self.g.add_undirected_edge(6, 5, 1)
    self.g.add_undirected_edge(3, 4, 1)
    self.g.add_undirected_edge(2, 3, 1)
    self.g.add_undirected_edge(2, 12, 1)
    self.g.add_undirected_edge(12, 8, 1)
    self.g.add_undirected_edge(8, 1, 1)
    self.g.add_undirected_edge(1, 10, 1)
    self.g.add_undirected_edge(10, 9, 1)
    self.g.add_undirected_edge(9, 8, 1)

  def test_bfs_00(self):
    path = bfs_graph.bfs_path(self.g, start=10, end=5)
    self.assertEqual(path, [10, 9, 0, 7, 6, 5])
コード例 #7
0
 def test_isomorphic_trees_01(self):
     g = Graph(2)
     g.add_undirected_edge(0, 1, 0)
     centers = IsomorphicTrees.get_tree_centers(g)
     self.assertCountEqual(centers, [0, 1])