Skip to content

Commit

Permalink
Use a better way to random a forest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Python-in-China committed Nov 14, 2024
1 parent 669b09f commit 23f6432
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,19 @@ def _calc_max_edge(point_count, directed, self_loop):

@staticmethod
def forest(point_count, tree_count, **kwargs):
"""
Return a forest with point_count vertexes and tree_count trees.
Args:
point_count: the count of vertexes
tree_count: the count of trees
"""
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())
need_delete = set(
i[0] for i in (Vector.random_unique_vector(tree_count - 1, [(
0, point_count - 2)]) if tree_count > 1 else []))
result = Graph(point_count, 0)

This comment has been minimized.

Copy link
@weilycoder

weilycoder Nov 20, 2024

Contributor

@Mr-Python-in-China 你的 Graph(point_count, 0)0 的语义是 $0$ 条边还是指定参数 directedFalse

这里运行上是后者,而且我觉得应该允许用户指定。

for i in range(point_count - 1):
if i not in need_delete:
result.add_edge(tree[i].start,
tree[i].end,
weight=tree[i].weight)
need_add = random.sample(tree, len(tree) - 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 23f6432

Please sign in to comment.