Skip to content

Commit fffe5f0

Browse files
Merge pull request #127 from weilycoder:master
Allow users to customize the generation method of parent nodes
2 parents 806d44f + 2d96d57 commit fffe5f0

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cyaron/graph.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def tree(point_count, chain=0, flower=0, **kwargs):
146146
int/float weight_gen()
147147
= lambda: random.randint(weight_limit[0], weight_limit[1])
148148
-> the generator of the weights. It should return the weight. The default way is to use the random.randint()
149+
int father_gen(cur)
150+
= lambda cur: random.randrange(1, cur)
151+
-> the generator of the fathers of current point.
149152
"""
150153
directed = kwargs.get("directed", False)
151154
weight_limit = kwargs.get("weight_limit", (1, 1))
@@ -154,6 +157,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
154157
weight_gen = kwargs.get(
155158
"weight_gen", lambda: random.randint(
156159
weight_limit[0], weight_limit[1]))
160+
father_gen = kwargs.get("father_gen", lambda cur: random.randrange(1, cur))
157161

158162
if not 0 <= chain <= 1 or not 0 <= flower <= 1:
159163
raise Exception("chain and flower must be between 0 and 1")
@@ -174,7 +178,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
174178
for i in range(chain_count + 2, chain_count + flower_count + 2):
175179
graph.add_edge(1, i, weight=weight_gen())
176180
for i in range(point_count - random_count + 1, point_count + 1):
177-
u = random.randrange(1, i)
181+
u = father_gen(i)
178182
graph.add_edge(u, i, weight=weight_gen())
179183

180184
return graph

0 commit comments

Comments
 (0)