def test_acyclic_one_lca(self): root = LCA.Node(1) node_2 = LCA.Node(2) node_3 = LCA.Node(3) root.succ = [node_2] node_2.pred = [root] node_2.succ = [node_3] node_3.pred = [node_2] self.assertEqual(LCA.dagLCA(root, node_2, node_3), [2])
def test_cyclic(self): root = LCA.Node(1) node_2 = LCA.Node(2) node_3 = LCA.Node(3) node_4 = LCA.Node(4) root.succ = [node_2, node_3] root.pred = [node_3] node_2.succ = [node_3] node_2.pred = [root, node_4] node_3.succ = [root] node_3.pred = [node_2, node_4, root] node_4.succ = [node_2, node_3] self.assertEqual(LCA.dagLCA(root, node_2, node_3), [1, 4])
def test_acyclic_with_multiple_lca_maxima_(self): root = LCA.Node(1) node_2 = LCA.Node(2) node_3 = LCA.Node(3) node_4 = LCA.Node(4) root.succ = [node_2, node_3] node_2.pred = [root, node_4] node_2.succ = [node_3] node_3.pred = [root, node_2, node_4] node_4.succ = [node_2, node_4] self.assertEqual(LCA.dagLCA(root, node_2, node_3), [1, 4]) #lca of 2 # slash with dot/apostrophe signifies direction of relation # 4 5 # | \/ | # .|./\.|. # 1->2 -> 3 # \_____/' node_5 = LCA.Node(5) node_4.succ = [node_2, node_3] node_3.pred = [root, node_2, node_4, node_5] node_2.pred = [root, node_4, node_5] self.assertEqual(LCA.dagLCA(root, node_2, node_3), [1, 4, 5]) # lca of 3
def test_strongly_connected(self): root = LCA.Node(1) node_2 = LCA.Node(2) node_3 = LCA.Node(3) node_4 = LCA.Node(4) root.succ = [node_2, node_3, node_4] root.pred = [node_2, node_3, node_4] node_2.succ = [root, node_3, node_4] node_2.pred = [root, node_3, node_4] node_3.succ = [root, node_2, node_4] node_3.pred = [root, node_2, node_4] node_4.succ = [root, node_2, node_3] node_4.pred = [root, node_2, node_3] self.assertEqual(LCA.dagLCA(root, node_2, node_4), [1, 3])
def test_LCA_base_cases(self): root = LCA.Node(1) node_2 = LCA.Node(2) node_3 = LCA.Node(3) root.succ = [node_2] node_2.succ = [node_3] node_2.pred = [root] node_3.pred = [node_2] self.assertEqual(LCA.dagLCA(root, root, node_3), 1) # root and different key self.assertEqual(LCA.dagLCA(root, node_3, root), 1) # different key and root self.assertEqual(LCA.dagLCA(root, node_3, node_3), 3) # different key and root self.assertEqual(LCA.dagLCA(root, root, root), 1) # root and root self.assertEqual(LCA.dagLCA(root, None, root), None) # None and root self.assertEqual(LCA.dagLCA(root, root, None), None) # root and none self.assertEqual(LCA.dagLCA(None, root, node_3), None) # root is none
def test_lca(self): self.assertEqual( LCA.dagLCA(LCA.root, LCA.root.data, LCA.r3.data).data, 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r2, LCA.r2), 2) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r3, LCA.root), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r2, LCA.r3), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r2, LCA.r4), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r3, LCA.r4), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r2, LCA.r5), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r3, LCA.r6), 1) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r4, LCA.r5), 3) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r5, LCA.r6), 4) self.assertEqual(LCA.dagLCA(LCA.root, LCA.r4, LCA.r6), 4)
def test_null(self): self.assertEqual(LCA.dagLCA(None, 4, 6), None)