PROLOG ASSIGNMENT 3 (SAU).(MIN_COST)

path_C(A, Z, Graph, Path, Cost) :-
          path1_C(A, [Z], 0, Graph, Path, Cost).

path1_C(A, [A|Path1], Cost1, Graph, [A|Path1], Cost1).
path1_C(A, [Y|Path1], Cost1, graph(Nodes, Edges), Path, Cost) :-
          (member(e(X,Y,CostXY), Edges); member(e(Y,X,CostXY), Edges)),
           not (member(X, Path1)),
          Cost2 is Cost1 + CostXY,
          path1_C(A, [X,Y|Path1], Cost2, graph(Nodes, Edges), Path, Cost).

shortest_path(A, Z, Graph, Path, Mini_Cost) :-
         path_C(A, Z, Graph, Path, Mini_Cost),
         no_shorter_path(A, Z, Graph, Path1, Cost1, Mini_Cost).

no_shorter_path(A, Z, Graph, Path, Cost, Mini_Cost) :-
         not( and(A, Z, Graph, Path, Cost, Mini_Cost) ).

and(A, Z, Graph, Path, Cost, Mini_Cost) :-
        path_C(A, Z, Graph, Path, Cost),
        Cost < Mini_Cost.

member(X, [X|L]).
member(X, [Y|L]) :- member(X, L).

sujit@sujit-desktop:~/sunil/prolog$ yap
% Restoring file /usr/lib/Yap/startup
YAP version Yap-5.1.3
   ?- [min].
 % consulting /home/sujit/sunil/prolog/min.pl...
 % consulted /home/sujit/sunil/prolog/min.pl in module user, 0 msec 2560 bytes
yes
   ?-  path_C(a,d,graph([a,b,c,d],[e(a,b,2),e(b,d,1),e(b,c,3),e(c,d,1)]),P,X).
P = [a,b,d],
X = 3 ? ;
P = [a,b,c,d],
X = 6 ? ;
no
   ?-  shortest_path(a,d,graph([a,b,c,d],[e(a,b,2),e(b,d,1),e(b,c,3),e(c,d,1)]),P,X).
P = [a,b,d],
X = 3 ? ;
no
   ?-

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .

STRING PALINDROME USING STACK AND QUEUE