예제 #1
0
def test_maximum_cardinality_search():
    """Test maximum cardinality search algorithm"""

    # Read graph
    import qtree.operators as ops
    import os
    this_dir = os.path.dirname((os.path.abspath(__file__)))
    nq, c = ops.read_circuit_file(this_dir + '/../../inst_2x2_7_0.txt')
    old_g, *_ = circ2graph(nq, c)

    # Make random clique
    vertices = list(np.random.choice(old_g.nodes, 4, replace=False))
    while is_clique(old_g, vertices):
        vertices = list(np.random.choice(old_g.nodes, 4, replace=False))

    g = make_clique_on(old_g, vertices)

    # Make graph completion
    peo, tw = get_peo(g)
    g_chordal = get_fillin_graph2(g, peo)

    # MCS will produce alternative PEO with this clique at the end
    new_peo = maximum_cardinality_search(g_chordal, list(vertices))

    # Test if new peo is correct
    assert is_peo_zero_fillin(g_chordal, peo)
    assert is_peo_zero_fillin(g_chordal, new_peo)
    new_tw = get_treewidth_from_peo(g, new_peo)
    assert tw == new_tw

    print('peo:', peo)
    print('new_peo:', new_peo)
예제 #2
0
def test_tree_from_peo(filename='inst_2x2_7_0.txt'):
    import qtree.operators as ops
    nq, c = ops.read_circuit_file(filename)
    graph, *_ = circ2graph(nq, c, omit_terminals=False)

    peo, _ = get_peo(graph)
    tree = get_tree_from_peo(get_simple_graph(graph), peo)
    elements = list(range(1, graph.number_of_nodes() + 1))

    for element in elements:
        nodes_containing_element = []
        for node in tree.nodes():
            if element in node:
                nodes_containing_element.append(node)

        subtree = nx.subgraph(tree, nodes_containing_element)
        if subtree.number_of_nodes() > 0:
            connected = nx.connected.is_connected(subtree)
        else:
            connected = True  # Take empty graph as connected
        if not connected:
            # draw_graph(subtree, f"st_{element}")
            pass

    draw_graph(tree, f"tree")
예제 #3
0
파일: splitters.py 프로젝트: qbit-/qtree
def split_graph_random(old_graph, n_var_parallel=0):
    """
    Splits a graphical model with randomly chosen nodes
    to parallelize over.

    Parameters
    ----------
    old_graph : networkx.Graph
                graph to contract (after eliminating variables which
                are parallelized over)
    n_var_parallel : int
                number of variables to eliminate by parallelization

    Returns
    -------
    idx_parallel : list of Idx
          variables removed by parallelization
    graph : networkx.Graph
          new graph without parallelized variables
    """
    graph = copy.deepcopy(old_graph)

    indices = [var for var in graph.nodes(data=False)]
    idx_parallel = np.random.choice(
        indices, size=n_var_parallel, replace=False)

    idx_parallel_var = [Var(var, size=graph.nodes[var])
                        for var in idx_parallel]

    for idx in idx_parallel:
        remove_node(graph, idx)

    log.info("Removed indices by parallelization:\n{}".format(idx_parallel))
    log.info("Removed {} variables".format(len(idx_parallel)))
    peo, treewidth = get_peo(graph)

    return sorted(idx_parallel_var, key=int), graph