manual
manual
AIM: To write a program to implement Breadth first search algorithm using Python.
Theory:
Graph traversals:
Graph traversal means visiting every vertex and edge exactly once in a well-defined order. While
using certain graph algorithms, one must ensure that each vertex of the graph is visited exactly
once. The orders in which the vertices are visited are important and may depend upon the
algorithm or question that you are solving.
During a traversal, it is important to track the vertices have been visited. The most common way
of tracking vertices is to mark them.
There are many ways to traverse graphs. BFS is the most commonly used approach.
BFS is a traversing algorithm where traversing starts from a selected node (source or starting
node) and then traverse the graph layer wise thus exploring the neighbor nodes (nodes which are
directly connected to source node). Then move towards the next-level neighbor nodes.
As the name BFS suggests, it is required to traverse the graph breadth wise as follows:
1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
A graph can contain cycles, which may bring to the same node again while traversing the graph.
To avoid processing of same node again, a Boolean array is used which marks the node after it is
processed. While visiting the nodes in the layer of a graph, they are stored in a manner such that
you can traverse the corresponding child nodes in a similar order.
In the earlier diagram, start traversing from 0 and visit its child nodes 1, 2, and 3. Store them in
the order in which they are visited. This will allow you to visit the child nodes of 1 first (i.e. 4
and 5), then of 2 (i.e. 6 and 7), and then of 3 (i.e. 7) etc.
To make this process easy, use a queue to store the node and mark it as 'visited' until all its
neighbors (vertices that are directly connected to it) are marked. The queue follows the First In
First Out (FIFO) queuing method, and therefore, the neighbors of the node will be visited in the
order in which they were inserted in the node i.e. the node that was inserted first will be visited
first, and so on.
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
____________________________________________________________________________
Experiment No:
AIM: Write a Program to Implement Depth First Search algorithm using Python.
Theory:
It is a recursive algorithm to search all the vertices of a tree data structure or a graph. The depth-
first search (DFS) algorithm starts with the initial node of graph G and goes deeper until we find
the goal node or the node with no children.
Because of the recursive nature, stack data structure can be used to implement the DFS
algorithm. The process of implementing the DFS is similar to the BFS algorithm.
The step by step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that vertex into the
stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the
top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's
top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
The time complexity of the DFS algorithm is O(V+E), where V is the number of vertices and E
is the number of edges in the graph. The space complexity of the DFS algorithm is O(V).
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
____________________________________________________________________________
Experiment No:
AIM: Write a program to implement the Vacuum Cleaner world and calculate its performance
measure
Theory:
This world is so simple that one can describe everything that happens; it’s also a made-up world,
so one can invent many variations.
This particular world has just two locations:
Square A and square B.
The vacuum agent perceives which square it is in and whether there is dirt in the square. It can
choose to move left, move right, suck up the dirt, or do nothing.
One very simple agent function is the following:
If the current square is dirty, then suck; otherwise, move to the other square.
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
____________________________________________________________________________
Experiment No:
Theory:
Tic Tac Toe is one of the most played games and is the best game that you can play anywhere
with just a pen and paper. The game is played by two individuals. First, we draw a board with a
3×3 square grid. The first player chooses ‘X’ and draws it on any of the square grid, then it’s the
chance of the second player to draw ‘O’ on the available spaces. Like this, the players draw ‘X’
and ‘O’ alternatively on the empty spaces until a player succeeds in drawing 3 consecutive marks
either in the horizontal, vertical or diagonal way. Then the player wins the game otherwise the
game draws when all spots are filled.
First, let’s check the steps to build Tic Tac Toe program in Python:
Theory:
What is backtracking?
Backtracking is finding the solution of a problem whereby the solution depends on the previous
steps taken. For example, in a maze problem, the solution depends on all the steps you take one-
by-one. If any of those steps is wrong, then it will not lead us to the solution. In a maze problem,
we first choose a path and continue moving along it. But once we understand that the particular
path is incorrect, then we just come back and change it. This is what backtracking basically is.
In backtracking, we first take a step and then we see if this step taken is correct or not i.e.,
whether it will give a correct answer or not. And if it doesn’t, then we just come back and change
our first step. In general, this is accomplished by recursion. Thus, in backtracking, we first start
with a partial sub-solution of the problem (which may or may not lead us to the solution) and
then check if we can proceed further with this sub-solution or not. If not, then we just come back
and change it.
If not, then come back and change the sub-solution and continue again
One of the most common examples of the backtracking is to arrange N queens on an NxN
chessboard such that no queen can strike down any other queen. A queen can attack horizontally,
vertically, or diagonally. The solution to this problem is also attempted in a similar way. We first
place the first queen anywhere arbitrarily and then place the next queen in any of the safe places.
We continue this process until the number of unplaced queens becomes zero (a solution is found)
or no safe place is left. If no safe place is left, then we change the position of the previously
placed queen.
Conclusion:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
___.
Experiment No:
Theory:
The popular Sudoku puzzle has introduced millions of people to constraint satisfaction problems,
although they may not recognize it. A Sudoku board consists of 81 squares, some of which are
initially filled with digits from 1 to 9. The puzzle is to fill in all the remaining squares such that
no digit appears twice in any row, column, or 3 × 3 box (see Figure 6.4). A row, column, or box
is called a unit. The Sudoku puzzles that are printed in newspapers and puzzle books have the
property that there is exactly one solution. Although some can be tricky to solve by hand, taking
tens of minutes, even the hardest Sudoku problems yield to a CSP solver in less than 0.1 second.
A Sudoku puzzle can be considered a CSP with 81 variables, one for each square. We use the
variable names A1 through A9 for the top row (left to right), down to I1 through I9 for the
bottom row. The empty squares have the domain {1, 2, 3, 4, 5, 6, 7, 8, 9} and the prefilled
squares have a domain consisting of a single value.
Time complexity: O(9(N*N)), For every unassigned index, there are 9 possible options so the
time complexity is O(9^(n*n)).
Like all other Backtracking problems, Sudoku can be solved by assigning numbers one by one
to empty cells. Before assigning a number, check whether it is safe to assign.
Check that the same number is not present in the current row, current column and current
3X3 subgrid. After checking for safety, assign the number, and recursively check whether this
assignment leads to a solution or not. If the assignment doesn’t lead to a solution, then try the
next number for the current empty cell. And if none of the number (1 to 9) leads to a solution,
return false and print no solution exists.
Conclusion:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
___.
Experiment No:
Theory:
Travelling Salesman Problem (TSP) : Given a set of cities and distances between every pair of
cities, the problem is to find the shortest possible route that visits every city exactly once and
returns to the starting point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to
find if there exists a tour that visits every city exactly once. Here we know that Hamiltonian Tour
exists (because the graph is complete) and in fact, many such tours exist, the problem is to find a
minimum weight Hamiltonian Cycle.
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is
1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.
The problem is a famous NP-hard problem. There is no polynomial-time known solution for this
problem.
10 + 25 + 30 + 15 := 80
implementation
1. Consider city 1 as the starting and ending point. Since the route is cyclic, we can consider any
point as a starting point.
2. Generate all (n-1)! permutations of cities.
3. Calculate the cost of every permutation and keep track of the minimum cost permutation.
4. Return the permutation with minimum cost.
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________.