CS 261 Lab G - Anagram Finder

Due Wed Mar 12 at 9:00am

Overview

In this lab, you will practice using recursive backtracking to write a program that finds anagrams of a provided phrase. These anagrams can be combinations of words--in effect, the words of a sentence that would be an anagram of the given sentence. Your program will look through a dictionary of words and find all combinations of words that contain the same letters as the given phrase.

An example output of your progam should look like the following:

Welcome to the anagram finder!
First, enter the name of a dictionary file to use: words4000.txt

Enter phrase to anagram-ify (return to quit): Hello World!
Max words to include (0 for no max!): 0
[hell, old, row]
[hell, row, old]
[hello, world]
[hold, or, well]
[hold, roll, we]
[hold, we, roll]
[hold, well, or]
[how, led, roll]
[how, roll, led]
[led, how, roll]
[led, roll, how]
[led, roll, who]
[led, who, roll]
[old, hell, row]
[old, row, hell]
[or, hold, well]
[or, well, hold]
[roll, hold, we]
[roll, how, led]
[roll, led, how]
[roll, led, who]
[roll, we, hold]
[roll, who, led]
[row, hell, old]
[row, old, hell]
[we, hold, roll]
[we, roll, hold]
[well, hold, or]
[well, or, hold]
[who, led, roll]
[who, roll, led]
[world, hello]
  Anagrams generated in 0.464 seconds

This phrase ("Hello World") happened to produce a lot of anagrams that are three words long; longer phrases or a bigger dictionary will produce more variety. However, you can use this output as a sample for testing.

You will be required to use recursive backtracking to complete this lab. Note that your solution will be very similar to the solution to the n-Queens problem that we discussed in class; I recommend you review that code (available on Moodle) for this lab!

This lab will be completed in pairs. Remember to review the pair programming guidelines before you get started!

Objectives

Necessary Files

You will need to download the copy of the AnagramLab.zip file. This file contains a number of classes and files you will need to import into Eclipse:

Assignment Details

For this lab, you will be making a single class: AnagramFinder.java. In order to work with the provided tester, your class will need to have the following public interface:

And that's it! Your task is to complete the AnagramFinder class so that these two methods are supported. You are welcome to use as many or as few other helper methods as you like (I suggest at least one: the recursive backtracking method!)

Backtracking

Your recursive backtracking method will work very similar to the method used in the n-Queens problem (and that you are using for your Sudoku game).

Once you have everything working, try making anagrams of a few choice phrases! For example, what are anagrams of your name?

Optimization [Optional]

Recursive backtracking is, in general, highly inefficient because it is a brute force technique that checks every possibility. But there are still things you can do to make sure that your solution is as efficient as it can be. For example, you can be careful not to compute something twice if you don’t need to (e.g., similar to how we fixed the Fibonacci calculation). Or you can stop exploring solution paths that you know will never work out. To this end, once you have finished your recursive backtracker, you should consider and explore at least one possible way to optimize the efficiency of your program:

  1. One common way of optimizing algorithms is to precompute values or data that we may need later. You might compute LetterSets for all of your dictionary words in advance, so that you don't need to do it again every time you want to consider a word.
  2. Similarly, for any given phrase, you can reduce the dictionary to to a smaller dictionary of “relevant” words. A word is relevant if it can be constructed from the letters of a given phrase. Only a fraction of the dictionary will be relevant to any given phrase, so reducing the dictionary before you begin the recursive search will allow you to speed up the searches that happen on each recursive invocation. You can do this once before the recursion begins, or once during each recursive call--the later will involve some overhead for each call, but the reduction in search space may outweight the increased cost of the computation. You might try both options and see which is faster!
  3. You might also be able to "trim" down your solution space by ordering your dictionary words by size; this way if you get to a word that is longer than the number of letters you have left, you do not have to keep searching down that branch.

For this lab, you must include at least two (1) of the above optimizations, but can earn extra credit for including more. You should add these optimizations after you have implemented a working backtracking algorithm.

Extensions

Submitting

Make sure your name is in a class comment at the top of your AnagramFinder class, and upload it to the LabG submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at the start of class on the day after the lab.

After you have submitted your solution, log onto Moodle and submit the Lab G Partner Evaluation. Both partners need to submit evaluations.

Grading

This assignment will be graded based on approximately the following criteria:

  • You have implemented an AnagramFinder that works with the provided tester [15%]
  • Your AnagramFinder uses recursive backtracking to print all anagrams for a given phrase [35%]
  • Your AnagramFinder respects the provided maximum number of words [15%]
  • Your AnagramFinder reports the amount of time spent searching for anagrams [5%]
  • Your AnagramFinder includes and documents the required optimizations [25%]
  • You completed your lab partner evaluation [5%]