Skip to main content

A simple base implementation of a Directed Acyclic Graph, intended to be subclassed for more specific functionality.

Project description

base-dag

A simple base implementation of a DAG. Users should feel free to subclass this DAG class to provide their desired functionality. The API is intended to be similar to the NetworkX API, though not an exact replica. Note that multiple edges between the same two nodes are not supported by this package.

Creating a DAG.

from base_dag import DAG

# Initialize the DAG
dag = DAG()

# Add nodes and edges
dag.add_nodes_from([1, 2])
dag.add_node(3)
dag.add_edge((1, 2))
dag.add_edge((2, 3))

# Remove nodes and edges
dag.remove_edge((2, 3))
dag.remove_node(3)

Subgraph

# Re-add a third node.
dag.add_node(3)
# Extract the subgraph of nodes 1 and 2.
sub_dag = dag.subgraph([1, 2])

Other Operations

All nodes & edges

all_nodes = dag.nodes() # (1, 2, 3)
all_edges = dag.edges() # ( (1, 2), (2, 3) )

Successors and Predecessors

successors = dag.successors(1) # [2]
predecessors = dag.predecessors(2) # [1]

Descendants and Ancestors

descendants = dag.descendants(1) # [2, 3]
ancestors = dag.ancestors(2) # [1]

Indegree and Outdegree

in_degree = dag.indegree(2) # 1
out_degree = dag.outdegree(1) # 1

Reverse

# Reverse the direction of the edges in the graph in place, without affecting the nodes at all.
dag.reverse()

Topological sort

sorted_nodes = dag.topological_sort()

Topological generations

generations = dag.topological_generations()

Sorted topological generations

sorted_generations = dag.sorted_topological_generations()

has_path

has_path = dag.has_path(1, 3) # True

is_acyclic

is_acyclic = dag.is_acyclic() # True

Project details


Download files

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

Source Distribution

base_dag-0.1.8.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

base_dag-0.1.8-py2.py3-none-any.whl (5.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file base_dag-0.1.8.tar.gz.

File metadata

  • Download URL: base_dag-0.1.8.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for base_dag-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c3edfee15c3f352694a38d70d5a115ee19570bedb7ba4e6ffbc0adbf2c8f8f49
MD5 44c5e7a81ff9bc9d23953d19d3002d9f
BLAKE2b-256 f4ed27e85ed81bc0cc3857bbd85361288efdf56d6f5f46652c73294ffef0ba80

See more details on using hashes here.

File details

Details for the file base_dag-0.1.8-py2.py3-none-any.whl.

File metadata

  • Download URL: base_dag-0.1.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for base_dag-0.1.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4a8ebb5dad682ff826f147c17b28d52750ec61ecbc0cb39b5b5faa32b37c650d
MD5 ed911da6d5bdde769268a598518d8394
BLAKE2b-256 b8c4ef7ee6a1313612cf351a42e9ef56c842da1d799e346fb1eac4151228e0a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page