dimanche 24 juillet 2016

Random topological sorting with uniform distribution in near linear time

I want an algorithm for topological sorting that doesn't provide the same sorting each time, but a random one, each sorting being equally likely to all others.

Generating all possible topological sortings and picking one at random is correct, but far too slow. Generating all permutations and filtering the invalid topological sorts is also very slow; the first one degrades into the second one if the tree/forest is wide enough.

Inserting new nodes into a random position in the queue of nodes to be checked seems like it would produce a biased result, and putting it at the end and doing a fisher-yates shuffle also seems biased, since both fail to account for the number of nodes "hidden" under each node, i.e. how many nodes depend on a or b being scheduled. a could have no children, while b holds the rest of the tree.

How can I generate a random topological sorting with each valid sorting being equally likely, in near-linear time?




Aucun commentaire:

Enregistrer un commentaire