code: private int[] linearize(final FSA fsa) {
      int[] linearized = new int[0];
      int last = 0;
  
      BitSet visited = new BitSet();
      IntStack nodes = new IntStack();
      nodes.push(fsa.getRootNode());
  
      while (!nodes.isEmpty()) {
        final int node = nodes.pop();
        if (visited.get(node)) {
          continue;
        }
  
        if (last >= linearized.length) {
          linearized = Arrays.copyOf(linearized, linearized.length + 100000);
        }
  
        visited.set(node);
        linearized[last++] = node;
  
        for (int arc = fsa.getFirstArc(node); arc != 0; arc = fsa.getNextArc(arc)) {
          if (!fsa.isArcTerminal(arc)) {
            int target = fsa.getEndNode(arc);
            if (!visited.get(target))
              nodes.push(target);
          }
        }
      }
  
      return Arrays.copyOf(linearized, last);
    }
docs: Linearization of states.