Word Ladder
stone Atone aLone Clone clonS cOons coNns conEs coneY Money |
Word ladders were invented by Lewis Carroll in 1878, the author of Alice in Wonderland.
A ladder is a sequence of words that starts at the starting word, ends at the ending word,
In a word ladder puzzle you have to change one word into another by altering a single letter at
each step. Each word in the ladder must be a valid English word, and
must have the same length. For example, to
turn stone into money, one possible ladder
is given on the left.
Many ladder puzzles have more than one possible solutions. Your program must determine a shortest word ladder. Another path from stone to money is stone store shore chore choke choky cooky cooey coney money |
Your program will accept starting and ending words from the input file called "input.txt". Then, you read the dictionary file from the course web page at
http://www.andrew.cmu.edu/course/15-121/dictionary.txt
using the URL class and store it in a HashSet. Finally, you build a word ladder between starting and ending words
There are several ways to solve this problem. One simple method involves using stacks and queues. The algorithm (that you must implement) works as it follows
Get the starting word and search through the dictionary to find all words that are one letter different. Create stacks for each of these words, containing the starting word (pushed first) and the word that is one letter different. Enqueue each of these stacks into a queue. This will create a queue of stacks! Then dequeue the first item (which is a stack) from the queue, look at its top word and compare it with the ending word. If they equal, you are done - this stack contains the ladder. Otherwise, you find all the words one letter different from it. For each of these new words create a deep copy of the stack and push each word onto the stack. Then enqueue those stacks to the queue. And so on. You terminate the process when you reach the ending word or the queue is empty.You have to keep the track of used words! Otherwise an infinite loop occurs.
----------------------------------------- | scart | start | swart | smalt | smarm | | smart | smart | smart | smart | smart | -----------------------------------------
Now dequeue the front and find all words one letter different from the top word scart. This will spawn seven stacks:
--------------------------------------------------------- | scant | scatt | scare | scarf | scarp | scars | scary | | scart | scart | scart | scart | scart | scart | scart | | smart | smart | smart | smart | smart | smart | smart | ---------------------------------------------------------
which we enqueue to the queue. The queue size now is 11. Again dequeue the front and find all words one letter different from the top word start. This will spawn four stacks:
--------------------------------- | sturt | stare | stark | stars | | start | start | start | start | | smart | smart | smart | smart | ---------------------------------
Add them to the queue. The queue size now is 14. Repeat the procedure until either you find the ending word or such a word ladder does not exist. Make sure you do not run into an infinite loop!
http://www.andrew.cmu.edu/course/15-121/dictionary.txt.
The dictionary file contains exactly one word per line. However, the dictionary might contains
empty lines.
The number of lines in the dictionary is not specified.
For testing purposes we provide you with the input file and the expected output.
public ArrayList‹Stack‹String>> buildLadder(String start, String end, int ladderLength)
ladderLength,
not necessarily shortest. For example. there are only two shortest ladders between sail
and ruin:
sail, rail, rain, ruin sail, sain, rain, ruin
sail, mail, main, rain, ruin sail, pail, pain, rain, ruin sail, bail, rail, rain, ruin sail, wail, rail, rain, ruin sail, sain, rain, rein, ruin sail, jail, rail, rain, ruin sail, tail, rail, rain, ruin
-Xmx and -Xms. Here is an example:
java -Xmx500m MainWordLadderDriver
Exhaustive examination of all possibilities produces the entire solution space for the problem, which could be exceptionally larger than your computer resources. Therefore, you will get a full credit if your implementation finds all ladders for the following four-letter words:
sail ruin slow fast monk perl
Do the extra credit part but with NO extra memory. Think of some kind of heuristic that will allow you to decrease the solution space.
You FTP the following java files
Do not submit anything else like zip files, folders, desktops, class files. Failure to do so will result in 10 points penalty.
Your assignment will be graded first by compiling and testing it. After that, we will read your code to determine whether appropriate method/classes were used, as well as judge the overall style and modularity of your code. Points will be deducted for poor design decisions and un-commented, or un- readable code.
Here is the point breakdown:
Victor S.Adamchik, CMU, 2009