Don't need answer anymore. its done. Thanks !
a(S,E,[S|R]) :- S\=E,(c(S, N);c(N,S)),a(N,E,R),\+member(S,R).
a(X,X,P):- P=[X].
The above code cause out of local stack problem, Could someone tell me how to fix? Thanks.
Don't need answer anymore. its done. Thanks !
a(S,E,[S|R]) :- S\=E,(c(S, N);c(N,S)),a(N,E,R),\+member(S,R).
a(X,X,P):- P=[X].
The above code cause out of local stack problem, Could someone tell me how to fix? Thanks.
To complete a visit of a graph with loops (introduced in your DAG from c(S, N);c(N,S)
), we must check the path seen so far (just to say), but you attempt to inspect after it has been built. This 'logical loop' reflects in non termination of your code. The easier solution is to add an argument that holds the path seen so far:
a(S,E,P) :- visit(S,E,[S],P).
visit(X,X,P,R) :- reverse(P,R).
visit(S,E,P,R) :- (c(S,N);c(N,S)), \+memberchk(N,P), visit(N,E,[N|P],R).
yields
?- a(1,4,L).
L = [1, 2, 4] ;
L = [1, 3, 4] ;
false.
see this thread about some high level thoughts on this theme