def build_match_graph(self, graph, tree): """ Get a minimal matching for the odd degree vertices """ odd_degree_nodes = self.get_odd_degree_nodes(tree) edges = [edge for edge in graph.edges if (edge[0] in odd_degree_nodes and edge[1] in odd_degree_nodes)] match_graph = type(graph)() return fix_utils.migrate_edges(graph, match_graph, edges)
def build_onetree(unspecial_graph, special_graph): """ Compute the assumes edges incident to special vertex have been removed """ model = mst_model.mstConstructor() tree = model.minimum_spanning_tree(unspecial_graph) tree = fix_utils.migrate_edges(special_graph, tree, special_graph.edges) return tree
def build_storage_graph(graph, vertex=None): """ Build a graph to store the unspecial edges incident with the given vertex """ storage_graph = nx.Graph() edges = np.array([edge for edge in graph.edges if vertex in edge]) storage_graph = fix_utils.migrate_edges(graph, storage_graph, edges) graph.remove_edges_from(edges) return storage_graph
def ensure_minimum_degree(original_graph, pruned_graph, threshold=None): """ Make sure that each vertex has minium degree of threshold in the pruned graph """ threshold = threshold or graph_utils.logceil(pruned_graph) if fix_utils.is_weakly_connected(pruned_graph): new_edges = fix_utils.get_all_k_min_strenghtheners( original_graph, pruned_graph, threshold) else: return pruned_graph return fix_utils.migrate_edges(original_graph, pruned_graph, new_edges)
def build_onetree(self, graph, vertex=None): if vertex is None: vertex = self.vertex print("No vertex, given, using {}!".format(self.vertex)) special_graph = self._identify_special_pair(graph, vertex) storage_graph = self._build_storage_graph(graph, vertex) tree = build_onetree(graph, special_graph) graph = fix_utils.migrate_edges(storage_graph, graph, storage_graph.edges) return tree
def ensure_doubletree(original_graph, pruned_graph, iterations=None): """ Make sure that the graph at least has two doubletree edge sets """ if iterations is None: # iterations = int(np.ceil(np.log2(len(original_graph.nodes)))) iterations = 1 model = mst_model.doubleTreeSparsifier() new_edge_sets = model.run_sparsify(original_graph, iterations) for new_edges in new_edge_sets: pruned_graph = fix_utils.migrate_edges(original_graph, pruned_graph, new_edges) model = mst_model.christofidesSparsifier() new_edge_sets = model.run_sparsify(original_graph, iterations) for new_edges in new_edge_sets: pruned_graph = fix_utils.migrate_edges(original_graph, pruned_graph, new_edges) return pruned_graph
def find_special_pair(graph, vertex=None): """ Find the two smallest edges (special edges) in the graph incident with the vertex """ if vertex is None: vertex = assume_vertex(graph) edges = np.array([edge for edge in graph.edges if vertex in edge]) incident_weights = graph_utils.get_edges_weights(graph, edges) indices = np.argpartition(incident_weights, 2)[:2] special_graph = nx.Graph() special_graph = fix_utils.migrate_edges(graph, special_graph, edges[indices]) return special_graph
def ensure_connected(original_graph, pruned_graph, threshold=None): """ Make sure that the graph is connected with threshold edges between components """ threshold = threshold or graph_utils.logceil(pruned_graph) if not fix_utils.is_connected(pruned_graph): new_edges = fix_utils.get_all_k_min_connectors( original_graph, pruned_graph, threshold, ) else: return pruned_graph return fix_utils.migrate_edges(original_graph, pruned_graph, new_edges)