def test_tgt_decomposition__tree_reverse(non_trivial_tree):
    non_trivial_tree.reverse()
    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(non_trivial_tree)

    assert tree_1 == non_trivial_tree
    assert graph is None
    assert tree_2 is None
def test_tgt_decomposition__tree_mutable(non_trivial_tree):
    original = non_trivial_tree.immutable()
    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(
        non_trivial_tree.mutable()
    )

    assert tree_1 is None
    assert graph is None
    assert original == tree_2  # == original
def test_tgt_decomposition__general_case_5():
    middle_graph = datastructures.Graph.build_from_edge_label_tuples(
        [("a", "c"), ("c", "d"), ("b", "d"), ("c", "e"), ("e", "f")]
    )

    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(middle_graph)

    assert tree_1 is None
    assert graph == middle_graph
    assert tree_2 is None
def test_tgt_decomposition__general_case_1(in_tree_piece, out_tree_piece):
    in_tree_piece.create_node("b1")
    in_tree_piece.create_edge_using_labels("a1", "b1")

    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(
        datastructures.Graph.union([in_tree_piece, out_tree_piece])
    )

    assert tree_1 == datastructures.Tree(in_tree_piece, in_tree_piece.get_node("b1"))
    assert graph is None
    assert tree_2 == datastructures.Tree(out_tree_piece, out_tree_piece.get_node("b1"))
def test_tgt_decomposition__general_case_3(in_tree_piece, out_tree_piece):
    middle_graph = datastructures.Graph.build_from_edge_label_tuples(
        [("a1", "c1"), ("c1", "b1"), ("a1", "c2"), ("c2", "b1")]
    )

    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(
        datastructures.Graph.union([in_tree_piece, middle_graph, out_tree_piece])
    )

    assert tree_1 == datastructures.Tree(in_tree_piece, in_tree_piece.get_node("a1"))
    assert graph == middle_graph
    assert tree_2 == datastructures.Tree(out_tree_piece, out_tree_piece.get_node("b1"))
def test_tgt_decomposition__general_case_2(in_tree_piece):
    out_tree_piece = datastructures.Graph.build_from_edge_label_tuples(
        [("a1", "b2"), ("a1", "b3")]
    )

    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(
        datastructures.Graph.union([in_tree_piece, out_tree_piece])
    )

    assert tree_1 == datastructures.Tree(in_tree_piece, in_tree_piece.get_node("a1"))
    assert graph is None
    assert tree_2 == datastructures.Tree(out_tree_piece, out_tree_piece.get_node("a1"))
def test_tgt_decomposition__edge_into_four_cycle(
    four_node_cycle: datastructures.Graph,
):
    node_into_four_cycle = datastructures.MutableGraph(four_node_cycle)
    node_into_four_cycle.create_node("x")
    node_into_four_cycle.create_edge_using_labels("x", "a")

    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(node_into_four_cycle)

    assert tree_1 is None
    assert graph == node_into_four_cycle
    assert tree_2 is None
Exemple #8
0
def tgt_sort(
        graph: datastructures.AbstractGraph) -> datastructures.OrderedGraph:
    tree_1, inner_graph, tree_2 = operations.calculate_tgt_decomposition(
        operations.calculate_transitive_reduction(graph.immutable()))

    labels = []
    for sub_graph, sorting_method in (
        (tree_1, optimal_sort_for_trees),
        (inner_graph, basic_sort),
        (tree_2, optimal_sort_for_trees),
    ):
        if sub_graph is None:
            continue
        new_labels = [node.label for node in sorting_method(sub_graph).nodes()]
        if len(labels) > 0:
            assert labels[-1] == new_labels[0]
            labels += new_labels[1:]
        else:
            labels = new_labels
    return datastructures.OrderedGraph(
        graph, [graph.get_node(label) for label in labels])
def test_tgt_decomposition__cycle(cycle):
    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(cycle)

    assert tree_1 is None
    assert graph == cycle
    assert tree_2 is None
def test_tgt_decomposition__tree(non_trivial_tree):
    tree_1, graph, tree_2 = operations.calculate_tgt_decomposition(non_trivial_tree)

    assert tree_1 is None
    assert graph is None
    assert tree_2 == non_trivial_tree