3 8 (Amar)
3 8 (Amar)
Shubham A20405220146
Shubham A20405220146
- Check if (new_monkey_x, new_monkey_y, new_box_x, new_box_y) is not in the visited set.
g. If the new state is valid, enqueue new_state into the queue.
5. If the loop exits without finding a solution, return None to indicate that no solution exists.
Code:
Shubham A20405220146
Output:
Subham A20405220146
Experiment- 04
Aim: To implement Tic Tac Toe Problem
Algorithm:
Shubham A20405220146
Fig 4.1: Tic Tac Toe
Shubham A20405220146
Shubham A20405220146
Shubham A20405220146
Shubham A20405220146
Experiment- 05
Aim: Implement 8 Puzzle Problem
8 Puzzle Problem
A 3 by 3 board with 8 tiles (each tile has a number from 1 to 8) and a single empty space is
provided. The goal is to use the vacant space to arrange the numbers on the tiles such that they
match the final arrangement. Four neighbouring (left, right, above, and below) tiles can be
moved into the available area.
Shubham A20405220146
4. Open and Closed Lists: Create two lists, the "open" list (to store states to be explored) and the
"closed" list (to store states already explored).
5.A Search: Use the A search algorithm to explore states. At each step, consider all possible
moves from the current state, evaluate their cost based on the heuristic function, and select the
one with the lowest cost.
6. RepeatUntil Goal: Continue exploring states and selecting moves until the goal state is
reached or no more moves are possible.
7. Backtracking:Once the goal state is reached, backtrack from the goal state to the initial state to
find the sequence of moves that solved the puzzle.
Shubham A20405220146
By avoiding searching in sub-trees which do not include an answer node, an "intelligent" ranking
function, also known as an approximatsion costs function, may frequently speed up the search
for an answer node. However, instead of using the backtracking method, it does a BFS-style
search.
Basically, Branch and Bound involves three different kinds of nodes.
1.A live node is a created node whose children have not yet been formed. 2.The offspring of the
E-node which is a live node, are now being investigated. Or to put it another way, an E-node is a
node that is currently expanding. 3.A created node which is not to be developed or examined
further is referred to as a dead node. A dead node has already extended all of its offspring.
Costs function:
In the search tree, each node Y has a corresponding costs. The next E-node may be found using
the costs function. The E-node with the lowest costs is the next one. The function can be defined
as :
1.C(Y) = g(Y) + h(Y)
2.where
3.g(Y) = the costs of reaching to the current node
4.from the root.
5.h(Y) = the costs of reaching to an answer node from the Y.
The optimum costs function for an algorithm for 8 puzzles is : We suppose that it will costs one
unit to move a tile in any direction. In light of this, we create the following costs function for the
8-puzzle algorithm :
1.c(y) = f(y) + h(y)
2.where
3.f(y) = the path's total length from the root y.
4.and
5.h(y) = the amount of the non-blank tiles which are not in
6.their final goal position (misplaced tiles).
7.To change state y into a desired state, there are at least h(y) movements required.
Final algorithm :
1. In
order to maintain the list of live nodes, algorithm LCSearch employs the functions Least()
and Add().
2. Least() identifies a live node with the least c(y), removes it from the list, and returns it.
Shubham A20405220146
3. Add(y) adds y to the list of live nodes.
4. Add(y) implements the list of live nodes as a min-heap.
The route taken by the aforementioned algorithm to arrive at the final configuration of the 8-
Puzzle from the starting configuration supplied is shown in the diagram below. Keep in mind
that only nodes with the lowest costs function value are extended.
Shubham A20405220146
Shubham A20405220146
Shubham A20405220146
Shubham A20405220146
Experiment- 06
Aim: Implement Tower of Hanoi Problem
The Rule For disk movement in TOH:
The rules according to which the disks have to be moved in Tower of Hanoi are the following:
1. Initially, the disks are placed in Increasing Order in size on Tower 1 from top to bottom.
2. the objective is to move them to tower 2, making also use of an auxiliary tower 3.
3. the conditions for moving the disks are:
4. all disks (except the one to be moved) have to be on one of the three towers.
5. Only one disk is possible to move at a time.
6. The only top disk can be moved i.e. taking from the top of one tower and placing on the
top of another tower.
7. A larger disk can never be placed on a smaller disk.
if we will have n number of disk then our aim is to move bottom which is disk n from source to
destination. And then put all other (n-1) disks onto it.
if n == 1:
Shubham A20405220146
return
TowerOfHanoi(n-1, source, destination, auxiliary) // Move n-1 disks from source to auxiliary
Move disk from source to destination // Move the largest disk from source to destination
TowerOfHanoi(n-1, auxiliary, source, destination) // Move n-1 disks from auxiliary to destination
Shubham A20405220146
Experiment- 07
Aim: Implement Water Jug Problem
Algorithm:
1. Import the deque class from the collections module.
2. Define the BFS function with parameters a (capacity of Jug1), b (capacity of Jug2), and target
(the target amount to measure).
3. Initialize a dictionary m to keep track of visited states, a boolean variable isSolvable to check
if a solution exists, and an empty list path to store the path from the initial state to the solution
state.
4. Create a deque q and enqueue the initial state (0, 0) as a tuple (Jug1's current amount, Jug2's
current amount).
5. Perform a BFS traversal while the queue q is not empty:
a. Dequeue the front element u.
b. Check if the state (u[0], u[1]) has already been visited. If so, continue to the next iteration.
c. Check if the current state is valid (within the jug capacities and non-negative).
d. Add the current state to the path list.
e. Mark the current state as visited in the m dictionary.
f. Check if the target amount is reached in either Jug1 or Jug2. If so, set isSolvable to True and
break from the loop.
g. Enqueue the following states:
- Filling Jug2 to its maximum capacity.
- Filling Jug1 to its maximum capacity.
- Pouring water from Jug2 to Jug1 and vice versa, while maintaining valid states.
- Emptying Jug2 and Jug1 separately.
6. If a solution is found (isSolvable is True), print the path from the initial state to the solution
state.
7. If no solution is found (isSolvable is still False), print "No solution."
Code:
Shubham A20405220146
Shubham A20405220146
Output:
Shubham A20405220146
Experiment- 08
Aim: Implement Travelling Salesman Problem
The traveling salesman problems abide by a salesman and a set of cities. The salesman has to
visit every one of the cities starting from a certain one (e.g., the hometown) and to return to the
same city. The challenge of the problem is that the traveling salesman needs to minimize the
total length of the trip. Suppose the cities are x 1 x2..... xn where cost cij denotes the cost of
travelling from city xi to xj. The travelling salesperson problem is to find a route starting and
ending at x1 that will take in all cities with the minimum cost.
Algorithm-
Travelling salesman problem takes a graph G {V, E} as an input and declare another
graph as the output (say G’) which will record the path the salesman is going to take from
one node to another.
The algorithm begins by sorting all the edges in the input graph G from the least distance
to the largest distance.
The first edge selected is the edge with least distance, and one of the two vertices (say A
and B) being the origin node (say A).
Then among the adjacent edges of the node other than the origin node (B), find the least
cost edge and add it onto the output graph.
Continue the process with further nodes making sure there are no cycles in the output
graph and the path reaches back to the origin node A.
If the origin is mentioned in the given problem, then the solution must always start from
that node only.
Shubham A20405220146
Shubham A20405220146