I have a Prolog function path(A,B,Path)
that yields all the valid paths on a board from A to B.
The output of this function looks like this:
?- path(0,2,Path).
Path = [0, 1, 2] ;
Path = [0, 3, 2] ;
Path = [0, 1, 4, 2] ;
Path = [0, 3, 4, 2] ;
Path = [0, 1, 4, 5, 3, 2] ;
etc, etc.
It generates an infinite set of lists containing valid paths. I would simply like to get the shortest of these paths (however many there are). That is, I want a function shortest(A,B,Path)
that wil yield the shortest valid paths on a board from A to B.
The output I want is:
?- shortest(0,2,Path).
Path = [0, 1, 2] ;
Path = [0, 3, 2] ;
false.
I've been playing around with the setof
function in Prolog to bind all of the paths to a set where I impose some length restriction on it, but I haven't gotten that to work yet.
My poor work so far looks like this. It's definitely wrong, and I'd appreciate any help understanding how setof
works and how I can find the shortest lists from this set. Thanks!
shortest(A,B,MinPath) :-
setof(Path,path(A,B,Path),MinPath),
min(length(Path), length(MinPath)).