Python module for finding transitive edges in a directed acyclic graph
Project description
tredge
This is tiny yet fast module to get set of explicitly defined transitive edges from a directed acyclic graph. Given a DAG with edges child
<--parent
represented as dictionary (keys are children, values are iterables with parents), or as iterable of iterables representing edges ((child
, parent
)), or as file object pointing to tab-delimited file with 2 columns (child
, parent
), it returns set of transitive edges found there. Original intent of this package was to use it for removing redundant edges from tree structures.
If a given graph is cyclic, transitive_edges
function will not return edges that include vertices participating in loops. To find such vertices beforehand or make sure there are none, there is a function cycles(g)
.
Usage:
import tredge
g = {
'b': {'a'},
'c': {'a'},
'd': {'b', 'c', 'a'},
'e': {'d', 'a'}
}
result = tredge.transitive_edges(g)
print(result)
# {('d', 'a'), ('e', 'a')}
or
import tredge
g = [
('b', 'a'),
('c', 'a'),
('d', 'b'),
('d', 'c'),
('e', 'd'),
('e', 'a'),
('d', 'a')
]
result = tredge.transitive_edges(g)
print(result)
# {('d', 'a'), ('e', 'a')}
or
"""input_file.tab:
b a
c a
d b
d c
e d
e a
d a
"""
import tredge
with open('input_file.tab', mode='r', encoding='utf8') as g:
result = tredge.transitive_edges(g)
print(result)
# {('d', 'a'), ('e', 'a')}
To check if a graph has cycles:
import tredge
g = {
'b': {'a'},
'c': {'a'},
'd': {'b', 'c', 'a'},
'e': {'d', 'a'}
}
result = tredge.cycles(g)
print(result)
# {'e', 'c', 'd'}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tredge-0.0.3.tar.gz
.
File metadata
- Download URL: tredge-0.0.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b6c47321e194274f6cb69c6f5ae6fe3889b6b025cb6f823524b3e505940a8f2 |
|
MD5 | 920c3d92ffaab6aa616bd14500ef5fe7 |
|
BLAKE2b-256 | 7af1f26401faae3130232afae16640c361a3acca4010bcd6802bc8e9225343da |
File details
Details for the file tredge-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: tredge-0.0.3-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8638e20fd181d7b60efe130155050a9dc40ed5c1135fdcfd646cb1c93c821cb |
|
MD5 | c6bb53b911d06995b217cc6523957722 |
|
BLAKE2b-256 | a5f97439095c81d3837be8361ce76281bb2aed6801c8d7e76b1b0e225fad21df |