View Single Post
Old Jan 18th, 2008, 10:15 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Recursive permutation of string

To prevent multiple occurrences of the same value, you have several weapons at your disposal. The main methods are:
  • Use "chalk" (in the form of a list, or hash table) to mark visited nodes. Then check the chalk each time you propose a visitation to another node.
  • Look more closesly at the structure of the tree to see exactly what makes each leaf unique. Use this in determining what values must change each permutation/recurse.

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 O(1), so it's not inefficient. However, it's not the most elegant solution for this problem.

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.
Sane is offline   Reply With Quote