~/ learn/ comp-456/ cards/ Depth-first search & iterative deepening
1 of 5

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/7f8c809e-e65a-486a-8d79-39f3d175cca4/flashcard utf-8 LF