Define graph in Prolog: edge and path, finding if there is a path between two vertices

前端 未结 4 1429
情歌与酒
情歌与酒 2020-12-06 10:43

I\'m very new to Prolog. I defined in graph.pl the following graph:

\"graph\"

And here\'s

4条回答
  •  难免孤独
    2020-12-06 10:47

    It is checking in a cycle! I executed the example with the trace. command before hand, and saw, that the program returns to the problem to find the path from a to f after cycling around. path(a,f) needs path(e,f) to be true, following in short notation we need to be true:

    (d,f), (c,f), (b,f), an then (a,f) again.

    To resolv the loop you need a mechanism to prevent loop. My first Idea would be to keep a list of node-names and also check, if the list does not include the current X while checking the path.

提交回复
热议问题