Graph Module¶
This module contains graph-related functionality for representing and manipulating pipeline structures.
graph
¶
Graph utility functions for cycle detection in directed graphs.
This module provides utilities for working with graph structures, particularly for detecting cycles in directed acyclic graphs (DAGs).
FUNCTION | DESCRIPTION |
---|---|
check_cycle |
Checks the given DAG for cycles and returns nodes that are part of a cycle. |
check_cycle
¶
check_cycle(
node_successors: dict[str, list[str]],
) -> tuple[bool, list[str]]
Checks the given DAG for cycles and returns nodes that are part of a cycle.
This function implements a topological sorting algorithm to detect cycles in a directed graph. It works by tracking the in-degree (number of incoming edges) for each node and progressively removing nodes with zero in-degree. If any nodes remain with non-zero in-degree after the process, a cycle exists.
PARAMETER | DESCRIPTION |
---|---|
|
A dictionary where keys are node names and values are lists of successor node names. This represents the graph structure as an adjacency list.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[bool, list[str] or None]
|
A tuple containing: - bool: True if a cycle was detected, False otherwise - list[str] or None: If a cycle exists, a list of nodes that are part of the cycle; otherwise None. |
Examples:
>>> # Graph with no cycle
>>> no_cycle = {'A': ['B', 'C'], 'B': ['D'], 'C': ['D'], 'D': []}
>>> check_cycle(no_cycle)
(False, None)
>>> # Graph with a cycle
>>> has_cycle = {'A': ['B'], 'B': ['C'], 'C': ['A']}
>>> check_cycle(has_cycle)
(True, ['A', 'B', 'C'])