next up previous
Next: About this document Up: No Title Previous: Lists - Use

Searching Graphs

Consider the following :

  figure77
Figure 2: A graph

The graph above can be described, by simply stating the interconnections :

 
arc(a,c).
arc(a,b).
arc(c,a).
arc(c,b).
arc(c,d).

Also, we can define a simple procedure which evaluates to true if a path between two given nodes can be found :

path(X,Y) :-
 arc(X,Y).

path(X,Y) :-
 arc(X,Z),
 path(X,Z).

If issued the query :

path(a,d)

path(X,Y) :-
 path(X,Y, []).

path(X,Y,_) :-
 arc(X,Y).

path(X,Y,Trail) :- 
 arc(X,Z),
 not(member(Z,Trail)),
 path(Z,Y,[X|Trail]).

what does this do ?

More complex arithmetic manipulation of lists via recursion

squares([],[]).

squares([ N| Tail],[S | Stail]) :-
 S is N*N,
 squares(Tail, Stail).


next up previous
Next: About this document Up: No Title Previous: Lists - Use

Omer F Rana
Fri Feb 14 20:23:31 GMT 1997