Below is a trace of the execution of the query
?- path(1, 6, [1], Path).
using the Prolog code for path given in lectures, and the edges
given in lectures, that is:
path(Node, Node, _, [Node]). % rule 1
path(Start, Finish, Visited, [Start | Path]) :- % rule 2
edge(Start, X),
not(member(X, Visited)),
path(X, Finish, [X | Visited], Path).
edge(1, 5). edge(1, 7).
edge(2, 1). edge(2, 7).
edge(3, 1). edge(3, 6).
edge(4, 3). edge(4, 5).
edge(5, 8).
edge(6, 4). edge(6, 5).
edge(7, 5).
edge(8, 6). edge(8, 7).
The trace was produced using the SWI Prolog trace facility. Comments on what is happening are shown at right.
?- trace.
?- path(1, 6, [1], Path).
Call: (7) path(1, 6, [1], _G289) | |||||||||||||||||||||||||||||||||||||||||||||||||||
Call: (8) edge(1, _L208) | |||||||||||||||||||||||||||||||||||||||||||||||||||
Exit: (8) edge(1, 5) | |||||||||||||||||||||||||||||||||||||||||||||||||||
^ Call: (8) not(member(5, [1])) | |||||||||||||||||||||||||||||||||||||||||||||||||||
Call: (9) lists:member(5, [1]) | |||||||||||||||||||||||||||||||||||||||||||||||||||
Call: (10) lists:member(5, []) |
Path = [1, 5, 8, 6] Yes
To turn off tracing, type notrace! at the prompt (?-).
UNSW's CRICOS Provider No. is 00098G
Copyright © Bill Wilson, 2002, 2006. The original path code is due
to Claude Sammut.