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"
6 from itertools import chain, combinations
9 return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
11 nodes = set(range(int(sys.argv[1])))
13 # Queue of (tree, free, to_expand) where tree is a {node: parent}
14 # dictionary, free is a set of unattached nodes, and to_expand is
15 # itself a queue of nodes in the tree that need to be expanded.
16 # The queue starts with all single-node trees.
17 queue = [({root: None}, nodes - {root}, (root,)) for root in nodes]
21 tree, free, to_expand = queue.pop()
23 if len(to_expand) == 0:
24 # Only print full-sized trees
26 print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))
28 # Expand node to_expand[0] with each possible set of children
29 for children in subsets(free):
30 ntree = {child: to_expand[0] for child in children}
32 nfree = free.difference(children)
33 queue.append((ntree, nfree, to_expand[1:] + tuple(children)))