Adsa Lab Manual-2-27
Adsa Lab Manual-2-27
Adsa Lab Manual-2-27
Department Vision:
To produce Industry ready Software Engineers to meet the
challenges of 21st Century.
Department Mission:
Impart core knowledge and necessary skills in Computer Science and
Engineering through innovative teaching and learning methodology.
2
2. PO, PEO& PSO Statements
PROGRAMME OUTCOMES (POs)
3
Program Educational Objectives (PEOs):
PEO 1:Graduates will be prepared for analyzing, designing, developing and testing the
software solutions and products with creativity and sustainability.
PEO 2: Graduates will be skilled in the use of modern tools for critical problem
solvingandanalyzing industrial and societal requirements.
PEO 3:Graduates will be prepared with managerial and leadership skills for career
andstarting up own firms.
Program Specific Outcomes (PSOs):
PSO 1:Develop creative solutions by adapting emerging technologies / tools for real
timeapplications.
PSO 2: Apply the acquired knowledge to develop software solutions and innovative
mobileapps for various automation applications
MON
TUE
WED ADSA
THU
FRI
SAT
4
LIST OF EXPERIMENTS (SYLLABUS)
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR
Course Objectives:
Course Outcomes(CO):
5
11. Write a program to find the solution for a 0-1 knapsack problem using dynamic programming.
12. Write a program to solve Sum of subsets problem for a given set of distinct numbers using
backtracking.
13. ImplementNQueen'sproblemusingBackTrackin
6
1. Write a program to implement the following operations on Binary Search Tree:
a) Insert b) Delete c) Search d) Display
return
root# Find
the node to be deleted
if key < root.key:
7
root.left = deleteNode(root.left, key)
elif(key > root.key):
root.right = deleteNode(root.right, key)
else:
# If the node is with only one child or no child
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
# If the node has two children,
# place the inorder successor in position of the node to be deleted
temp = minValueNode(root.right)
root.key = temp.key
# Delete the inorder successor
root.right = deleteNode(root.right, temp.key)
return root
root = None
root = insert(root, 8)
root = insert(root, 3)
root = insert(root, 1)
root = insert(root, 6)
root = insert(root, 7)
root = insert(root, 10)
root = insert(root, 14)
root = insert(root, 4)
print("Inorder traversal: ", end=' ')
inorder(root)
print("\nDelete 10")
root = deleteNode(root, 10)
print("Inorder traversal: ", end=' ')
inorder(root)
OUTPUT
Inorder traversal: 1-> 3-> 4-> 6-> 7-> 8-> 10-> 14->
Delete 10
Inorder traversal: 1-> 3-> 4-> 6-> 7-> 8-> 14->
8
2. Write a program to perform a Binary Search for a given set of integer values.
OUTPUT
Element is present at index 1
9
3. Write a program to implement Splay trees.
class Node:
def init (self, data):
self.data = data
self.parent = None
self.left = None
self.right = None
class SplayTree:
def init (self):
self.root = None
def maximum(self, x):
while x.right != None:
x = x.right
return x
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != None:
y.left.parent = x
y.parent = x.parent
if x.parent == None: #x is root
self.root = y
elif x == x.parent.left: #x is left child
x.parent.left = y
else: #x is right child
x.parent.right = y
y.left = x
x.parent = y
def right_rotate(self, x):
y = x.left
x.left = y.right
if y.right != None:
y.right.parent = x
y.parent = x.parent
if x.parent == None: #x is root
self.root = y
elif x == x.parent.right: #x is right child
x.parent.right = y
else: #x is left child
x.parent.left = y
y.right = x
x.parent = y
def splay(self, n):
while n.parent != None: #node is not root
if n.parent == self.root: #node is child of root, one rotation
10
if n == n.parent.left:
self.right_rotate(n.parent)
else:
self.left_rotate(n.parent)
else:
p = n.parent
g = p.parent #grandparent
if n.parent.left == n and p.parent.left == p: #both are left children
self.right_rotate(g)
self.right_rotate(p)
elif n.parent.right == n and p.parent.right == p: #both are right children
self.left_rotate(g)
self.left_rotate(p)
elif n.parent.right == n and p.parent.left == p:
self.left_rotate(p)
self.right_rotate(g)
elif n.parent.left == n and p.parent.right == p:
self.right_rotate(p)
self.left_rotate(g)
def insert(self, n):
y = None
temp = self.root
while temp != None:
y = temp
if n.data < temp.data:
temp = temp.left
else:
temp = temp.right
n.parent = y
if y == None: #newly added node is root
self.root = n
elif n.data < y.data:
y.left = n
else:
y.right = n
self.splay(n)
def search(self, n, x):
if x == n.data:
self.splay(n)
return n
elif x < n.data:
return self.search(n.left, x)
elif x > n.data:
return self.search(n.right, x)
else:
return None
11
def delete(self, n):
self.splay(n)
left_subtree = SplayTree()
left_subtree.root = self.root.left
if left_subtree.root != None:
left_subtree.root.parent = None
right_subtree = SplayTree()
right_subtree.root = self.root.right
if right_subtree.root != None:
right_subtree.root.parent = None
if left_subtree.root != None:
m = left_subtree.maximum(left_subtree.root)
left_subtree.splay(m)
left_subtree.root.right = right_subtree.root
self.root = left_subtree.root
else:
self.root = right_subtree.root
def inorder(self, n):
if n != None:
self.inorder(n.left)
print(n.data)
self.inorder(n.right)
if name == ' main ':
t = SplayTree()
a = Node(10)
b = Node(20)
c = Node(30)
d = Node(100)
e = Node(90)
f = Node(40)
g = Node(50)
h = Node(60)
i = Node(70)
j = Node(80)
k = Node(150)
l = Node(110)
m = Node(120)
t.insert(a)
t.insert(b)
t.insert(c)
t.insert(d)
t.insert(e)
t.insert(f)
t.insert(g)
t.insert(h)
t.insert(i)
12
t.insert(j)
t.insert(k)
t.insert(l)
t.insert(m)
t.delete(a)
t.delete(m)
t.inorder(t.root)
13
4. Write a program to implement Merge sort for the given list of integer values.
14
if l < r:
# Same as (l+r)//2, but avoids overflow for large l and h
m = l+(r-l)//2
# Sort first and second halves
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
# Driver code to test above
arr = [12, 11, 13, 5, 6, 7]
n = len(arr)
print("Given array is")
for i in range(n):
print("%d" % arr[i]),
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i]),
OUTPUT
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
15
5. Write a program to implement Quick Sort for the given list of integer values.
# Python program for implementation of Quicksort Sort
def partition(arr, low, high):
i = (low-1) # index of smaller element
pivot = arr[high] # pivot
for j in range(low, high):
# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:
# increment index of smaller element
i = i+1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)
# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index
# Function to do Quick sort
def quickSort(arr, low, high):
if len(arr) == 1:
return arr
if low < high:
# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr, low, high)
# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr, 0, n-1)
print("Sorted array is:")
for i in range(n):
print("%d" % arr[i])
16
OUTPUT
Sorted array is:
1
5
7
8
9
10
17
6. Write a program to find the solution for the knapsack problem using the greedy method.
# Greedy Approach
class FractionalKnapSack:
# Driver Code
If name == " main ":
wt = [10, 40, 20, 30]
val = [60, 40, 100, 120]
capacity = 50
OUTPUT:
19
7. Write a program to find minimum cost spanning tree using
Prim's Algorithm
INF = 9999999
# number of vertices in graphN = 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
20
8. Write a program to find minimum cost spanning tree using Kruskal’s algorithm.
21
1
# The main function to construct MST using Kruskal's# algorithm
def KruskalMST(self):
result = [] # This will store the resultant MST
22
# An index variable, used for sorted edgesi = 0
# An index variable, used for result[ ]e = 0
# Step 1: Sort all the edges in
# non-decreasing order of their
# weight. If we are not allowed to change the# given graph,
we can create a copy of graph
self.graph = sorted(self.graph, key=lambda item: item[2])parent = []
rank = []
# Create V subsets with single elementsfor node in
range(self.V):
parent.append(node
)rank.append(0)
# Number of edges to be taken is equal to V-1while e < self.V
- 1:
# Step 2: Pick the smallest edge and
increment# the index for next iteration
u, v, w =
self.graph[i]i = i + 1
x = self.find(parent,
u)y =
self.find(parent, v)
# If including this edge does't
# cause cycle, include it in result
# and increment the indexof
result# for next edge
if x != y:
e=e+1
result.append([u, v,
w])
self.union(parent, rank, x,
y)# Else discard the edge
minimumCost = 0
print ("Edges in the constructed
MST")for u, v, weight in result:
minimumCost += weight
print("%d -- %d == %d" % (u, v,
# Driver weight)) print("Minimum Spanning Tree" ,
codeg = minimumCost)
Graph(4)
g.addEdge(0, 1, 10)
g.addEdge(0, 2, 6)
23
g.addEdge(0, 3, 5)
g.addEdge(1, 3, 15)
g.addEdge(2, 3,
4)# Function call
g.KruskalMST()
OUOTPUT:
Edges in the constructed
MST2 - - 3 = = 4
0--3==5
0 - - 1 = = 10
Minimum Spanning Tree 19
24
9. Write a program to find a single source shortest path for a given graph.
import sys
class Graph():
def init (self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
def printSolution(self, dist):
print "Vertex \tDistance from Source"
for node in range(self.V):
print node, "\t", dist[node]
def minDistance(self, dist, sptSet):
min = sys.maxint
for u in range(self.V):
if dist[u] < min and sptSet[u] == False:
min = dist[u]
min_index = u
return min_index
def dijkstra(self, src):
dist = [sys.maxint] * self.V
dist[src] = 0
sptSet = [False] * self.V
for cout in range(self.V):
x = self.minDistance(dist, sptSet)
sptSet[x] = True
for y in range(self.V):
if self.graph[x][y] > 0 and sptSet[y] == False and \
dist[y] > dist[x] + self.graph[x][y]:
dist[y] = dist[x] + self.graph[x][y]
self.printSolution(dist)
g = Graph(9)
g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
];
g.dijkstra(0);
25
Output:
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
26
Program 10:
Title:
Write a program to find the solution for job sequencing with deadlines problems.
Program:
def printJobScheduling(arr, t):n =
len(arr)
for i in range(n):
for j in range(n - 1 - i):
if arr[j][2] < arr[j + 1][2]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
result = [False] * tjob = ['-
1'] * t
for i in range(len(arr)):
for j in range(min(t - 1, arr[i][1] - 1), -1, -
1):if result[j] is False:
result[j] = True
job[j] =
arr[i][0]break
print(job)
arr = [['a', 2, 100],
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]]
print("Following is maximum profit sequence of jobs")
printJobScheduling(arr, 3)
OUTPUT:
Following is maximum profit sequence of
jobs['c', 'a', 'e']
27