minimum_spanning_tree#
- graph_tool.structure.minimum_spanning_tree(g, weights=None, root=None, tree_map=None, include=None, exclude=None)[git][source]#
Return the minimum spanning tree of a given graph.
- Parameters:
- g
Graph Graph to be used.
- weights
EdgePropertyMap(optional, default:None) The edge weights. If provided, the minimum spanning tree will minimize the edge weights.
- root
Vertex(optional, default:None) Root of the minimum spanning tree. If this is provided, Prim’s algorithm is used. Otherwise, Kruskal’s algorithm is used.
- tree_map
EdgePropertyMap(optional, default:None) If provided, the edge tree map will be written in this property map.
- include
EdgePropertyMap(optional, default:None) Boolean edge property map marking edges that must belong to the tree.
- exclude
EdgePropertyMap(optional, default:None) Boolean edge property map marking edges that must not belong to the tree.
- g
- Returns:
- tree_map
EdgePropertyMap Edge property map which marks the tree edges: 1 for tree edge, 0 otherwise.
- tree_map
See also
minimum_spanning_arborescenceMinimum spanning tree of a directed graph
random_spanning_treeUniformly sampled spanning tree
steiner_treeApproximate minimum tree connecting a subset of vertices
graph_tool.spectral.num_spanning_treesNumber of spanning trees
Notes
The algorithm runs with \(O(E\log E)\) complexity, or \(O(E\log V)\) if root is specified.
With
includeandexclude, the tree is the minimum one among those that contain the included edges and none of the excluded ones, as used to enumerate spanning trees in order of weight [sorensen-algorithm-2005]: the excluded edges are removed, and the included ones get a weight smaller than all others, so that they are taken first (if they form a cycle, not all of them are in the tree). If the remaining edges do not connect the graph, a spanning forest is returned.References
[kruskal-shortest-1956]J. B. Kruskal. “On the shortest spanning subtree of a graph and the traveling salesman problem”, In Proceedings of the American Mathematical Society, volume 7, pages 48-50, 1956. DOI: 10.1090/S0002-9939-1956-0078686-7 [sci-hub, @tor]
[prim-shortest-1957]R. Prim. “Shortest connection networks and some generalizations”, Bell System Technical Journal, 36:1389-1401, 1957.
[sorensen-algorithm-2005]Kenneth Sörensen and Gerrit K. Janssens, “An algorithm to generate all spanning trees of a graph in order of increasing cost”, Pesquisa Operacional 25, 219-229 (2005), DOI: 10.1590/S0101-74382005000200004 [sci-hub, @tor]
Examples
>>> from numpy.random import random >>> g, pos = gt.triangulation(random((400, 2)) * 10, type="delaunay") >>> weight = g.new_edge_property("double") >>> for e in g.edges(): ... weight[e] = linalg.norm(pos[e.target()].a - pos[e.source()].a) >>> tree = gt.minimum_spanning_tree(g, weights=weight) >>> gt.graph_draw(g, pos=pos, output="triang_orig.pdf") <...> >>> u = gt.GraphView(g, efilt=tree) >>> gt.graph_draw(u, pos=pos, output="triang_min_span_tree.pdf") <...>
Left: Original graph, Right: The minimum spanning tree.