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.
(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
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.
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.
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.
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.
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.
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));