Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>connected/3</code> clauses that will prevent backtracking once you have successfully determined that two people are connected, e.g.:</p> <pre><code>connected(X,Y,_) :- X == Y,!. connected(X,Y,Visited) :- ancestor(X,Z), notmember(Z,Visited), connected(Z,Y,[Z|Visited]),!. ... </code></pre> <p>2) I didn't get infinitely increasing values for N when I tested your code, but still the <code>distance/3</code> 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 <code>distance/3</code> to something like this:</p> <pre><code>distance(X,Y,N) :- findall(N0, distance(X,Y,[],N0), Ns), !, minimum(N, Ns). </code></pre> <p>This is reusing your <code>minimum/2</code> predicate. Note that you should add cuts to the first two clauses of this predicate in order to avoid spurious choicepoints.</p> <p>Regarding your additional questions:</p> <p>3) you have to distinguish between looking for the smallest N and looking for people matching degree N:</p> <pre><code>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). </code></pre> <p>Also, you need to remove the cuts that you added to <code>distance/4</code>. This is different from the cuts added to <code>connected/3</code>!</p> <p>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)...</p> <p>4) I don't get duplicate answers for queries like <code>?- connected(X,victoria).</code> Do you have an example?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload