/*! 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} Q12E How many lines, as a function of... [FREE SOLUTION] | 91影视

91影视

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 (鈥樷榮till 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鈥檚 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 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

A linear, time-invariant system has the following impulse response:


(a) Describe in words the effect of this system.

(b) What is the corresponding polynomial

In Section 1.2.3, we studied Euclid鈥檚 algorithm for computing the greatest common divisor (gcd) of two positive integers: the largest integer which divides them both. Here we will look at an alternative algorithm based on divide-and-conquer.

(a) Show that the following rule is true.

gcd(a,b)={2gcd(a2,b2)ifa,bareevengcd(ab2)ifaisodd,bisevengcd(a-b2,b)ifa,bareodd

(b) Give an efficient divide-and-conquer algorithm for greatest common divisor.

(c) How does the efficiency of your algorithm compare to Euclid鈥檚 algorithm if a and b are n-bit -bit integers? (In particular, since n might be large you cannot assume that basic arithmetic operations like addition take constant time.)

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

  • H0 is the 11matrix[1]
  • For k>0,Hkisthe2k2k 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.

Suppose we want to evaluate the polynomial P(x) = a0 + a1x + a2x2 + ... + anxn at point x.

  1. Show that the following simple routine, known as Horner鈥檚 rule, does the job and leaves the answer in z.
    z = an
    for I = n-1 down to 0 :
    z = zx + ai
  2. How many additions and multiplications does this routine use, as a function of n ? Can you find a polynomial for which an alternative method is substantially better?

Question: On page 66 there is a high-level description of the quicksort algorithm.

(a) Write down the pseudocode for quicksort.

(b) Show that its worst - case running time on an array of size n is (n)2.

(c) Show that its expected running time satisfies the recurrence relation.

T(n)O(n)+1ni=1n-1(Ti+Tn-i)

Then, show that the solution to this recurrence is 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.