CS 261 Lab G - Anagram Solver

Due Wed Mar 13 at 9:00am

Overview

In this lab, you will practice using recursive backtracking to write a program that finds anagrams of provided phrases. Your program will look through a dictionary of words and find all combinations of words that contain the same letters as the given phrase. You will also report on the time required by your algorithm (and experiment with some possible optimizations!)

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]
  search completed in 0.464 seconds

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; I recommend you review that code for this lab!

This lab will be completed in pairs. Review the pair programming guidelines!

Objectives

Necessary Files

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

You should NOT modify these classes in any way---we will be testing your code using them as written, so any modifications will not be respected.

In addition, the zip file contains a number of different dictionaries files: some that we have used before, and a couple new ones.

Details

Submitting

Submit your AnagramFinder class 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.

Remember to fill out your lab partner evaluation!

Grading

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

Documentation

For your convenience, below is the documentation of the LetterSet class.



Class LetterSet

java.lang.Object
  LetterSet

public class LetterSet extends java.lang.Object

An inventory of letters, without order. Inventories contain the count of each letter. Based on program by Stuart Reges

Version:
Sp2013
Author:
Joel Ross

Constructor Summary
LetterSet()
          Constructs a new empty LetterSet object (all letters have count of 0)
LetterSet(String data)
          Constructs a new LetterSet object that represents the alphabetic letters in the given string, ignoring the case of letters and ignoring any non-alphabetic characters.
 
Method Summary
 LetterSet add(LetterSet other)
          Returns a new LetterSet object that represents the "sum" (the union) of this and the given LetterSet.
 int getCount(char c)
          Returns a count of how many of this letter are in the set, ignoring case.
 boolean isEmpty()
          Returns whether or not the set is empty (has size 0)
 void setCount(char c, int num)
          Sets the count of how many times this letter appears in the set, ignoring case.
 int size()
          Gives the "size" (the number of total characters) in the set.
 LetterSet sub(LetterSet other)
          Returns a new LetterSet object that represents the "difference" of this and the given LetterSet.
 String toString()
          Returns a String represention of the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LetterSet

public LetterSet()
Constructs a new empty LetterSet object (all letters have count of 0)


LetterSet

public LetterSet(String data)
Constructs a new LetterSet object that represents the alphabetic letters in the given string, ignoring the case of letters and ignoring any non-alphabetic characters.

Parameters:
data - The String used to populate the LetterSet
Method Detail

add

public LetterSet add(LetterSet other)
Returns a new LetterSet object that represents the "sum" (the union) of this and the given LetterSet. The new LetterSet has the sum of the counts of each individual letter

Parameters:
other - the LetterSet to add
Returns:
a new LetterSet with the sum of the letter counts

getCount

public int getCount(char c)
Returns a count of how many of this letter are in the set, ignoring case.

Parameters:
c - The character to get the count of
Returns:
The number of times the character appears in the set

isEmpty

public boolean isEmpty()
Returns whether or not the set is empty (has size 0)

Returns:
Whether the set is empty.

setCount

public void setCount(char c,
                     int num)
Sets the count of how many times this letter appears in the set, ignoring case.

Parameters:
c - The character to set the count of
num - The number of times the character should appear in the set

size

public int size()
Gives the "size" (the number of total characters) in the set.

Returns:
The size of the set

sub

public LetterSet sub(LetterSet other)
Returns a new LetterSet object that represents the "difference" of this and the given LetterSet. The new LetterSet has letter counts that are the current counts minus the couts of the other object. If any of the letter counts would be negative (so the other cannot be subtracted), this method returns null

Parameters:
other - the LetterSet to subtract
Returns:
A new LetterSet with the subtracted letter counts, or null if could not be subtracted

toString

public String toString()
Returns a String represention of the set. For example, an inventory of 4 a's, 1 b, 1 l and 1 m would be represented as "[aaaablm]".

Overrides:
toString in class java.lang.Object