slim.py
def find_subtree(td: TreeDecomposition, budget: int, history: Counter = None,
                 debug=False):
    """
    finds a subtree that fits within the budget

    :param td: tree decomposition in which to find the subtree
    :param budget: max number of vertices allowed in the union of selected bags
    :param history: tally of bags picked in previous iterations
    :param debug: debug mode
    :return: (selected_bag_ids, seen_vertices)
    """
    start_bag_id = find_start_bag(td, history, debug)
    selected = {start_bag_id}
    seen = set(td.bags[start_bag_id])
    if debug: print(f"starting bag {start_bag_id}: {td.bags[start_bag_id]}")
    queue = [(0, start_bag_id)]  # (sorting metric, bag_id)
    visited = set()
    while queue:
        _, bag_id = heappop(queue)
        visited.add(bag_id)
        bag = td.bags[bag_id]
        if len(seen.union(bag)) > budget:
            continue
        else:  # can include bag in local instance
            selected.add(bag_id)
            seen.update(bag)
            # add neighboring bags to queue
            for nbr_id in td.decomp.neighbors(bag_id):
                if nbr_id not in visited:
                    nbr_bag = td.bags[nbr_id]
                    if not USING_COMPLEXITY_WIDTH:
                        expansion_metric = 0  # no sorting, so set same value for all bags
                        # todo[feature]: set to random value to randomize expansion
                    else:
                        if CW_EXP_STRAT == "max":
                            expansion_metric = -compute_complexity(nbr_bag, DOMAIN_SIZES)
                        elif CW_EXP_STRAT == "min":
                            expansion_metric = compute_complexity(nbr_bag, DOMAIN_SIZES)
                        else:
                            expansion_metric = bag.intersection(nbr_bag)
                    heappush(queue, (expansion_metric, nbr_id))
            if debug: print(f"added bag {bag_id}: {td.bags[bag_id]}")
    if debug: print(f"final seen: {seen}")
    return selected, seen

    finds a subtree that fits within the budget

    :param td: tree decomposition in which to find the subtree
    :param budget: max number of vertices allowed in the union of selected bags
    :param history: tally of bags picked in previous iterations
    :param debug: debug mode
    :return: (selected_bag_ids, seen_vertices)
    
finds a subtree that fits within the budget
finds a subtree that fits within the budget
def find_subtree(td: TreeDecomposition, budget: int, history: Counter = None,
                 debug=False):
    start_bag_id = find_start_bag(td, history, debug)
    selected = {start_bag_id}
    seen = set(td.bags[start_bag_id])
    if debug: print(f"starting bag {start_bag_id}: {td.bags[start_bag_id]}")
    queue = [(0, start_bag_id)]  
    visited = set()
    while queue:
        _, bag_id = heappop(queue)
        visited.add(bag_id)
        bag = td.bags[bag_id]
        if len(seen.union(bag)) > budget:
            continue
        else:  
            selected.add(bag_id)
            seen.update(bag)
            for nbr_id in td.decomp.neighbors(bag_id):
                if nbr_id not in visited:
                    nbr_bag = td.bags[nbr_id]
                    if not USING_COMPLEXITY_WIDTH:
                        expansion_metric = 0  
                    else:
                        if CW_EXP_STRAT == "max":
                            expansion_metric = -compute_complexity(nbr_bag, DOMAIN_SIZES)
                        elif CW_EXP_STRAT == "min":
                            expansion_metric = compute_complexity(nbr_bag, DOMAIN_SIZES)
                        else:
                            expansion_metric = bag.intersection(nbr_bag)
                    heappush(queue, (expansion_metric, nbr_id))
            if debug: print(f"added bag {bag_id}: {td.bags[bag_id]}")
    if debug: print(f"final seen: {seen}")
    return selected, seen
def find_subtree ( td : TreeDecomposition , budget : int , history : Counter = None , debug = False ) : start_bag_id = find_start_bag ( td , history , debug ) selected = { start_bag_id } seen = set ( td . bags [ start_bag_id ] ) if debug : print ( f"starting bag {start_bag_id}: {td.bags[start_bag_id]}" ) queue = [ ( 0 , start_bag_id ) ] visited = set ( ) while queue : _ , bag_id = heappop ( queue ) visited . add ( bag_id ) bag = td . bags [ bag_id ] if len ( seen . union ( bag ) ) > budget : continue else : selected . add ( bag_id ) seen . update ( bag ) for nbr_id in td . decomp . neighbors ( bag_id ) : if nbr_id not in visited : nbr_bag = td . bags [ nbr_id ] if not USING_COMPLEXITY_WIDTH : expansion_metric = 0 else : if CW_EXP_STRAT == "max" : expansion_metric = - compute_complexity ( nbr_bag , DOMAIN_SIZES ) elif CW_EXP_STRAT == "min" : expansion_metric = compute_complexity ( nbr_bag , DOMAIN_SIZES ) else : expansion_metric = bag . intersection ( nbr_bag ) heappush ( queue , ( expansion_metric , nbr_id ) ) if debug : print ( f"added bag {bag_id}: {td.bags[bag_id]}" ) if debug : print ( f"final seen: {seen}" ) return selected , seen
finds a subtree that fits within the budget
finds a subtree that fits within the budget
"""
    finds a subtree that fits within the budget

    :param td: tree decomposition in which to find the subtree
    :param budget: max number of vertices allowed in the union of selected bags
    :param history: tally of bags picked in previous iterations
    :param debug: debug mode
    :return: (selected_bag_ids, seen_vertices)
    """ # (sorting metric, bag_id) # can include bag in local instance # add neighboring bags to queue # no sorting, so set same value for all bags # todo[feature]: set to random value to randomize expansion
None
td
TreeDecomposition
tree decomposition in which to find the subtree
tree decomposition in which to find the subtree
budget
int
max number of vertices allowed in the union of selected bags
max number of vertices allowed in the union of selected bags
history
Counter
tally of bags picked in previous iterations
tally of bags picked in previous iterations
debug
None