hw1-soln by huanghengdong


									I. Map coloring (20 points)
Express the following problem as a search state problem:

“You have to color the various regions of a planar map (for example, the map of the 48
contiguous states of the United States) using only four colors (Red, Blue, Yellow, and
Green), in such a way that no two adjacent regions have the same color.”

Give the general description of a state, the initial state, the goal test, and the successor function.
For the successor function it is not necessary that you write every possible “state  state”
combination, but you should make it clear how one would derive the successor states of any
given state. Note that we do not ask you to describe a solution of the problem!

One possible answer
State: For each region, an assignment of R,B,Y,G, or U (uncolored)
Initial state: all regions set to U
Successor function: All regions are ordered from 1 to N. Let x be the first region with a U
assignment. The four successors are derived by assigning x either R,B,Y, or G.
Goal test: N regions are colored, and no two adjacent regions have the same color.

Another possible formulation
State: For each region, an assignment of R,B,Y,G, or U (uncolored) where no two regions have
the same color
Initial state: all regions set to U
Successor function: All regions are ordered from 1 to N. For all x with a U assignment, up to
four successors are derived by assigning x either R,B,Y, or G, such that the color of x does not
conflict with any adjacent regions.
Goal test: N regions are colored
II. Missionaries and Cannibals (20 points)

In the classic Missionaries and Cannibals puzzle, your job is to move three missionaries
and three cannibals to the other side of a river using a boat that can only hold one or two
people. You may not leave the missionaries outnumbered on any bank of the river –
otherwise the cannibals will eat them.

     Figure 1. A missionaries and cannibals game that can be played on the web at

1. State the Missionaries and Cannibals puzzle as a search problem.          Give precise
definitions of the:
        a. State space
        b. Successor function
        c. Initial state
        d. Goal test
        (There may be multiple formulations)

2. Draw a complete, labeled state graph for this problem (not a search graph). Indicate an
optimal solution path. (The graph should not be prohibitively large if your formulation in
problem 1 is correct)

3. What blind search algorithm would you use to solve this problem? Would you check for
repeated states?

4. Consider a generalized Missionaries and Cannibals puzzle with N missionaries and N
cannibals. How quickly does the state space grow with N? Can these problems always be
solved for N>3? Why or why not?

1. a. State space: a tuple (ML,MR,CL,CR,B) where ML, MR, CL, CR denote the number of
missionaries (M) and cannibals (C) on the left (L) and right (R) sides of the river and B{L,R}
denoting which side the boat is on. We have the constraints that ML+MR = 3, CL+CR = 3,
CL,CR >= 0, ML,MR>=0, and (ML==0 or ML >= CL), and (MR==0 or MR >= CR).
b. Successor function. The successor function returns all possible valid states formed by moving
either 1 or 2 people from the side of the river that the boat is on to the opposite side, and also
switching B=L to B=R or vice versa.
c. Initial state (0,3,0,3,R)
d. The two goal states are (3,0,3,0,R) and (3,0,3,0,L), although the first is unreachable.



                         (0,3,2,1,L)         (1,2,1,2,L)        (0,3,1,2,L)


                         (0,3,3,0,L)        (1,2,1,2,R)         (3,0,1,2,L)       (0,3,3,0,R)

                         (0,3,2,1,R)        (2,1,2,1,L)         (3,0,0,3,R)       (3,0,0,3,L)

                                                                (3,0,2,1,L)       (3,0,3,0,R)

                         (3,0,2,1,R)        (2,1,2,1,R)         (3,0,1,2,R)


3. Acceptable answers are breadth first or iterative deepening with or without revisited state
checking, as long as a rationale is given. Depth first search with revisited state checking is also
acceptable. The problem is small enough that the algorithm doesn’t matter much, except that
depth first search may end up in an infinite loop.
4. Generalizing the above formulation of the state space, the size of possible states grows with
the square of N. No, the problem can’t be solved for N=4. Below is the entire component of the
state graph that is reachable from the start node

(0,4,2,2,L)   (1,3,1,3,L)   (0,4,1,3,L)


