| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Topological sorting routines.""" |
| 18 |
|
| 19 |
from .known_graph import KnownGraph |
| 20 |
|
| 21 |
__all__ = ["MergeSorter", "TopoSorter", "merge_sort", "topo_sort"] |
| 22 |
|
| 23 |
|
| 24 |
from ._graph_rs import MergeSorter, TopoSorter, merge_sort |
| 25 |
|
| 26 |
|
| 27 |
def topo_sort(graph): |
| 28 |
"""Topological sort a graph. |
| 29 |
|
| 30 |
graph -- sequence of pairs of node->parents_list. |
| 31 |
|
| 32 |
The result is a list of node names, such that all parents come before their |
| 33 |
children. |
| 34 |
|
| 35 |
node identifiers can be any hashable object, and are typically strings. |
| 36 |
|
| 37 |
This function has the same purpose as the TopoSorter class, but uses a |
| 38 |
different algorithm to sort the graph. That means that while both return a |
| 39 |
list with parents before their child nodes, the exact ordering can be |
| 40 |
different. |
| 41 |
|
| 42 |
topo_sort is faster when the whole list is needed, while when iterating |
| 43 |
over a part of the list, TopoSorter.iter_topo_order should be used. |
| 44 |
""" |
| 45 |
kg = KnownGraph(dict(graph)) |
| 46 |
return kg.topo_sort() |