all_topological_sorts

all_topological_sorts#

graph_tool.structure.all_topological_sorts(g)[git][source]#

Return an iterator over all topological orderings of a directed acyclic graph.

Parameters:
gGraph

Directed acyclic graph.

Returns:
ordersiterator over numpy.ndarray

Every ordering of the vertices in which all edges point forward.

See also

topological_sort

A single topological ordering

antichains

All antichains of a directed acyclic graph

Notes

The orderings are generated by backtracking over the vertices without remaining in-edges [knuth-structured-1974], in time \(O(V)\) per ordering. Their number can grow factorially with the number of vertices. A ValueError is raised if the graph has a cycle.

References

[knuth-structured-1974]

Donald E. Knuth and Jayme L. Szwarcfiter, “A structured program to generate all topological sorting arrangements”, Information Processing Letters 2, 153-157 (1974), DOI: 10.1016/0020-0190(74)90001-5 [sci-hub, @tor]

Examples

>>> g = gt.Graph([(0, 1), (0, 2), (1, 3), (2, 3)])
>>> for o in gt.all_topological_sorts(g):
...     print(o)
[0 1 2 3]
[0 2 1 3]