def test_Pnode_iterator(self): data = [Data(i) for i in range(0, 10, 2)] tree = PQtree(data) index = 0 for child in tree.get_root().iter_children(): self.assertEqual(child.data, data[index]) index += 1
def test2(self): data = [Data(i) for i in range(0, 10)] test_universe = data test_subset = [data[i] for i in [1, 3, 5]] T = PQtree(test_universe) T = reduce_tree(T, test_subset) tmp = [data[i] for i in [5, 7]] T = reduce_tree(T, tmp) self.assertTrue( check_consecutive(T.get_frontier(), [[1, 3, 5], [5, 7]]))
def test1(self): data = [Data(i) for i in range(0, 7)] test_universe = data test_subset = data[1:3] T = PQtree(test_universe) T = reduce_tree(T, test_subset) tmp = [data[3], data[6], data[5]] T = reduce_tree(T, tmp) self.assertTrue( check_consecutive(T.get_frontier(), [[1, 2], [3, 6, 5]]))
def upward_embed(graph): graph.compute_st_numbering() start_idx = list(graph.adj_list.keys())[0] universe = graph.get_edges_lower(start_idx) n = graph.get_num_of_vertices() tree = PQtree(universe) #print(tree) for iteration in range(start_idx + 1, n + 1): subset = graph.get_edges_higher(iteration) if len(subset) == 0: print("####SKIP ITERATION###") continue for node in subset: if node.node_reference is None: tree.root.add_child(Type.LEAF, node) #print("NONE REFS" + str(node)) try: tree = reduce_tree(tree, subset) except ReductionFailed: return False tmp_list = DirectionIndicator.list_of_instances #print([str(i) for i in tmp_list]) subset1 = graph.get_edges_lower(iteration) # Save pertinent root before its re-written on the next iteration pertinent_root = tree.get_pertinent_root(subset) assert pertinent_root is not None #print("---------------END of reduction --------------") # print(tree) if pertinent_root.node_type == Type.Q_NODE: #print("Replacing Q-node") adj_list = tree.replace_full_children( pertinent_root, PQtree(subset1, True).get_root(), iteration) else: #print("Replacing P-node") adj_list = tree.replace_node(pertinent_root, PQtree(subset1, True).get_root()) tree.reset_pseudo_node() #print(tree) tmp123 = [] for tmp in adj_list: if type(tmp) == str: tmp123.append(tmp) else: tmp123.append(tmp.data.data.get_lower()) for vertex in tmp123: graph.new_adj_list[iteration].append(vertex) graph.new_adj_list = correct_direction(graph.new_adj_list, n) return True
def test_Qnode_iterator(self): tree = PQtree([], True) root = tree.get_root() qnode = root.add_child(Type.Q_NODE) data = [Data(i) for i in range(0, 10, 2)] new_data = [data[i] for i in [1, 3, 0, 4, 2]] for i in new_data: qnode.add_child(Type.LEAF, i) index = 0 for child in qnode.iter_children(): self.assertEqual(child.data, new_data[index]) index += 1
def test3(self): data = [Data(i) for i in range(0, 10)] test_universe = data test_subset = [data[i] for i in [0, 1, 2, 3, 4, 5]] T = PQtree(test_universe) T = reduce_tree(T, test_subset) tmp = [data[i] for i in [3, 4, 5]] T = reduce_tree(T, tmp) tmp = [data[i] for i in [2, 4, 5]] T = reduce_tree(T, tmp) tmp = [data[i] for i in [0, 3, 7]] print("------------------") T = reduce_tree(T, tmp) print(T) self.assertTrue( check_consecutive( T.get_frontier(), [[0, 1, 2, 3, 4, 5], [3, 4, 5], [2, 4, 5], [0, 3, 7]]))
def test_Q2_template1(self): tree = PQtree([], True) root = tree.get_root() root.node_type = Type.Q_NODE data = [Data(i) for i in range(0, 10)] root.add_child(Type.LEAF, data[9]) root.add_child(Type.LEAF, data[4]) q1 = root.add_child(Type.Q_NODE, None) q1.add_child(Type.LEAF, data[5]) q1.add_child(Type.LEAF, data[0]) q1.add_child(Type.LEAF, data[6]) root.add_child(Type.LEAF, data[2]) root.add_child(Type.LEAF, data[7]) tree = reduce_tree(tree, [data[i] for i in [0, 7, 2, 6]]) self.assertTrue(check_consecutive(tree.get_frontier(), [[0, 2, 6, 7]]))
def test_Q3_template1(self): tree = PQtree([], True) root = tree.get_root() root.node_type = Type.Q_NODE data = [Data(i) for i in range(0, 15)] root.add_child(Type.LEAF, data[9]) root.add_child(Type.LEAF, data[4]) q1 = root.add_child(Type.Q_NODE, None) q1.add_child(Type.LEAF, data[5]) q1.add_child(Type.LEAF, data[0]) q1.add_child(Type.LEAF, data[6]) root.add_child(Type.LEAF, data[2]) root.add_child(Type.LEAF, data[7]) q2 = root.add_child(Type.Q_NODE, None) q2.add_child(Type.LEAF, data[3]) q2.add_child(Type.LEAF, data[14]) q2.add_child(Type.LEAF, data[8]) root.add_child(Type.LEAF, data[10]) root.add_child(Type.LEAF, data[13]) tree = reduce_tree(tree, [data[i] for i in [0, 7, 2, 6, 14, 3]]) tree.reset_pseudo_node() print(tree) self.assertTrue( check_consecutive(tree.get_frontier(), [[0, 2, 3, 6, 7, 14]])) # Check that endmost_children of pseudo_node has a root as a parent, since pseudo_node # is already deleted at this point self.assertEqual(data[8].node_reference.parent, root) self.assertEqual(data[5].node_reference.parent, root)
def test4(self): tree = PQtree([], True) root = tree.get_root() data = [Data(i) for i in range(0, 10)] root.add_child(Type.LEAF, data[9]) root.add_child(Type.LEAF, data[4]) q1 = root.add_child(Type.Q_NODE, None) q1.add_child(Type.LEAF, data[5]) q1.add_child(Type.LEAF, data[0]) q1.add_child(Type.LEAF, data[6]) root.add_child(Type.LEAF, data[2]) root.add_child(Type.LEAF, data[7]) q2 = root.add_child(Type.Q_NODE, None) q2.add_child(Type.LEAF, data[1]) q2.add_child(Type.LEAF, data[8]) q2.add_child(Type.LEAF, data[3]) tree = reduce_tree(tree, [data[i] for i in [0, 7, 2, 6, 3, 8]]) self.assertTrue( check_consecutive(tree.get_frontier(), [[0, 2, 3, 6, 7, 8]]))