Path from one node to another one

Verified

Added on  2022/10/09

|11
|1208
|10
AI Summary

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Document Page
Document Page

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Solution 4.1 )
split([],[],
[]).
split([HP | TL], [HP | TP], N) :-
HP >= 0, !, % green cut
split(TL, TP, N).
split([HN | TL], P, [HN | TN]) :-
HN < 0, !, % green cut
split(TL, P, TN).
Solution 4.2 )
a)
A leaf is a node with no successors. Write a predicate leaves/2 to collect them in a list.
leaves :: Tree a -> [a]
leaves Empty = []
leaves (Branch a Empty Empty) = [a]
Document Page
leaves (Branch a left right) = leaves left ++ leaves right
b)
% P81 (**) Path from one node to another one
% path(G,A,B,P) :- P is a (acyclic) path from node A to node B in the graph G.
% G is given in graph-term form.
% (+,+,+,?)
:- ensure_loaded(p80). % conversions
path(G,A,B,P) :- path1(G,A,[B],P).
path1(_,A,[A|P1],[A|P1]).
path1(G,A,[Y|P1],P) :-
adjacent(X,Y,G), \+ memberchk(X,[Y|P1]), path1(G,A,[X,Y|P1],P).
% A useful predicate: adjacent/3
adjacent(X,Y,graph(_,Es)) :- member(e(X,Y),Es).
adjacent(X,Y,graph(_,Es)) :- member(e(Y,X),Es).
adjacent(X,Y,graph(_,Es)) :- member(e(X,Y,_),Es).
adjacent(X,Y,graph(_,Es)) :- member(e(Y,X,_),Es).
adjacent(X,Y,digraph(_,As)) :- member(a(X,Y),As).
Document Page
adjacent(X,Y,digraph(_,As)) :- member(a(X,Y,_),As).
Solution 4.3)
/*
* This is the code for the Farmer, Sheep, Goat and a bag of Hay Problem
* using the ADT Stack.
*
* To run give PROLOG a "go" goal.
* For example, to find a path from the west bank to the east bank,
* give PROLOG the query:
*
* go(state(w,w,w,w), state(e,e,e,e)).

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
*/
:- [adts]. /* consults (reconsults) file containing the
various ADTs (Stack, Queue, etc.) */
go(Start,Goal) :-
empty_stack(Empty_been_stack),
stack(Start,Empty_been_stack,Been_stack),
path(Start,Goal,Been_stack).
/*
* Path predicates
*/
path(Goal,Goal,Been_stack) :-
write('Solution Path Is:' ), nl,
reverse_print_stack(Been_stack).
path(State,Goal,Been_stack) :-
move(State,Next_state),
not(member_stack(Next_state,Been_stack)),
stack(Next_state,Been_stack,New_been_stack),
path(Next_state,Goal,New_been_stack),!.
/*
Document Page
* Move predicates
*/
move(state(X,X,G,C), state(Y,Y,G,C))
:- opp(X,Y), not(unsafe(state(Y,Y,G,C))),
writelist(['try farmer takes wolf',Y,Y,G,C]).
move(state(X,W,X,C), state(Y,W,Y,C))
:- opp(X,Y), not(unsafe(state(Y,W,Y,C))),
writelist(['try farmer takes goat',Y,W,Y,C]).
move(state(X,W,G,X), state(Y,W,G,Y))
:- opp(X,Y), not(unsafe(state(Y,W,G,Y))),
writelist(['try farmer takes cabbage',Y,W,G,Y]).
move(state(X,W,G,C), state(Y,W,G,C))
:- opp(X,Y), not(unsafe(state(Y,W,G,C))),
writelist(['try farmer takes self',Y,W,G,C]).
move(state(F,W,G,C), state(F,W,G,C))
:- writelist([' BACKTRACK from:',F,W,G,C]), fail.
/*
* Unsafe predicates
*/
Document Page
unsafe(state(X,Y,Y,C)) :- opp(X,Y).
unsafe(state(X,W,Y,Y)) :- opp(X,Y).
/*
* Definitions of writelist, and opp.
*/
writelist([]) :- nl.
writelist([H|T]):- print(H), tab(1), /* "tab(n)" skips n spaces. */
writelist(T).
opp(e,w).
opp(w,e).
reverse_print_stack(S) :-
empty_stack(S).
reverse_print_stack(S) :-
stack(E, Rest, S),
reverse_print_stack(Rest),
write(E), nl.

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1 out of 11
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]