ADA Python Lab All 18
ADA Python Lab All 18
PART-A
1. Write a program to sort a list of N elements using
Selection Sort Technique.
2. Write a program to Solve Travelling Salesman Problem.
3. Write a Program to implement 0/1 Knapsack Problem using
dynamic Programming.
4. Write a program to implement DFS and BFS algorithm for a
graph.
5. Quick sort algorithm for sorting list of integers in
ascending order.
6. Quick Sort with Time Complexity for 5000 numbers.
7. Write a program to implement the backtracking algorithm
for the sum of subsets problem.
8. Implement function to print In-Degree, Out-Degree and to
display that matrix.
9. To Solve Optimal Binary Search Tree Problem.
PART-B
10. Write a program to find minimum and maximum value in an
array using divide and conquer technique.
11. Python program for implementation of Merge Sort.
12. Merge Sort with Time Complexity for 5000 Numbers.
13. 0-1 Fractional Knapsack problem using Greedy Solution.
14. Program for Job Sequencing problem using Greedy Technique.
15. Python program to solve N Queen Problem using backtracking
16. Create Adjacency Matrix for entered graph.
17. Prim’s Algorithm for Calculating MST using Greedy
Technique.
18. Kruskal’s Algorithm to calculate MST using Greedy
Technique.
Part – A
def Selection_Sort(array):
for i in range(0, len(array) - 1):
smallest = i
for j in range(i + 1, len(array)):
if array[j] < array[smallest]:
smallest = j
array[i], array[smallest] = array[smallest], array[i]
print("pass",i+1,"=",array)
array = input('Enter the list of numbers: ').split()
array = [int(x) for x in array]
Selection_Sort(array)
print('List after sorting is : ', end='')
print(array)
graph = [
[0, 20, 25, 10],
[20, 0, 35, 55],
[25, 35, 0, 40],
[10, 55, 40, 0]
]
src = 0
print("Possible Paths from source ",src)
total=tsp(graph, src)
print("Minimum cost among these path =", total);
def bfs(graph,start,path):
q=[start]
path.append(start)
while q:
node=q.pop(0)
for v in graph[node]:
if not v in path:
path.append(v)
q.append(v)
def dfs(graph,node,path):
path.append(node)
for v in graph[node]:
if v not in path:
dfs(graph,v,path)
graph={'A':['B','C'], 'B':['D','E'], 'C':['F'], 'D':[], 'E':[],
'F':[]}
dfs_path=[]
bfs_path=[]
dfs(graph,'A',dfs_path)
bfs(graph,'A',bfs_path)
print("DFS travel path:",dfs_path)
print("BFS travel path:",bfs_path)
import random
def partition(array, low, high):
pivot = array[high]
print("Pivot=",pivot,end='=')
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[high]) = (array[high], array[i + 1])
return i + 1
def quickSort(array, low, high):
if low < high:
pi = partition(array, low, high)
print(array)
quickSort(array, low, pi - 1)
quickSort(array, pi + 1, high)
alist = random.sample(range(1, 100), 10)
print('the unsorted elements are:',alist)
quickSort(alist, 0, len(alist)-1)
print('Sorted list: ', end='')
print(alist)
import random
import time
def partition(array, low, high):
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[high]) = (array[high], array[i + 1])
return i + 1
def sum_of_subset(s,k,rem):
x[k]=1
if s+my_list[k]==t_sum:
list1=[]
for i in range (0,k+1):
if x[i]==1:
list1.append(my_list[i])
print( list1 )
elif s+my_list[k]+my_list[k+1]<=t_sum :
sum_of_subset(s+my_list[k],k+1,rem-my_list[k])
if s+rem-my_list[k]>=t_sum and s+my_list[k+1]<=t_sum :
x[k]=0
sum_of_subset(s,k+1,rem-my_list[k])
my_list=[]
n=int(input("Enter number of elements: "))
print("Enter the number list")
total=0
for i in range (0,n):
ele=int(input())
my_list.append(ele)
total=total+ele
my_list.sort()
t_sum=int(input("Enter required Sum: "))
x=[0]*(n+1)
sum_of_subset(0,0,total)
Part – B
global N
N = int(input("Enter number of Queens: "))
def printSolution(board):
for i in range(N):
for j in range(N):
print (board[i][j],end=' ')
print()
class Graph(object):
# Initialize the matrix
def __init__(self, size):
self.adjMatrix = []
for i in range(size):
self.adjMatrix.append([0 for i in range(size)])
self.size = size
# Add edges
def add_edge(self, v1, v2):
if v1 == v2:
print("Same vertex %d and %d" % (v1, v2))
self.adjMatrix[v1][v2] = 1
self.adjMatrix[v2][v1] = 1
def __len__(self):
return self.size
import heapq
class Graph:
def __init__(self, V):
self.V = V
self.adj = [[] for _ in range(V)]
def add_edge(self, u, v, w):
self.adj[u].append((v, w))
self.adj[v].append((u, w))
def prim_mst(self):
pq = []
src = 0
key = [float('inf')] * self.V
parent = [-1] * self.V
in_mst = [False] * self.V
heapq.heappush(pq, (0, src))
key[src] = 0
while pq:
u = heapq.heappop(pq)[1]
if in_mst[u]:
continue
in_mst[u] = True
for v, weight in self.adj[u]:
if not in_mst[v] and key[v] > weight:
key[v] = weight
heapq.heappush(pq, (key[v], v))
parent[v] = u
for i in range(1, self.V):
print(f"{parent[i]} - {i}")
print("Prims Algorithm to calculate Minimum Spanning Tree")
V=int(input("Enter the number of vertices: "))
g = Graph(V)
E=int(input("Enter the number of edges: "))
for i in range(E):
print("enter the src for edge-",i+1,end=" ")
src=int(input())
print("enter the destination for edge-",i+1,end=" ")
dest=int(input())
print("enter the weight of the edge-",i+1,end=" ")
wt=int(input())
g.add_edge(src, dest,wt)
print("Selected Edges are")
g.prim_mst()
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
def addEdge(self, u, v, w):
self.graph.append([u, v, w])
def find(self, parent, i):
if parent[i] != i:
parent[i] = self.find(parent, parent[i])
return parent[i]
def KruskalMST(self):
result = []
i = 0; e = 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.find(parent, u)
y = self.find(parent, v)
if x != y:
e = e + 1
result.append([u, v, w])
self.union(parent, rank, x, y)
minimumCost = 0
for u, v, weight in result:
minimumCost += weight
print("%d -- %d == %d" % (u, v, weight))
print("Minimum Weight in MST is :", minimumCost)
########################################################