def test_heuristic_first_steps(self): """Test first steps of min_fill_in heuristic""" graph = { n: set(self.deterministic_graph[n]) - set([n]) for n in self.deterministic_graph } print("Graph {}:".format(graph)) elim_node = min_fill_in_heuristic(graph) steps = [] while elim_node is not None: print("Removing {}:".format(elim_node)) steps.append(elim_node) nbrs = graph[elim_node] for u, v in itertools.permutations(nbrs, 2): if v not in graph[u]: graph[u].add(v) for u in graph: if elim_node in graph[u]: graph[u].remove(elim_node) del graph[elim_node] print("Graph {}:".format(graph)) elim_node = min_fill_in_heuristic(graph) # check only the first 2 elements for equality assert_equals(steps[:2], [6, 5])
def test_heuristic_first_steps(self): """Test first steps of min_fill_in heuristic""" graph = {n: set(self.deterministic_graph[n]) - set([n]) for n in self.deterministic_graph} print("Graph {}:".format(graph)) elim_node = min_fill_in_heuristic(graph) steps = [] while elim_node is not None: print("Removing {}:".format(elim_node)) steps.append(elim_node) nbrs = graph[elim_node] for u, v in itertools.permutations(nbrs, 2): if v not in graph[u]: graph[u].add(v) for u in graph: if elim_node in graph[u]: graph[u].remove(elim_node) del graph[elim_node] print("Graph {}:".format(graph)) elim_node = min_fill_in_heuristic(graph) # check only the first 2 elements for equality assert_equals(steps[:2], [6, 5])
def treeDecomposition(G): tree = [] graph = {n: set(G[n]) - set([n]) for n in G} node_stack = [] elim_node = treewidth.min_fill_in_heuristic(graph) while elim_node is not None: neigh = graph[elim_node] for u, v in itertools.permutations(neigh, 2): if v not in graph[u]: graph[u].add(v) node_stack.append((elim_node, neigh)) for u in graph[elim_node]: graph[u].remove(elim_node) del graph[elim_node] elim_node = treewidth.min_fill_in_heuristic(graph) decomp = nx.Graph() first_bag = frozenset(graph.keys()) tree.append(set(graph.keys())) decomp.add_node(first_bag) while node_stack: # get node and its neighbors from the stack (curr_node, nbrs) = node_stack.pop() # find a bag all neighbors are in old_bag = None for bag in decomp.nodes: if nbrs <= bag: old_bag = bag break if old_bag is None: # no old_bag was found: just connect to the first_bag old_bag = first_bag # create new node for decomposition nbrs.add(curr_node) new_bag = frozenset(nbrs) # add edge to decomposition (implicitly also adds the new node) decomp.add_edge(old_bag, new_bag) tree.append((nbrs)) return tree, decomp
def test_heuristic_abort(self): """Test if min_fill_in returns None for fully connected graph""" graph = {} for u in self.complete: graph[u] = set() for v in self.complete[u]: if u != v: # ignore self-loop graph[u].add(v) next_node = min_fill_in_heuristic(graph) if next_node is None: pass else: assert False
def test_heuristic_abort(self): """Test if min_fill_in returns None for fully connected graph""" graph = {} for u in self.complete: graph[u] = set() for v in self.complete[u]: if u != v: # ignore self-loop graph[u].add(v) next_node = min_fill_in_heuristic(graph) if next_node is None: pass else: assert False
def treeDecomposition(G): # T = nx.Graph() # nodeList = copy.deepcopy(G.nodes()) # tree = list() # # for i in nodeList: # vertex = i # bag = list() # bag.append(vertex) # # for j in T.neighbors(i): # bag.append(j) # tree.append((bag)) # T.add_node(frozenset(bag)) # G.remove_node(i) # # for n1 in range(len(nodeList)): # checker = False # for n2 in range(n1+1, len(nodeList)): # if checker == False and len(nodeList[n1].intersection(nodeList[n2])) > 0: # T.add_edge(nodeList[n1], nodeList[n2]) # checker = True # # nx.draw(T, with_labels=True, font_weight='bold') tree = [] graph = {n: set(G[n]) - set([n]) for n in G} node_stack = [] elim_node = treewidth.min_fill_in_heuristic(graph) while elim_node is not None: neigh = graph[elim_node] for u, v in itertools.permutations(neigh, 2): if v not in graph[u]: graph[u].add(v) node_stack.append((elim_node, neigh)) for u in graph[elim_node]: graph[u].remove(elim_node) del graph[elim_node] elim_node = treewidth.min_fill_in_heuristic(graph) decomp = nx.Graph() first_bag = frozenset(graph.keys()) tree.append(set(graph.keys())) decomp.add_node(first_bag) while node_stack: # get node and its neighbors from the stack (curr_node, nbrs) = node_stack.pop() # find a bag all neighbors are in old_bag = None for bag in decomp.nodes: if nbrs <= bag: old_bag = bag break if old_bag is None: # no old_bag was found: just connect to the first_bag old_bag = first_bag # create new node for decomposition nbrs.add(curr_node) new_bag = frozenset(nbrs) # add edge to decomposition (implicitly also adds the new node) decomp.add_edge(old_bag, new_bag) tree.append((nbrs)) return tree, decomp