code: public static int[] cycle(int[][] graph, int[] vertices) {
  
          int n = graph.length;
          int m = vertices.length;
  
          // mark vertices
          boolean[] marked = new boolean[n];
          for (int v : vertices) {
              marked[v] = true;
          }
  
          int[] path = new int[m + 1];
  
          path[0] = path[m] = vertices[0];
          marked[vertices[0]] = false;
  
          for (int i = 1; i < m; i++) {
              int w = firstMarked(graph[path[i - 1]], marked);
              if (w < 0) throw new IllegalArgumentException("broken path");
              path[i] = w;
              marked[w] = false;
          }
  
          // the path is a cycle if the start and end are adjacent, if this is
          // the case return the path
          for (int w : graph[path[m - 1]]) {
              if (w == path[0]) return path;
          }
  
          throw new IllegalArgumentException("path does not make a cycle");
      }
docs: Arrange the {@literal vertices} in a simple cyclic path. If the vertices
  do not form such a path an {@link IllegalArgumentException} is thrown.
  
  @param graph    a graph
  @param vertices set of vertices
  @return vertices in a walk which makes a cycle (first and last are the
  same)
  @throws IllegalArgumentException thrown if the vertices do not form a
  cycle
  @see org.openscience.cdk.ringsearch.RingSearch#isolated()