https://github.com/pgolo/tredge
Tiny module for finding transitive edges in a directed acyclic graph
https://github.com/pgolo/tredge
depth-first-search dfs graphs transitive-edges transitive-reduction
Last synced: 5 months ago
JSON representation
Tiny module for finding transitive edges in a directed acyclic graph
- Host: GitHub
- URL: https://github.com/pgolo/tredge
- Owner: pgolo
- License: mit
- Created: 2020-07-16T18:26:31.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T05:07:36.000Z (over 5 years ago)
- Last Synced: 2025-11-09T19:08:01.749Z (7 months ago)
- Topics: depth-first-search, dfs, graphs, transitive-edges, transitive-reduction
- Language: Python
- Homepage:
- Size: 1.06 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# tredge
[![pypi][pypi-img]][pypi-url]
[pypi-img]: https://img.shields.io/pypi/v/tredge?style=plastic
[pypi-url]: https://pypi.org/project/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:
```python
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
```python
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
```python
"""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:
```python
import tredge
g = {
'b': {'a'},
'c': {'a'},
'd': {'b', 'c', 'a'},
'e': {'d', 'a'}
}
result = tredge.cycles(g)
print(result)
# {'e', 'c', 'd'}
```