I guess it's just me, but last time I needed to do something like this (verify that there were no cycles in macro definitions), Graph let concentrate on what I wanted to do and let me forget about the nit-picky graph management.
Then again, I have written an AVL tree implementation in FORTRAN, so I shouldn't talk too much about doing things the hard way.
I'd implement something like this:
Create root-node hash
Current level is zero
Establish limits: max & min nodes per level, number of levels
root{A0} = add_levels($current_level, $level_limit, $min_nodes, $max_n
+odes);
sub add_levels {
return if current level is > the limit, bump it if not
calculate a random number between high and low limit
new_level = {};
for $n (0..$local_limit) {
generate $name from level and $n
new_level{$name} = add_levels($current_level, $limit, $min, $max
+)
}
return $new_level;
}
The printing code will be similar, but you'll instead pass a string down the recursive call tree, adding level names until you run out of levels, then printing it. The recursion plus the loop will automatically follow all paths for you.
I have written a program to do this but I'm not posting it; you'll learn a lot more if you follow this outline and write it yourself; as you don't want to handwave the details via Graph, I assume there's a reason for writing it yourself -- and me writing it for you won't teach you anywhere near as much.
I recommend running under the debugger, as you'll be able to x the growing data structure as you go along and see if it's working. |