Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 19th, 2008, 10:42 AM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Recursive permutation of string

So if you wanted to use the more 'standard' permute algorithm, as is the one I posted, but need the signature to be: void permute(char[] str, int low, int high). You will notice the address of "r" is not changing each recursion. All you need to do is make r a global variable or accessable by the class. That leaves you with the same format for the prototype: void permutate(char *s, int k, int n).

If your algorithm works, it may be more obfucated (or difficult to prove its correctness), but it's certainly more clever. I've never seen a permutation done by swapping characters repeatedly. Where did you get the idea from?
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 2:53 PM   #12
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Recursive permutation of string

Well, the idea for swapping characters came from the method signature constraint. After thinking about the problem for a long time, I decided that it couldn't be done without modifying the array in some way so that's what I did. It's not that complicated either; basically, I move each character in the string to the beginning and recurse through the rest of the string in the same way. Although since I have to create n copies of the string (where n is number of characters remaining) on each recursive call, it certainly isn't suitable for large strings.

Also, I still don't understand how your method works. Could you explain it a little more? Thanks.

Last edited by titaniumdecoy; Jan 19th, 2008 at 3:18 PM.
titaniumdecoy is offline   Reply With Quote
Old Jan 19th, 2008, 3:17 PM   #13
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Recursive permutation of string

Well the memory is still freed every time you back edge. So, at most you only ever have as many as n strings in memory. Which isn't bad considering there are even more than n number of permutations.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 3:29 PM   #14
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Recursive permutation of string

The first step is to look at tree that's required to be made...



Where each node denotes a character in the result string, r, and the current depth of the tree is the index in the string.

So, for example, if we follow down the right-most path.

r[0] = 'c'
r[1] = 'b'
r[2] = 'a'

Therefore, r = "cba".

But how do we make this tree? As I said, the key is to keep track of what nodes have already been visited. This is, in a way, the "chalk" approach that I said in my second post, and is the only other "fast" approach to permuting a string.

You keep track of what nodes have already been visited before it. That is my "new_s" variable, which represents visited nodes by using the null terminator, so that they will not be visited again. This could also be viewed as a deletion process, to form a "new string" to be permuted.

But what's most important to realize is that we are only keeping track of what nodes have been visited in a level above the current node. It does not include nodes on the same level in a different branch. That implies that we can't share chalk between nodes. Each node has its own set of memory for what nodes have already been visited. Thus, creating new strings is the only way this can be done efficiently, because the problem consists of subproblems of a dynamic nature.

Edit: So the character swapping seems to be a popular replacement for chalk. But in essence, the same thing is done, only its less obvious about its correctness. Note that new strings are still made in this example as well. http://www.cprogramming.com/challenges/permutesol.html But it's nothing to worry about. You get n strings of n length. That's <1MB of ram for a 1000 character string. And I don't see why you would ever permute something of that size.

Last edited by Sane; Jan 19th, 2008 at 3:52 PM.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 6:35 PM   #15
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Recursive permutation of string

so are you trying to reverse the string or to create every permutation?
mbd is offline   Reply With Quote
Old Jan 19th, 2008, 6:49 PM   #16
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Recursive permutation of string

There was a typo in his initial post. He said "reverse" when he meant "recurse". That is what threw the first 10 posts in an alternate direction.
Sane is offline   Reply With Quote
Old Jan 19th, 2008, 6:50 PM   #17
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Recursive permutation of string

Yeah... sorry 'bout that. I'll be more careful when I describe the problem next time.
titaniumdecoy is offline   Reply With Quote
Old Jan 20th, 2008, 1:00 PM   #18
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Recursive permutation of string

i wanted to tell you about the internals of the c++ next_permutation algorithm because it does some things which the recursive algorithm will not. it recognizes that "aaa" has only identical permutations. i found this article which explains permutations pretty well. i thought you might find it interesting as well.
mbd is offline   Reply With Quote
Old Feb 11th, 2008, 8:13 AM   #19
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 24
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
Re: Recursive permutation of string

This problem came into my way many times. For such, first algorithm designing should be done it helps...
__________________
[Pushkaraj]


Imagination is more important than knowledge – Albert Einstein
pushkarajthorat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC