Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <topologicalsorter.h>
Public Member Functions | |
TopologicalSorter () | |
TopologicalSorter (const TopologicalSorter &)=delete | |
This type is neither copyable nor movable. | |
TopologicalSorter & | operator= (const TopologicalSorter &)=delete |
~TopologicalSorter () | |
void | AddNode (const T &node) |
void | AddEdges (const std::vector< std::pair< T, T > > &edges) |
Shortcut to AddEdge() in bulk. Not optimized. | |
void | AddEdge (const T &from, const T &to) |
bool | GetNext (T *node, bool *cyclic_ptr, std::vector< T > *output_cycle_nodes=nullptr) |
int | GetCurrentFringeSize () |
void | StartTraversal () |
bool | TraversalStarted () const |
A copy of each Node is stored internally. Duplicated edges are allowed, and discarded lazily so that AddEdge() keeps an amortized constant time, yet the total memory usage remains O(number of different edges + number of nodes).
DenseIntTopologicalSorter implements the core topological sort algorithm. For greater efficiency it can be used directly (TopologicalSorter<int> is about 1.5-3x slower).
TopologicalSorter requires that all nodes and edges be added before traversing the nodes, otherwise it will die with a fatal error.
TopologicalSorter is -compatible
Note(user): since all the real work is done by DenseIntTopologicalSorterTpl, and this class is a template, we inline every function here in the .h.
If stable_sort is true then the topological sort will preserve the original order of the nodes as much as possible. Note, the order which is preserved is the order in which the nodes are added (if you use AddEdge it will add the first argument and then the second).
Definition at line 325 of file topologicalsorter.h.
|
inline |
Definition at line 327 of file topologicalsorter.h.
|
delete |
This type is neither copyable nor movable.
|
inline |
Definition at line 332 of file topologicalsorter.h.
|
inline |
Adds a directed edge with the given endpoints to the graph. There is no requirement (nor is it an error) to call AddNode() for the endpoints. Dies with a fatal error if called after a traversal has been started (see TraversalStarted()).
The lookups are not inlined into AddEdge because we need to ensure that "from" is inserted before "to".
Definition at line 353 of file topologicalsorter.h.
|
inline |
Shortcut to AddEdge() in bulk. Not optimized.
Definition at line 345 of file topologicalsorter.h.
|
inline |
Adds a node to the graph, if it has not already been added via previous calls to AddNode()/AddEdge(). If no edges are later added connecting this node, then it remains an isolated node in the graph. AddNode() only exists to support isolated nodes. There is no requirement (nor is it an error) to call AddNode() for the endpoints used in a call to AddEdge(). Dies with a fatal error if called after a traversal has been started (see TraversalStarted()), or if more than INT_MAX nodes are being added.
Definition at line 342 of file topologicalsorter.h.
|
inline |
Returns the number of nodes that currently have zero indegree. This starts a traversal (if not started already).
Definition at line 399 of file topologicalsorter.h.
|
inline |
Visits the least node in topological order over the current set of nodes and edges, and marks that node as visited, so that repeated calls to GetNext() will visit all nodes in order. Writes the newly visited node in *node and returns true with *cyclic set to false (assuming the graph has not yet been discovered to be cyclic). Returns false if all nodes have been visited, or if the graph is discovered to be cyclic, in which case *cyclic is also set to true.
If you set the optional argument "output_cycle_nodes" to non-NULL and a cycle is detected, it will dump an arbitrary cycle of the graph (whose length will be between 1 and #number_of_nodes, inclusive), in the natural order: for example if "output_cycle_nodes" is filled with ["A", "C", "B"], it means that A->C->B->A is a directed cycle of the graph.
This starts a traversal (if not started already). Note that the graph can only be traversed once.
Definition at line 378 of file topologicalsorter.h.
|
delete |
|
inline |
Start a traversal. See TraversalStarted(). This initializes the various data structures of the sorter. Since this takes O(num_nodes
We move elements from the absl::flat_hash_map to this vector, without extra copy (if they are movable).
Definition at line 408 of file topologicalsorter.h.
|
inline |
Whether a traversal has started. If true, AddNode() and AddEdge() can no longer be called.
Definition at line 422 of file topologicalsorter.h.