def test_minimal_spanning_tree_with_negative_weigths(self): gr = graph() gr.add_nodes((0,1,2,3)) gr.add_edge((0,1), wt=-0.4) gr.add_edge((1,2), wt=-0.6) gr.add_edge((2,0), wt=-0.5) gr.add_edge((3,2), wt=-0.1) gr.add_edge((3,1), wt=-0.2) print minimal_spanning_tree(gr, 0)
def duplicate_component(graph, node): """ Duplicates component in a graph @type graph: GraphWrapper @param graph: the graph in which to duplicate the component @type node: graph_representation.node.Node @param node: node which roots the component to be duplicated @rtype Node @return the duplicated node of the input node, which roots the duplicated component """ nodesMap = {} nodes = minimal_spanning_tree(graph=graph, root=node) for curNode in nodes: dupNode = duplicate_node(graph=graph, node=curNode, connectToNeighbours=False) nodesMap[curNode.uid] = dupNode for curNode in nodes: curDupNode = nodesMap[curNode.uid] for curNeighbour in graph.neighbors(curNode): curDupNeighbour = nodesMap[curNeighbour.uid] graph.add_edge(edge=(curDupNode, curDupNeighbour), label=graph.edge_label((curNode, curNeighbour))) return nodesMap[node.uid]
def duplicate_component(graph, node): """ Duplicates component in a graph @type graph: GraphWrapper @param graph: the graph in which to duplicate the component @type node: graph_representation.node.Node @param node: node which roots the component to be duplicated @rtype Node @return the duplicated node of the input node, which roots the duplicated component """ nodesMap = {} nodes = minimal_spanning_tree(graph=graph, root=node) for curNode in nodes: dupNode = duplicate_node(graph=graph, node=curNode, connectToNeighbours=False) nodesMap[curNode.uid] = dupNode for curNode in nodes: curDupNode = nodesMap[curNode.uid] for curNeighbour in graph.neighbors(curNode): curDupNeighbour = nodesMap[curNeighbour.uid] graph.add_edge(edge=(curDupNode, curDupNeighbour), label=graph.edge_label((curNode, curNeighbour))) return nodesMap[node.uid]
def test_minimal_spanning_tree_on_graph(self): gr = testlib.new_graph(wt_range=(1,10)) mst = minimal_spanning_tree(gr, root=0) wt = tree_weight(gr, mst) len_dfs = len(depth_first_search(gr, root=0)[0]) for each in mst: if (mst[each] != None): mst_copy = deepcopy(mst) del(mst_copy[each]) for other in gr[each]: mst_copy[each] = other if (tree_weight(gr, mst_copy) < wt): gr2 = graph() add_spanning_tree(gr2, mst_copy) assert len(depth_first_search(gr2, root=0)[0]) < len_dfs
def delete_component(graph, node): """ deletes component in a graph @type graph: GraphWrapper @param graph: the graph in which to delete the component @type node: graph_representation.node.Node @param node: node which roots the component to be deleted """ nodes = minimal_spanning_tree(graph=graph, root=node) for node in nodes: graph.del_node(node)
def test_minimal_spanning_tree_on_graph(self): gr = new_graph(wt_range=(1,10)) mst = minimal_spanning_tree(gr, root=0) wt = tree_weight(gr, mst) len_dfs = len(depth_first_search(gr, root=0)[0]) for each in mst: if (mst[each] != None): mst_copy = deepcopy(mst) del(mst_copy[each]) for other in gr[each]: mst_copy[each] = other if (tree_weight(gr, mst_copy) < wt): gr2 = graph() add_spanning_tree(gr2, mst_copy) assert len(depth_first_search(gr2, root=0)[0]) < len_dfs
def delete_component(graph, node): """ deletes component in a graph @type graph: GraphWrapper @param graph: the graph in which to delete the component @type node: graph_representation.node.Node @param node: node which roots the component to be deleted """ nodes = minimal_spanning_tree(graph=graph, root=node) for node in nodes: graph.del_node(node)
def component_to_string(graph, node): """ get a textual value of a component in a graph @type graph: GraphWrapper @param graph: the graph in which to delete the component @type node: graph_representation.node.Node @param node: node which roots the component """ nodes = minimal_spanning_tree(graph=graph, root=node) texts = [] for node in nodes: texts.extend([w for w in node.get_text(graph) if w.index != NO_INDEX]) chars = '\'\"-,.:;!? ' return " ".join([w.word for w in sorted(texts, key=lambda w: w.index) ]).rstrip(chars).lstrip(chars)
def minimal_spanning_tree(weighted_graph): """ Return the minimal spanning tree of a weighted graph. Wraps pygraph's minimal spanning tree. """ min_weight = min(weighted_graph.edge_weight(edge) for edge in weighted_graph.edges()) if min_weight < 0: reweighted_graph = Graph.undirected() reweighted_graph.add_graph(weighted_graph) for edge in weighted_graph.edges(): weight = weighted_graph.edge_weight(edge) reweighted_graph.set_edge_weight(edge, weight - min_weight) return Graph.minimal_spanning_tree(reweighted_graph) min_tree = Graph.directed() min_tree.add_nodes(weighted_graph.nodes()) for edge in minimal_spanning_tree(weighted_graph).items(): if not None in edge: min_tree.add_edge(edge) return min_tree
def component_to_string(graph, node): """ get a textual value of a component in a graph @type graph: GraphWrapper @param graph: the graph in which to delete the component @type node: graph_representation.node.Node @param node: node which roots the component """ nodes = minimal_spanning_tree(graph=graph, root=node) texts = [] for node in nodes: texts.extend([w for w in node.get_text(graph) if w.index != NO_INDEX]) chars = '\'\"-,.:;!? ' return " ".join([w.word for w in sorted(texts, key=lambda w:w.index)]).rstrip(chars).lstrip(chars)
if DEBUG: print(row, col, cost) g = graph() g.add_nodes(range(N)) for r in xrange(N): for c in xrange(r,N): if matrix[r][c] != 0: g.add_edge((r,c), matrix[r][c]) if DEBUG: print(r, c, matrix[r][c]) st = minimal_spanning_tree(g) ming = graph() ming.add_spanning_tree(st) if DEBUG: print(st) print() print(ming) print() print(total_weight(g,g)) print(total_weight(ming,g)) print(total_weight(g,g) - total_weight(ming,g))