java - StackOverflow error: How can I avoid it or turn this DFS into an iterative one? -
i'm using depth first search maze generation.
the adjacency matrix of m*n vertices traversed in random order using dfs, i'm interested in generating random route.
the thing works fine reduced number of vertices, i'm getting stackoverflow exception when using with
graph thegraph = new graph(1000,1000);
questions: a)how can change recursive calls iterative ones using stack?
b)is there way assign more memory method call stack?
class ij { int i; int j; ij (int i,int j){ = this.i; j= this.j; } } class graph { int m; int n; int adjacencymatrix[][]; arraylist <ij> orderofvisits; graph(int m,int n){ this.m=m; this.n=n; adjacencymatrix=new int[m][n]; (int i=0; i<m; i++) (int j=0;j<n;j++){ adjacencymatrix[i][j]=-1; //mark vertices not visited } orderofvisits = new arraylist<ij>(); } void dfs(int i, int j){ // i,j identifies 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<0)) northvalid=true; isouth=i+1; if(!((isouth)>=m)) southvalid=true; jeast=j+1; if(!((jeast)>=n)) eastvalid=true; jwest= j-1; if (!(jwest<0)) westvalid=true; if (adjacencymatrix[i][j]==-1){ //if vertex unvisited adjacencymatrix[i][j]=0; //mark vertex visited ij ij = new ij(i,j); orderofvisits.add(ij); //add vertex visit list system.out.println("visit i,j: " + +" " +j); double lottery = math.random(); (int rows=i; rows<m; rows++) (int cols=j; cols<n; cols++){ if (lottery>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<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>=0.25d)&&(lottery<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>=0.5d)&&(lottery<=0.75d)) { if(eastvalid){ dfs(i, jeast); } if(westvalid){ dfs(i,jwest); } if(southvalid){ dfs(isouth,j); } if(northvalid){ dfs(inorth,j); } } } } //end nested } //end dfs // } public class main { /** * @param args command line arguments */ public static void main(string[] args) { // todo code application logic here graph thegraph = new graph(1000,1000); thegraph.dfs(0,0); } }
some pseudo code:
stack<ij> nodestovisit; nodestovisit.push(new ij(0, 1)); nodestovisit.push(new ij(1, 0)); while (nodestovisit.count > 0) { var ij = nodestovisit.pop(); if (visited ij) continue; .... mark ij visited ... check north/south/east/west validity list<ij> directions = new list<ij>(); if (cangonorth) directions.add(new ij(inorth, j)); if (cangosouth) directions.add(new ij(isouth, j)); if (cangoeast) directions.add(new ij(i, jeast)); if (cangowest) directions.add(new ij(i, jwest)); ... randomize list foreach (direction in directions) nodestovisit.push(direction); }
basically:
- push possible directions on stack in random order
- pick top item
- go there
- repeat until stack empty (nor more nodes visited)
i don't think increasing stack limit solution problem.
Comments
Post a Comment