20AIL58A (Lab Manual Final) - 1
20AIL58A (Lab Manual Final) - 1
20AIL58A (Lab Manual Final) - 1
Academic Year:2022-2023
BATCH: 2021 – 2025
YEAR: II
SEMESTER: IV
Vision
Mission
To strengthen the theoretical, practical and ethical dimensions of the learning process by
fostering a culture of research and innovation among faculty members and students.
To encourage long-term interaction between the academia and industry through their
involvement in the design of curriculum and its hands-on implementation.
To strengthen and mould students in professional, ethical, social and environmental dimensions
by encouraging participation in co-curricular and extracurricular activities.
To develop value based socially responsible professionals for the betterment of the society
Quality Policy
Values
❖ Innovation ❖ Inclusiveness
Vision
Mission
To disseminate strong theoretical and practical exposure to meet the emerging trends in the
industry.
To promote a freethinking environment with innovative research and teaching-learning
pedagogy.
To develop value based socially responsible professionals with high degree of leadership skills
will support for betterment of the society.
Develop and excel in their chosen profession on technical front and progress
PEO1 towards advanced continuing education or Inter-disciplinary Research and
Entrepreneurship
Become a reputed innovative solution provider- to complex system problems or
PEO2 towards research or challenges relevant to Artificial Intelligence and Machine
learning
Progress as skilled team members achieving leadership qualities with trust and
PEO3 professional ethics, pro-active citizens for progress and overall welfare of the
society
Program Specific Outcomes (PSOs)
PSO1: Develop models in Data Science, Machine learning, Deep learning and Bigdata
PO2 Problem analysis: Identify, formulate, review research literature, and analyze
sciences.
meet the specified needs with appropriate consideration for the public health and
conclusions.
PO5 Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and
limitations.
PO6 The Engineer and Society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent
Engineering.
contexts, demonstrate the knowledge of, and need for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics, responsibilities,
PO9 Individual and Team Work: Function effectively as an individual, and as a member
with the Engineering community and with society, such as, being able to comprehend
and write effective reports and design documentation, make effective presentations,
the Engineering and management principles and apply these to one’s own work, as a
Environments.
PO12 Life-long Learning: Recognize the need for, and have the preparation and ability to
change.
DESIGN AND ANALYSIS OF ALGORITHM LABORATORY
Course Outcomes: At the end of the Course, the Student will be able to
CO# COURSE OUTCOME
20AIL58A. Analyze the complexities of various applications in different domains
1
20AIL58A. Implement efficient algorithms to solve problems in various domains
2
20AIL58A. Use suitable design technique to develop efficient algorithms
3
20AIL58A. Compare, implement and understand when to apply various design techniques
4
Reference Book:
1. Thomas H Cormen, Charles E Leiserson, Ronald R Rivest& Clifford Stein, “Introduction to
Algorithms”, THIRD Edition, Eastern Economy Edition
Exp. List of
No Experiments
1 Write a program to find GCD of two numbers using different algorithms
2 Write a program to implement string matching using Brute force
3 Write a program to implement Merge Sort
4 Write a program to implement Quick Sort
5 Write a program to obtain minimum cost spanning tree using Prim’s Algorithm
6 Write a program to obtain minimum cost spanning tree using Kruskal’s Algorithm
7 Write a program to obtain shortest path using Djikstra’s algorithm
8 Write a program to obtain shortest path using Floyds algorithms
9 Write a program to compute Transitive closure using Warshall’s algorithm
10 Write a program to implement Topological sorting
11 Write a program to implement Subset Sum problem using Backtracking
12 Write a program to implement N Queens problem using Backtracking
LAB RUBRICS
1. Continuous Assessment:
i) Will be carried out in every lab (for labs -10 programs)
ii) Each program will be evaluated for 10 marks
iii) Totally for 12 lab programs it will be 120 marks. This will be scaled down to 10.
iv) During the semester, 2 internal tests will be conducted for 25 marks each. The total 50 marks for
the internal tests, will be scaled down to 15.
Break up of 25 marks (for each of the 2 internal tests) which is scaled down to 15marks after the conduction of 2
internal tests:
The 1st lab internal will comprise of the first 6 lab programs and the 2nd lab internal will comprise of the next
6 lab programs.
Assessment Marks: 25
Session End Examination is conducted for 50 marks which is scaled down to 25 marks.
def gcdc(m,n):
t=min(m, n)
while t > 0:
if m % t == 0 and n % t == 0:
return t
t =t - 1
1:Euclid’s Algorithm
24
24
EXERCISE 2
def string_match(a,b):
a=" "+a+" "
b=" "+b+" "
n=len(a)
m=len(b)
count=0
for i in range(n-m+1):
if a[i:i+m]==b:
print("pattern found at",i+1)
count=count+1
if count==0:
print("pattern not found")
OUTPUT
5
EXERCISE 3
for i in range(0,n1):
L[i] = arr[l + i]
for j in range(0 , n2):
R[j] = arr[m + 1+j]
i=0
j=0
k=l
OUTPUT
Given array is 12 11 13 5 6 7
Sorted array is
5
6
7
11
12
13
EXERCISE 4
low = 0
up = len(Array) - 1
def partition(Array,low,up):
i = low+1
j = up
pivot = Array[low]
while(i<=j):
while(Array[i]<=pivot and i<up): // move I to right until element of i>pivot
i = i+1
while(Array[j]>pivot): //move j to left until element of j is lesser than pivot
j = j-1
def quick(Array,low,up):
if(low>=up):
return
piv_loc = partition(Array,low,up)
quick(Array,low,piv_loc-1)
quick(Array,piv_loc+1,up)
quick(Array,low,up)
print('Sorted elements are:')
for i in Array:
print (i);
OUTPUT
5. Write a program to obtain minimum cost spanning tree using Prim’s Algorithm
INF = 9999999
# number of vertices in graph
N=5
#creating graph by adjacency matrix method
G = [[0, 19, 5, 0, 0],
[19, 0, 5, 9, 2],
[5, 5, 0, 1, 6],
[0, 9, 1, 0, 1],
[0, 2, 6, 1, 0]]
selected_node = [0, 0, 0, 0, 0]
no_edge = 0
selected_node[0] = True
minimum = INF
a=0
b=0
for m in range(N):
if selected_node[m]:
for n in range(N):
if ((not selected_node[n]) and G[m][n]):
# not in selected and there is an edge
if minimum > G[m][n]:
minimum = G[m][n]
a=m
b=n
print(str(a) + "-" + str(b) + ":" + str(G[a][b]))
selected_node[b] = True
no_edge += 1
OUTPUT
Edge : Weight
0-2:5
2-3:1
3-4:1
4-1:2
EXERCISE 6
6. Write a program to obtain minimum cost spanning tree using Kruskal’s Algorithm
class Graph:
def __init__(self, vertex):
self.V = vertex
self.graph = []
def kruskal(self):
result = []
i, e = 0, 0
self.graph = sorted(self.graph, key=lambda item: item[2])
parent = []
rank = []
for node in range(self.V):
parent.append(node)
rank.append(0)
while e < self.V - 1:
u, v, w = self.graph[i]
i=i+1
x = self.search(parent, u)
y = self.search(parent, v)
if x != y:
e=e+1
result.append([u, v, w])
self.apply_union(parent, rank, x, y)
for u, v, weight in result:
print("Edge:",u, v,end =" ")
print("-",weight)
g = Graph(5)
g.add_edge(0, 1, 4)
g.add_edge(0, 2, 4)
g.add_edge(1, 2, 2)
g.add_edge(1, 0, 4)
g.add_edge(2, 0, 4)
g.add_edge(2, 1, 2)
g.add_edge(2, 3, 3)
g.add_edge(2, 5, 2)
g.add_edge(2, 4, 4)
g.add_edge(3, 2, 3)
g.add_edge(3, 4, 3)
g.add_edge(4, 2, 4)
g.add_edge(4, 3, 3)
g.add_edge(5, 2, 2)
g.add_edge(5, 4, 3)
g.kruskal()
self.graph = sorted(self.graph, key=lambda item: item[2]
for node in range(self.V):
34 parent.append(node)
35 rank.append(0)
Edge 2-----------------4
Parent(2)=2,parent(4)=4.So edge can be formed.
Rank of 2 and 4 are 0.
Now, let us point 2 to 4 2----🡪4
So, now parent(2)=4.
And new absolute parent will have one rank higher,So
update rank(4)=1
Edge 0-----------------2
Parent(0)=1,parent(2)=4. So edge can be formed.
Rank(0) =rank(1)=1
Rank(2)=Rank(4)=1
Both have same rank , so point 1 to 4 .Now parent(1) =4,
update rank(4)to 2
Edge 2-----------------3
Parent(2)=4 Parent(3)=parent(1)=4.. So same parent cannot form
OUTPUT
Edge: 0 2 - 5
Edge: 3 4 - 7
Edge: 0 1 - 8
Edge: 2 4 - 10
EXERCISE 7
g.dijkstra(0)
OUTPUT
def floyd(G):
dist = list(map(lambda p: list(map(lambda q: q, p)), G))
for r in range(nV):
for p in range(nV):
for q in range(nV):
dist[p][q] = min(dist[p][q], dist[p][r] + dist[r][q])
sol(dist)
def sol(dist):
for p in range(nV):
for q in range(nV):
if(dist[p][q] == INF):
print("INF", end=" ")
else:
print(dist[p][q], end=" ")
print(" ")
0 5 15 10
20 0 10 5
30 35 0 15
15 20 5 0
EXERCISE 9
def transitiveClosure(self,graph):
reach =[i[:] for i in graph]
for k in range(self.V):
for i in
range(self.V):
for j in range(self.V):
reach[i][j] = reach[i][j] or (reach[i][k] and reach[k]
[j]) self.printSolution(reach)
g= Graph(4)
graph = [[1, 1, 0, 1],
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]]
g.transitiveClosure(graph)
OUTPUT
Following matrix transitive closure of the given graph
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
EXERCISE 10
10. Write a program to implement Topological sorting
from collections import defaultdict
class Graph:
def __init (self,vertices):
self.graph = defaultdict(list) #dictionary containing adjacency
List self.V = vertices #No. of vertices
def addEdge(self,u,v):
self.graph[u].append(v)
def topologicalSortUtil(self,v,visited,stack):
visited[v] = True
for i in self.graph[v]:
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
stack.insert(0,v)
def topologicalSort(self):
visited = [False]*self.V
stack =[]
for i in range(self.V):
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
print stack
g= Graph(6)
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
print "Following is a Topological Sort of the given
graph" g.topologicalSort()
OUTPUT
Following is a Topological Sort of the given graph
[5, 4, 2, 3, 1, 0]
EXERCISE 11
OUTPUT
- -Q–
Q - - -
- --Q
- Q--
- Q--
- --Q
Q-- -
- Q--
VIVA QUESTIONS
1.Define Algorithm
2.What are the steps in fundamentals of Algorithmic problem solving
3.Define Searching
4.What are the examples of searching
5.Which is best merge sort or quick sort?
6.What is the time complexity of Merge sort?
7.What is the worst-case time complexity of Quick sort?
8. What is the best-case time complexity of Strassen’s matrix multiplication?
9.Specify the advantages and disadvantages of divide and conquer?
10.What are the criteria for constructing minimum spanning tree?
11.Which is best method for constructing MST?
12. Define dynamic programming?
13. What is the time complexity of Dijkstra’s algorithm
14.Explain transitive closure property of Warshall’s algorithm?
15.Difference between BFS and DFS
16.Difference between AVL trees and binary search trees
17.In how many ways a queen can be attacked in 4-queens problem
18.Define Travelling Salesman problem
19.Which method is used to solve subset sum problem?
20.Difference between decrease and conquer and divide and conquer?