def bfs(graph, root): """Return vertices of a component of graph in some bfs order, starting with root.""" done = 0 front = root while front: v = first(front) yield v done |= v front |= graph.neighborhoods[v] front = subtract(front, done)
def incremental_un_heuristic(G): lboolw_components = [] decomposition_components = [] for component in components(G): best_lboolw = Infinity best_decomposition = None for i, start in enumerate([first(component)]): #for i, start in enumerate(iterate(component)): print('{}th starting vertex'.format(i)) right = subtract(component, start) left = start un_left = increment_un(G, 0, {0}, start) booldim_left = 1 decomposition = [start] lboolw = len(un_left) for _ in range(size(component) - 1): best_vertex, best_un, _ = greedy_step(G, left, right, un_left, booldim_left, {}, Infinity) booldim_left = len(best_un) lboolw = max(lboolw, booldim_left) un_left = best_un decomposition.append(best_vertex) right = subtract(right, best_vertex) left = left | best_vertex if lboolw < best_lboolw: best_lboolw = lboolw best_decomposition = decomposition lboolw_components.append(best_lboolw) decomposition_components.append(best_decomposition) total_lboolw = max(lboolw_components) total_decomposition = [v for part in decomposition_components for v in part] return total_lboolw, total_decomposition