CS 4521 Fall Semester, 2009

20 Points

**
The assignment
**
consists of three required tasks and one extra credit task involving
depth-first and breadth-first search and an application of them
to a particular problem.

**
Discussion:
**
Artificial intelligence researchers often test theories of problem solving by
modeling problems in a "**blocks world**", where state representation is
simplified and possible actions are well defined. Consider a world in which
there are two *blocks*, **A** and **B**,
and three *places* (or locations), **p**,
**q** and **r**, where blocks may
be placed. The possible states in this world are:

+-------+ +-------+ +-------+ +-------+ +-------+ +-------+ S1| A | S2| | S3| | S4| B | S5| | S6| B | | B | | B A | | B A | | A | | A B | | A | | ----- | | ----- | | ----- | | ----- | | ----- | | ----- | | p q r | | p q r | | p q r | | p q r | | p q r | | p q r | +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ +-------+ S7| A | S8| | S9| | S10| B | S11| | S12| A | | B | | A B | | B A | | A | | A B | | B | | ----- | | ----- | | ----- | | ----- | | ----- | | ----- | | p q r | | p q r | | p q r | | p q r | | p q r | | p q r | +-------+ +-------+ +-------+ +-------+ +-------+ +-------+The rule of action in this world is that a block can be moved (by an idealized robot arm, say) and placed on another place or block provided no other block is on top of it. Suppose the problem is to come up with a sequence of moves which will get us from the initial state to the goal state as depicted below:

+-------+ +-------+ S1| A | S12| A | | B | | B | | ----- | | ----- | | p q r | | p q r | +-------+ +-------+ initial goalIt is immediately obvious to humans that a solution to this problem is a three step solution that moves

Pictorially, this graph could be represented as shown below:

+-------+ S1 | A | | B | | ----- | | p q r | +-------+ / \ / \ +-------+ +-------+ S2 | | S3 | | | B A |-------------| B A | | ----- | | ----- | | p q r | | p q r | +-------+ +-------+ / \ \ \ / \ \ \ +-------+ +-------+ \ +-------+ S4 | B | S5 | | \ S6 | B | | A |-------------| A B | \ | A | | ----- | | ----- | \ | ----- | | p q r | | p q r | \ | p q r | +-------+ +-------+ \ +-------+ / | \ | ________/ | \ | / | \ | +-------+ / +-------+ +-------+ S7 | A |/ S8 | | S9 | | | B |-------------| A B | | B A | | ----- | | ----- | | ----- | | p q r | | p q r | | p q r | +-------+ +-------+ +-------+ / | / | ________/ | ________/ | / | / | +-------+ / +-------+ / +-------+ S10| B |/ S11| |/ S12| A | | A |-------------| A B |-------------| B | | ----- | | ----- | | ----- | | p q r | | p q r | | p q r | +-------+ +-------+ +-------+We can represent this graph using the adjacency-list method and then use the depth-first or breadth-first search algorithms from Chapter 22 to find paths within it.

The vertices *V* and edges *E* for this graph are contained in the
file
** graphdata.txt**,
the beginning of which looks like the picture below.

12 | |

1 | 2 |

1 | 3 |

2 | 1 |

2 | 3 |

2 | 4 |

2 | 5 |

3 | 1 |

3 | 2 |

3 | 6 |

3 | 9 |

4 | 2 |

4 | 5 |

. | . |

. | . |

. | . |

BFS(G, start) PRINT-PATH(G, start, end)The following output shows how the breadth-first search test works on the

+---------------------+ | | | 1 -> 3 -> 9 -> 12 | | | +---------------------+

DFS(G, start) |> note addition of start node for each vertex u in V[G] do color[u] <- WHITE pi[u] <- NIL DFS-Visit(start) |> search from start only, not from all u in VNote that although the DFS examples given in the text and in class were on directed graphs, the DFS algorithms work just as well on undirected graphs. The PRINT-PATH algorithm given in the text for printing paths in breadth-first predecessor trees can also be used on depth-first predecessor trees. Testing depth-first search on our blocks problem is completely analogous to the breadth-first case:

DFS(G, start) PRINT-PATH(G, start, end)Implement these depth-first search algorithms and apply them to some blocks world problems. The following output shows one possibility for how a depth-first search test works on the

+-----------------------------------------------------+ | | | 1 -> 3 -> 9 -> 12 -> 11 -> 10 -> 8 -> 7 -> 5 -> 4 | | | +-----------------------------------------------------+Obviously, this is not the shortest solution to the problem, although DFS

** Helpful code: **
Here is a .h file for the Graph class
** graph.h**
and skeleton code for the Graph class implementation

+-------+ +-------+ +-------+ | C | | | | | | A | | C | | | | B | | B A | | B C A | . . . etc. | ----- | | ----- | | ----- | | p q r | | p q r | | p q r | +-------+ +-------+ +-------+Your final tasks for this part:

- (1 point) Determine exactly how many states would be required to completely represent the blocks world problem in a graph given 3 blocks.
- (1 points) Answer the same question if there are 4 blocks
(using the same places
**p**,**q**, and**r**as above). - (3 points) Answer the same question in general: if there are b blocks and n places.

- Well documented code implementing the algorithms (
**Task 1**):- BFS
- DFS (this is done for you)
- DFS-Visit
- PRINT-PATH

- A script clearly showing the results of the tests for
**Task 2**:- BFS-Test(graph.txt, 1, 12)
- BFS-Test(graph.txt, 12, 1)
- BFS-Test(graph.txt, 1, 4)
- BFS-Test(graph.txt, 4, 1)

**Task 3**:- DFS-Test(graph.txt, 1, 12)
- DFS-Test(graph.txt, 12, 1)
- DFS-Test(graph.txt, 1, 4)
- DFS-Test(graph.txt, 4, 1)

- Your solutions to problems
**i**,**ii**, and**iii**of**Task 4**(if done) including all of your work in arriving at them.