How many lines, as a function of n (in (.)form), does the following program print? Write a recurrence and solve it. You may assume is a power of . function f (n) if n > 1:

print_line (‘‘still going’’)

f (n/2)

f (n/2)

Short Answer

Expert verified

The number of lines will be: n

Step by step solution

01

Creating Recurrence Relation

If indeed the data size n is bigger than 1 , the supplied function f(n) displays one line "still continuing" and executes the function f(n) recursively by passing the input of half the size.

That is, each call divides the issue into two half-sized sub-problems. In addition, the function displays the line in a fixed time for each call.

As a result, a recurrence relation for the number of lines printed by the provided function may be established as follows:

Tn=2Tn/2+01 …… (1)

Here, T1=0andT2=1

02

Solving Recurrence Relation

• This stored procedure recursive calls produce a recursion tree structure. Because the issue divides into two subtasks of half original size in each recursive iteration, there are two sub-problems at each level of the tree.

• Assumen=2k. That really is, n is a two-digit power. As a result, the recursion finishes at the k th level, as well as the sub-problems' input sizes are reduced to size1 . The supplied method does not display the lines when the input size is 1 .

• As a result, only the lines from level to level are printed k-1.

• Since, each 2isub problem prints the line only once and there are sub problems at level 2i, the number of times the line is printed at level i=2i.

• Add up the total number of characters printed on each level.

As a result, the overall amount of times each line is printed is calculated.

=1+21+22+...+2k-1=12k-12k-1=n-1

Thus, Tn=n-1

Then, for some constant c>0 ,

Tn=n-1c.n=0n

Also, for some constant c>0,

Tn=n-1c.n=Ωn

Thus, role="math" localid="1658921049129" Tn=Θn.

As a result, the software prints a certain amount of lines Θn.

To use the Master’s theorem to solve the recurrence:

To solve the recurrence relation, apply the master theorem (1). Compare and contrast the recurrence relation (1) with both the formula (2).

Tn=aTn/b+Ond …… (2)

Then, a=2,b=2 and d=0

Compare d and logba

Where logba=log22=1andd=0.

Since d<logba, by the third case of master theorem,

Tn=Θnlogba=Θn

Therefore, the number of lines printed by the program is Θn .

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

Thesquare of a matrix A is its product with itself, AA.

(a) Show that five multiplications are sufficient to compute the square of a 2 x 2 matrix.

(b) What is wrong with the following algorithm for computing the square of an n x n matrix?

“Use a divide-and-conquer approach as in Strassen’s algorithm, except that instead of getting 7 subproblems of size n2, we now get 5 subproblems of size n2 thanks to part (a). Using the same analysis as in Strassen’s algorithm, we can conclude that the algorithm runs in time O (nc) .”

(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if n x n matrices can be squared in time S(n) = O(nc), then any two n x n matrices can be multiplied in time O(nc) .

  1. Given two n x n matrices A and B, show that the matrix AB + BA can be computed in time 3S(n) + O(n2 ) .
  2. Given two n x n matrices X and Y, define the 2n x 2n matrices A and B,L as follows:
    A=X000andB=0Y00
    What is AB + BA, in terms of X and Y?
  3. Using (i) and (ii), argue that the product XY can be computed in time 3S(2n) + O(n2 ). Conclude that matrix multiplication takes time O(nc ).

Section 2.2 describes a method for solving recurrence relations which is based on analyzing the recursion tree and deriving a formula for the work done at each level. Another (closely related) method is to expand out the recurrence a few times, until a pattern emerges. For instance, let’s start with the familiar T(n)=2T(n/2)+o(n). Think of o(n) as being role="math" localid="1658920245976" <cnfor some constant , so: T(n)<2T(n/2)+cn. By repeatedly applying this rule, we can bound T(n) in terms of T(n/2), then T(n/4), then T(n/8), and so on, at each step getting closer to the value of T(.) we do know, namely .

T(1)=0(1).

T(n)2T(n/2)+cn2[2Tn/4+cn/2]+cn=4T(n/4)+2cn4[2Tn/8+cn/4]+2cn=8T(n/8)+3cn8[2Tn/16+cn/8]+3cn=16T(n/16)+4cn

.

.

.

A pattern is emerging... the general term is

T(n)2kT(n/2k)+kcn

Plugging in k=log2n, we get T(n)nT(1)+cnlog2n=0(nlogn).

(a)Do the same thing for the recurrence T(n)=3T(n/2)+0(n). What is the general kth term in this case? And what value of should be plugged in to get the answer?(b) Now try the recurrence T(n)=T(n-1)+0(1), a case which is not covered by the master theorem. Can you solve this too?

A binary tree is full if all of its vertices have either zero or two children. Let Bndenote the number of full binary trees with n vertices. (a)By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact values of B3, B5, and B7. Why have we left out even numbers of vertices, like B4?

(b) For general n, derive a recurrence relation for Bn.

(c) Show by induction that Bnis Ω(2n).

You are given two sorted lists of size mandn. Give an O(logm+logn)time algorithm for computing the k th smallest element in the union of the two lists.

You are given an array of nelements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O(nlogn) .

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