0% found this document useful (0 votes)
33 views8 pages

22BDS0212 DAA Assignment 2

The document summarizes the analysis of various divide and conquer and dynamic programming algorithms conducted by a student. It includes pseudocode and code implementations of: 1) Dynamic programming algorithms - assembly line scheduling, matrix chain multiplication, longest common subsequence, 0/1 knapsack problem 2) Divide and conquer algorithms - maximum subarray problem, Karatsuba multiplication algorithm The output for each implementation is also presented.

Uploaded by

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

22BDS0212 DAA Assignment 2

The document summarizes the analysis of various divide and conquer and dynamic programming algorithms conducted by a student. It includes pseudocode and code implementations of: 1) Dynamic programming algorithms - assembly line scheduling, matrix chain multiplication, longest common subsequence, 0/1 knapsack problem 2) Divide and conquer algorithms - maximum subarray problem, Karatsuba multiplication algorithm The output for each implementation is also presented.

Uploaded by

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

School of Computer Science and Engineering

Student Name: Arin Parmod Singla


Reg No: 22BDS0212
E-mail : - [email protected]
Mobile: 9930570385
Faculty Name: Shalini L

Subject Name with code: Design and Analysis of Algorithm Lab – BCSE204P

Assessment No.: 02
Date of Submission: 18/02/2024

Aim: - The objective of this assessment is to analyse 2 divide and conquer algorithms-
Maximum Sub-Array and Karatsuba Algorithm – and 4 Dynamic Programming algorithms-
Matrix Chain multiplication, Longest Common subsequence, Assembly Line Scheduling and
0/1 Knapsack

Objective: - To conduct a thorough analysis of divide and conquer algorithms (Maximum


Sub-Array, Karatsuba) and dynamic programming algorithms (Matrix Chain Multiplication,
Longest Common Subsequence, Assembly Line Scheduling, 0/1 Knapsack), exploring
theoretical foundations, algorithmic efficiency, and practical applications.

Pseudocode: -
Code: -
1)Dynamic Programming: -
a)Assembly Line Scheduling : -
def
time_scheduling(station_time,switch_time,curr_line,curr_station,exit1,exit2,n)
:
if curr_station == n-1:
if curr_line == 0:
return exit1
else:
return exit2
same =
time_scheduling(station_time,switch_time,curr_line,curr_station+1,exit1,exit2,
n) + station_time[curr_line][curr_station+1]
diff = time_scheduling(station_time,switch_time,not
curr_line,curr_station+1,exit1,exit2,n) + station_time[not
curr_line][curr_station+1] + switch_time[curr_line][curr_station+1]
return min(same,diff)
n = 4
a = [[4, 5, 3, 2], [2, 10, 1, 4]]
t = [[0, 7, 4, 5], [0, 9, 2, 8]]
e1 = 10
e2 = 12
x1 = 18
x2 = 7
x = time_scheduling(a, t, 0, 0, x1, x2, n) + e1 + a[0][0]
y = time_scheduling(a, t, 1, 0, x1, x2, n) + e2 + a[1][0]
print(f'The minimum time required to complete the task is: {min(x, y)}')
Output: -

b)Matrix chain multiplication: -


def matrix_chain_multiplication(dimensions):
n = len(dimensions) - 1
m = [[0] * n for i in range(n)]
s = [[0] * n for i in range(n)]
for i in range(n):
m[i][i] = 0
for chain_length in range(2, n + 1):
for i in range(n - chain_length + 1):
j = i + chain_length - 1
m[i][j] = float('inf')
for k in range(i, j):
cost = m[i][k] + m[k + 1][j] + dimensions[i] * dimensions[k +
1] * dimensions[j + 1]
if cost < m[i][j]:
m[i][j] = cost
s[i][j] = k
def print_optimal_parenthesization(i, j):
if i == j:
print(f' Mat{i + 1}', end='')
else:
print('(', end='')
print_optimal_parenthesization(i, s[i][j])
print_optimal_parenthesization(s[i][j] + 1, j)
print(')', end='')
print('Optimal Parenthesization:', end=' ')
print_optimal_parenthesization(0, n - 1)
print('\nMinimum Number of Multiplications:', m[0][n - 1])
matrix_dimensions = [7,6,5,4]
matrix_chain_multiplication(matrix_dimensions)
Output: -

c)Longest Common Sub sequence:-


def lcs_algo(S1, S2, m, n):
L = [[0 for x in range(n+1)] for x in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0:
L[i][j] = 0
elif S1[i-1] == S2[j-1]:
L[i][j] = L[i-1][j-1] + 1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])
index = L[m][n]
lcs_algo = [""] * (index+1)
lcs_algo[index] = ""
i = m
j = n
while i > 0 and j > 0:
if S1[i-1] == S2[j-1]:
lcs_algo[index-1] = S1[i-1]
i -= 1
j -= 1
index -= 1
elif L[i-1][j] > L[i][j-1]:
i -= 1
else:
j -= 1
print("LCS: " + "".join(lcs_algo))
S1 = "AGGTAB"
S2 = "GXTXAYB"
m = len(S1)
n = len(S2)
lcs_algo(S1, S2, m, n)
Output: -

d)0/1 Knapsack Problem:-


def knapsack(weights, profits, W):
n = len(weights)
w=dict()
for i in range(n):
w[profits[i]]=weights[i]
w = dict(sorted(w.items(), key=lambda x: x[1]))
weights = list(w.values())
profits = list(w.keys())
tab = [[0 for i in range(W + 1)] for j in range(n)]
for i in range(n):
for j in range(W + 1):
if i == 0 or j == 0:
tab[i][j] = 0
elif weights[i] <= j:
tab[i][j] = max(tab[i-1][j], tab[i-1][j-weights[i]] +
profits[i])
else:
tab[i][j] = tab[i-1][j]
return tab[n-1][W]
weights = [10, 20, 30]
profits = [60, 100, 120]
W = 50
print(f'The maximum profit obtainable is: {knapsack(weights, profits, W)}')
Output: -

2)Divide and Conquer : -


a)Maximum Subarray : -
def maxSubArray(nums):
if len(nums) == 1:
return nums[0], nums
mid = len(nums) // 2
left_max, left_subarray = maxSubArray(nums[:mid])
right_max, right_subarray = maxSubArray(nums[mid:])
left_sum = -100000
current_sum = 0
start_index = mid - 1
for i in range(mid-1, -1, -1):
current_sum += nums[i]
if current_sum > left_sum:
left_sum = current_sum
start_index = i
right_sum = -100000
current_sum = 0
end_index = mid
for i in range(mid, len(nums)):
current_sum += nums[i]
if current_sum > right_sum:
right_sum = current_sum
end_index = i
cross_max = left_sum + right_sum
cross_subarray = nums[start_index:end_index+1]
return max(left_max, right_max, cross_max), cross_subarray
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
max_sum, max_subarray = maxSubArray(nums)
print("Maximum Subarray Sum:", max_sum)
print("Maximum Subarray:", max_subarray)
Output: -

b)Karatsuba Algorithm: -
def karatsuba(x,y):
if x<10 or y<10:
return x*y
else:
n=max(len(str(x)),len(str(y)))
half=n//2
a=x//(10**(half))
b=x%(10**(half))
c=y//(10**(half))
d=y%(10**(half))
ac = karatsuba(a,c)
bd = karatsuba(b,d)
ad_plus_bc=karatsuba(a+b, c+d)-ac-bd
return ac*(10**(2*half)) + (ad_plus_bc*(10**half)) + bd

x=1226
y=1345
xy=karatsuba(x,y)
print("Product of",x,"and",y,"is",xy)
Output: -

You might also like