title: How to get all the solutions of topological sorting
body: <p>Hi everyone I was trying to solve this problem <a href="http://uva.onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=813" rel="nofollow">http://uva.onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=813</a> and I realized that it wants to get all the solutions of topological sorting problem, I know how to get only one possible solution and this is my code <a href="http://ideone.com/IiQxiu" rel="nofollow">http://ideone.com/IiQxiu</a></p>
  
  <pre><code>static ArrayList&lt;Integer&gt; [] arr;  
  static int visited [];
  static Stack&lt;Integer&gt; a = new Stack&lt;Integer&gt;();
  static boolean flag=false;
  
  public static void graphcheck(int node){  //method to check if there is a cycle in the graph
      visited[node] = 2;
      for(int i=0;i&lt;arr[node].size();i++){
          int u =arr[node].get(i);
          if(visited[u]==0){
              graphcheck(u);
          }else if(visited[u]==2){
                  flag=true;
                  return; 
              }
      }
      visited[node] = 1;
  }
  
  public static void dfs2(int node){            //method to get one possible topological sort which I want to extend to get all posibilites
      visited[node] = 1;
      for(int i=0;i&lt;arr[node].size();i++){
          int u =arr[node].get(i);
          if(visited[u]==0){
              dfs2(u);
          }   
      }
      a.push(node);
  }
  
  public static void main(String[] args) throws NumberFormatException, IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int tc = Integer.parseInt(br.readLine());
      for(int k=0;k&lt;tc;k++){
          br.readLine();
  
          String h[]= br.readLine().split(" ");
          int n= h.length;
          arr=new ArrayList[n];
          visited = new int[n];
          for( int i = 0; i &lt; n; i++) {
              arr[ i] = new ArrayList&lt;Integer&gt;();
          }
          String q[]=br.readLine().split(" ");
          int y=q.length;
          for(int i=0;i&lt;y;i++){
              int x=0;
              int z=0;
              for(int j=0;j&lt;n;j++){
                  if(q[i].charAt(0)==h[j].charAt(0)){
                      x=j;
                  }else if(q[i].charAt(2)==h[j].charAt(0)){
                      z=j;
                  }
              }
              if(q[i].charAt(1)=='&lt;'){
                          arr[x].add(z);
              }
          }
          for(int i=0;i&lt;n;i++){
              if(visited[i]==0)
                  graphcheck(i);
          }
          if(flag){
              System.out.println("NO");
          }else{
          a.clear();
          Arrays.fill(visited, 0);
          for(int i=0;i&lt;n;i++){
              if(visited[i]==0){
                  dfs2(i);
              }   
          }
          int z= a.size();
          for(int i=0;i&lt;z;i++){
              int x =a.pop();
              System.out.print(h[x]+" ");
          }
          System.out.println();
      }
  
  
  }
  }
  </code></pre>