Skip to main content

Python module for finding transitive edges in a directed acyclic graph

Project description

tredge

pypi

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for tredge, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size tredge-0.0.3-py3-none-any.whl (3.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size tredge-0.0.3.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page