human
What is a Python program to find simple cycles (elementary circuits) of a directed graph given the following:
Complete the below
@not_implemented_for('undirected')
def simple_cycles(G):

    ''''Find simple cycles (elementary circuits) of a directed graph.
    A `simple cycle`, or `elementary circuit`, is a closed path where
    no node appears twice. Two elementary circuits are distinct if they
    are not cyclic permutations of each other.
    This is a nonrecursive, iterator/generator version of Johnson\'s
    algorithm [1]_.  There may be better algorithms for some cases [2]_ [3]_.
    Parameters
    G : NetworkX DiGraph
    A directed graph
    Returns
    cycle_generator: generator
    A generator that produces elementary cycles of the graph.
    Each cycle is represented by a list of nodes along the cycle.
    Examples
    >>> edges = [(0, 0), (0, 1), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)]
    >>> G = nx.DiGraph(edges)
    >>> len(list(nx.simple_cycles(G)))
    5
    To filter the cycles so that they don\'t include certain nodes or edges,
    copy your graph and eliminate those nodes or edges before calling
    >>> copyG = G.copy()
    >>> copyG.remove_nodes_from([1])
    >>> copyG.remove_edges_from([(0, 1)])
    >>> len(list(nx.simple_cycles(copyG)))
    3
    Notes
    The implementation follows pp. 79-80 in [1]_.
    The time complexity is `O((n+e)(c+1))` for `n` nodes, `e` edges and `c`
    elementary circuits.
    References
    .. [1] Finding all the elementary circuits of a directed graph.
    D. B. Johnson, SIAM Journal on Computing 4, no. 1, 77-84, 1975.
    http://dx.doi.org/10.1137/0204007
    .. [2] Enumerating the cycles of a digraph: a new preprocessing strategy.
    G. Loizou and P. Thanish, Information Sciences, v. 27, 163-182, 1982.
    .. [3] A search strategy for the elementary cycles of a directed graph.
    J.L. Szwarcfiter and P.E. Lauer, BIT NUMERICAL MATHEMATICS,
    v. 16, no. 2, 192-204, 1976.
    See Also
    cycle_basis'
    '''
assistant
#Here is Python program to find simple cycles (elementary circuits) of a directed graph.
    def _unblock(thisnode, blocked, B):
        stack = set([thisnode])
        while stack:
            node = stack.pop()
            if (node in blocked):
                blocked.remove(node)
                stack.update(B[node])
                B[node].clear()
    subG = type(G)(G.edges())
    sccs = list(nx.strongly_connected_components(subG))
    while sccs:
        scc = sccs.pop()
        startnode = scc.pop()
        path = [startnode]
        blocked = set()
        closed = set()
        blocked.add(startnode)
        B = defaultdict(set)
        stack = [(startnode, list(subG[startnode]))]
        while stack:
            (thisnode, nbrs) = stack[(-1)]
            if nbrs:
                nextnode = nbrs.pop()
                if (nextnode == startnode):
                    (yield path[:])
                    closed.update(path)
                elif (nextnode not in blocked):
                    path.append(nextnode)
                    stack.append((nextnode, list(subG[nextnode])))
                    closed.discard(nextnode)
                    blocked.add(nextnode)
                    continue
            if (not nbrs):
                if (thisnode in closed):
                    _unblock(thisnode, blocked, B)
                else:
                    for nbr in subG[thisnode]:
                        if (thisnode not in B[nbr]):
                            B[nbr].add(thisnode)
                stack.pop()
                path.pop()
        subG.remove_node(startnode)
        H = subG.subgraph(scc)
        sccs.extend(list(nx.strongly_connected_components(H)))