I’m using: SWI-Prolog version 8.0.3
I want the code to: Find the shortest path in between 2 nodes, and print the shortest path
But what I’m getting is: All the paths between 2 nodes
My code looks like this:
oh(placeA, placeB, 13) oh(placeB, placeC, 3) oh(placeB, placeD, 45) oh(placeC, placeD, 7) oh(placeD, placeF, 34) path(A, B, [A, B], X) :- oh(A, B, X). path(A, B, PathAB, Length) :- oh(A, C, X), path(C, B, PathCB, LengthCB), PathAB = [A | PathCB], Length is X + LengthCB. find_paths(A, B) :- path(A, B, Path, Length), printPath(Path), writef(' distance %d\n', [Length]), fail. printPath(). printPath([X]) :- !, write(X). printPath([X|T]) :- write(X), write(', '), printPath(T).
What do i have to change in my code to only give me the shortest path ?