def tree_sequence(self, strategy): """Create a sequence of mixed trees with associated maps. Depending on the given strategy, the algorithm 2 or 4 will be computed. The given tree `self.mixed_tree` must be *consistent* and have only *undirected* edges. Args: strategy: a list of the functions needed, depending on which algorithm we want to execute (2 or 4) Returns: seq (list): a sequence ((T_0, S_0), ..., (T_i, S_i)...) of mixed trees and there maps """ strategy.algo = self current_tree = self.mixed_tree current_map = s_0(current_tree) self.maps = current_map seq = [(current_tree, current_map)] while len(current_tree) > 1: current_tree, current_map = self.step(strategy) seq.append((current_tree, current_map)) return seq
def test_tree_sequence(self): g = BinaryMixedTree(MixedGraph({0, 1}, [(0, 1)])) g2 = BinaryMixedTree(MixedGraph()) g2.add(frozenset([0, 1])) expected = [ (g, s_0(g)), (g2, { frozenset([1]): {frozenset([1])}, frozenset([0]): {frozenset([0])}, frozenset([0, 1]): {frozenset([0]), frozenset([1])} } ) ] value = BasicTreeConstruction(g, s_0(g)).tree_sequence(StratAlgo1()) self.assertEqual(value, expected)
def test_A_set(self): t = BinaryMixedTree(MixedGraph({1, 2, 3})) t.add(frozenset([1, 3])) t.add_directed(frozenset([1]), frozenset([2])) t.add_directed(frozenset([1]), frozenset([1, 3])) s = s_0(t) s[frozenset([1, 3])] = {frozenset([3]), frozenset([1])} delta_plus = t(frozenset([1]), undirected=False, begin=True, end=False, closed=False) A = line_14(delta_plus, s, frozenset([1])) self.assertEqual(A, {frozenset([2]), frozenset([3])})
def test_one_edge_available(self): h = HyperGraph(frozenset([frozenset([1]), frozenset([2]), frozenset([3])]), frozenset([frozenset([frozenset([1])]), frozenset([frozenset([2])]), frozenset([frozenset([3])]), frozenset([frozenset([1]), frozenset([2])]), frozenset([frozenset([2]), frozenset([3])]), frozenset([frozenset([i + 1]) for i in range(3)]) ])) t = BinaryMixedTree(MixedGraph({1, 2, 3}, [(1, 3)])) t.add_directed(frozenset([1]), frozenset([2])) self.assertEqual(edge_choice_for_algo3(BasicTreeConstruction(t, s_0(t), h)), frozenset([frozenset([1]), frozenset([3])]))
def test_one_undirected(self): g = BinaryMixedTree(MixedGraph({0, 1, 2}, [(0, 1)])) g.add_directed(frozenset([1]), frozenset([2])) value_graph_2, value_map_2 = BasicTreeConstruction(g, s_0(g)).step(StratAlgo1()) expected_graph = BinaryMixedTree(MixedGraph({2})) expected_graph.add(frozenset([0, 1])) expected_graph.add_undirected(frozenset([2]), frozenset([0, 1])) expected_map = {frozenset([0]): {frozenset([0])}, frozenset([1]): {frozenset([1])}, frozenset([2]): {frozenset([2])}, frozenset([0, 1]): {frozenset([0]), frozenset([1])}} self.assertEqual(value_graph_2, expected_graph) self.assertEqual(value_map_2, expected_map)
def test_empty_delta_z(self): h = HyperGraph(frozenset([frozenset([1]), frozenset([2]), frozenset([3])]), frozenset([frozenset([frozenset([1])]), frozenset([frozenset([2])]), frozenset([frozenset([3])]), frozenset([frozenset([1]), frozenset([2])]), frozenset([frozenset([2]), frozenset([3])]), frozenset([frozenset([i + 1]) for i in range(3)]) ])) t = BinaryMixedTree(MixedGraph({1, 2, 3})) t.add(frozenset([1, 3])) t.add_directed(frozenset([1]), frozenset([2])) self.assertEqual( delta_z_subset_algo3(BasicTreeConstruction(t, s_0(t), h), set(), frozenset([1, 3]), frozenset([1])), set())
def test_line_16_2(self): t = BinaryMixedTree(MixedGraph({1, 2, 3, 4, 5, 6}, [(3, 6)])) t.add(frozenset([1, 3])) t.add_directed(frozenset([1]), frozenset([2])) t.add_directed(frozenset([3]), frozenset([4])) t.add_directed(frozenset([1]), frozenset([1, 3])) t.add_undirected(frozenset([1, 3]), frozenset([5])) s = s_0(t) s[frozenset([1, 3])] = {frozenset([1]), frozenset([3])} delta_plus = t(frozenset([1]), undirected=False, begin=True, end=False, closed=False) A = line_14(delta_plus, s, frozenset([1])) dict_s = line_16(A, delta_plus, s) self.assertEqual(dict_s, {frozenset([2]): frozenset([2]), frozenset([3]): frozenset([1, 3])})
def test_edge_choice(self): g = HyperGraph(frozenset([frozenset([i]) for i in range(1, 7)])) for i in range(1, 7): g.add_edge(frozenset([frozenset([i])])) g.add_edge(frozenset([frozenset([i]) for i in range(1, 7)])) g.add_edge(frozenset([frozenset([1]), frozenset([2])])) g.add_edge(frozenset([frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([1]), frozenset([2]), frozenset([3]), frozenset([4]), frozenset([5])])) t = BinaryMixedTree(MixedGraph({1, 2, 3, 4, 5, 6}, [(1, 3), (3, 6), (1, 5)])) t.add_directed(frozenset([1]), frozenset([2])) t.add_directed(frozenset([3]), frozenset([4])) self.assertEqual(edges_in_homogeneous_subset(t, t.homogeneous_subset()), list(t.edges[0])) value = edge_choice_for_algo3(BasicTreeConstruction(t, s_0(t), g)) self.assertEqual(value, frozenset([frozenset([1]), frozenset([3])]))
def test_edge_choice_for_algo3(self): h = HyperGraph( frozenset([frozenset([1]), frozenset([2]), frozenset([3]), frozenset([4]), frozenset([5]), frozenset([6])]), frozenset([frozenset([frozenset([1])]), frozenset([frozenset([2])]), frozenset([frozenset([3])]), frozenset([frozenset([4])]), frozenset([frozenset([5])]), frozenset([frozenset([6])]), frozenset([frozenset([4]), frozenset([5])]), frozenset([frozenset([5]), frozenset([6])]), frozenset([frozenset([1]), frozenset([2]), frozenset([3]), frozenset([4]), frozenset([5]), frozenset([6])])])) g = BinaryMixedTree(MixedGraph({1, 2, 3, 4, 5, 6}, [(1, 2), (2, 4), (4, 3), (4, 5), (5, 6)])) map = s_0(g) value = edge_choice_for_algo3(BasicTreeConstruction(g, map, h)) expected = frozenset({frozenset([4]), frozenset([5])}) self.assertEqual(list(g.edges[0]), edges_in_homogeneous_subset(g, g.homogeneous_subset())) self.assertEqual(expected, value)
def test_delta_z(self): g = HyperGraph(frozenset([frozenset([i]) for i in range(1, 7)])) for i in range(1, 7): g.add_edge(frozenset([frozenset([i])])) g.add_edge(frozenset([frozenset([i]) for i in range(1, 7)])) g.add_edge(frozenset([frozenset([1]), frozenset([2])])) g.add_edge(frozenset([frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([1]), frozenset([2]), frozenset([3]), frozenset([4]), frozenset([5])])) t = BinaryMixedTree(MixedGraph({1, 2, 3, 4, 5, 6}, [(3, 6), (1, 5)])) s = s_0(t) s[frozenset([1, 3])] = {frozenset([1]), frozenset([3])} t.add(frozenset([1, 3])) t.add_directed(frozenset([1]), frozenset([2])) t.add_directed(frozenset([3]), frozenset([4])) expected = {frozenset([5])} value = delta_z_subset_algo3(BasicTreeConstruction(t, s, g), frozenset([frozenset([5])]), frozenset([1, 3]), frozenset([1])) self.assertEqual(expected, value)
def test_hypergraph_2(self): g = HyperGraph(frozenset([frozenset([i]) for i in range(1, 7)])) for i in range(1, 7): g.add_edge(frozenset([frozenset([i])])) g.add_edge(frozenset([frozenset([i]) for i in range(1, 7)])) g.add_edge(frozenset([frozenset([1]), frozenset([2])])) g.add_edge(frozenset([frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5])])) g.add_edge(frozenset([frozenset([3]), frozenset([4]), frozenset([5]), frozenset([6])])) g.add_edge(frozenset([frozenset([1]), frozenset([2]), frozenset([3]), frozenset([4]), frozenset([5])])) t = BinaryMixedTree(MixedGraph({1, 2, 3, 4, 5, 6}, [(3, 6), (1, 5), (1, 3)])) t.add_directed(frozenset([1]), frozenset([2])) t.add_directed(frozenset([3]), frozenset([4])) next_tree, next_map = BasicTreeConstruction(t, s_0(t), g).step(StratAlgo3()) self.assertEqual({frozenset([1]): {frozenset([1])}, frozenset([2]): {frozenset([2])}, frozenset([3]): {frozenset([3])}, frozenset([4]): {frozenset([4])}, frozenset([5]): {frozenset([5])}, frozenset([6]): {frozenset([6])}, frozenset([1, 3]): {frozenset([1]), frozenset([3])} }, next_map) expected_tree = BinaryMixedTree(MixedGraph({2, 5, 3, 4, 6}, [(3, 6)])) expected_tree.add(frozenset([1, 3])) expected_tree.add_undirected(frozenset([1, 3]), frozenset([5])) expected_tree.add_undirected(frozenset([1, 3]), frozenset([2])) expected_tree.add_directed(frozenset([3]), frozenset([1, 3])) expected_tree.add_directed(frozenset([3]), frozenset([4])) self.assertEqual(expected_tree, next_tree)
def test_hypergraph_1(self): h = HyperGraph(frozenset([frozenset([1]), frozenset([2]), frozenset([3])]), frozenset([frozenset([frozenset([1])]), frozenset([frozenset([2])]), frozenset([frozenset([3])]), frozenset([frozenset([1]), frozenset([2])]), frozenset([frozenset([2]), frozenset([3])]), frozenset([frozenset([i + 1]) for i in range(3)]) ])) t = BinaryMixedTree(MixedGraph({1, 2, 3}, [(1, 3)])) t.add_directed(frozenset([1]), frozenset([2])) maps = s_0(t) next_tree, next_map = BasicTreeConstruction(t, maps, h).step(StratAlgo3()) self.assertEqual( {frozenset([1]): {frozenset([1])}, frozenset([2]): {frozenset([2])}, frozenset([3]): {frozenset([3])}, frozenset([1, 3]): {frozenset([1]), frozenset([3])}}, next_map) expected_graph = BinaryMixedTree(MixedGraph({2})) expected_graph.add(frozenset([1, 3])) expected_graph.add_undirected(frozenset([2]), frozenset([1, 3])) self.assertEqual(expected_graph, next_tree)