03-134181-026 Daa PDF
03-134181-026 Daa PDF
03-134181-026 Daa PDF
(Spring-2020,Semester)
Muhammad Arif
Bahria University Lahore Campus
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:
}
• Else
{
matrix[row][column] = matrix[row - 1][column];
}
}
}
page. 3