Note
This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.
TopologicalSorter#
- class TopologicalSorter(dag, /, check_cycle=True, *, reverse=False, initial=None, check_args=True)#
Bases:
object
Provides functionality to topologically sort a directed graph.
A topological sorter is used to arrange the nodes of a directed acyclic graph in a linear order such that for every directed edge from node u to node v, node u` appears before node v in the sequence. This ordering is particularly useful for resolving dependencies in scenarios such as task scheduling, where certain tasks must be completed before others, and in build systems, where files or modules depend on one another. The topological sorting process ensures that all dependencies are satisfied, allowing for efficient execution and processing of tasks or data.
The steps required to perform the sorting of a given graph are as follows:
Create an instance of the TopologicalSorter with an initial graph.
While
is_active()
is True, iterate over the nodes returned byget_ready()
and process them.Call
done()
on each node as it finishes processing.
For example:
import rustworkx as rx G = rx.PyDiGraph() G.add_nodes_from(["A", "B", "C", "D", "E", "F"]) G.add_edges_from_no_data([(0, 2),(1, 2), (2, 3), (3, 4), (3, 5)]) sorter = rx.TopologicalSorter(G) while sorter.is_active(): nodes = sorter.get_ready() print(nodes) sorter.done(nodes)
[0, 1] [2] [3] [5, 4]
The underlying graph can be mutated and TopologicalSorter will pick-up the modifications but it’s not recommended doing it as it may result in a logical-error.
- Parameters:
graph (PyDiGraph) – The directed graph to be used.
check_cycle (bool) – When this is set to
True
(the default), we search for cycles in the graph during initialization of topological sorter and raiseDAGHasCycle
if any cycle is detected. If it’s set toFalse
, topological sorter will output as many nodes as possible until cycles block more progress.reverse (bool) – If
False
(the default), perform a regular topological ordering, i.e. for a directed edgeA -> B
theA
appears before theB
. IfTrue
, the ordering will be a reversed topological ordering, i.e. for a directed edgeA -> B
, theB
appears before theA
.initial (Iterable[int]) – By default, the topological ordering will include all nodes in the graph. If
initial
node indices are provided, the ordering will only include those nodes and any nodes that are dominated by them. In this case, the first output fromget_ready()
will match the initial set, and any node with a natural in-degree of zero will be excluded from the output if it is not part of the initial set. Additionally, providing an initial set where the nodes have even a partial topological order among themselves will raise aValueError
, although this may not be detected until a call todone()
.check_args (bool) – If
True
(the default), then all arguments todone()
are checked for validity, and aValueError
is raised if any were not ready, already done, or not indices of the circuit. IfFalse
, the tracking for this is disabled, which can provide a meaningful performance and memory improvement, but the results will be undefined if invalid values are given.
Methods
Marks a set of nodes returned by
get_ready()
as processed.Return a list of all the nodes that are ready.
Return
True
if more progress can be made andFalse
otherwise.