Prathamesh Foa 52

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

FOA PRACTICAL JOURNAL

Name:-Prathamesh P. Sonkamble Roll No:-52

Exam Seat No:52

Satish Pradhan Dnyanasadhana College


Thane
Certificate

This is to certify that Mr. : PRATHAMESH PRUTHVEERAJ SONKAMBLE of


SYBSC.Computer Science (Semester-IV) Class has been successfully
completed allthe practical work in subject Fundamental Of Algorithm(FOA)
,under the guidance of Prof. Kavita Chowk (Subject in charge) during Year
2021-22 inpartial fulfillment of Computer Science Practical Examination
conducted by University of Mumbai.

Subject in charge
Head of the
Department

Date

1
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

INDEX
Sr.No. PRACTICAL NAME DATE

1 Write a Python program to perform matrix multiplication. 17/11/21

2 Write a Python program to sort n names using Quick sort algorithm. 18/11/21
Discuss the complexity of algorithms used.

3 Write a Python program to sort n numbers using Merge sort 20/11/21


algorithm. Discuss the complexity of algorithms used.

4 Write a Python program for inserting an element into a binary tree 23/11/21

5 Find the smallest and largest number and get input from the user. 24/11/21

6 Write a Python program for finding the smallest and largest elements 26/11/21
in an array A of size n using Selection algorithm.

7 Write a Python program for implementing Strassen's Matrix 30/11/21


multiplication using Divide and Conquer method. Discuss the
complexity of algorithms.

8 Write a Python program for checking whether a given graph G has a 02/12/21
simple path from source s to destination d. Assume the graph G is
represented using an adjacency matrix.

9 Write a Python program for finding the second largest element in an 07/12/21
array A of size n using Tournament Method.

10 Write a Python program for implementing Huffman Coding 00/12/21


Algorithm. Discuss the complexity of algorithms.

2
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

PRACTICAL NO:-1 DATE:- 17-11-21


Aim:- Write a Python program to perform matrix multiplication.
Source Code:-
a = [[1,2,4],[3,4,5],[8,4,2]]
b = [[0,1,3],[5,6,2],[1,8,2]]
c = [[0,0,0],[0,0,0],[0,0,0]]
d = [[0,0,0],[0,0,0],[0,0,0]]
e = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
c[i][j] = c[i][j]+a[i][k]*b[k][j]
d[i][j] = d[i][j]+a[i][k]+b[k][j]
e[i][j] = e[i][j]+a[i][k]-b[k][j]
print("Select operation:")
print("1:Multiplication:")
print("2:Addition:")
print("3:Subtraction:")
print("4:Exit")
while True:
ch = int(input("Enter choice:"))
if ch==1:
c[i][j] = c[i][j]+a[i][k]*b[k][j]
print(c)
elif ch==2:
d[i][j] = d[i][j]+a[i][k]+b[i][j]
print(d)
elif ch==3:
e[i][j] = e[i][j]+a[i][k]-b[k][j]
print(e)
else:
print("Exit")

3
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

O/P:-

PRACTICAL NO:-2 DATE:- 18-11-21


Aim:- Write Python program to sort n names using Quick sort algorithm. Discuss
the complexity of algorithm used.
Source Code:-
def partition(arr,low,high):
i = (low-1)
pivot = arr[high]
for j in range(low,high):
if arr[j]<=pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return (i+1)
def quickSort(arr,low,high):
if low<high:
pi = partition(arr,low,high)
quickSort(arr,low,pi-1)
quickSort(arr,pi+1,high)
arr = [52,37,63,14,17,8,6,25]
n = len(arr)
quickSort(arr,0,n-1)
print("Sorted arry is:")
for i in range(n):
print("%d"%arr[i])

4
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

O/P:-

PRACTICAL NO:-3 DATE:- 20-11-21


Aim:-Write Python program to sort n numbers using Merge sort algorithm.
Discuss the complexity of algorithm used.
Source Code:-
def mergeSort(nlist):
print("Splitiong",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]

mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
nlist[k] = lefthalf[i]
i=i+1
else:
nlist[k] = righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):

5
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

nlist[k] = lefthalf[i]
i=i+1
k=k+1

while j<len(righthalf):
nlist[k] = righthalf[j]
j=j+1
k=k+1
print("Merging:",nlist)
nlist = [14,46,43,27,57,41,45,21,70]
mergeSort(nlist)
print(nlist)

O/P:-

6
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

PRACTICAL NO:-4 DATE:- 23-11-21


Aim:- Write a Python program for inserting an element into a binary tree.
Source Code:-
class Node:
def _init_(self,data):
self.left=None
self.right=None
self.data=data

def insert(self,data):

if self.data:
if data<self.data:
if self.left is None:
self.left=Node(data)
else:
self.left.insert(data)
elif data>self.dat:
if self.right is None:
self.right=Node(data)
else:
self.right.insert(data)

else:
self.data=data
def PrintInordertree(self):
if self.left:
self.left.PrintInordertree()
print(self.data),
if self.right:
self.right.PrintInordertree()

def PrintPreordertree(self):
print(self.data)
if self.left:
self.left.PrintPreordertree()
if self.right:
self.right.PrintPreordertree()

7
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

def PrintPostordertree(self):
if self.left:
self.left.PrintPostordertree()
if self.right:
self.right.PrintPostordertree()
print(self.data)

root=Node(10)
root.insert(4)
root.insert(13)
root.insert(1)
root.insert(5)
print("Inorder of tree")
root.PrintInordertree()
print("Preorder of tree")
root.PrintPreordertree()
print("Postorder of tree")
root.PrintPostordertree()

O/P:-

PRACTICAL NO:-5 DATE:- 24-11-21


Aim:- Find the smallest and largest number and get input from user.
Source code:-
def smallestrarr(arr,n):
min = arr[0]
for i in range(1,n):

8
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

if arr[i]<min:
min = arr[i]
return min
def largestarr(arr,n):
max = arr[0]
for i in range(1,n):
if arr[i]>max:
max = arr[i]
return max
arr =[]
n =int(input("Enter no of element:"))
for i in range(n):
element = int(input("Enter an element:"))
arr.append(element)
print(arr)
ans = largestarr(arr,n)
print("Largest in given array is:",ans)
ans1 = smallestrarr(arr,n)
print("Smallest in given array is:",ans1)

O/P:-

9
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

PRACTICAL NO:-6 DATE:- 26-11-21


Aim:- Write Python program for finding the smallest and largest elements in an
array A of size n using Selection algorithm.
Source Code:-
def smallestrarr(arr,n):
min = arr[0]
for i in range(1,n):
if arr[i]<min:
min = arr[i]
return min
arr = [15,25,55,60]
n = len(arr)
ans = smallestrarr(arr,n)
print("Smallest in given array is:", ans)

def largestarr1(arr1,a):
max = arr1[0]
for i in range(1,a):
if arr1[i]>max:
max = arr1[i]
return max
arr1 = [15,25,55,60]
a = len(arr1)
ans1 = largestarr1(arr,a)
print("Largest in given array is:",ans1)

O/P:-

PRACTICAL NO:-7 DATE:- 30-11-21

10
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

Aim:-Write Python program for implementing Strassen's Matrix multiplication


using Divide and Conquer method. Discuss the complexity of algorithms.
Source Code:-
x = [[0,2],[0,1]]
print("matrix")
for i in range(len(x)):
print("\t",x[i])
y = [[0,0],[3,4]]
print("matrix")
for j in range(len(y)):
print("\t",y[i])

def strassen(a,b):
s = [b[0][1]-b[1][1],
a[0][0]+a[0][1],
a[1][0]+a[1][1],
b[1][0]-b[0][0],
a[0][0]+a[1][1],
b[0][0]+b[1][1],
a[0][1]-a[1][1],
b[1][0]+b[1][1],
a[0][0]-a[1][0],
b[0][0]+b[0][1]]
p = [a[0][0]*s[0],
s[1]+b[1][1],
s[2]*b[0][0],
a[1][1]*s[3],
s[4]*s[5],
s[6]*s[7],
s[8]*s[9]]
c = [[p[4]+p[3]-p[1]+p[5],p[0]+p[1]],
[p[2]+p[3],
p[4]+p[0]-p[2]-p[6]]]

print("Strassens matrix")
print(s)
print(p)
print(c)
strassen(x,y)

11
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

O/P:-

PRACTICAL NO:-8 DATE:- 02-12-21


Aim:- Write Python program for checking whether a given graph G has a simple
path from source s to destination d. Assume the graph G is represented using an
adjacency matrix.
Source Code:-
from collections import defaultdict
class Graph:
def init (self,vertices):
self.v = vertices
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def printAllPathsUtil(self,u,d,visited,path):
visited[u] = True
path.append(u)
if u==d:
print(path)
else:
for i in self.graph[u]:

12
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

if visited[i]==False:
self.printAllPathsUtil(i,d,visited,path)
path.pop()
visited[u]=False
def printAllPaths(self,s,d):
visited=[False]*(self.v)
path=[]
self.printAllPathsUtil(s,d,visited,path)

g = Graph(4)
g.addEdge(0,1)
g.addEdge(0,2)
g.addEdge(0,3)
g.addEdge(2,0)
g.addEdge(2,1)
g.addEdge(1,3)

s=2
d=3
print("Following are all different path from %d to %d:",s,d)
g.printAllPaths(s,d)

O/P:-

13
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

PRACTICAL NO:-9 DATE:- 07-12-21


Aim:- Write Python program for finding the second largest element in an array A
of size n using Tournament Method.
Source Code:-
def smallestrarr(arr,n):
min = arr[0]
for i in range(1,n):
if arr[i]<min:
min = arr[i]
return min
def largestarr(arr,n):
max = arr[0]
for i in range(1,n):
if arr[i]>max:
max = arr[i]
return max
arr =[]
n =int(input("Enter no of element:"))
for i in range(n):
element = int(input("Enter an element:"))
arr.append(element)
print(arr)
ans = largestarr(arr,n)
print("Largest in given array is:",ans)
ans1 = smallestrarr(arr,n)
print("Smallest in given array is:",ans1)

O/P:-

14
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52

PRACTICAL NO:-10 DATE:- 00-00-21


Aim:- Write Python program for implementing Huffman Coding Algorithm.
Discuss the complexity of algorithms.
Source Code:-

15

You might also like