KMA
KMA

Reputation: 211

Limited depth of the result

I have the following Prolog code that limits the depth of the result.

road(a,b,10).
road(b,c,15).
road(a,d,20).
road(d,b,30).
road(d,e,5).
road(e,c,25).
road(c,a,30).

path(A,B,D,L):-
   L>0,
   road(A,B,D).
path(A,C,D,L):-
   road(A,B,D1),
   L1 is L -1,
   L1>0,
   path(B,C,D2,L1),
   D is D1 + D2.

In here the depth (L) limits the number of intermediate points associated with the result. 'L' is the maximum number of nodes that can be visited while generating the result.

I want to get only the results when the depth is 'L', not in the range of (0<=depth<=L). How can I get that result?

Upvotes: 1

Views: 55

Answers (1)

CapelliC
CapelliC

Reputation: 60034

I think

path(A,B,D,1):-road(A,B,D).
path(A,C,D,L):-L > 1, road(A,B,D1),L1 is L -1,L1>0,path(B,C,D2,L1),D is D1 + D2.

should do. Note: untested code...

Upvotes: 1

Related Questions