Пример #1
0
 def test_two_component_graph(self):
     """Test empty graph"""
     G = nx.Graph()
     G.add_node(1)
     G.add_node(2)
     treewidth, _ = treewidth_min_fill_in(G)
     assert_equals(treewidth, 0)
Пример #2
0
 def test_two_component_graph(self):
     """Test empty graph"""
     G = nx.Graph()
     G.add_node(1)
     G.add_node(2)
     treewidth, _ = treewidth_min_fill_in(G)
     assert_equals(treewidth, 0)
Пример #3
0
 def test_small_tree_treewidth(self):
     """Test if the computed treewidth of the known self.small_tree is 2"""
     G = self.small_tree
     # the order of removal should be [1,2,4]3[5,6,7]
     # (with [] denoting any order of the containing nodes)
     # resulting in treewidth 2 for the heuristic
     treewidth, _ = treewidth_min_fill_in(G)
     assert_equals(treewidth, 2)
Пример #4
0
 def test_small_tree_treewidth(self):
     """Test if the computed treewidth of the known self.small_tree is 2"""
     G = self.small_tree
     # the order of removal should be [1,2,4]3[5,6,7]
     # (with [] denoting any order of the containing nodes)
     # resulting in treewidth 2 for the heuristic
     treewidth, _ = treewidth_min_fill_in(G)
     assert_equals(treewidth, 2)
Пример #5
0
    def test_small_tree_treewidth(self):
        """Test small tree

        Test if the computed treewidth of the known self.small_tree is 2.
        As we know which value we can expect from our heuristic, values other
        than two are regressions
        """
        G = self.small_tree
        # the order of removal should be [1,2,4]3[5,6,7]
        # (with [] denoting any order of the containing nodes)
        # resulting in treewidth 2 for the heuristic
        treewidth, _ = treewidth_min_fill_in(G)
        assert_equals(treewidth, 2)
Пример #6
0
    def test_small_tree_treewidth(self):
        """Test small tree

        Test if the computed treewidth of the known self.small_tree is 2.
        As we know which value we can expect from our heuristic, values other
        than two are regressions
        """
        G = self.small_tree
        # the order of removal should be [1,2,4]3[5,6,7]
        # (with [] denoting any order of the containing nodes)
        # resulting in treewidth 2 for the heuristic
        treewidth, _ = treewidth_min_fill_in(G)
        assert_equals(treewidth, 2)
Пример #7
0
 def test_empty_graph(self):
     """Test empty graph"""
     G = nx.Graph()
     _, _ = treewidth_min_fill_in(G)
Пример #8
0
 def test_petersen_graph(self):
     """Test Petersen graph tree decomposition result"""
     G = nx.petersen_graph()
     _, decomp = treewidth_min_fill_in(G)
     is_tree_decomp(G, decomp)
Пример #9
0
 def test_empty_graph(self):
     """Test empty graph"""
     G = nx.Graph()
     _, _ = treewidth_min_fill_in(G)
Пример #10
0
 def test_petersen_graph(self):
     """Test Petersen graph tree decomposition result"""
     G = nx.petersen_graph()
     _, decomp = treewidth_min_fill_in(G)
     is_tree_decomp(G, decomp)
Пример #11
0
def process_graph(G, savefigures=True):

    edges = list(G.edges)
    edges = [frozenset(e) for e in edges]

    treewidth, treedecomposition = approximation.treewidth_min_fill_in(G)

    print("Treewidth ", treewidth)

    if savefigures:
        pos = nx.spring_layout(treedecomposition, scale=3)
        nx.draw(treedecomposition, pos, with_labels=True, font_size=6)
        plt.savefig("./raw_treedecomposition.svg", format="svg")
        plt.clf()

    nodes_dictionary, node_identifier_root = get_rooted_tree_decomposition(
        treedecomposition)
    print(
        "Tree size ",
        check_tree_size(nodes_dictionary[node_identifier_root], verbose=False))

    nice_tree_root = get_rooted_nice_decomposition(
        nodes_dictionary[node_identifier_root])
    print("Nice tree size ", check_tree_size(nice_tree_root, verbose=False))
    leaves = find_leaves(nice_tree_root, verbose=False)
    nice_tree_root_subgraphs, max_edges_in_subgraph = append_subgraphs(
        nice_tree_root, G, verbose=False)
    nice_tree_root_subgraphs = clean_raw_nice_decomposition(
        nice_tree_root_subgraphs)

    ### REMOVE THISSS BELOW
    if savefigures:
        tree = get_tree(nice_tree_root_subgraphs)
        pos = nx.spring_layout(tree, scale=200)
        nx.draw(tree, pos, with_labels=True, font_size=2)
        plt.savefig("./raw_nice_tree_gas.svg", format="svg")
        plt.clf()

    ### Add all the extra edges
    nice_tree_root_plus_edge = nice_tree_root_subgraphs
    for edge in edges:
        nice_tree_root_plus_edge = add_edge_leaf(nice_tree_root_plus_edge,
                                                 edge)
    nice_tree_root_plus_edge = clean_raw_nice_decomposition(
        nice_tree_root_plus_edge)
    nice_tree_root_subgraphs_identifier, identifier_list = append_identifier(
        nice_tree_root_plus_edge, verbose=False)

    if savefigures:
        tree = get_tree(nice_tree_root_subgraphs_identifier)
        pos = nx.spring_layout(tree, scale=200)
        nx.draw(tree, pos, with_labels=True, font_size=2)
        plt.savefig("./nice_tree_gas.svg", format="svg")
        plt.clf()

    print("Find the edge")
    leaves = find_leaves(nice_tree_root_subgraphs_identifier, verbose=False)
    for edge in edges:
        found_edge = False
        for l in leaves:
            if edge in l.extra_info["subgraph edges"]:
                found_edge = True
        if not found_edge:
            raise ValueError("Edge not found among the leaves!!!!!!!!!!!!!!")

    print("Checing the stability of join nodes.")
    check_join_nodes(nice_tree_root_subgraphs_identifier)
    num_nice_nodes = check_tree_size(nice_tree_root_subgraphs_identifier)
    print("Nice tree has ", num_nice_nodes, " nodes.")

    return nice_tree_root_subgraphs_identifier