parent(a,b).
parent(a,c).
parent(c,e).
parent(c,d).
parent(b,f).
parent(f,g).

ancestor(A,X) :- parent(A, X).
ancestor(A,X) :- parent(P, X), ancestor(A,P).
%ancestor(A,X) :- ancestor(A,P), parent(P, X). -- try it!
