![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Recursive permutation of string
This is not homework. It is an exercise out of a textbook I used last year which I am reviewing. I am trying to write a recursive algorithm to reverse a character array. I wrote the code below which works, except that it prints out each permutation multiple times, and I can't figure out how to prevent that from happening. Please advise...
Java Syntax (Toggle Plain Text)
Output: abc abc abc acb acb bac bac bac bca bca cba cba cba cab cab |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Recursive permutation of string
To prevent multiple occurrences of the same value, you have several weapons at your disposal. The main methods are:
The first one is a a thoughtless and powerful fix. It's also generally applicable to many similar situations. And the hash table has an amortized cost of The second one is more "proper", but I don't understand the problem enough to recommend anything less broad. It seems perfectly possible, and more along the lines of what you're asking for an answer. Could you explain the question further? Last edited by Sane; Jan 18th, 2008 at 10:33 PM. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Re: Recursive permutation of string
I believe there is a fairly straightforward way to prevent each permutation from being multiple times; e.g., the "second way." That is what I am looking for.
Also, you will notice that in each loop within the method I make a copy of the array (via the charArrayWithSwappedChars method). I spent a long time thinking about it but I don't believe there is any way to accomplish this task without making copies. If there is, I would certainly be interested to hear it. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Recursive permutation of string
Well could you explain the question? I'm trying to figure it out from your output, but it seems random how the 2nd letter is shifted to the 3rd, then to the 1st. And it continues on in this seemingly arbitrary pattern. Problem statement?
You're not just trying to permutate a string, right...? |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: Recursive permutation of string
Actually, yes, I am
![]() My approach makes sense to me, logically anyway. I take the string, swap each character into the low index, and repeat on the next piece of the string for each of these (low+1). I can see why each permutation gets printed multiple times but can't think of how to prevent this. I'm just looking for a suggestion on how to get it working. EDIT: I just looked at my first post and it says "reverse a character array" rather than "permute a character array". |
|
|
|
|
|
#6 |
|
Expert Programmer
|
Re: Recursive permutation of string
I fixed it:
Java Syntax (Toggle Plain Text)
Is there a way to do this without making a copy of the char array each loop iteration? Last edited by titaniumdecoy; Jan 18th, 2008 at 11:55 PM. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Recursive permutation of string
I was very much confused, because you said "reverse" instead of "recurse" in your initial post at one point. So I was assuming there was some complicated problem statement missing. Okay.
The key is to keep track of what you haven't added. See how the tree stems apart, and what data you want to keep track of. This algorithm can be proven qualitatively by drawing the trees. It can be proven mathematically by subproblems and dynamic programming. I'll leave that excercise up to you. Edit: In reference to your question about doing it without copying new strings: I do not believe so. This is due to the nature of dynamic programming and subproblems. If you look at the tree the reason becomes obvious. But there might be some fancy ways. Such as brute forcing it iteratively, and checking for invalid entries. So I doubt any other solutions would be efficient in any case. But I am not sure. Here it is in C: C Syntax (Toggle Plain Text)
Last edited by Sane; Jan 19th, 2008 at 12:17 AM. |
|
|
|
|
|
#8 |
|
Expert Programmer
|
Re: Recursive permutation of string
I am having trouble understanding how your code works but I will figure it out eventually. How does its runtime compare to mine? (Thanks, by the way.)
I really should have specified this in the beginning, but the (Java) method signature is supposed to be void permute(char[] str, int low, int high).Is it possible to accomplish this without copying the string on each iteration of the loop? |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Recursive permutation of string
Does it work perfectly with your revision? If it does, then ours would both perform the same. It looks like we did the same thing except you swap characters in the array, rather than marking and deleting them as I did.
About the string copying each iteration, I added an edit to my previous post. Heading to bed. Good luck. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
Re: Recursive permutation of string
Thanks, Sane. I appreciate your help with this problem.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| An Attempt at a DBMS | grimpirate | PHP | 8 | Apr 17th, 2007 1:01 PM |
| Throwing an exception when using string constructor | csrocker101 | C# | 3 | Apr 8th, 2007 2:04 PM |
| Help with breaking apart a string | csrocker101 | C# | 6 | Apr 6th, 2007 7:50 AM |
| Permutation | Mcoy | C++ | 5 | Mar 15th, 2006 3:22 AM |
| Recursive Permutation | lhk_mgtf2008 | Visual Basic | 2 | Jul 6th, 2005 11:18 PM |