Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:12:41+00:00 2026-06-03T03:12:41+00:00

I have two problems that have bugged me for hours. connected/2 is supposed to

  • 0

I have two problems that have bugged me for hours. connected/2 is supposed to judge whether two people are connected or not; distance/3 is supposed to measure the kinship. But:

  1. I keep getting trues infinitely for the query connected(x,y);
  2. and I’m getting infinitely increasing N for distance(x,y,N) query. Any suggestions?

Here are my facts:

male(ted).
male(barney).
male(ranjit).
male(marshall).
male(tony).
male(swarley).
male(steve).
male(chuck).
male(john).
male(devon).
male(morgan).

female(robin).
female(lily).
female(wendy).
female(stellar).
female(abby).
female(victoria).
female(carina).
female(sarah).
female(ellie).

married(ted,      robin).
married(marshall, lily).
married(ranjit,   wendy).
married(stellar,  tony).
married(steve,    carina).
married(sarah,    chuck).
married(ellie,    devon).

father(ted,      barney).
father(ted,      ranjit).
father(marshall, wendy).
father(ranjit,   stellar).
father(tony,     abby).
father(tony,     swarley).
father(tony,     victoria).
father(steve,    chuck).
father(steve,    ellie).
father(chuck,    john).
father(devon,    morgan).

mother(robin,    barney).
mother(robin,    ranjit).
mother(lily,     wendy).
mother(wendy,    stellar).
mother(stellar,  abby).
mother(stellar,  swarley).
mother(stellar,  victoria).
mother(carina,   chuck).
mother(carina,   ellie).
mother(sarah,    john).
mother(ellie,    morgan).

Now, my predicates:

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).

son(X,Y) :-
    male(X),
    parent(Y,X).

daughter(X,Y) :-
    female(X),
    parent(Y,X).

sibling(X,Y) :-
    parent(Z,X),
    parent(Z,Y).

cousin(X,Y) :-
    parent(Z,X),
    parent(W,Y),
    parent(G,Z),
    parent(G,W).

ancestor(X,Y) :-
    parent(X,Z),
    ancestor(Z,Y).
ancestor(X,Y) :- parent(X,Y).

notmember(X,[]).
notmember(X,[H|T]) :- 
    X \= H,
    notmember(X,T).

connected(X,Y,_) :- X == Y.
connected(X,Y,Visited) :- 
    ancestor(X,Z),
    notmember(Z,Visited),
    connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :- 
    ancestor(Z,X),
    notmember(Z,Visited),
    connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :- 
    sibling(X,Z),
    notmember(Z,Visited),
    connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :- 
    married(X,Z),
    notmember(Z,Visited),
    connected(Z,Y,[Z|Visited]).
connected(X,Y) :- connected(X,Y,[X]).

minimum(X,[X]).
minimum(X,[M,H|T]) :- 
    M =< H,
    minimum(X,[M|T]).
minimum(X,[M,H|T]) :-
    M > H,
    minimum(X,[H|T]).

distance(X,X,_,0).
distance(X,Y,Visited,N) :- 
    parent(X,Z),
    notmember(Z,Visited),
    distance(Z,Y,[Z|Visited],N1),
    N is N1+1.
distance(X,Y,Visited,N) :- 
    parent(Z,X),
    notmember(Z,Visited),
    distance(Z,Y,[Z|Visited],N1),
    N is N1+1.
distance(X,Y,N) :- distance(X,Y,[],N).

EDIT:
Thank you, i think i’ve managed to solve half way through the problems now.
Taking @twinterer’s advice, I have fixed the predicates like this

connected(X,Y,_) :- X == Y.
connected(X,Y,V) :-
    married(X,Z),
    notmember(Z,V),
    connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
    sibling(X,Z),
    notmember(Z,V),
    connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
    parent(X,Z),
    notmember(Z,V),
    connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
    parent(Z,X),
    notmember(Z,V),
    connected(Z,Y,[Z|V]),!.
connected(X,Y) :- connected(X,Y,[X]).

minimum(X,[X]).
minimum(X,[M,H|T]) :- 
    M =< H,
    minimum(X,[M|T]).
minimum(X,[M,H|T]) :-
    M > H,
    minimum(X,[H|T]).

count(X,[],0).
count(X,[X|T],N) :-
    count(X,T,N1),
    N is N1+1.
count(X,[H|T],N) :-
    X \== H,
    count(X,T,N1),
    N is N1.

distance(X,X,Visited,0) :-
    count(X,Visited,N),
    N =< 1, !.
distance(X,Y,Visited,N) :- 
    parent(X,Z),
    (notmember(Z,Visited)->
        distance(Z,Y,[Z|Visited],N1),
        N is N1+1
    ;
        fail
    ),!.
distance(X,Y,Visited,N) :- 
    parent(Z,X),
    (notmember(Z,Visited)->
        distance(Z,Y,[Z|Visited],N1),
        N is N1+1
    ;
        fail
    ),!.
distance(X,Y,N) :- 
    findall(N1,distance(X,Y,[X],N1),L),!,
    minimum(N,L),!.

But there is a new set of problems now

  1. it can’t take arbitrary queries like distance(X,y,n)
  2. queries like connected(X,y) return duplicate results

I think removing duplicate results can be achieved by using that findall/3 predicate,
but I am clueless as to how I can actually implement it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:12:41+00:00Added an answer on June 3, 2026 at 3:12 am

    1) I don’t think that you end in an infinite loop, but you let your program explore all ways that two people are connected, which will be a very large number. Since you probably are only interested in whether they are connected at all, you should add cuts at the end of the connected/3 clauses that will prevent backtracking once you have successfully determined that two people are connected, e.g.:

    connected(X,Y,_) :- X == Y,!.
    connected(X,Y,Visited) :- 
        ancestor(X,Z),
        notmember(Z,Visited),
        connected(Z,Y,[Z|Visited]),!.
    ...
    

    2) I didn’t get infinitely increasing values for N when I tested your code, but still the distance/3 predicate would determine different paths how two people are connected. Depending on the people, the minimum distance would not be the first to be computed. I would change the definition of distance/3 to something like this:

    distance(X,Y,N) :- 
        findall(N0, distance(X,Y,[],N0), Ns), !,
        minimum(N, Ns).
    

    This is reusing your minimum/2 predicate. Note that you should add cuts to the first two clauses of this predicate in order to avoid spurious choicepoints.

    Regarding your additional questions:

    3) you have to distinguish between looking for the smallest N and looking for people matching degree N:

    distance(X,Y,N) :-
      nonground(N),!,
      findall(N0, distance(X,Y,[],N0), Ns), !,
      minimum(N, Ns).
    distance(X,Y,N) :-
      distance(X,Y,[X],N).
    

    Also, you need to remove the cuts that you added to distance/4. This is different from the cuts added to connected/3!

    There’s probably a better way that avoids distinguishing between these two modes, but all I can think of at the moment is using some kind of breadth-first-search (in order to guarantee the minimum degree)…

    4) I don’t get duplicate answers for queries like ?- connected(X,victoria). Do you have an example?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two problems that I'm not sure are related: I have two DropDownList
I have my program that can draw rectangles. I have two problems I can't
I have some localization problems in my webpage. There are basically two problems (that
I spent around two hours on that problem, and I have visited these stackoverflow
I'm quite a beginner at database design. I have two problems that I'd like
I have two problems which are related. 1) I have a batch file that
I have two related problems that occur in the following situation. I have a
I have two problems with android SDK.The first is that after installing sdk manager
I have two problems. One is that it is only pulling one row and
I have two problems. The first one is that I'm using JSP and that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.