/*-------------------------------------------------------------------------
    program: searching a path in a graph (without cycling!)
    file:    paths2
    date:    December 97
    author:  Pablo Lopez (lopez@lcc.uma.es)
             Dept. of CS
             University of Malaga, SPAIN

    notes:   This code is a Forum version of a Lygon program originally
             written by Michael Winikoff and James Harland
             Lygon home page: http://www.cs.mu.oz.au/~winikoff/lygon

    sample goals:
         go(3,b,d,P).
         go(3,a,D,P).
         go(2,S,b,P).
         go(N,X,Y,P).
-------------------------------------------------------------------------*/

module paths2.

% As edges and nodes are represented by linear premises, they are consumed
% when visited along a path, so we don't have to worry about cycles.
% Note that given the goal `path(X,Y,P) x top', the `top' connective
% consumes all edges and nodes not used along the path P.
% While in module `paths1' only edges were represented by linear premises,
% this version uses the same technique to represent nodes, so we avoid 
% that crazy paths revisiting them.

path(X,Y,[X,Y]) # edge(X,Y) # node(X) # node(Y).

path(X,Y,[X|P]) # edge(X,Z) # node(X) *- path(Z,Y,P).

go(N,X,Y,P) *- graph(N) # path(X,Y,P) x top.

% sample graphs

graph(1) *- node(a) # node(b) # node(c) # node(d) # node(e) #
            edge(a,b) # edge(b,c) # edge(c,d) # edge(d,e) # edge(d,a).

graph(2) *- node(a) # node(b) # node(c) # node(d) #
            edge(a,b) # edge(a,c) #
            edge(b,a) # edge(b,d) #
            edge(c,a) # edge(c,d).

graph(3) *- node(a) # node(b) # node(c) # node(d) #
            edge(a,b) # edge(a,c) # edge(a,d) #
            edge(b,a) # edge(b,c) #
            edge(c,a) # edge(c,b) # edge(c,d) #
            edge(d,a) # edge(d,b) # edge(d,c).

graph(4) *- node(a) # node(b) # node(c) # node(d) # node(e) # node(f) #
            node(g) # node(h) #
            edge(a,b) # edge(a,c) # edge(a,d) # edge(a,e) # edge(a,f) # 
            edge(b,c) # edge(b,d) # edge(b,e) # edge(b,f) # edge(b,g) #
            edge(c,a) # edge(c,c) # edge(c,e) # edge(c,f) #
            edge(d,a) # edge(d,b) # edge(d,f) # edge(d,g) #
            edge(e,a) # edge(e,c) # edge(e,d) # edge(e,f) # edge(e,g) #
            edge(f,a) # edge(f,b) # edge(f,d) # edge(d,d) #
            edge(g,a) # edge(g,c) # edge(g,f) #
            edge(h,b) # edge(h,f).


