CS 5541
Program 1 - Search
Due October 8, 2009
40 points


Search, as we have discussed in class, is a key process in artificial intelligence. In this program you will get some experience with search and with phrasing a problem for search. We are going to implement breadth-first search, depth-first search and A* search for two different problems.

In the code found at http://www.d.umn.edu/~rmaclin/cs5541/Programs/cs5541_Program1.zip I have defined two interfaces, one for an overall Problem and one for a state of that Problem, ProblemState. In addition, I have defined an example of the N Queens problem using these interfaces. Your job will be to implement a similar description of the N Puzzle problem that can work for any square N puzzle with one open spot (so a 3x3 N Puzzle with tiles numbered 1-8 and an open spot, a 4x4 N Puzzle with tiles numbered 1-15 and an open spot, etc.).

In addition, you will need to implement a version of breadth-first search, depth-first search and A* search that works with both problem representations.

Some Implementation Details

N Queens - The N Queens representation should work as is, but you will need to add to the representation to add parent links (see below).

N Puzzle - Each state of the N puzzle problem should be an individual state of all of the tiles. The successors of a state are all the states that can be reached by moving a tile up, down, left or right (when legal) into the open spot. As part of implementing the N Puzzle problem you should implement a method to randomly permute the initial tile positions. One way to do this is to start with the correct board and to perform some large number of randomly selected moves.

Randomness - Java has several random number generator options. One simple way to get random numbers is to use the Math.random() function. To get a random number X where MinValue <= X < MaxValue is to perform the following computation

Math.random * (MaxValue - MinValue) + MinValue .

Similarly, one can get a random integer X where MinValue <= X < MaxValue (assuming MinValue and MaxValue are integers) with the following computation

(int) (Math.floor(Math.random * (MaxValue - MinValue) + MinValue))

Your search methods - You should implement only one version of each search method, that should work on both of the problems we will use for testing.

Breadth-First Search - You should implement a breadth first search as discussed in class and are welcome to use built-in Java structures (queues, linked lists, etc.). In addition to the regular Breadth-First Search result of the final state you should print out the set of states that lead to the goal (but only those states on the actual path to the goal). This means that for each state you will need to keep track of its parent state.

Depth-First Search - Similar to Breadth-First Search, you should implement Depth-First Search as discussed in class, but make sure to also print out the path from the start state to the goal that is found during the search.

A* Search - Implement A* search and make sure to test it on the N Puzzle Problem, using both heuristics h1 and h2 suggested in the Russell and Norvig book. Again you should print out the path from start to goal that is found in your search.


You should test breadth-first search and depth-first search on N Queens problems of size 4, 5, 6 and 8. In addition to showing the path to the solution you should also print out how many states made it to the closed list.

Test breadth-first search and depth-first search on two 3 Puzzle Problems. Make sure to test them (and A* search) on the same problem. This can be achieved in testing by cloning the start state and using it again for the next problem. Also test A* search using both heuristics on these two problems. For each of these cases report the solution found and the number of states on the closed list when the problem is solved.

In addition, run additional tests (I would recommend at least 10 total tests) for 3 and 4 Puzzle Problems using both heuristics of A* search and report the average number of states on the closed list. Show your results in a bar chart by problem and calculate the mean and standard deviation of the number of states on the closed list.

What To Hand In

Hand in clean, nicely commented copies of all of your code (make sure to include the class code so we can tell if anything is changed). In addition, hand in results for all of your tests. You should also write up a summary of your code and all of the test results with any conclusions you might reach. In addition, email a copy of your code (zipped) to the TA.

Extra Credit (5 points)

Using a map of Minnesota or Wisconsin (or both) create a table of at least 25 towns or cities and driving distances between those cities connected by roads. Use your date to represent a problem (where you choose a start and end city). Run BFS, DBS and A* on your data (you should chose multiple start/end state combinations).