minimum_spanning_arborescence#
- graph_tool.structure.minimum_spanning_arborescence(g, weights=None, root=None, tree_map=None, maximum=False, spanning=True)[git][source]#
Return the minimum spanning arborescence of a directed graph.
- Parameters:
- g
Graph Directed graph to be used.
- weights
EdgePropertyMap(optional, default:None) The edge weights. If not supplied, every edge has unit weight. They may be negative.
- root
Vertex(optional, default:None) Root of the arborescence. If not supplied, the root is chosen so that the total weight is minimal.
- tree_map
EdgePropertyMap(optional, default:None) If provided, the edge tree map will be written in this property map.
- maximum
bool(optional, default:False) If
True, the arborescence (or branching) of maximum weight is returned instead.- spanning
bool(optional, default:True) If
False, a minimum (or maximum) branching is returned instead of a spanning arborescence, androotis ignored.
- g
- Returns:
- tree_map
EdgePropertyMap Edge property map that marks the tree edges: 1 for tree edge, 0 otherwise.
- tree_map
See also
all_spanning_arborescencesSpanning arborescences in order of weight
minimum_spanning_treeMinimum spanning tree of an undirected graph
random_spanning_treeUniformly sampled spanning tree
Notes
A spanning arborescence is a directed spanning tree in which every vertex except the root has exactly one incoming tree edge, so that there is a directed path from the root to every vertex. The one with the smallest sum of edge weights is the directed analogue of the minimum spanning tree (see
minimum_spanning_tree(), which treats directed graphs as undirected), and is also known as the optimum branching [edmonds-optimum-1967] [chu-shortest-1965]. A maximum spanning arborescence (maximum == True) is obtained with negated weights. If no spanning arborescence exists (because some vertex cannot be reached from the root, or from any single vertex ifroot is None), aValueErroris raised. Self-loops are ignored.The implementation follows the \(O(E\log V)\) algorithm of [tarjan-finding-1977] (with the correction of [camerini-note-1979]), where every vertex selects its cheapest incoming edge, and the cycles that form are contracted, using mergeable heaps of the incoming edges. If
rootisNone, a virtual root is connected to every vertex with an edge heavier than any arborescence.A branching is a forest of arborescences, i.e. a set of edges in which every vertex has at most one incoming edge and that has no cycles. With
spanning == False, the branching of minimum (or maximum) weight is obtained as the minimum spanning arborescence of the graph extended with a virtual root connected to every vertex by an edge of weight zero, without these edges. Hence, a minimum branching contains only edges of negative weight, and a maximum branching only edges of positive weight.The spanning arborescences in order of their weight are generated by
all_spanning_arborescences().References
[edmonds-optimum-1967]Jack Edmonds, “Optimum branchings”, Journal of Research of the National Bureau of Standards B 71, 233-240 (1967), DOI: 10.6028/jres.071B.032 [sci-hub, @tor]
[chu-shortest-1965]Yoeng-Jin Chu and Tseng-Hong Liu, “On the shortest arborescence of a directed graph”, Scientia Sinica 14, 1396-1400 (1965).
[tarjan-finding-1977]Robert E. Tarjan, “Finding optimum branchings”, Networks 7, 25-35 (1977), DOI: 10.1002/net.3230070103 [sci-hub, @tor]
[camerini-note-1979]Paolo M. Camerini, Luigi Fratta and Francesco Maffioli, “A note on finding optimum branchings”, Networks 9, 309-312 (1979), DOI: 10.1002/net.3230090403 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["polblogs"] >>> g = gt.extract_largest_component(g, directed=True, prune=True) >>> w = g.new_ep("double", vals=np.random.random(g.num_edges())) >>> tree = gt.minimum_spanning_arborescence(g, weights=w) >>> print(tree.a.sum(), (tree.a * w.a).sum()) 792 154.328281612... >>> b = gt.minimum_spanning_arborescence(g, weights=w, maximum=True, ... spanning=False) >>> print(b.a.sum(), (b.a * w.a).sum()) 792 644.162253899...
The arborescence can be visualized for a smaller graph, obtained from the Delaunay triangulation of random points in the plane, with the edges directed away from vertex 0 (or arbitrarily, between vertices at the same distance from it), and the lengths of the edges as weights:
>>> points = np.random.random((60, 2)) * 4 >>> u, pos = gt.triangulation(points, type="delaunay") >>> d = gt.shortest_distance(u, 0) >>> es = u.get_edges() >>> back = d.a[es[:, 0]] > d.a[es[:, 1]] >>> es[back] = es[back][:, ::-1] >>> g = gt.Graph(es) >>> w = g.new_ep("double", vals=np.linalg.norm(points[es[:, 0]] - ... points[es[:, 1]], axis=1)) >>> tree = gt.minimum_spanning_arborescence(g, weights=w, root=0) >>> vcolor = g.new_vp("string", vals=["#f28c8e"] + ... ["#cccccc"] * (g.num_vertices() - 1)) >>> ecolor = g.new_ep("string", vals=np.where(tree.a, "#e41a1c", ... "#999999")) >>> ewidth = g.new_ep("double", vals=np.where(tree.a, 2, 1)) >>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, edge_color=ecolor, ... edge_pen_width=ewidth, eorder=tree, ... output="minimum_spanning_arborescence-triang.pdf") <...>
Minimum spanning arborescence rooted at vertex 0 (in red) of the Delaunay triangulation of 60 random points, with the edges directed away from the root and weighted by their Euclidean lengths. The edges of the arborescence are shown in red, and the other edges in gray.#