I have an easy problem that I'm having trouble with although I think the answer is quite easy. Here it goes:
Assume a weighted directed graph is described by means of a predicate edge/3, such that edge(X,Y,C) is true is there is an edge from vertex X to vertex Y of cost C. For instance, to the right is a graph and its description using edge/3:
edge(a, c,1).
edge(a,d,3).
edge(b,d,2).
edge(c,e,5).
edge(e, c,2).
edge(e,f,2).
edge(d,f,10).
I have to . Define a predicate reachable/2 that computes the list of nodes that can be reached from a given node. For instance, to the query reachable(a, L) Prolog should answer L=[c,e,d,f] (in any order). (Remember findall.)
Here is what I wrote for the moment
path(X,Y):-edge(X,Y,_).
path(X,Y):-edge(X,Z,_),path(Z,Y).
reachable(X,L):-findall(Y,path(X,Y),L).
I can't see what is wrong but it's going in circles and stopping because of a memory issue. Any ideas how to solve that?
Please that would creatly help!
