this might be a simple problem but I need to do it differently. The problem is that I have to find in prolog the possible routes of airflights. I have this knowledge base
from_to(fresno,seattle).
from_to(fresno,albany).
from_to(albany,dallas).
from_to(fresno,boston).
from_to(dallas,seattle).
from_to(dallas,albany).
from_to(seattle,dallas).
from_to(seattle,omaha).
from_to(atlanta,albany).
from_to(atlanta,dallas).
from_to(atlanta,boston).
from_to(omaha,atlanta).
from_to(omaha,albany).
from_to(albany,seattle).
And I have to make a predicate route(X,Y) that checks if we can go from X to Y. What I did is this:
route(X,Y):-from_to(X,Y).
route(X,Y):-from_to(X,Z), route(Z,Y).
But it doesn't work because the graph is cyclic. I searched on the internet and the only thing everyone said is to use a list and check the visited paths. But I can't use lists! I have to make a predicate route(X,Y) without using lists, how can I accomplish this without a list? Thank you
I would try
test:
Since the graph is defined in source code, an alternative could be:
but after the first call, a reload of the KB is needed.