

def _contains_cycle(fgraph, orderings):
    '\n\n    Parameters\n    ----------\n    fgraph\n        The FunctionGraph to check for cycles.\n    orderings\n        Dictionary specifying extra dependencies besides those encoded in\n        Variable.owner / Apply.inputs.\n\n        If orderings[my_apply] == dependencies, then my_apply is an Apply\n        instance, dependencies is a set of Apply instances, and every member\n        of dependencies must be executed before my_apply.\n\n        The dependencies are typically used to prevent\n        inplace apply nodes from destroying their input before\n        other apply nodes with the same input access it.\n\n    Returns\n    -------\n    bool\n        True if the graph contains a cycle, False otherwise.\n\n    '
    outputs = fgraph.outputs
    assert isinstance(outputs, (tuple, list, deque))
    parent_counts = {
        
    }
    node_to_children = {
        
    }
    visitable = deque()
    for var in fgraph.variables:
        owner = var.owner
        if owner:
            node_to_children.setdefault(owner, []).append(var)
            parent_counts[var] = 1
        else:
            visitable.append(var)
            parent_counts[var] = 0
    for a_n in fgraph.apply_nodes:
        parents = list(a_n.inputs)
        parents.extend(orderings.get(a_n, []))
        if parents:
            for parent in parents:
                node_to_children.setdefault(parent, []).append(a_n)
            parent_counts[a_n] = len(parents)
        else:
            visitable.append(a_n)
            parent_counts[a_n] = 0
    visited = 0
    while visitable:
        node = visitable.popleft()
        visited += 1
        for client in node_to_children.get(node, []):
            parent_counts[client] -= 1
            if (not parent_counts[client]):
                visitable.append(client)
    return (visited != len(parent_counts))

dataset/ETHPy150Open rizar/attention-lvcsr/libs/Theano/theano/gof/destroyhandler.py _contains_cycle/original