The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Software Design
|
Anagram Algorithm
Discuss Anagram Algorithm in the Software Design forum on Dev Shed. Anagram Algorithm Software design forum discussing design principles and non-language specific algorithms. Get help with logic, algebraic, or relational concepts.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 22nd, 2004, 08:40 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Anagram Algorithm
I am trying to create an anagram algorithm. It must be a recursive backtracking algorithm. For example if the input is "raziber" the ouput would be "bizarre" and "brazier". If anyone can offer me some help in creating this algorithm it would be greatly appreciated.
|

January 22nd, 2004, 11:59 AM
|
 |
Doggie
|
|
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751
  
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 11
|
|
|
First thing you need is a dictionary files of legal words to use.
Then just loop through all the combinations the letters can make up. Check that against the dictionary. You could have it cancel the loop once a word is started that no word in the dictionary starts with. (ie: start with 'rz', no words found, skip rest of combinations starting with 'rz')
Make sense?
You'll have to google to see if you can find a free dictionary file. Good luck.
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare
|

January 22nd, 2004, 12:32 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
yea i already have the dictionary part all setup using a dlb trie, as of right now a user can enter a phrase and my program will say if it is a Valid word, a Valid prefix, or Invalid. I just don't know the best way to go about writing an algorithm to go through each combination of the phrase using recursive backtracking.
|

January 22nd, 2004, 05:21 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
Code:
<?
class backtrack {
# constructor
function backtrack($input_array)
{
// the array you want to backtrack and get each configuration of
$this->_array = $input_array;
// the current configuration of elements
$this->configuration = array();
// if $this->taken[$pos] == false, $this->_array[$pos] is yet to be used
// in the current configuration
$this->taken = array();
// set all as NOT taken
for ($i=0; $i<count($this->_array); $i++) $this->taken[$i] = false;
}
# recursive function:
# fill each position ($pos) in configuration by trying each element from $this->_array
function do_backtrack($pos)
{
// if configuration is complete
if ($pos == count($this->_array))
{
$this->test_configuration();
return;
}
// try fill each position in the configuration with each element in $_array.
// but we can only use it if it's not in the current configuration (ie its 'taken')
for ($i=0; $i<count($this->_array); $i++)
{
if ($this->taken[$i]) continue;
$this->configuration[$pos] = $this->_array[$i]; // take it
$this->taken[$i] = true; // set it as now taken
$this->do_backtrack($pos+1); // continue backtracking to get rest of configuration
$this->taken[$i] = false; // set as untaken as we try next configuration
}
}
# we can do something with configuration here since configuration is done
# but we currently just print it out
function test_configuration()
{
for ($i=0; $i<count($this->_array); $i++)
print $this->configuration[$i].' ';
print '<br>';
}
}
$btrack = new backtrack(array('1','3','5'));
$btrack->do_backtrack(0);
Last edited by lazy_yogi : January 22nd, 2004 at 06:39 PM.
|

January 22nd, 2004, 05:53 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
is there a wayto do it without using pointers?? I am using C++ also.
|

January 22nd, 2004, 06:41 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
|
This doesnt use pionters.
And it should be a straight conversion over to c++.
Just declare 3 vector objects in the class which I created in the constructor. And declare the variables such as $i. Otherwise its a straight conversion to c++
|

January 22nd, 2004, 06:51 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I'm sorry i am kinda confused with the sintax, "this->array" that is a vector? I am really not good with vectors, I have only used them once before, how do they differ from arrays? I really appreciate all your help here.
|

January 22nd, 2004, 07:17 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
|
$this->_array
Anything infront of '$this' is a class varible
just as in c++
I've declared '_array' as the name for the class variable that holds the initial array
Learn to use vectors. They do all the work for you in c++. They hold the length of the vector so you don't have to iterate throug it each time. It also adds elements for you so you never have to resize the vector yourself manually. Has some other nice features like searching and so on.
Seach the web for how to use a vector in c++ and ask for help in the c/c++ forum if you still don't understand it.
|

January 24th, 2004, 11:03 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I am trying to create an Anagram finder. ok this is what I came up with so far, keep in mind i have a working dictionary dlb trie that tells me if a phrase is a Valid Prefix, Valid Word, or Invalid. So basically I just have to come up with this recursive bactracking algorithm to find the anagrams. usedChars is 0 if char was not used and 1 if char was used, origWord is phrase taken from user, currWord is phrase being built to form anagram. I am getting some funny output. Any suggestions would be appreciated.
code:
void findAnagram(int usedChars[], char* origWord[], char* currWord[]){
if(Valid Prefix){
for(i=0; i<strlen(origWord); i++){
//test to see if char was used already
if(usedChars[i] == 0){
currWord[i] = origWord[i]; //append char to end of curr
usedChars[i] = 1; //set char to 1 saying it was used
//recurse
find(usedChars, origWord, currWord);
//remove last char from curr
i--;
currWord[i] = '\0';
//set char to 0 saying it was unused
usedChars[i] = 0;
}//end if
}//end for
}//end if
else if(currWord == Valid word){
cout<<anagram = <<currWord<<endl;
}//else if
|

October 20th, 2009, 10:50 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 3
Time spent in forums: 38 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by catz423 I am trying to create an anagram algorithm. It must be a recursive backtracking algorithm. For example if the input is "raziber" the ouput would be "bizarre" and "brazier". If anyone can offer me some help in creating this algorithm it would be greatly appreciated. |
Hey I'm doing a very similar project and was wondering where you found your dictionary, and if it's comprehensive (the best i've been able to find so far are word lists of about 2/3k words which omit some pretty important ones!)
muchos gracias hombre
|

October 20th, 2009, 09:46 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
|
Reviving a five year old thread was pointless.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/
|

November 27th, 2009, 10:26 AM
|
|
Registered User
|
|
Join Date: Nov 2009
Posts: 1
Time spent in forums: 12 m 20 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by jimbobstrangemo Hey I'm doing a very similar project and was wondering where you found your dictionary, and if it's comprehensive (the best i've been able to find so far are word lists of about 2/3k words which omit some pretty important ones!)
muchos gracias hombre |
Sorry, I can't post URLs.
Try google search for "owl-lwl.txt". It's an official Scrabble tournament word list.
I used the OWL word list as one of the sources for my Xworder.com project.
You can also find various versions of the linux.words file which is used for spellcheck in Linux.
Good luck,
- bbzippo.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|