out of memory - Depth First Search is running into java.lang.OutOfMemoryError -
out of memory - Depth First Search is running into java.lang.OutOfMemoryError -
i'm running java.lang.outofmemoryerror when utilize depth first search on undirected graph determine if node1 reachable node2. code listed below. (some irrelevant details intended removed.)
//definition of nodes , edges set<node> nodes = new hashset<node>(); map<node, set<node>> edges = new hashmap<node, set<node>>(); //method determine if node1 reachable node2 public boolean isreachable(int p1, methodnode m1, classnode c1, int p2, methodnode m2, classnode c2) { node node1 = new node (p1,m1,c1); node node2 = new node (p2,m2,c2); stack<node> stack = new stack<node>(); stack.push(node1); while(!stack.isempty()){ node current = null; current = stack.pop(); //test current node, if kid nodes contains node2, homecoming true //otherwise, force kid nodes stack for(final node temp : edges.get(current)){ if(temp.equals(node2)){ homecoming true; } else{ stack.push(temp); } } } homecoming false; }
i guess there must infinite calls run out of memory, can't locate it.
this problem
for (final node temp : edges.get(current)){ if(temp.equals(node2)){ homecoming true; } else { stack.push(temp); } }
this force neighbors on stack, take 1 of them, force neighbors on stack (including 1 started at), , on advertisement infinitum. need mark nodes visited doesn't happen. cases won't getting infinite loop when either node you're looking straight adjacent node start @ or if nodes on path set onto stack in right order pure chance.
java out-of-memory
Comments
Post a Comment