prolog - Error 1, Backtrack Stack Full

后端 未结 2 1008
失恋的感觉
失恋的感觉 2020-12-20 01:32

im trying to write a program in prolog that determine if there is a way from place to place. these are the relations:

road(ny,florida).
road(washington,flori         


        
相关标签:
2条回答
  • 2020-12-20 02:06

    If you are treating the road/2 as a directed edge, then you can simply have:

    road(ny,florida).
    road(washington,florida).
    road(washington,texas).
    road(vegas,california).
    
    there_is_way(X,Y):- road(X,Y).
    there_is_way(X,Y):- road(X,Z),there_is_way(Z,Y).
    

    If however you have a loop in your graph, either because of the directions or you assume that road/2 is an undirected edge (and correspondingly implement this) then you need to keep track of where you have been while route finding. Otherwise you will get into an infinite loop. Remember prolog searches depth first by default so you need to make a check that when adding an item to a route it is not already there, otherwise prolog will find the same route over and over..

    See chapter 5 of this book: https://www.cs.bris.ac.uk/~flach/SL/SL.pdf for a complete discussion.

    0 讨论(0)
  • 2020-12-20 02:19

    First, we need a symmetric definition:

    :- meta_predicate symm(2, ?,  ?).
    
    symm(P_2, A, B) :-
       call(P_2, A, B).
    symm(P_2, A, B) :-
       call(P_2, B, A).
    

    Now, using closure0/3

    there_is_way(A, B) :-
       closure0(symm(road), A, B).
    
    0 讨论(0)
提交回复
热议问题