Hello!

I have such a problem on the use of Prolog. It is necessary to find whether A and B are connected and to go through all possible steps on the way from A to B, but here you need to use a predicate with 2 atoms and only that. How to do it better?

можнодобраться(А,Б) :- связана(А,Б); (связана(А,В), связана(В,Б)); (связана(А,В), связана(В,Г), связана(Г,Б). 

Something like that, just somehow it is necessary that in the place where (C and D ... queries were going on with other paths until G and B are found, somehow, I understand that there is a recursion, I just Prolog never worked, and here the task.

    1 answer 1

    The logic in this case is quite simple:

    From A you can get to B if A is connected to B,
    OR from A you can get to such a B , from which you can get to B.

    It is almost literally written like this.

     можнодобраться(А,Б) :- связана(А,Б); можнодобраться(А,В), можнодобраться(В,Б).