Search

An important notion used with these programs is the notion of "search trees". The program searches for an expression or a function which gives the correct answer. How is this done? The program starts with a small list of candidate functions. The program tests one function from this list on the input sequence. If it works, then the program returns with "success". If it fails, then the current function is "expanded" - that is, new functions are created and added to the list. The new functions are created according to the recursive defintion.
The above figure shows an example of a search tree. At the root (A1), one candidate function/expression is present. (This can be generalized.) This function/expression is "evaluated". If it is "correct", then the search returns with success. Otherwise the function/expression is "expanded" using the rules given in the problem. The search procedure keeps a list of functions to be evaluated. It removes one function from the list and tests it. If correct, search is terminated. If not, the function is expanded and the new functions are added to the list and search continues.

Breadth first search

In this case, The expanded functions are added to the end of the list of functions. (The list is a queue.) For example, initially the list has (A1). After A1 is evaluated, it contains (A2, B2). It evolves as below:
(A1) -> (A2, B2) -> (B2, A3, B3) -> (A3, B3, C3) -> (B3, C3, A4, B4) -> ...

The problem with BFS is that it takes lot of space. In the worst case, the number of nodes in the list is expoential in the depth of the tree.

Depth first search

There are another alternative to BFS, called the depth first search. Depth first search is equivalent to adding the expanded nodes to the beginning of the list. (The list acts as a stack.) In DFS, the list evolves as shown below.

(A1) -> (A2, B2) -> (A3, B3, B2) -> (A4, B4, B3, B2) -> ...

DFS code looks as below.
     searchList = initialList;
     loop
        if (searchList == NIL) return FAILURE;
        node = firstNode(searchList); searchList = rest(searchList);
        if (goal(node)) return node;
        else searchList = insert(expand(node), searchList);
     end
Two points to note about DFS are
  1. How big can searchList get? Suppose B is the maximum number of nodes returned by expand(). Then, at depth d, the maximum size of searchList is dB. In contrast, in BFS, the maximum size grows exponentially in d.
  2. How does the search stop? When a solution is found, of course. Otherwise, searchList grows unless expand(node) return returns NIL. In this case, searchList reduces in size and finally searchList becomes NIL and the program terminates with FAILURE.
It can be seen from the above discussion that if expand() keeps returning new nodes, the search might never stop. So the problem with DFS is that it can lead to endless recursive expansion, even if a solution exists. In the above tree it can expands functions A1, A2, A3, A4, A5, A6 .... If B2 is the solution and none of the A nodes are solutions, the DFS does not terminate.

Depth-limited search

A way to solve this problem is to limit the depth of the search. Functions at a level of maxDepth, are not expanded. (The search routine should keep track of depth explicitly.) After visiting depth maxDepth, the program backtracks to depth maxDepth-1 and finally terminates. If maxDepth=2 in the above figure, the nodes visited are: A1, A2, A3, B3, B2, C3.

As you can see, depth-limited search is not guaranteed to find the solution if a solution does not exist at depths less than maxDepth.

Iterative deepening

Iterative deepening does repeated depth-limited searches. It first searches with maxDepth=1. If a solution is not found, then maxDepth is incremented by 1. This process is repeated till a solution is found (or till the program runs out of depth :-)).

Iterative deepening is optimal in space. The worst case time complexity is same as that of BFS. But many nodes are repeatedly searched. It is the best search procedure the works with limited resources.

Example code

The following example programs in ML solve the problem of finding g(n) in some special cases. These example programs implement the breadth first search.
(* This function handles only two operatiors: + and *.
   It does *not* expand expressions.
   It *prints* the first 10 values of the function
*)
datatype opr = SUM | MUL;

datatype expr = One | N | B of opr * expr * expr;

val init = [ One, N, (B (SUM, One, N)), (B ( MUL, N, N))];

fun eval (One,  n)  = 1
    | eval (N, n) = n
    | eval ((B (SUM, a, b)), n) = eval(a, n) + eval(b, n)
    | eval ((B (MUL, a, b)), n) = eval(a, n) * eval(b, n);

fun match ([],_,_) = true
  | match (a::rest, exp, n) = if a=eval(exp, n) then match(rest, exp, n+1)
                  else false;

fun printVals(ex, n) =
        (print(eval(ex,n));
         print(" ");
         if (n=10) then print("Done\n")
          else printVals(ex, n+1) );

fun search(ilist, nil) = print("Not found\n")
       | search (ilist, ex::rest) =
               if match(ilist, ex, 1) then printVals(ex, 1)
               else search(ilist, rest);

fun complete(ilist) =  search(ilist, init);
The following program uses higher order functions.
(* The following program computes g(n).
   Suppose you want to calculate more members of the sequence
   [1, 4, 9, 16], try "check([1, 4, 9 16], nil);". You should
   get the answer
            81 64 49 36 25 16 9 4 1 Done
   These are nine entries of the sequence (in reverse order).
   The call "check([0,2,6,12], nil);" returns "72 56 42 30 20 12 6 2 0 Done".
*)

fun f1(n) = 1;
fun f2(n) = n;

fun expo(m:int, n:int) =
    if (n=0) then 1
    else if m=0 then 0
         else let val temp=expo(m, n div 2)
              in if (0=(n mod 2)) then temp*temp
                 else temp*temp*m
              end;

fun f3(m:int, n) = m + n;
fun f4(m:int, n) = m - n;
fun f5(m:int, n) = m * n;
fun f6(m:int, n) = expo(m , n);

fun eval1(f, nil, _) = true
   | eval1(f, a::rest, n) =
        if (f(n) = a) then eval1(f, rest, n+1)
          else false;

fun eval(f, ilist) = eval1(f, ilist, 1);

fun expand(f, flist) = flist@[
fn n => f3(f(n), f1(n)),
fn  n => f3(f(n), f2(n)),
fn n => f4(f(n), f1(n)),
fn  n => f4(f(n), f2(n)),
fn  n => f5(f(n), f2(n)),
fn n => f6(f(n), f2(n))
];

fun outPut(f:int->int, n) =  if (0 = n) then print("Done\n")
      else (print(f(n)); print(" ");
                    outPut(f, n-1));

fun check(ilist, nil) = check(ilist, [f1, f2])
   | check(ilist, f::rest) =
        if eval(f, ilist) then (outPut(f, 9);
                                true )
        else check(ilist, expand(f, rest));