Debian Codegraph @

vcsgraph/tsort.py

raw symbols SCIP
1 # Copyright (C) 2005, 2006, 2008 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 """Topological sorting routines."""
18
19 from .known_graph import KnownGraph
20
21 __all__ = ["MergeSorter", "TopoSorter", "merge_sort", "topo_sort"]
22
23 # The Rust implementations are optional
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()