Below is a minimal, “text-book” depth-first search that finds a SIMPLE (no repeated vertices) path between two nodes in a directed graph described only by facts of the form
code
edge(u,v). % there is an arc u → v
Copy–paste the code into any Prolog (SWI, SICStus, GNU, …) and try the sample session at the bottom.
prolog
/******************************************************************/
/* A very small path finder */
/* edge(U,V) : graph facts */
/* path(S,T,P): P is a simple path S ↝ T (list of vertices) */
/******************************************************************/
/* --- interface predicate -------------------------------------- */
path(Start, Goal, Path) :-
dfs(Start, Goal, [Start], RevPath), % run the DFS
reverse(RevPath, Path). % final path in L-to-R order
/* --- depth-first search with a visited list ------------------- */
dfs(Node, Node, Path, Path). % reached the goal
dfs(Node, Goal, Visited, Path) :-
edge(Node, Next), % follow an outgoing edge
\+ memberchk(Next, Visited), % keep the path simple
dfs(Next, Goal, [Next|Visited], Path).
How it works
We enter through path/3, seeding Visited with [Start].
dfs/4 explores edges depth-first.
Visited is threaded forward; memberchk/2 prevents revisits, so the path is “simple”.
Once the current node equals the goal, the accumulator (Path) is returned.
The final reverse/2 turns the accumulator order (Goal→…→Start) into the natural order (Start→…→Goal).
Undirected graphs?
If your graph is undirected just declare both directions, e.g.: