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

Suppose you are choosing between the following three algorithms: • Algorithm A solves problems by dividing them into five sub-problems of half the size, recursively solving each sub-problem, and then combining the solutions in linear time. •

Algorithm B solves problems of size n by recursively solving two sub-problems of size n-1and then combining the solutions in constant time. • Algorithm C solves problems of size n by dividing them into nine sub-problems of size n/3, recursively solving each sub-problem, and then combining the solutions in O(n2)time.

What are the running times of each of these algorithms (in big- O notation), and which would you choose?

Consider the task of searching a sorted array A[1,,n] for a given element x: a task we usually perform by binary search in time O(logn) . Show that any algorithm that accesses the array only via comparisons (that is, by asking questions of the form “is A[i]z 0?”), must take Ω(logn) steps.

Question: Use the divide-and-conquer integer multiplication algorithm to multiply the two binary integers 10011011and10111010 and .

The Hadamard matricesH0,H1,H2, are defined as follows:

  • H0 is the 1×1matrix[1]
  • For k>0,Hkisthe2k×2k matrix

localid="1658916810283" Hk=[Hk-1|Hk-1Hk-1|-Hk-1]

Show that if υ is a column vector of lengthlocalid="1658916598888" n=2k, then the matrix-vector product localid="1658916618774" Hkvcan be calculated using localid="1658916637767" O(nlogn) operations. Assume that all the numbers involved are small enough that basic arithmetic operations like addition and multiplication take unit time.

In Section 2.1 we described an algorithm that multiplies two n-bit binary integers x and y in time na, where a=log23. Call this procedure fast multiply (x,y).

(a) We want to convert the decimal integer 10n(a 1 followed by n zeros) into binary. Here is the algorithm (assume n is a power of 2):

function pwr2bin(n)

if n = 1: return10102

else:

z= ???

return fastmultiply(z,z)

Fill in the missing details. Then give a recurrence relation for the running time of the algorithm, and solve the recurrence.

(b) Next, we want to convert any decimal integer x with n digits (where n is a power of 2) into binary. The algorithm is the following:

function dec2bin(x)

if n=1: return binary [ x ]

else:

split x into two decimal numbers xt,xRwith n/2 digits each

return ???

Here binary [.] is a vector that contains the binary representation of all one-digit integers. That is, binary role="math" localid="1659333641173" [0]=02, binary [1]=12, up to binary [9]=10012. Assume that a lookup in binary takes 0(1) time. Fill in the missing details. Once again, give a recurrence for the running time of the algorithm, and solve it.

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