Results 1 to 7 of 7
Thread: Break Recursion
- 05-08-2010, 06:16 AM #1
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Break Recursion
Hey all. I'm not that new to Java, but it seems like a good topic for other beginners to learn as well. My question concerns recursive techniques for permutations. I lifted a code directly from a website (don't remember which) that permutes a string. My problem is, breaking out of the recursive function before it completes the cycle of all possibilities. I can use a public variable to store the value I am looking for, but the permutation continues until all possibilities are exhausted. How can I get out of the execution of that thread? Granted, I've tried break and returns, but it still seems to execute until the end.
I just hate using global variables, even though this is a one time use program, mainly for practice.Java Code:private void permuteString(String beginningString, String endingString) { if (endingString.length() <= 1) { compareTo(beginningString + endingString); } else for (int i = 0; i < endingString.length(); i++) { try { String newString = endingString.substring(0, i) + endingString.substring(i + 1); permuteString(beginningString + endingString.charAt(i), newString); } catch (StringIndexOutOfBoundsException exception) { exception.printStackTrace(); } } } //will compare referenced string to an array and set the value to a position in a public array if they're equal private void compareTo(String stringToCompare) { System.out.println(stringToCompare); }
- 05-09-2010, 03:35 AM #2
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
Generally if a recursive method is void, the only way you can really get it to break is if you add a global variable. just when you've found what you wanted, change some boolean like endRecursion or something to true, and then at the top of the method check that, and if true, break;
- 05-09-2010, 09:35 AM #3
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
But if you break out of a recursive function, wouldn't it return you to the previous call? For instance...
Function
--Function
----Function //found what i'm looking for.
--Function //returned back to this point
Which I guess with the global would be okay. I've already solved the problem I was looking at, using globals. I still had to look through every permutation, thus making it an inefficient algorithm. I guess encompassing the entire function from the first if statement in the permuteString function in a condition where one logical path would be the break, would be what you're suggesting? I can see that skipping that one recursion, returning to the previous and inevitably exiting the routine. Good call.
- 05-09-2010, 11:53 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
right, even with void methods you still need to "return" forgot about that.
But you could also put the check after the method call itself to return it, so as they return to the previous method (we don't call them functions in Java as far as i know) the next condition they reach is this one checking if we should still be doing our thing, and then will return out before it runs any code, and this should return out all of your methods. :)
however, even putting it around the entire method SHOULD work too because the new ones that you call will ust get returned instantly, but i think putting the condition after the method comes back would be more efficient? possibly? recursion can be confusing.
- 05-10-2010, 06:14 AM #5
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Sounds good to me. I'm not familiar with optimizing code patterns. I figure either way, it's a conditional statement either during the loop or after. So it should be the same in the processor. The only optimizing I know of, is to set the most likely condition first, in a conditional statement.
I forget about language specific jargon. I know a good bit of c++, java, javascript, php, basic, and sql. The only difference I know of in method and function, is that a method is a function used within a class. So, yeah, you are right. Java is always a class structure, so they'd always be methods. I forget these things sometimes. Forgive me.
- 05-10-2010, 06:39 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Breaking out of recursion can be a bit annoying with cases like yours, where the recursive call is within a loop. Like it was said before, you'd need a global variable and check it in each loop iteration:
Java Code:boolean finished = false; void recursive(some value) { if(base case) { finished = true; return; } for(...) { if(finished) return; recursive(new value); } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-14-2010, 04:47 AM #7
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Break to certain class
By Arez in forum New To JavaReplies: 4Last Post: 10-20-2008, 04:13 AM -
Break Clock
By BruenorBH in forum Advanced JavaReplies: 20Last Post: 09-12-2008, 05:27 AM -
How to use Break with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
How to use Break
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks