0% found this document useful (0 votes)
16 views22 pages

Shri Vaishnav Vidyapeeth Vishwavidyalaya

The document discusses an artificial intelligence practical assignment on Python basics, arrays, lists, depth first search, breadth first search, and the water jug problem. Code and outputs are provided for some of the problems.

Uploaded by

neeraj petel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Shri Vaishnav Vidyapeeth Vishwavidyalaya

The document discusses an artificial intelligence practical assignment on Python basics, arrays, lists, depth first search, breadth first search, and the water jug problem. Code and outputs are provided for some of the problems.

Uploaded by

neeraj petel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ARTIFICIAL INTELLIGENCE BTCS502N

SHRI VAISHNAV VIDYAPEETH VISHWAVIDYALAYA

SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

January-July 2023

NAME: NEERAJ PATIL


ENROLLMENT NUMBER : 2110DMTCSE10147
SUBJECT: ARTIFICIAL INTELLIGENCE
SUBJECT CODE: (BTCS502N)
YEAR/ SEM: 3rd / 6th SEM
BRANCH: CSE
SECTION: H
SUBMITTED TO: PROF. PRIYANKA SHARMA
NEERAJ PATIL 2110DMTCSE10147
ARTIFICIAL INTELLIGENCE
BTCSDS311

INDEX

S.N PRACTICAL DATE REMARKS


O
1) PYTHON BASIC PROGRAM 21/02/23
( hello world, functions, datatypes,
operators, loops).
2) WRITE A PROGRAM FOR ARRAY 28/02/23
AND LIST.
3) WRITE A PROGRAM FOR DEPTH 14/03/23
FIRST SEARCH (DFS).
4) WRITE A PROGRAM FOR 21/03/23
BREADTH FIRST SEARCH (BFS).

5) WRITE A PROGRAM FOR WATER 28/03/23


JUG PROBLEM.
6) WRITE A PROGRAM FOR TOWER 11/04/23
OF HANOI ALGORITHM.
7) WRITE A PROGRAM FOR MAGIC 10/04/23
SQUARE PROBLEM.

8) WRITE A PROGRAM FOR TIC 25/03/23


TAC TOE PROBLEM.

NEERAJ PATIL 2110DMTCSE10147


ARTIFICIAL INTELLIGENCE BTCS502N

PRACTICAL 01

AIM: PYTHON BASIC PROGRAM (hello world, functions, datatypes,

operators, loops).

THEORY:
This program begins with a simple "Hello World" statement printed to the console.

The next section demonstrates how to create a function in Python. The add_numbers function
takes two arguments, a and b, and returns their sum. The function is then called with a equal
to 2 and b equal to 3, and the result is printed to the console.

The next section demonstrates different data types in Python, including integers, floats,
strings, and booleans.

The next section demonstrates different operators in Python, including addition, subtraction,
multiplication, division, modulus, and exponentiation.

Finally, the program demonstrates a simple for loop that prints the numbers from 0 to 9 to the
console.

CODE:

NEERAJ PATIL 2110DMTCSE10147


ARTIFICIAL INTELLIGENCE
BTCSDS311

OUTPUT:

NEERAJ PATIL 2110DMTCSE10147


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

2 AIM:
WRITE A PROGRAM FOR ARRAY AND LIST.

THEORY:

Array in Python

An array is also a linear data structure that stores the data. It is also ordered,
mutable, and enclosed in square brackets. It can store the non-unique items. But
there are restrictions to store the different data type values.

To work with the Array in Python, we need to import either an array module or
a Numpy.

Import array as ar

Import numphy as np

List in Python:

A list in Python is used to store the sequence of various types of data. A list can
be defined as a collection of values or items of different types. Python lists are
mutable type which implies that we may modify its element after it has been
formed. The items in the list are separated with the comma (,) and enclosed with
the square brackets [].

Although Python has six data types that may hold sequences, the list is the most
popular and dependable form. The collection of data is stored in a list, a
sequence data type. Similar sequence data formats are Tuples and String.

Python lists are identical to dynamically scaled arrays that are specified in other
languages, such as Java's ArrayList and C++'s vector. A list is a group of items
that are denoted by the symbol [] and subdivided by commas.

Example:

thislist = ["apple", "banana", "cherry"]

print(thislist)

NEERAJ PATIL 2110DMTCSE10147


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

WRITE A PROGRAM FOR

CODE:

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

OUTPUT:

AIM: DEPTH FIRST SEARCH (DFS).

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 –

 First, create a stack with the total number of vertices in the graph.

• Now, choose any vertex as the starting point of traversal, and push that vertex
into the stack.

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

WRITE A PROGRAM FOR

• After that, push a non-visited vertex (adjacent to the vertex on the top of the
stack) to the top of the stack.
• Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on
the stack's top.
• If no vertex is left, go back and pop a vertex from the stack.
• Repeat steps 2, 3, and 4 until the stack is empty.

Applications of DFS algorithm

The applications of using the DFS algorithm are given as follows –

• DFS algorithm can be used to implement the topological sorting.


• It can be used to find the paths between two vertices.
• It can also be used to detect cycles in the graph.
• DFS algorithm is also used for one solution puzzles.
• DFS is used to determine if a graph is bipartite or not.

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

CODE AND OUTPUT:

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

WRITE A PROGRAM FOR

AIM: BREADTH FIRST SEARCH (BFS).

THEORY:

Breadth-first search is a graph traversal algorithm that starts traversing the graph
from the root node and explores all the neighboring nodes. Then, it selects the
nearest node and explores all the unexplored nodes. While using BFS for
traversal, any node in the graph can be considered as the root node.

There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of
a tree or graph data structure. BFS puts every vertex of the graph into two
categories - visited and non-visited. It selects a single node in a graph and, after
that, visits all the nodes adjacent to the selected node.

Applications of BFS algorithm

The applications of breadth-first-algorithm are given as follows - o BFS can be


used to find the neighboring locations from a given source location.

o In a peer-to-peer network, BFS algorithm can be used as a traversal


method to find all the neighboring nodes. Most torrent clients, such as
BitTorrent, uTorrent, etc. employ this process to find "seeds" and "peers"
in the network.
o BFS can be used in web crawlers to create web page indexes. It is one of
the main algorithms that can be used to index web pages. It starts
traversing from the source page and follows the links associated with the
page. Here, every web page is considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree. o
BFS is also used in Cheney's technique to duplicate the garbage
collection. o It can be used in ford-Fulkerson method to compute the
maximum flow in a flow network.

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

CODE AND OUTPUT:

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

WRITE A PROGRAM FOR

ANKIT PATIDAR
20100BTCSBS07457
ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0

AIM: WRITE A PROGRAM FOR

WATER JUG PROBLEM

THEORY:

Problem Statement
Given two water jugs with capacities X and Y litres. Initially, both the jugs are
empty. Also given that there is an infinite amount of water available. The jugs do
not have markings to measure smaller quantities.
One can perform the following operations on the jug: 

Fill any of the jugs completely with water.

• Pour water from one jug to the other until one of the jugs is either empty or
full, (X, Y) -> (X – d, Y + d)
• Empty any of the jugs

The task is to determine whether it is possible to measure Z litres of water using


both the jugs. And if true, print any of the possible ways.

Example:

Input: X = 4, Y = 3, Z = 2

Explanation:

• Fill the 4 litre jug completely with water.


• Empty water from 4-litre jug into 3-litre (leaving 1L water in 4L jug and 3L
completely full).
• Empty water from 3L.
• Pour water from 4L jug into 3L jug (4L being completely empty and 1L water
in 3L litre jug)
• Fill the 4L jug with water completely again.

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0 AIM:
WRITE A

THEROY:

• Transfer water from 4L jug to 3L jug, resulting in 2L water in 4L jug.

CODE AND OUTPUT:

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

PROGRAM FOR TOWER OF HANOI ALGORITHM.

Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N
disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest
disk is placed on the top and they are on rod A. The objective of the puzzle is to move the
entire stack to another rod (here considered C), obeying the following simple rules:

• Only one disk can be moved at a time.


• Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a
stack.
• No disk may be placed on top of a smaller disk.

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0 AIM:
WRITE A

THEROY:

CODE AND OUTPUT:

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0 AIM:
WRITE A

THEROY:

PROGRAM FOR MAGIC SQUARE PROBLEM.

A magic square of order n is an arrangement of n2 numbers, usually distinct


integers, in a square, such that the n numbers in all rows, all columns, and both
diagonals sum to the same constant. A magic square contains the integers from 1 to
n2.

The constant sum in every row, column and diagonal are called the magic constant
or magic sum, M. The magic constant of a normal magic square depends only on n
and has the following value:

M = n(n2+1)/2

For normal magic squares of order n = 3, 4, 5, ..., the


magic constants are: 15, 34, 65, 111, 175, 260, ...

Three conditions hold:

• The position of next number is calculated by decrementing row number of the


previous number by 1, and incrementing the column number of the previous
number by 1. At any time, if the calculated row position becomes -1, it will
wrap around to n-1. Similarly, if the calculated column position becomes n, it
will wrap around to 0.
• If the magic square already contains a number at the calculated position,
calculated column position will be decremented by 2, and calculated row
position will be incremented by 1.
• If the calculated row position is -1 & calculated column position is n, the new
position would be: (0, n-2).

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

CODE AND OUTPUT:

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

PRACTICAL 0 AIM:
WRITE A

THEROY:

PROGRAM FOR TIC TAC TOE PROBLEM.

Tic-Tac-Toe is among the games played between two players played on a 3 x 3


square grid. Each player inhabits a cell in their respective turns, keeping the
objective of placing three similar marks in a vertical, horizontal, or diagonal pattern.
The first player utilizes the Cross (X) as the marker, whereas the other utilizes the
Naught or Zero (O).
Tic-tac-toe is a very popular game, so let’s implement an automatic Tic-tac-toe
game using Python. The game is automatically played by the program and hence, no
user input is needed. Still, developing an automatic game will be lots of fun. Let’s
see how to do this. NumPy and random Python libraries are used to build this game.
Instead of asking the user to put a mark on the board, the code randomly chooses a
place on the board and put the mark. It will display the board after each turn unless a
player wins. If the game gets drawn, then it returns -1.

Explanation: play_game() is the main function, which performs the following tasks :

• Calls create_board() to create a 3×3 board and initializes with 0.


• For each player (1 or 2), calls the random_place() function to randomly
choose a location on board and mark that location with the player number,
alternatively.
• Print the board after each move.

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

• Evaluate the board after each move to check whether a row or column or
diagonal has the same player number. If so, displays the winner’s name.
If after 9 moves, there is no winner then displays -1.

CODE:

ANKIT PATIDAR 20100BTCSBS07457


ARTIFICIAL INTELLIGENCE BTCSDS311

OUTPUT:

ANKIT PATIDAR 20100BTCSBS07457

You might also like