Prolog displaying a path -


i have following simple graph:

x <---> y <---> z

edge(x,y). edge(y,z).  path(start,end,path) :-           % path/3: there `path` `start` `end`     path(start,end,[start],path).  path(end,end,rpath,path) :-       % internal helper predicate `path/4`     reverse(rpath,path). path(start,end,visited,path) :-     edge(start,next),     \+ memberchk(next,visited),     path(next,end,[next|visited],path). 

sample queries:

?- path(x,z,p). p = [x, y, z] ;                   % works expected false.  ?- path(z,x,p).                         false.                            % unexpectedly fails! 

what can make above query succeed?

if want handle undirected graph:

path(start, end, visited, path) :-     ( edge(start, next) ; edge(next, start) ),     ... 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -