Stay organized with collections
Save and categorize content based on your preferences.
Generate docstrings and comments for Python functions.
You can query a model directly and test the results returned when using
different parameter values with the Cloud console, or by calling the
Vertex AI API directly.
defdfs(node):ifnodeinon_stack:returnFalse# Back-edge found (cycle)ifnodeinvisited:returnTrueon_stack.add(node)visited.add(node)forneighboringraph.get(node,[]):ifnotdfs(neighbor):returnFalseon_stack.remove(node)returnTruereturnall(dfs(node)fornodeingraph)
Response
defis_dag_dfs(graph):"""Checks if a directed graph is a Directed Acyclic Graph (DAG). This function uses a Depth-First Search (DFS) approach. It maintains two sets: 'visited' to keep track of all nodes that have been visited, and 'on_stack' to keep track of nodes currently in the recursion stack for the current DFS path. A cycle is detected if a node is encountered that is already on the recursion stack (a back-edge). Args: graph (dict): A dictionary representing the graph as an adjacency list. Keys are the nodes, and values are lists of their neighboring nodes (to which they have an outgoing edge). Example: {'A': ['B', 'C'], 'B': ['D']} Returns: bool: True if the graph is a DAG, False otherwise. """visited=set()on_stack=set()# Track nodes on recursion stackdefdfs(node):"""Recursively performs a Depth-First Search from a given node. This helper function explores the graph, using the 'on_stack' set from the parent scope to detect cycles and the 'visited' set to avoid re-processing nodes. Args: node: The node to start the DFS from. Returns: bool: False if a cycle is detected in the path starting from this node, True otherwise. """ifnodeinon_stack:returnFalse# Back-edge found (cycle)ifnodeinvisited:returnTrueon_stack.add(node)visited.add(node)forneighboringraph.get(node,[]):ifnotdfs(neighbor):returnFalseon_stack.remove(node)returnTruereturnall(dfs(node)fornodeingraph)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-23 UTC."],[],[],null,[]]