title: Simplified Travelling Salesman in Prolog
body: <p>I've looked through the similar questions but can't find anything that's relevant to my problem. I'm struggling to find an algorithm or set of 'loops' that will find a path from <code>CityA</code> to <code>CityB</code>, using a database of</p>
  
  <pre><code>distance(City1,City2,Distance)
  </code></pre>
  
  <p>facts. What I've managed to do so far is below, but it always backtracks at <code>write(X),</code> and then completes with the final iteration, which is what I want it to do but only to a certain extent.</p>
  
  <p>For example, I don't want it to print out any city names that are dead ends, or to use the final iteration. I want it to basically make a path from <code>CityA</code> to <code>CityB</code>, writing the name of the cities it goes to on the path.</p>
  
  <p>I hope somebody can help me!</p>
  
  <pre><code>all_possible_paths(CityA, CityB) :-
      write(CityA),
      nl,
      loop_process(CityA, CityB).
  
  loop_process(CityA, CityB) :- 
      CityA == CityB.
  loop_process(CityA, CityB) :-
      CityA \== CityB,
      distance(CityA, X, _),
      write(X),
      nl,
      loop_process(X, CityB).
  </code></pre>