aryagraph.core.dag.DAG#

class aryagraph.core.dag.DAG(data: Any = None, *, nodes: Iterable[Any] | None = None, name: str | None = None, **attrs: Any)[source]#

Bases: DiGraph

Directed acyclic graph.

Adding an edge that would create a cycle raises CycleError whose cycle attribute lists the offending loop; the graph is left unchanged.

>>> dag = DAG([("extract", "clean"), ("clean", "train"), ("clean", "report")])
>>> dag.topological_order()
['extract', 'clean', 'train', 'report']
>>> dag.add_edge("report", "extract")
Traceback (most recent call last):
CycleError: edge 'report' → 'extract' would close the cycle 'report' → 'extract' → 'clean' → 'report'
add_edge(u: Hashable, v: Hashable, **attrs: Any) → None[source]#

Add arc u → v; raises CycleError if it would close a cycle.

add_edges(edges: Iterable[Any], **common: Any) → None[source]#

Add many arcs atomically: all of them, or none if any cycle would appear.

Validation runs once over the whole graph (linear time) instead of once per arc, so bulk loading stays fast.

is_dag() → bool[source]#

True when the graph has no directed cycle.

to_dag() → DAG[source]#

Copy as a DAG (raises CycleError if a cycle exists).

topological_order() → list[Hashable][source]#

Nodes so that every arc points forward (Kahn’s algorithm, insertion-order tie-break).

sources() → list[Hashable][source]#

Nodes with no incoming arcs.

sinks() → list[Hashable][source]#

Nodes with no outgoing arcs.

ancestors(node: Hashable) → set[Hashable][source]#

Every node with a path to node (excluding node).

descendants(node: Hashable) → set[Hashable][source]#

Every node reachable from node (excluding node).

levels() → NodeMap[source]#

Longest-path level of each node: sources are 0, every arc goes to a higher level.

generations() → list[list[Hashable]][source]#

Nodes grouped by levels() into the stages a pipeline can run in parallel.

depth() → int[source]#

Number of nodes on the longest path (0 for an empty DAG).

longest_path(weight: str | None = None) → list[Hashable][source]#

Heaviest source-to-sink path (by node count when weight is None).

critical_path(duration: str = 'duration', default: float = 1.0)[source]#

Critical-path analysis (CPM); see aryagraph.algorithms.critical_path().

transitive_reduction() → DAG[source]#

Smallest DAG with the same reachability.

transitive_closure() → DAG[source]#

DAG with an arc u → v for every path u ⇝ v.