Recursive acyclic-path DFS in Prolog (the A1/A2 assignment pattern)
Recursive acyclic-path DFS in Prolog (the A1/A2 assignment pattern)
Answer
path(X, X, _). path(X, Y, Visited) :- edge(X, Z), \+ member(Z, Visited), path(Z, Y, [Z|Visited]).
Base case: a node reaches itself. Recursive step: take an edge to Z, check Z is NOT already in the Visited list (\+ member = the cycle guard), then recurse with Z added. This is the exact acyclic-path idiom examined in Assignments 1 and 2.