generate_random_forest#
- graph_tool.generation.generate_random_forest(N, labeled=True, max_size=None)[git][source]#
Generate a uniformly random rooted forest.
- Parameters:
- N
int Number of vertices.
- labeled
bool(optional, default:True) If
True, the forest is chosen uniformly among the labeled rooted forests, and otherwise among the unlabeled ones.- max_size
int(optional, default:None) Maximum number of vertices of every tree (only for unlabeled forests).
- N
- Returns:
- g
Graph The generated undirected forest.
- root
VertexPropertyMap Boolean vertex property map marking the roots.
- g
See also
generate_random_treeUniformly random trees
Notes
The rooted labeled forests with \(N\) vertices are in bijection with the labeled trees with \(N + 1\) vertices, by joining all roots to an extra vertex, so that they are generated from a uniformly random labeled tree whose vertex \(N\) is removed, in time \(O(N)\).
Unlabeled rooted forests are generated with the algorithm of Nijenhuis and Wilf [nijenhuis-combinatorial-1978], which adds \(j\) copies of a random rooted tree with \(d\) vertices to a random rooted forest with \(N - jd\) vertices, choosing \((j, d)\) with the probability that makes every forest equally likely.
References
[nijenhuis-combinatorial-1978]Albert Nijenhuis and Herbert S. Wilf, “Combinatorial Algorithms for Computers and Calculators”, 2nd ed., Academic Press (1978), Chapter 29, DOI: 10.1016/C2013-0-11243-3 [sci-hub, @tor]
Examples
>>> g, root = gt.generate_random_forest(100) >>> print(g.num_edges(), root.a.sum()) 98 2
A smaller forest is drawn as follows, with the roots highlighted:
>>> g, root = gt.generate_random_forest(50) >>> cmap = matplotlib.colors.ListedColormap(["#cccccc", "#f28c8e"]) >>> gt.graph_draw(g, pos=gt.sfdp_layout(g), vertex_fill_color=root, ... vcmap=cmap, output="generate_random_forest-small.pdf") <...>
Uniformly random rooted labeled forest with \(N=50\) vertices, with the roots in red.#