Stasis
Stasis

Reputation: 605

Breaking out of a recursion in java

The recursion is sort of a 'divide and conquer' style, it splits up while getting smaller (Tree data structure), and I want it to break completely if a violation is found, meaning break all the recursive paths, and return true. Is this possible?

Upvotes: 34

Views: 104637

Answers (11)

samdoj
samdoj

Reputation: 144

The best way to get out of a recursive loop when an error is encountered is to throw a runtime exception.

throw new RuntimeException("Help!  Somebody debug me!  I'm crashing!");

Of course, this kills your program, but you should employ range checking and algorithm analysis to make sure your recursion does not throw such an exception. One reason you might want to break out of a recursive algorithm is that you are running low on memory. Here, it is possible to determine how much memory your algorithm will use on the stack. If you are coding Java, say, compare that calculation to

getMemoryInfo.availMem().

Let's say you are using recursion to find n!. Your function looks something like this:

public long fact(int n)
{
    long val = 1;
    for (int i  = 1, i<=n,i++)
        return val *= fact(i);
    return val;
}

Before you run it, check that you have (number of bytes in a long, 8 in Java) * n bytes in memory to hold the whole stack. Basically, with range and error checking in advance of the recursive method/function, you shouldn't need to break out. Depending on your algorithm, however, you may need to signal to the whole stack that you're good to go. If that's the case, Tom's answer works.

Upvotes: 0

itb
itb

Reputation: 7

better to have a Boolean array

that way there is no global state

if(stop[0]) return;

Upvotes: -2

murphy
murphy

Reputation: 544

Maybe you want to avoid recursion and replace it with a stack. This gives you the control to break out of the operation, while being able to do something that resembles a recursive operation. In fact you just emulate recursion by yourself.

I found a nice explanation here: http://haacked.com/archive/2007/03/04/Replacing_Recursion_With_a_Stack.aspx/

Upvotes: 1

roho
roho

Reputation: 9

I was able to move out of all recursive calls using a global variable.

boolean skipRecursivePaths = false;
private callAgain(){

if(callAgain){
  if(getOutCompletely){

    skipRecursivePaths = true;
    }
    if(skipRecursivePaths){
    return;
    }
}

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114767

I'd recommend an exception handling. That makes clear, that the recursion was aborted because of some violation (or other exception):

public void outer() {
  try {
    int i = recurse(0);
  } catch (OuchException ouch) {
    // handle the exception here
  }
}

private int recurse(int i) throws OuchException {

    // for tree-like structures
    if (thisIsALeaf) {
       return i;
    }

    // the violation test
    if (itHurts)
       throw new OuchException("That really hurts");

    // we also can encapsulate other exceptions
    try {
       someMethod();
    } catch (Exception oops) {
       throw new OuchException(oops);
    }

    // recurse
    return recurse(i++);
}

Sure, it violates the initial requirement to return 'true' upon abortion. But I prefer a clean separation of return values and notification on abnormal behaviour.

Upvotes: 7

Emre K&#246;se
Emre K&#246;se

Reputation: 654

What you are asking is the definition of recursion.

At some point all recursive paths should break. Otherwise it will be an infinite recursion and stack overflow exception occurs.

So you should design the recursion function like that. An example binary search in a sorted array:

BinarySearch(A[0..N-1], value, low, high) {
       if (high < low)
           return -1 // not found
       mid = low + ((high - low) / 2)  // Note: not (low + high) / 2 !!
       if (A[mid] > value)
           return BinarySearch(A, value, low, mid-1)
       else if (A[mid] < value)
           return BinarySearch(A, value, mid+1, high)
       else
           return mid // found
}

Upvotes: 1

Tom
Tom

Reputation: 45104

You could return an error code, or modify some global variable so that each recursive instance knows to "kill itself".

Something of the sort.

int foo(bar){
     int to_the_next;

      if (go_recursive){
            to_the_next = foo(whisky_bar);

            if (to_the_next ==DIE) return DIE;
      }

      if (something_unexpected_happened) return DIE;

      process;//may include some other recursive calls, etc etc
}

Upvotes: 22

Kevin Day
Kevin Day

Reputation: 16383

No matter what you do, you are going to have to unwind the stack. This leaves two options:

  1. Magic return value (as described by one of the Toms)
  2. Throw an exception (as mentioned by thaggie)

If the case where you want things to die is rare, this may be one of those situations where throwing an exception might be a viable choice. And before everyone jumps down my throat on this, remember that one of the most important rules of programming is knowing when it's appropriate to break the rule.

As it turns out, I spent today evaluating the zxing library from google code. They actually use exception throws for a lot of control structures. My first impression when I saw it was horror. They were literally calling methods tens of thousands of times with different parameters until the method doesn't throw an exception.

This certainly looked like a performance problem, so I made some adjustments to change things over to using a magic return value. And you know what? The code was 40% faster when running in a debugger. But when I switched to non-debugging, the code was less than 1% faster.

I'm still not crazy about the decision to use exceptions for flow control in this case (I mean, the exceptions get thrown all the time). But it's certainly not worth my time to re-implement it given the almost immeasurable performance difference.

If your condition that triggers death of the iteration is not a fundamental part of the algorithm, using an exception may make your code a lot cleaner. For me, the point where I'd make this decision is if the entire recursion needs to be unwound, then I'd use an exception. IF only part of the recursion needs to be unwound, use a magic return value.

Upvotes: 48

Greg Case
Greg Case

Reputation: 47421

Unless recursive calls are being evaluated in parallel, you probably just need to add some logic to check the first recursive call's value prior to making the second (and subsequent, if not a binary tree structure) recursive call.

public abstract class Tree {

    protected abstract boolean headIsViolation();

    protected abstract boolean isLeaf();

    public Tree getLeft();

    public Tree getRight();

    // Recursive
    public boolean checkViolation() {
        if(this.isLeaf()) {
            return this.headIsViolation();
        }

        // If needed, you could pass some sort of 'calculation state'
        // object through the recursive calls and evaluate the violation
        // through that object if the current node is insufficient
        if(this.headIsViolation()) {
            // Terminate the recursion
            return true;
        }

        // Fortunately, Java short-circuits ||
        // So if the left child is in violation, the right child will
        // not even be checked
        return this.getLeft().checkViolation() || this.getRight().checkViolation();
    }
}

Upvotes: 0

Tom
Tom

Reputation: 44821

If it's a single thread doing the recursion you could throw an exception. Bit ugly though - kind of using an exception as a goto.

boolean myPublicWrapperMethod(...) {
    try {
        return myPrivateRecursiveFunction(...);
    } catch (MySpecificException e) {
        return true;
    } 
} 

A better approach would be to eliminate the recursion and use a Stack collection holding a class representing what would have been recursive state, iterate in a loop and just return true when you want.

Upvotes: 6

Joe Phillips
Joe Phillips

Reputation: 51110

You can do something similar by storing a variable that tracks whether the recursions should break or not. You'd have to check it every recursion unfortunately but you can do it.

Upvotes: 1

Related Questions