title: StackOverflow error: How can I avoid it or turn this DFS into an iterative one?
body: <p>I'm using Depth First Search for maze generation.</p>
  
  <p>The adjacency matrix of M*N vertices is traversed in a random order using DFS, I'm only interested in generating a random route.</p>
  
  <p>The thing works fine with a reduced number of vertices, but I'm getting a StackOverflow Exception when using it with</p>
  
  <pre><code> Graph theGraph = new Graph(1000,1000);
  </code></pre>
  
  <p><strong>Questions:</strong>
  a)How can I change this recursive calls to iterative ones using a stack? </p>
  
  <p>b)Is there a way to assign more memory to the method call stack?</p>
  
  <pre><code>class IJ {
  
          int i;
          int j;
  
          IJ (int i,int j){
              i = this.i;
              j= this.j;
  
          }
  
  }
  
  
  class Graph {
  
      int M;
      int N;
  
      int adjacencyMatrix[][];
  
      ArrayList &lt;IJ&gt; orderOfVisits;
  
      Graph(int M,int N){
  
          this.M=M;
          this.N=N;
          adjacencyMatrix=new int[M][N];
  
          for (int i=0; i&lt;M; i++)
              for (int j=0;j&lt;N;j++){
                      adjacencyMatrix[i][j]=-1; //mark all vertices as not visited
              }
  
          orderOfVisits = new ArrayList&lt;IJ&gt;();
  
      }
  
   void DFS(int i, int j){ // i,j identifies the vertex
  
       boolean northValid= false;
       boolean southValid= false;
       boolean eastValid = false;
       boolean westValid = false;
  
  
       int iNorth, jNorth;
       int iSouth, jSouth;
       int iEast, jEast;
       int iWest, jWest;
  
       iNorth=i-1;
       if (!(iNorth&lt;0)) northValid=true;
  
       iSouth=i+1;
       if(!((iSouth)&gt;=M)) southValid=true;
  
       jEast=j+1;
       if(!((jEast)&gt;=N)) eastValid=true;
  
       jWest= j-1;
       if (!(jWest&lt;0)) westValid=true;
  
  
      if (adjacencyMatrix[i][j]==-1){ //if the vertex is unvisited
  
          adjacencyMatrix[i][j]=0; //mark the vertex as visited
          IJ ij = new IJ(i,j);
          orderOfVisits.add(ij); //add the vertex to the visit list
          System.out.println("Visit i,j: " + i +" " +j);
  
  
  
          Double lottery = Math.random();
  
         for (int rows=i; rows&lt;M; rows++)
             for (int cols=j; cols&lt;N; cols++){
  
  
          if (lottery&gt;0.75D){
              if(northValid)
              {
                  DFS(iNorth,j);
              }
  
              if(southValid){
                  DFS(iSouth,j);
              }
  
              if(eastValid){
                  DFS(i, jEast);
              }
  
              if(westValid){
                  DFS(i,jWest);
              }
  
  
          }
  
         else if (lottery&lt;0.25D)
         {
  
              if(westValid){
                  DFS(i,jWest);
              }
  
               if(eastValid){
                  DFS(i, jEast);
              }
  
               if(southValid){
                  DFS(iSouth,j);
              }
  
              if(northValid)
              {
                  DFS(iNorth,j);
              }
  
         }
  
         else if ((lottery&gt;=0.25D)&amp;&amp;(lottery&lt;0.5D))
         {
  
               if(southValid){
                  DFS(iSouth,j);
              }
  
               if(eastValid){
                  DFS(i, jEast);
              }
  
              if(westValid){
                  DFS(i,jWest);
              }
  
              if(northValid){
                  DFS(iNorth,j);
              }
  
         }
  
          else if ((lottery&gt;=0.5D)&amp;&amp;(lottery&lt;=0.75D))
         {
  
              if(eastValid){
                  DFS(i, jEast);
              }
  
              if(westValid){
                  DFS(i,jWest);
              }
  
              if(southValid){
                  DFS(iSouth,j);
              }
  
              if(northValid){
                  DFS(iNorth,j);
              }
  
         }
  
      }
  
   } //end nested for
  
  } //end DFS
  
  //
  }
  
  
  public class Main {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          // TODO code application logic here
  
  
  
      Graph theGraph = new Graph(1000,1000);
      theGraph.DFS(0,0);
  
  
  
      }
  
  }
  </code></pre>