Session 9: Sorting and Searching

Merge sort

Searching: warm-up exercise

Recall the cube stacking problem. Remember that in session 5 we have written a predicate generate_and_test/2 that given a list with 4 cubes, returns a list of orientations giving a 'correct' stack.
generate_and_test([Cube1,Cube2,Cube3,Cube4],[O1,O2,O3,O4]) :-
% generate a stack with the cubes in some orientation:
basic_orientation(O1), % 3 possibilities for cube1
valid_orientation(O2), % 24 possibilities for cube2
valid_orientation(O3), % 24 possibilities for cube3
valid_orientation(O4), % 24 possibilities for cube4
% test the stack:
test_stack([Cube1,Cube2,Cube3,Cube4],[O1,O2,O3,O4]).
Given this generate_and_test/2 predicate, write a program to find 4 cubes that have only 2 different correct stacks. With "finding a cube" we mean finding figures for all six sides of the cube (as before you can use four possible figures: star, circle, rectangle and triangle).

Searching: missionaries and cannibals

For many problems we have a starting situation, a desired end situation and a set of rules that define which 'actions' we can perform in a given situation (remember the cube problem from session 5). We then have to find the sequence of actions that brings us from the starting situation to the end situation. Prolog makes it easy to write this kind of programs because backtracking (an essential part of searching) is hardwired into prolog. The general schema for such problems is as follows:
search(CurrentState,_Trace):-
     endstate(CurrentState), !,
     write_solution.

search(CurrentState,Trace):-
     generate_action(Action),
     valid_action(CurrentState,Action), 
     apply_action(Trace,CurrentState,Action,NewTrace,NewState),
     no_loops(NewState,Trace),
     search(NewState,NewTrace).
If we found the end state we stop searching and print the result. If we did not yet find a solution, we generate an action that is allowed and does not create a loop in the solution. We apply this action and then continue searching. Of course, if necessary we can backtrack over this action if we do not find a solution.

Now try to use this general idea in the following "Missionaries and cannibals" exercise: