You are on page 1of 4

DAA Mid Term Assessment

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

Rehan Ejaz 5/15/20 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,nu
mber,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 11 12 13 14 15 16 17 18 19 20
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 10 10 10 10 10 10 10 10 10 10
2 0 0 0 0 0 0 0 0 8 8 10 10 10 10 10 10 10 10 10 10 10
3 0 0 0 0 0 0 0 0 8 8 10 10 12 12 12 12 12 12 12 12 12
4 0 0 0 0 0 5 5 5 8 8 10 10 12 13 13 15 15 15 15 15 15

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