假设我有以下谓词:
father(aaron, chloe).
father(aaron, dan).
father(aaron, emily).
father(frank, george).
mother(beth, chloe).
mother(beth, dan).
mother(beth, emily).
mother(emily, george).
sibling(X,Y) :-...
parents(X,Y) :-...
我想找到家谱的两个成员之间的最短路线(距离)。例如,父母之间的距离是 2,兄弟(兄弟姐妹)之间的距离是 1。我尝试了以下方法(但没有奏效):
parent(X,Y) :-
father(X,Y);
mother(X,Y).
sibling(X,Y):-
parent(Z,X), !, parent(Z,Y),
not(X=Y).
not_in_list(_,[]).
not_in_list(X,[Y|L]):-
not(X=Y),
not_in_list(X,L).
edge(X,Y):-
(parent(X,Y);
parent(Y,X);
sibling(X,Y)).
list_length(0,[]).
list_length(N,[_|Ys]):-
list_length(N1,Ys),
N is N1+1.
travel_graph(X,X,_).
travel_graph(From,To,[From|Path]):-
edge(From,Next),
not_in_list(Next,Path),
travel_graph(Next,To,Path).
degree(X,Y,N):-
travel_graph(X,Y,L),
list_length(N,L).