Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 22nd, 2004, 08:40 AM
catz423 catz423 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 33 catz423 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old January 22nd, 2004, 11:59 AM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
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

Reply With Quote
  #3  
Old January 22nd, 2004, 12:32 PM
catz423 catz423 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 33 catz423 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 22nd, 2004, 05:21 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old January 22nd, 2004, 05:53 PM
catz423 catz423 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 33 catz423 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
is there a wayto do it without using pointers?? I am using C++ also.

Reply With Quote
  #6  
Old January 22nd, 2004, 06:41 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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++

Reply With Quote
  #7  
Old January 22nd, 2004, 06:51 PM
catz423 catz423 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 33 catz423 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old January 22nd, 2004, 07:17 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old January 24th, 2004, 11:03 AM
catz423 catz423 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 33 catz423 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old October 20th, 2009, 10:50 AM
jimbobstrangemo jimbobstrangemo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 3 jimbobstrangemo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #11  
Old October 20th, 2009, 09:46 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
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/

Reply With Quote
  #12  
Old November 27th, 2009, 10:26 AM
bbzippo bbzippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 1 bbzippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Anagram Algorithm

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap