/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q6E Let us define a multiplication o... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

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[1…n]be original string that appear in any order

Let f(i,j)=setofallpossiblevaluesofthesubstrings[i…j]for1≤i≤j≤n

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[i……i+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)=∪i≤j≤i+k{mn:m∊f(i,j),n∊f(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(k∊a,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 91Ó°ÊÓ!

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

The garage sale problem (courtesy of Professor Lofti Zadeh). On a given Sunday morning, there are n garage sales going on, g1,g2,g3............gn. For each garage sale gj, you have an estimate of its value to you, vj. For any two garage sales you have an estimate of the transportation cost dijof getting from gito gj. You are also given the costs d0jand dj0of going between your home and each garage sale. You want to find a tour of a subset of the given garage sales, starting and ending at home, that maximizes your total benefit minus your total transportation costs. Give an algorithm that solves this problem in time O(n22n).

(Hint: This is closely related to the traveling salesman problem.)

Here is yet another variation on the change-making problem (Exercise 6.17). Given an unlimited supply of coins of denominations x1,x2,x3.........xnwe wish to make change for a value v using at most k coins; that is, we wish to find a set of≤kcoins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 and k=6, then we can make change for 55 but not for 65. Give an efficient dynamic-programming algorithm for the following problem. Input: ; x1,x2,x3.........xn;k;v.Question: Is it possible to make change for v using at most k coins, of denominations x1,x2,x3.........xn?

Counting heads. Given integersn and k, along with p1,...,pn∈[0,1], you want to determine the probability of obtaining exactly heads when biased coins are tossed independently at random, where pi is the probability that the ith coin comes up heads. Give an 0(n2)algorithm for this task. Assume you can multiply and add two numbers in [0,1]in 0(1)time.

Reconstructing evolutionary trees by maximum parsimony. Suppose we manage to sequence a particular gene across a whole bunch of different species. For concreteness, say there are n species, and the sequences are strings of length k over alphabet∑={A,C,G,T}. How can we use this information to reconstruct the evolutionary history of these species?

Evolutionary history is commonly represented by a tree whose leaves are the different species, whose root is their common ancestor, and whose internal branches represent speciation events (that is, moments when a new species broke off from an existing one). Thus we need to find the following:

• An evolutionary tree with the given species at the leaves.

• For each internal node, a string of length K: the gene sequence for that particular ancestor.

For each possible tree T annotated with sequencess(u)∈∑kat each of its nodes , we can assign a score based on the principle of parsimony: fewer mutations are more likely.

localid="1659249441524" score(T)=∑(u.v)∈E(T)(numberofpositionsonwhichs(u)ands(v)disagree)

Finding the highest-score tree is a difficult problem. Here we will consider just a small part of it: suppose we know the structure of the tree, and we want to fill in the sequences s(u) of the internal nodes u. Here’s an example with k=4 and n=5:


(a) In this particular example, there are several maximum parsimony reconstructions of the internal node sequences. Find one of them.

(b) Give an efficient (in terms of n and k ) algorithm for this task. (Hint: Even though the sequences might be long, you can do just one position at a time.)

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1···xi+k-1=yjyj+1···yj+k-1. Show how to do this in time0(mn)

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.