Type the weighted edges and the min_path predicate (A1)
Type the weighted edges and the min_path predicate (A1)
Answer
edge(a,b,3). edge(b,c,1). edge(c,d,2). edge(b,d,5). min_path(S, G, Path, Cost) :- setof([C,P], path(S,G,[S],P,C), [[Cost,Path]|_]).
setof collects every [Cost, Path] pair the path/5 goal produces, sorted ascending by Cost (the leading term). The pattern [[Cost,Path]|_] takes the head of the sorted list — the minimum-cost solution. Putting Cost first is what makes the sort a minimisation.