code: def tarjan(sequence):
      r"""
      Tarjan algorithm for finding Strongly Connected Components (SCCs) of a graph.
  
      Args:
          sequence (list):
              List of head indices.
  
      Yields:
          A list of indices that make 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]
      """
  
      sequence = [-1] + sequence
      # record the search order, i.e., the timestep
      dfn = [-1] * len(sequence)
      # record the the smallest timestep in a SCC
      low = [-1] * len(sequence)
      # push the visited into the stack
      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])
  
          # a SCC is completed
          if low[i] == dfn[i]:
              cycle = [stack.pop()]
              while cycle[-1] != i:
                  onstack[cycle[-1]] = False
                  cycle.append(stack.pop())
              onstack[i] = False
              # ignore the self-loop
              if len(cycle) > 1:
                  yield cycle
  
      timestep = [0]
      for i in range(len(sequence)):
          if dfn[i] == -1:
              yield from connect(i, timestep)
  
file_path: underthesea/underthesea/utils/sp_alg.py
imports: import torch
  from underthesea.utils.sp_fn import pad, stripe
label_desc: Use image node_id 2 for calling a global function with example usage: tarjan(sequence) without return types