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:
DiGraphDirected acyclic graph.
Adding an edge that would create a cycle raises
CycleErrorwhosecycleattribute 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; raisesCycleErrorif 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.
- topological_order() list[Hashable][source]#
Nodes so that every arc points forward (Kahn’s algorithm, insertion-order tie-break).
- 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.
- longest_path(weight: str | None = None) list[Hashable][source]#
Heaviest source-to-sink path (by node count when weight is None).