def get_node_by_treewidth_reduction(graph): """ Returns a list of pairs (node : reduction_in_treewidth) for the provided graph. The graph is **ASSUMED** to be in the optimal elimination order, e.g. the nodes have to be relabelled by peo Parameters ---------- graph : networkx.Graph without self-loops and parallel edges Returns ------- nodes_by_treewidth_reduction : dict """ # Get flop cost of the bucket elimination initial_treewidth = get_treewidth_from_peo( graph, sorted(graph.nodes)) nodes_by_treewidth_reduction = [] for node in graph.nodes(data=False): reduced_graph = copy.deepcopy(graph) # Take out one node remove_node(reduced_graph, node) treewidth = get_treewidth_from_peo( reduced_graph, sorted(reduced_graph.nodes)) delta = initial_treewidth - treewidth nodes_by_treewidth_reduction.append((node, delta)) return nodes_by_treewidth_reduction
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)
def test_tree_to_peo(): """ Test tree reduction algorithm and also the conversion of tree to peo """ g, peo = make_test_graph() f = get_tree_from_peo(g, peo) fd, el = get_reduced_tree(f, 1) peo = get_peo_from_tree(fd, [5, 6]) g.remove_nodes_from(el) tw1 = get_treewidth_from_peo(g, peo) tw2 = len(find_max_cliques(fd)[0]) - 1 assert tw1 == tw2
def test_tree_reduction(): """ Tests the deletion of variables from tree """ g, peo = make_test_graph() f = get_tree_from_peo(g, peo) ff = rm_element_in_tree(f, 2) peo.remove(2) g.remove_node(2) tw1 = get_treewidth_from_peo(g, peo) tw2 = len(find_max_cliques(ff)[0]) - 1 assert tw1 == tw2