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

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.

Short Answer

Expert verified

To obtain minimum cost binary search tree, we need to calculate the cost of each possible binary search tree which can be obtain from main tree. This problem can be easily solve using dynamic programming paradigm because we have subproblem as each subtree at root node will be a problem itself to calculate minimum cost of subtree.

Step by step solution

01

Binary Search Tree (BST) and Dynamic programming approach.

A binary search tree has following properties:

  • The left subtree of a node contains nodes with keys having lesser value
  • The right subtree of a node contains node with keys having greater value.
  • The left and the right subtree must also be a binary search tree.

In dynamic programming there are all possibilities and more time as compared to greedy programming. and the Dynamic programming approach always gives the accurate or correct answer. In dynamic programming have to compute only distinct function call because as soon as compute and store in one data structure so that after this reuse afterward if it is needed.

02

Defining Recurrence Relation

Here we need to define two functions:

  • Wi,j:It is the sum of all probabilities of all the nodes within that tree or subtree.
  • Ei,j:It will pick the ’r’ root node that will create further two subtrees.

Our recurrence relations are:

Ei,j=minEi,r-1+wi,j;for  irjwi,j=Wi,j-1+p

In Ei,j:, Left Subtree is fromitor-1 and Right Subtree is from r+1toj.

Wi,j:will merge these two subtrees: Left Subtree and Right subtree.

03

Algorithm

p1.nis array of words frequencies of n

fors=1tonfori=0ton-sj=i+sifi=j

data-custom-editor="chemistry" Ti,j=pifork=itoj+1Ti,j=Ti,j+pkreturnT0,n-1

It will return minimum cost binary tree.

The runtime of above algorithm will beOn2 as two nested loops are running for n and n-1times which give combined On2.

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

Given an unlimited supply of coins of denominations, we wish to make change for a value ; that is, we wish to find a set of coins whose total value is . This might not be possible: for instance, if the denominations are and 10 then we can make change for 15 but not for 12. Give an dynamic-programming algorithm for the following problem.Input:,; .Question: Is it possible to make change for using coins of denominations ?

Cutting cloth. You are given a rectangular piece of cloth with dimensions X×Y, whereX and Yare positive integers, and a list of products that can be made using the cloth. For each producti[1,n] you know that a rectangle of cloth of dimensionsai×bi is needed and that the final selling price of the product is ci. Assume the,ai biandci are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an algorithm that determines the best return on theX×Y piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired.

Yuckdonald’s is considering opening a series of restaurant along Quaint Valley Highway(QVH). The n possible locations are along a straight line, and the distances of these locations from the start of QVH are, in miles and in increasing order,m1,m22,....,mn.. The constraints are as follows:

At each location, Yuckdonald may open at most one restaurant. The expected profit from opening a restaurant at location i is given aspi, wherepi>0andi=1,2,,n.

Any two restaurants should be at least k miles apart, where k is a positive integer.

Give an efficient algorithm to compute the maximum expected total profit subject to the given constraints.

A contiguous subsequence of a list Sis a subsequence made up of consecutive elements of S. For instance, if Sis 5,15,30,10,5,40,10

then15,30,10 is a contiguous subsequence but5,15,40 is not. Give a linear-time algorithm for the following task:Input: A list of numbers a1,a2,...,an.

Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).For the preceding example, the answer would be 10,5,40,10, with a sum of 55. (Hint: For each j{1,2,...,n}, consider contiguous subsequences ending exactly at position j.)

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.)

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