0% found this document useful (1 vote)
292 views4 pages

DAA Mid Term Assessment (Spring-2020, Semester) : Muhammad Arif Bahria University Lahore Campus

This document contains Muhammad Arif's midterm assessment from the Spring 2020 semester at Bahria University Lahore Campus. It includes two problems solved using algorithms and code. The first problem involves finding the longest common prefix of a set of strings using divide and conquer. The algorithm runs in O(mn) time where m is the length of the longest string. The second problem involves finding the maximum number of blueberries Teresa can pick given bushes with varying amounts, using a dynamic programming approach.

Uploaded by

Hamza Phanoti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
292 views4 pages

DAA Mid Term Assessment (Spring-2020, Semester) : Muhammad Arif Bahria University Lahore Campus

This document contains Muhammad Arif's midterm assessment from the Spring 2020 semester at Bahria University Lahore Campus. It includes two problems solved using algorithms and code. The first problem involves finding the longest common prefix of a set of strings using divide and conquer. The algorithm runs in O(mn) time where m is the length of the longest string. The second problem involves finding the maximum number of blueberries Teresa can pick given bushes with varying amounts, using a dynamic programming approach.

Uploaded by

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

DAA Mid Term Assessment

(Spring-2020,Semester)
Muhammad Arif
Bahria University Lahore Campus

Rehan Ejaz BSCS 5


ENROLLMENT Number : 03-134181-026

pg. 0
Problem,1:,Find,the,Longest,Common,Prefix,(LCP),between,given,set,of,strings.,,[08,Marks],
,
Write,an,efficient,algorithm,by,using,divide,and,conquer,technique,to,find,the,longest,Comm
on,Prefix,(LCP),between,given,set,of,strings.,,

,
For,Example:,,
Input:,,technique,,technician,,technology,,technical,
Output:,,The,longest,common,prefix,is,“techn”,,

,
Calculate,the,runtime,of,algorithm,and,represent,in,theta/,Oh/,Omega,notation.,

Algorithm Code

 Function of Longest_common_prefix () which string lcp(string array[], int low, int high)
take three arguments (array , starting and
ending index).
{
 When starting and ending index become equal if (low == high)
return (array[low]); // return the
the Recursion terminate , returning first and
array 0
only index of array.
if (high > low)
 If(endingindex>sarting index) {
 Mid= (starting index + ending index) / int mid = low + high// mid = strating +
2. ending /2
 Now recursion will be used.
 String1= Longest_common_prefix str1 = lcp(array, low, mid)
(array,startingindex,mid) str2 = lcp(array, mid + 1, high)
 String2= Longest_common_prefix (array, return (prefix(str1, str2));
mid+1, endingindex) }
 Using divide and conquer approach }
recursion call and store the array both
halves in two strings string 1 and
string 2.
 Function of prefix (), takes two
arguments, which return the common
Prefix in strings.
 String3 = prefix(string1, string2)
 Return string3

page. 1
Run Time:

2⋅T(2n)+O(m)
// O(m) Is the runtime of prefix() function.

Big O Notation:
O(mn)

*************************************************************

Problem,2:,

BlueBerries Problem

Teresa,wants,to,pick,up,the,blueberries,in,such,a,way,that,she,may,not,exceed,the,limit,propo
sed.,

,,,,,,,,,,,,,,,When,picking,the,blueberries,,she,noticed,that,if,she,picks,from,the,bush,i,,she,couldn’t,pick,th
e,blueberries,at,the,bush,i+1,(some,sort,of,magic,in,rainbow,land).,

Worried,about,this,,Teresa,wants,to,know,the,maximum,blueberries,she,can,pick,,given,the,n
umber,of,bushes,and,the,number,of,blueberries,in,each,bush.,

Will,contain,an,integer,T,,then,,T,cases,will,follow,,each,case,starts,with,a,number,N,and,K,,b
eing,N,the,number,of,bushes,and,K,the,number,of,blueberries,Teresa,will,pick,as,maximum,,
the,next,line,contains,N,integers,,each,one,representing,the,blueberries,there,is,on,the,i-
th,bush.,

Requirements:,

1- Design,the,solution,of,problem,by,using,Dynamic,Programming,approach.,,
2- Write,algorithm/,code,of,solution., [12,Marks],

page. 2
Solution:

Number of bushes: 4
Limit: 20
Number of blueberries in each bush: 10, 8, 12, 5

Expected result: 15

0 1 2 3 4 5 6 7 8 9 10 1 12 13 1 15 16 1 18 19 20
1 4 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 10 1 10 10 1 10 10 1 10 10 10
0 0 0
2 0 0 0 0 0 0 0 0 8 8 10 1 10 10 1 10 10 1 10 10 10
0 0 0
3 0 0 0 0 0 0 0 0 8 8 10 1 12 12 1 12 12 1 12 12 12
0 2 2
4 0 0 0 0 0 5 5 5 8 8 10 1 12 13 1 15 15 1 15 15 15
0 3 5

Algorithm:

 Loop(column = TotalBerries[0]; column <= limit; column++)


{
matrix[1][column] = TotalBerries[0];
}

 Loop(row = 2; row <= TotalBerries; row++)


{
 Loop(column = 1; column <= limit; column++)
{
 If(TotalBerries[row - 1] <= column)
{
matrix[row][column] = max(matrix[row - 1][column], TotalBerries[row - 1] +
matrix[row - 2][column - TotalBerries[row - 1]]);

}
 Else
{
matrix[row][column] = matrix[row - 1][column];
}
}
}

page. 3

You might also like