Skip to content

Commit

Permalink
Fix forest generation
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder authored and Mr-Python-in-China committed Nov 28, 2024
1 parent 23f6432 commit 1d7dc6b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,10 @@ def forest(point_count, tree_count, **kwargs):
"""
if tree_count <= 0 or tree_count > point_count:
raise ValueError("tree_count must be between 1 and point_count")
tree = list(Graph.tree(point_count, **kwargs).iterate_edges())
result = Graph(point_count, 0)
need_add = random.sample(tree, len(tree) - tree_count + 1)
tree = Graph.tree(point_count, **kwargs)
tree_edges = list(tree.iterate_edges())
result = Graph(point_count, tree.directed)
need_add = random.sample(tree_edges, len(tree_edges) - tree_count + 1)
for edge in need_add:
result.add_edge(edge.start, edge.end, weight=edge.weight)
return result
Expand Down

0 comments on commit 1d7dc6b

Please sign in to comment.