Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Develop an algorithm for finding a longest common subsequence of two sequence a1,a2,...,amand b1,b2,...,bnusing the values L(i,j)found by the algorithm in Exercise 17.

Short Answer

Expert verified

The procedurelcsuba1,a2....,am:sequencewithm1;b1,b2,...bn:sequencewithn1

l=lengthlcsumm,n

If am=bnthen

cl=am

c1c2.....cl-1=lcsuba1,a2,...,am-1;b1,b2,...,bn-1

If ambnthen

c=lcsuba1,a2,...,am-1;b1,b2,...,bn

d=lcsuba1,a2,...,am;b1,b2,...,bn-1

Ifc>dthen

c1c2.....cl=lcsuba1,a2,...,am-1;b1,b2,...,bn-1

Else

c1c2.....cl=lcsuba1,a2,...,am-1;b1,b2,...,bm-1

Returnc1c2....cl

Step by step solution

Achieve better grades quicker with Premium

  • Unlimited AI interaction
  • Study offline
  • Say goodbye to ads
  • Export flashcards

Over 22 million students worldwide already upgrade their learning with Vaia!

01

Given information

We have given that,

The values L(i,j)found by the algorithm in Exercise17 .

02

Definition and formula to be used

In mathematics, the term "dynamic programming" refers to the process of breaking down a decision into a series of decision steps over time in order to simplify it, this is accomplished by creating a series of value functions V1,V2,...,Vn,each of which takes y as an argument and represents the state of the system at time ifrom 1to n.

We will use the formula of Dynamic programming algorithm

Fn=Fn-1+Fn-2

With base values FO=0and F1is equal to1 .

03

Calculation

From the previous exercise17 , we will use the algorithm to compute all lengthsL(i,j) which is necessary in this algorithm.

So, The algorithm "lcsub" and the input are 2sequencesa1,a2,...am andb1,b2,...bn .

procedurelcsuba1,a2....,am:sequencewithm1;b1,b2,...bn:sequencewithn1

Now, we compute the length of the longest common subsequence,

So, the number of elements is,

l:=lengthlcsumm,n

04

Solve the length of longest common subsequence

Now, we will solve the length of the longest common subsequence is,

If am=bn, then the last common element is,

am=bn

Then, we will determine the remaining l-1elements byexercise15.

If am=bnthen

cl=am

So,

c1c2.....cl-1=lcsuba1,a2,...,am-1;b1,b2,...,bn-1

Now,

If ambnthen

Lm,n=maxLm-1,n,Lm,n-1

If Lm-1,n>Lm,n-1, then the longest common subsequence has to be the longest common subsequence of a1,a2,...am-1and b1,b2,...bn.

If Lm-1,nLm,n-1, then the longest common subsequence has to be the longest common subsequence of a1,a2,...amand b1,b2,...bn-1.

Now,

If ambnthen

c=lcsuba1,a2,...,am-1;b1,b2,...,bnand dis equal to lcsuba1,a2,...,am;b1,b2,...,bn-1.

If c>dthen c1c2.....cl=lcsuba1,a2,...,am-1;b1,b2,...,bn-1else c1c2.....cl is equal to lcsuba1,a2,...,am-1;b1,b2,...,bm-1

So, finally we will return the longest common subsequence of the all above given sequences.

So,

Return c1c2....cl

Thus, the solution of the given condition is,

procedure lcsuba1,a2....,am:sequencewithm1;b1,b2,...bn:sequencewithn1

l=lengthlcsumm,n

If am=bnthen

cl=am

c1c2.....cl-1=lcsuba1,a2,...,am-1;b1,b2,...,bn-1

If ambnthen

c=lcsuba1,a2,...,am-1;b1,b2,...,bn

d=lcsuba1,a2,...,am;b1,b2,...,bn-1

If c>dthen

c1c2.....cl=lcsuba1,a2,...,am-1;b1,b2,...,bn-1

Else

c1c2.....cl=lcsuba1,a2,...,am-1;b1,b2,...,bm-1

Return\({{c}_{1}}{{c}_{2}}.....{{c}_{l}}\)

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free