III. Heuristic Search (20 points)
Consider the following state graph:

The state space consists of states S, A, B, C, D, E, G1, and G2. S is the initial state and G1 and G2
are the goal states. The possible actions between states are indicated by arrows. So, the successor
function for state S returns {A, B}; for A it returns {C, D}, etc... The number labeling each arc
(roughly at the mid-point of the arc) is the actual cost of the action. For example, the cost of
going from S to A is 3. The number in bold italic near each state is the value of the heuristic
function h at that state. For example, the value of h at state C is 3.

1. Fill the following table with the nodes successively added to the fringe by the best-first
   search algorithm using the evaluation function f(N) = g(N) + h(N), where g(N) is the cost of
   the path found from the initial node to node N. The algorithm does not check if a state is a re-
   visited one or not (hence, there may be several nodes with the same state in the search tree).
   It terminates only when it removes a goal node from the fringe. The states produced by the
   successor function are always ordered in alphabetic order. In the rightmost column (#exp),
   indicate the order in which nodes are expanded (i.e., are removed from the fringe). If a node
   is not expanded, leave the corresponding cell empty. The first line of the table is filled for
   you. It is possible that you do not need all the rows in the table. In this problem, you don’t
   have to justify your answers.

                   N      State                g(N)      h(N)     f(N)      #exp
                   1      S                    0         10       10        1
                   2      A                    3         5        8         2
                   3      B                    7         8        15
                   4      C                    4         3        7         3
                  5       D                   9         2        11        5
                  6       D                   8         2        10        4
                  7       S                   6         10       16
                  8       B                   10        8        18
                  9       G1                  14        0        14        6
                  10      B                   11        8        19
                  11      G1                  15        8        15

2. Is the heuristic function h defined by values provided in the figure admissible? How do you
   know? How does this affect the search?

No, it is not admissible. h(B) is 8, which is greater than the actual cost to go from B to G2. This
causes the search to return a suboptimal path to G1 of length 14, whereas the optimal path
S,B,E,G has length 13.
IV. Word Puzzle (Programming) (40 points)
Complete the programming assignment as described in hw1.pdf and run your program on the
provided test cases. Then answer the following written questions. (Don’t forget to submit your
source code on Oncourse!)

   1. Describe your successor function.
         a. How do you know it provides all legal successors and no other states?

   2. Assume that the input words are of length n. For your successor function, state an upper
      bound on the branching factor of the search tree.

   3. Describe your heuristic function.
         a. Show that it is admissible.
         b. Show that it is consistent.

   4. How does your heuristic function compare to the null heuristic?
        a. Does it provide shorter paths?
        b. Does it visit (i.e., expand) fewer nodes? How do you know?
        c. How does it affect the running time of the search as a whole?
        d. Does it take less time to run, per-state?

One possible answer
1. The successor function considers a set S of candidate successors formed by changing one
letter of the input state to a different letter of the alphabet. For each word w in S, it outputs w
only if w is in the dictionary. 1a) This function produces all words that differ from the input
word by at most one letter, and are valid English words.

2. 25n. Each of the n letters in the word can generate at most 25 successors by considering all
other letters in the dictionary.

3. The heuristic function is the Hamming distance, i.e. it counts the number of different letters
between the input word and the goal word.
3a. If the word differs from the goal by k letters, then at least k steps are needed to transform the
word into the goal word.
3b. Proof #1: Let w and w’ be two words such that w’ is a successor of w, and let g be the goal
word. We need to prove the triangle inequality h(w) <= h(w’)+c(w,w’). Since the cost of each
edge is 1, we need to prove that h(w) <= h(w’)+1. The heuristic decreases by at most 1 from any
word to its successors (since at most one letter may change). Thus the triangle inequality
follows. Since h satisfies the triangle inequality AND is admissible, h is consistent.
Proof #2: h returns 0 at the goal, and it satisfies the triangle inequality (proof as above).
Therefore it is consistent.

4.a. No, b. Yes, because it is more accurate than the null heuristic. c. Running time is reduced.
D. No, it takes slightly more time to run because it involves the overhead of computing the
Hamming distance.

To top