PROLOG ASSIGNMENT 3 (SAU).
path(A, Z, Graph, Path) :-
path1(A,[Z],Graph,Path).
path1(A, [A|Path1], Graph, [A|Path1]).
path1(A, [Y|Path1], graph(Nodes, Edges), Path) :-
(member(e(X,Y), Edges); member(e(Y,X), Edges)),
not (member(X, Path1)),
path1(A, [X,Y|Path1], graph(Nodes, Edges), Path).
member(X, [X|L]).
member(X, [Y|L]) :-
member(X, L).
eq_set(P, N) :-
subset(P,N),
subset(N,P).
subset([],[]).
subset( [X|Set],Subset) :- % X not in subset
subset(Set,Subset).
subset( [X|Set],[X|Subset]) :- % X included in subset
subset(Set,Subset).
hpath(graph(Nodes, Edges), Path) :-
path(A, Z, graph(Nodes, Edges), Path),
eq_set(Path, Nodes).
connected(graph(Nodes,Edges)):-
hpath(graph(Nodes, Edges), Path).
Run:
sujit@sujit-desktop:~/sunil/prolog$ yap
% Restoring file /usr/lib/Yap/startup
YAP version Yap-5.1.3
?- [tr].
% consulting /home/sujit/sunil/prolog/tr.pl...
% consulted /home/sujit/sunil/prolog/tr.pl in module user, 0 msec 3592 bytes
yes
?- path(a,d,graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
P = [a,b,d] ? ;
P = [a,b,c,d] ? ;
no
?- connected(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)])).
yes
?- connected(graph([a,b,c,d],[e(a,b),e(b,c),e(c,d)])).
yes
?- connected(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c)])).
no
?- path(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
no
?- hpath(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
P = [a,b,c,d] ? ;
no
?-
% Restoring file /usr/lib/Yap/startup
YAP version Yap-5.1.3
?- [tr].
% consulting /home/sujit/sunil/prolog/tr.pl...
% consulted /home/sujit/sunil/prolog/tr.pl in module user, 0 msec 3592 bytes
yes
?- path(a,d,graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
P = [a,b,d] ? ;
P = [a,b,c,d] ? ;
no
?- connected(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)])).
yes
?- connected(graph([a,b,c,d],[e(a,b),e(b,c),e(c,d)])).
yes
?- connected(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c)])).
no
?- path(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
no
?- hpath(graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]),P).
P = [a,b,c,d] ? ;
no
?-