Skip to main content

toposort

Overview

Implements a topological sort algorithm.

From Wikipedia <http://en.wikipedia.org/wiki/Topological_sorting>_: In computer science, a topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering.

Input data description

The input to the toposort function is a dict describing the dependencies among the input nodes. Each key is a dependent node, the corresponding value is a set containing the dependent nodes.

Note that toposort does not care what the input node values mean: it just compares them for equality. The examples here usually use integers, but they could be any hashable type.

Typical usage

The interpretation of the input data here is: If 2 depends on 11; 9 depends on 11, 8 and 10; 10 depends on 11 and 3 (and so on), then in what order should we process the items such that all nodes are processed before any of their dependencies?::

>>> from toposort import toposort, toposort_flatten
>>> list(toposort({2: {11},
...                9: {11, 8, 10},
...                10: {11, 3},
...                11: {7, 5},
...                8: {7, 3},
...               }))
[{3, 5, 7}, {8, 11}, {2, 10}, {9}]

And the answer is: process 3, 5, and 7 (in any order); then process 8 and 11; then process 2 and 10; then process 9. Note that 3, 5, and 7 are returned first because they do not depend on anything. They are then removed from consideration, and then 8 and 11 don't depend on anything remaining. This process continues until all nodes are returned, or a circular dependency is detected.

Circular dependencies

A circular dependency will raise a CyclicDependencyError, which is derived from ValueError. Here 1 depends on 2, and 2 depends on 1::

>>> list(toposort({1: {2},
...                2: {1},
...               }))
Traceback (most recent call last):
    ...
toposort.CircularDependencyError: Circular dependencies exist among these items: {1:{2}, 2:{1}}

In addition, the 'data' attribute of the raised CyclicDependencyError will contain a dict containing the subset of the input data involved in the circular dependency.

Module contents

toposort(data)

Returns an iterator describing the dependencies among nodes in the input data. Each returned item will be a set. Each member of this set has no dependencies in this set, or in any set previously returned.

toposort_flatten(data, sort=True)

Like toposort(data), except that it returns a list of all of the depend values, in order. If sort is true, the returned nodes are sorted within each group before they are appended to the result::

>>> toposort_flatten({2: {11},
...                   9: {11, 8, 10},
...                   10: {11, 3},
...                   11: {7, 5},
...                   8: {7, 3},
...                  })
[3, 5, 7, 8, 11, 2, 10, 9]

Note that this result is the same as the first example: [{3, 5, 7}, {8, 11}, {2, 10}, {9}], except that the result is flattened, and within each set the nodes are sorted.

Testing

To test, run 'python setup.py test'. On python >= 3.0, this also runs the doctests.

Metadata

Release files for toposort 1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toposort 1.10
File Size Uploaded
toposort-1.10.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for toposort 1.10
File Interpreter ABI Platform
toposort-1.10-py3-none-any.whl Python 3 none any Details

Total release size: 19.6 kB

Release files / toposort-1.10.tar.gz

Download URL toposort-1.10.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bfbb479c53d0a696ea7402601f4e693c97b0367837c8898bc6471adfca37a6bd
BLAKE2b-256 checksum
How to use checksums
69198e955d90985ecbd3b9adb2a759753a6840da2dff3c569d412b2c9217678b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.10

Release files / toposort-1.10-py3-none-any.whl

Download URL toposort-1.10-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cbdbc0d0bee4d2695ab2ceec97fe0679e9c10eab4b2a87a9372b929e70563a87
BLAKE2b-256 checksum
How to use checksums
f61757b444fd314d5e1593350b9a31d000e7411ba8e17ce12dc7ad54ca76b810
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.10

Release history Release notifications | RSS feed

This release

1.10 This release

2 release files

1.9

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

1 release file

1.3

1 release file

1.1

1 release file

1.0

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page