Exemplo n.º 1
0
def test_pre_order_dfs_traversal__tree_3__root_node(tree_3):
    expected_labels = {("a", "b", "c", "d", "e"), ("a", "b", "c", "e", "d")}

    nodes = traversals.pre_order_dfs_traversal(tree_3.get_node("a"))
    actual_labels = tuple(node.label for node in nodes)

    assert actual_labels in expected_labels
Exemplo n.º 2
0
def test_pre_order_dfs_traversal__tree_4__non_root_node_all(tree_4):
    expected_labels = {("b", "c", "a", "d", "e"), ("b", "a", "d", "e", "c")}

    nodes = traversals.pre_order_dfs_traversal(tree_4.get_node("b"),
                                               ignore_directionality=True)
    actual_labels = tuple(node.label for node in nodes)

    assert actual_labels in expected_labels
Exemplo n.º 3
0
def optimal_sort_for_trees(
        graph: datastructures.Tree) -> datastructures.OrderedTree:
    graph_reversed = False
    if graph.is_in_tree():
        graph.reverse()
        graph_reversed = True
    node_to_descendents_count = {}
    for node in traversals.post_order_dfs_traversal(graph.root):
        node_to_descendents_count[node] = 1 + sum(
            node_to_descendents_count[child_node]
            for child_node in node.out_nodes)
    ordered_graph = datastructures.OrderedTree(
        graph,
        traversals.pre_order_dfs_traversal(
            graph.root, sorting_key=node_to_descendents_count.get),
    )
    if graph_reversed:
        ordered_graph.reverse()
    return ordered_graph
Exemplo n.º 4
0
def test_pre_order_dfs_traversal__edge_into_three_cycle(edge_into_three_cycle):
    nodes = traversals.pre_order_dfs_traversal(
        edge_into_three_cycle.get_node("w"))
    actual_labels = tuple(node.label for node in nodes)

    assert actual_labels == ("w", "x", "y", "z")
Exemplo n.º 5
0
def test_pre_order_dfs_traversal__tree_4__root_node__with_sorting(tree_4):
    nodes = traversals.pre_order_dfs_traversal(
        tree_4.get_node("a"), sorting_key=lambda node: node.label)
    actual_labels = tuple(node.label for node in nodes)

    assert actual_labels == ("a", "b", "c", "d", "e")
Exemplo n.º 6
0
def test_pre_order_dfs_traversal__tree_3__non_root_node__reverse(tree_3):
    tree_3.reverse()
    nodes = traversals.pre_order_dfs_traversal(tree_3.get_node("c"))
    actual_labels = tuple(node.label for node in nodes)

    assert actual_labels == ("c", "b", "a")