Implementation of DFS for water jug problem using PROLOG.

 

1.    Implementation of DFS for water jug problem using PROLOG.

 

Description :-



Problem statement :

  we have two jugs, a 5-gallon (5-g) and the other 3-gallon (3-g) with no measuring marker on them. There is endless supply of water through tap. Our task is to get 4 gallon of water in the 5-g jug.

Solution: state space for this problem can be described as the set of ordered pairs of integers ( X, Y ) such that X represents the number of gallons of water in 5-g jug and Y for 3-g jug.

Source Code:


go:-start(Start),solve(Start,Solution),reverse(Solution,L),print(L). solve(Node,Solution):-dfs([],Node,Solution). dfs(Path,Node,[Node|Path]):-goal(Node). dfs(Path,Node,Sol):-next_state(Node,Node1), \+member(Node1,Path), dfs([Node|Path],Node1,Sol). start((0,0)). goal((4,_)). next_state((X,Y),(5,Y)):-X<5 .="" next_state="">0,X<5>=3,Z is Y-(5-X). next_state((X,Y),(Z,3)):-X>0,Y<3>=3,Z is X-(3-Y). next_state((X,Y),(Z,0)):-Y>0,X+Y=<5 is="" next_state="" y="">0,X+Y=<3 is="" next_state="" x="">0. next_state((X,Y),(X,0)):-Y>0. print([]). print([H|T]):-write(H),nl,print(T). code-box

OutPut :



Algorithm working :

The algorithm works by first finding out all the possible moves right from the initial state which is (0,0) all the way till the goal state (2,X)

It then searches Breadthwise until the required node is found then exits successfully with the shortest procedure to that node.

0/Post a Comment/Comments

Thanks, for contacting us.
We will reviewing us on your comments . Please wait an hour.
Air Reality Team....

Previous Post Next Post