networkx-reverse-topological-sort 0.1.0a0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Jifeng Wu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.4
2
+ Name: networkx-reverse-topological-sort
3
+ Version: 0.1.0a0
4
+ Summary: Reverse topological sort utilities for NetworkX: easily stratify or sort nodes in a DAG from leaves to root.
5
+ Author-email: Jifeng Wu <jifengwu2k@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/jifengwu2k/networkx-reverse-topological-sort
8
+ Project-URL: Bug Tracker, https://github.com/jifengwu2k/networkx-reverse-topological-sort/issues
9
+ Classifier: Programming Language :: Python :: 2
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=2
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: networkx
16
+ Dynamic: license-file
17
+
18
+ # `networkx-reverse-topological-sort`
19
+
20
+ Reverse topological sort utilities for [NetworkX](https://networkx.org/): easily stratify or sort nodes in a DAG from leaves to root.
21
+
22
+ ## Features
23
+
24
+ - **Reverse Topological Sort**: Get a valid reverse of NetworkX's `topological_sort`, yielding from "leaf" nodes up to roots.
25
+ - **Reverse Topological Generations**: Partition your DAG into generations in reverse order (each generation, all children are in earlier generations).
26
+
27
+ ## Motivation
28
+
29
+ NetworkX includes tools for forward (root-to-leaf) topological traversals, but some applications (e.g., evaluating dependencies after their dependents, post-order processing, certain scheduling schemes) need the reverse order: leaves to root.
30
+
31
+ This tiny package offers that for `networkx`.
32
+
33
+ ## Installation
34
+
35
+ ```
36
+ pip install networkx-reverse-topological-sort
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```python
42
+ import networkx as nx
43
+ from networkx_reverse_topological_sort import (
44
+ reverse_topological_generations,
45
+ reverse_topological_sort,
46
+ )
47
+
48
+ DG = nx.DiGraph([(2, 1), (3, 1)])
49
+ print([sorted(generation) for generation in reverse_topological_generations(DG)])
50
+ # Output: [[1], [2, 3]]
51
+
52
+ print(list(reverse_topological_sort(DG)))
53
+ # Output: [1, 2, 3] (or [1, 3, 2]: nodes before their parents)
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
59
+
60
+ ## License
61
+
62
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,45 @@
1
+ # `networkx-reverse-topological-sort`
2
+
3
+ Reverse topological sort utilities for [NetworkX](https://networkx.org/): easily stratify or sort nodes in a DAG from leaves to root.
4
+
5
+ ## Features
6
+
7
+ - **Reverse Topological Sort**: Get a valid reverse of NetworkX's `topological_sort`, yielding from "leaf" nodes up to roots.
8
+ - **Reverse Topological Generations**: Partition your DAG into generations in reverse order (each generation, all children are in earlier generations).
9
+
10
+ ## Motivation
11
+
12
+ NetworkX includes tools for forward (root-to-leaf) topological traversals, but some applications (e.g., evaluating dependencies after their dependents, post-order processing, certain scheduling schemes) need the reverse order: leaves to root.
13
+
14
+ This tiny package offers that for `networkx`.
15
+
16
+ ## Installation
17
+
18
+ ```
19
+ pip install networkx-reverse-topological-sort
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```python
25
+ import networkx as nx
26
+ from networkx_reverse_topological_sort import (
27
+ reverse_topological_generations,
28
+ reverse_topological_sort,
29
+ )
30
+
31
+ DG = nx.DiGraph([(2, 1), (3, 1)])
32
+ print([sorted(generation) for generation in reverse_topological_generations(DG)])
33
+ # Output: [[1], [2, 3]]
34
+
35
+ print(list(reverse_topological_sort(DG)))
36
+ # Output: [1, 2, 3] (or [1, 3, 2]: nodes before their parents)
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
42
+
43
+ ## License
44
+
45
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.4
2
+ Name: networkx-reverse-topological-sort
3
+ Version: 0.1.0a0
4
+ Summary: Reverse topological sort utilities for NetworkX: easily stratify or sort nodes in a DAG from leaves to root.
5
+ Author-email: Jifeng Wu <jifengwu2k@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/jifengwu2k/networkx-reverse-topological-sort
8
+ Project-URL: Bug Tracker, https://github.com/jifengwu2k/networkx-reverse-topological-sort/issues
9
+ Classifier: Programming Language :: Python :: 2
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=2
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: networkx
16
+ Dynamic: license-file
17
+
18
+ # `networkx-reverse-topological-sort`
19
+
20
+ Reverse topological sort utilities for [NetworkX](https://networkx.org/): easily stratify or sort nodes in a DAG from leaves to root.
21
+
22
+ ## Features
23
+
24
+ - **Reverse Topological Sort**: Get a valid reverse of NetworkX's `topological_sort`, yielding from "leaf" nodes up to roots.
25
+ - **Reverse Topological Generations**: Partition your DAG into generations in reverse order (each generation, all children are in earlier generations).
26
+
27
+ ## Motivation
28
+
29
+ NetworkX includes tools for forward (root-to-leaf) topological traversals, but some applications (e.g., evaluating dependencies after their dependents, post-order processing, certain scheduling schemes) need the reverse order: leaves to root.
30
+
31
+ This tiny package offers that for `networkx`.
32
+
33
+ ## Installation
34
+
35
+ ```
36
+ pip install networkx-reverse-topological-sort
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```python
42
+ import networkx as nx
43
+ from networkx_reverse_topological_sort import (
44
+ reverse_topological_generations,
45
+ reverse_topological_sort,
46
+ )
47
+
48
+ DG = nx.DiGraph([(2, 1), (3, 1)])
49
+ print([sorted(generation) for generation in reverse_topological_generations(DG)])
50
+ # Output: [[1], [2, 3]]
51
+
52
+ print(list(reverse_topological_sort(DG)))
53
+ # Output: [1, 2, 3] (or [1, 3, 2]: nodes before their parents)
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
59
+
60
+ ## License
61
+
62
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ networkx_reverse_topological_sort.py
4
+ pyproject.toml
5
+ setup.cfg
6
+ networkx_reverse_topological_sort.egg-info/PKG-INFO
7
+ networkx_reverse_topological_sort.egg-info/SOURCES.txt
8
+ networkx_reverse_topological_sort.egg-info/dependency_links.txt
9
+ networkx_reverse_topological_sort.egg-info/requires.txt
10
+ networkx_reverse_topological_sort.egg-info/top_level.txt
@@ -0,0 +1,45 @@
1
+ import networkx as nx
2
+
3
+
4
+ def reverse_topological_generations(G):
5
+ """
6
+ Stratifies a DAG into generations in reverse topological order.
7
+ That is, first generation are leaves (no outgoing edges), etc.
8
+ """
9
+ if not G.is_directed():
10
+ raise nx.NetworkXError("Topological sort not defined on undirected graphs.")
11
+
12
+ multigraph = G.is_multigraph()
13
+ # outdegree_map: only nodes with any outgoing edge
14
+ outdegree_map = {v: d for v, d in G.out_degree() if d > 0}
15
+ zero_outdegree = [v for v, d in G.out_degree() if d == 0]
16
+
17
+ while zero_outdegree:
18
+ this_generation = zero_outdegree
19
+ zero_outdegree = []
20
+ for node in this_generation:
21
+ if node not in G:
22
+ raise RuntimeError("Graph changed during iteration")
23
+ for parent in G.predecessors(node):
24
+ try:
25
+ outdegree_map[parent] -= len(G[parent][node]) if multigraph else 1
26
+ except KeyError:
27
+ raise RuntimeError("Graph changed during iteration")
28
+ if outdegree_map[parent] == 0:
29
+ zero_outdegree.append(parent)
30
+ del outdegree_map[parent]
31
+ yield this_generation
32
+
33
+ if outdegree_map:
34
+ raise nx.NetworkXUnfeasible(
35
+ "Graph contains a cycle or graph changed during iteration"
36
+ )
37
+
38
+
39
+ def reverse_topological_sort(G):
40
+ """
41
+ Yields the nodes of G in reverse topological order (from leaves to roots).
42
+ """
43
+ for generation in reverse_topological_generations(G):
44
+ for node in generation:
45
+ yield node
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "networkx-reverse-topological-sort"
7
+ version = "0.1.0a0"
8
+ description = "Reverse topological sort utilities for NetworkX: easily stratify or sort nodes in a DAG from leaves to root."
9
+ readme = "README.md"
10
+ requires-python = ">=2"
11
+ license = "MIT"
12
+ authors = [
13
+ { name="Jifeng Wu", email="jifengwu2k@gmail.com" }
14
+ ]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 2",
17
+ "Programming Language :: Python :: 3",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+ dependencies = [
21
+ "networkx"
22
+ ]
23
+
24
+ [project.urls]
25
+ "Homepage" = "https://github.com/jifengwu2k/networkx-reverse-topological-sort"
26
+ "Bug Tracker" = "https://github.com/jifengwu2k/networkx-reverse-topological-sort/issues"
@@ -0,0 +1,7 @@
1
+ [bdist_wheel]
2
+ universal = 1
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+