1 # Generate all possible single-root message thread structures of size
2 # argv[1]. Each output line is a thread structure, where the n'th
3 # field is either a number giving the parent of message n or "None"
7 from itertools import chain, combinations
10 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
12 nodes = set(range(int(sys.argv[1])))
14 # Queue of (tree, free, to_expand) where tree is a {node: parent}
15 # dictionary, free is a set of unattached nodes, and to_expand is
16 # itself a queue of nodes in the tree that need to be expanded.
17 # The queue starts with all single-node trees.
18 queue = [({root: None}, nodes - {root}, (root,)) for root in nodes]
22 tree, free, to_expand = queue.pop()
24 if len(to_expand) == 0:
25 # Only print full-sized trees
27 print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))
29 # Expand node to_expand[0] with each possible set of children
30 for children in subsets(free):
31 ntree = dict(tree, **{child: to_expand[0] for child in children})
32 nfree = free.difference(children)
33 queue.append((ntree, nfree, to_expand[1:] + tuple(children)))