Skip to content

Commit 23f6432

Browse files
Use a better way to random a forest
1 parent 669b09f commit 23f6432

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

cyaron/graph.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -537,18 +537,19 @@ def _calc_max_edge(point_count, directed, self_loop):
537537

538538
@staticmethod
539539
def forest(point_count, tree_count, **kwargs):
540+
"""
541+
Return a forest with point_count vertexes and tree_count trees.
542+
Args:
543+
point_count: the count of vertexes
544+
tree_count: the count of trees
545+
"""
540546
if tree_count <= 0 or tree_count > point_count:
541547
raise ValueError("tree_count must be between 1 and point_count")
542548
tree = list(Graph.tree(point_count, **kwargs).iterate_edges())
543-
need_delete = set(
544-
i[0] for i in (Vector.random_unique_vector(tree_count - 1, [(
545-
0, point_count - 2)]) if tree_count > 1 else []))
546549
result = Graph(point_count, 0)
547-
for i in range(point_count - 1):
548-
if i not in need_delete:
549-
result.add_edge(tree[i].start,
550-
tree[i].end,
551-
weight=tree[i].weight)
550+
need_add = random.sample(tree, len(tree) - tree_count + 1)
551+
for edge in need_add:
552+
result.add_edge(edge.start, edge.end, weight=edge.weight)
552553
return result
553554

554555

0 commit comments

Comments
 (0)