def tarjan(sequence):
    '\n    Tarjan algorithm for finding Strongly Connected Components (SCCs) of a graph.\n\n    Args:\n        sequence (list):\n            List of head indices.\n\n    Yields:\n        A list of indices making up a SCC. All self-loops are ignored.\n\n    Examples:\n        >>> next(tarjan([2, 5, 0, 3, 1]))  # (1 -> 5 -> 2 -> 1) is a cycle\n        [2, 5, 1]\n    '
    sequence = ([(- 1)] + sequence)
    dfn = ([(- 1)] * len(sequence))
    low = ([(- 1)] * len(sequence))
    (stack, onstack) = ([], ([False] * len(sequence)))

    def connect(i, timestep):
        dfn[i] = low[i] = timestep[0]
        timestep[0] += 1
        stack.append(i)
        onstack[i] = True
        for (j, head) in enumerate(sequence):
            if (head != i):
                continue
            if (dfn[j] == (- 1)):
                (yield from connect(j, timestep))
                low[i] = min(low[i], low[j])
            elif onstack[j]:
                low[i] = min(low[i], dfn[j])
        if (low[i] == dfn[i]):
            cycle = [stack.pop()]
            while (cycle[(- 1)] != i):
                onstack[cycle[(- 1)]] = False
                cycle.append(stack.pop())
            onstack[i] = False
            if (len(cycle) > 1):
                (yield cycle)
    timestep = [0]
    for i in range(len(sequence)):
        if (dfn[i] == (- 1)):
            (yield from connect(i, timestep))
Tarjan algorithm for finding Strongly Connected Components (SCCs) of a graph.

Args:
    sequence (list):
        List of head indices.

Yields:
    A list of indices making up a SCC. All self-loops are ignored.

Examples:
    >>> next(tarjan([2, 5, 0, 3, 1]))  # (1 -> 5 -> 2 -> 1) is a cycle
    [2, 5, 1]
def tarjan(sequence):
    '\n    Tarjan algorithm for finding Strongly Connected Components (SCCs) of a graph.\n\n    Args:\n        sequence (list):\n            List of head indices.\n\n    Yields:\n        A list of indices making up a SCC. All self-loops are ignored.\n\n    Examples:\n        >>> next(tarjan([2, 5, 0, 3, 1]))  # (1 -> 5 -> 2 -> 1) is a cycle\n        [2, 5, 1]\n    '
    sequence = ([(- 1)] + sequence)
    dfn = ([(- 1)] * len(sequence))
    low = ([(- 1)] * len(sequence))
    (stack, onstack) = ([], ([False] * len(sequence)))

    def connect(i, timestep):
        dfn[i] = low[i] = timestep[0]
        timestep[0] += 1
        stack.append(i)
        onstack[i] = True
        for (j, head) in enumerate(sequence):
            if (head != i):
                continue
            if (dfn[j] == (- 1)):
                (yield from connect(j, timestep))
                low[i] = min(low[i], low[j])
            elif onstack[j]:
                low[i] = min(low[i], dfn[j])
        if (low[i] == dfn[i]):
            cycle = [stack.pop()]
            while (cycle[(- 1)] != i):
                onstack[cycle[(- 1)]] = False
                cycle.append(stack.pop())
            onstack[i] = False
            if (len(cycle) > 1):
                (yield cycle)
    timestep = [0]
    for i in range(len(sequence)):
        if (dfn[i] == (- 1)):
            (yield from connect(i, timestep))