title: Define graph in Prolog: edge and path, finding if there is a path between two vertices
body: <p>I'm very new to Prolog. I defined in <code>graph.pl</code> the following graph:</p>
  
  <p><img src="https://i.stack.imgur.com/JP9Yi.png" alt="graph"></p>
  
  <p>And here's my Prolog code:</p>
  
  <pre><code>edge(a,e).
  edge(e,d).
  edge(d,c).
  edge(c,b).
  edge(b,a).
  edge(d,a).
  edge(e,c).
  edge(f,b).
  path(X,X).
  path(X,Y):- edge(X,Z) ; path(Z,Y).
  </code></pre>
  
  <p>I understand it like this: <em>there is a path between vertex <code>X</code> and vertex <code>Y</code> only if there is an edge between vertex <code>X</code> and vertex <code>Z</code> AND there is a path between vertex <code>Z</code> and vertex <code>Y</code></em> (some kind of recursion). </p>
  
  <p>Is that right for the presented graph? When I ask Prolog about the path between vertex <code>A</code> and vertex <code>F</code> it gives me <code>true</code> ...  which isn't even right! What might be wrong in this code?</p>