Let us define a multiplication operation on three symbols a,b,caccording to the following table; thus ab=b,ba=c, and so on. Notice that the multiplication operation defined by the table is neither associative nor commutative.

Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting expression is . For example, on input bbbbacyour algorithm should return yes because((b(bb))(ba))c=a.

Short Answer

Expert verified

We are going to use Dynamic Programming approach to examine the string and check if the strings can be parenthesize. In any dynamic programming, first approach is to create a sub-problem and the call it recursively.

Step by step solution

01

Defining the subproblem

Let us assume thats[1n]be original string that appear in any order

Let f(i,j)=setofallpossiblevaluesofthesubstrings[ij]for1ijn

Here, i and j are starting and ending position of a string and length of string would be (j-i).

If a belongs tof(i,j), then our algorithm will return true, else false.

Thus, if our output ‘a’ belongs to f(1,n)that means there we can parenthesize the string.

02

Recursion Expressiona

Let us assume a variable ‘j’ is use to find the position where the string s[ii+p]is separated.

Now, because of ‘j’ we know about the split position of the string into two sub-result, and we can compute the result by taking union of the sub-results.

Thus we have,

localid="1657276451955" f(i,i+k)=iji+k{mn:mf(i,j),nf(j+1,i+k)}

Now fori=j

There would be only one character in entires[1.n], which would be

  • Iff(i,i,x)=TRUE

Thens[i]=x

ElseFALSE

  • localid="1657277053120" Iff(i,i,x)=TRUEThens[i]=x

Else localid="1657275948758" FALSE

Now if i>j, this means our substring s[i.j]comprises of more than one character.

In this case if s[i.j]can be split into two parts, thenf(i,j,x)=TRUE.

03

Implementing Algorithm

We have matrix f() which will be storing value of ‘f’

Initiatef(n,n,3)

for(i=1ton)for(j=1ton)for(ka,b,c)if(i==jANDp[i]==k)f(i,j,k)=1elsef(i,j,k)=-1

function valid(i,j,m)

if(f(i,j,m)-1)

return f(i,j,m)

Now in this if our program return value of matrix f(i,j,m), this means the string would be parenthesize, and resulting expression will return ‘a’.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

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

One App. One Place for Learning.

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

Get started for free

Most popular questions from this chapter

Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1,a2,...,an; a positive integer t. Question: Does some subset of the ai’s add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form “does a subset of{a1,a2,...,ai} add up to ?”)

Consider the following variation on the change-making problem (Exercise 6.17): you are given denominations x1,x2,...,xn, and you want to make change for a value v, but you are allowed to use each denomination at most once. For instance, if the denominations are 1,5,10,20,then you can make change for 16=1+15and for 31=1+10+20but not for 40(because you can’t use 20 twice).

Input: Positive integers; x1,x2,...,xnanother integer v.

Output: Can you make change for v, using each denominationxi at most once?Show how to solve this problem in time O(nV).

Local sequence alignment. Often two DNA sequences are significantly different, but contain regions that are very similar and are highly conserved. Design an algorithm that takes an input two strings x[1Kn]and y[1Km]and a scoring matrix δ(as defined in Exercise 6.26), and outputs substrings x'andy'of x and y respectively, that have the highest-scoring alignment over all pairs of such substrings. Your algorithm should take time O(mn).

Suppose two teams, A and B, are playing a match to see who is the first to win games (for some particular n). We can suppose that A and B are equally competent, so each has a 50% chance of winning any particular game. Suppose they have already played i+j games, of which A has won i and B has won j. Give an efficient algorithm to compute the probability that A will go on to win the match. For example, if i=n-1 and j=n-3 then the probability that A will win the match is 78, since it must win any of the next three games.

Optimal binary search trees. Suppose we know the frequency with which keywords occur in programs of a certain language, for instance:

begin5%do40%else8%end4%

if10%then10%while23%

We want to organize them in a binary search tree, so that the keyword in the root is alphabetically bigger than all the keywords in the left subtree and smaller than all the keywords in the right subtree (and this holds for all nodes). Figure 6.12 has a nicely-balanced example on the left. In this case, when a keyword is being looked up, the number of comparisons needed is at most three: for instance, in finding “while”, only the three nodes “end”, “then”, and “while” get examined. But since we know the frequency 196 Algorithms with which keywords are accessed, we can use an even more fine-tuned cost function, the average number of comparisons to look up a word. For the search tree on the left, it is

cost=1(0.04)+2(0.40+0.10)+3(0.05+0.08+0.10+0.23)=2.42

By this measure, the best search tree is the one on the right, which has a cost of Give an efficient algorithm for the following task. Input: n words (in sorted order); frequencies of these words: p1,p2,...,pn.

Output: The binary search tree of lowest cost (defined above as the expected number of comparisons in looking up a word).

Figure 6.12 Two binary search trees for the keywords of a programming language.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free