Lecture Notes on Algorithm Analysis - | CS 4310, Assignments of Algorithms and Programming

Material Type: Assignment; Professor: Gupta; Class: Dsgn & Analysis Of Algorithms; Subject: Computer Science; University: Western Michigan University; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-svn
koofers-user-svn 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
50 Chapter 1. Algorithm Analysis
Creativityc.
c-
.I What is the amortized running time of the operations in a sequence of n opera-
tions P = PIP2... Pn if the running time of Pi is 8(i) if i is a multiple of 3, and is
constant otherwise?
.2 Let P = PI P2 ...Pn be a sequence of n operations, each either a red or blue oper-
ation, with PI being a red operation and P2 being a blue operation. The running
time of the blue operations is always constant. The running time of the first red
operation is constant, but each red operation Pi after that runs in time that is twice
as long as the previous red operation, P j (with j < i). What is the amortized time
of the red and blue operations under the following conditions?
a. There are always 8( I) blue operations between consecutive red operations.
b. There are always 8( Jii) blue operations between consecutive red opera-
tions.
c. The number of blue operations between a red operation Pi and the previous
red operation P j is always twice the number between P j and its previous
red operation.
c. .3 What is the total running time of counting from 1 to n in binary if the time needed
to add 1 to the current number i is proportional to the number of bits in the binary
expansion of i that must change in going from i to i + I?
.4 Consider the following recurrence equation, defining a function T(n):
c.
1 ifn=l
T(n-l) +n otherwise,
T(n) =
Show, by induction, that T(n) = n(n+ 1)/2.
.5 Consider the following recurrence equation, defining a function T(n):
c
1 if n = 0
T(n-l)+2n otherwise.
T(n) =
Show, by induction, that T(n) = 2n+l -I.
C-I.6 Consider the following recurrence equation, defining a function T(n)
1 ifn=O
2T(n -1) otherwise,
T(n) =
Show, by induction, that T(n) = 2ft.
C-l.7 Al and Bill are arguing about the performance of their sorting algorithms. AI
claims that his O(nlogn)-time algorithm is aLways faster than BilI's O(n2)-time
algorithm. To settle the issue, they implement and run the
many randomly generated data sets. To AI's dismay, they find that if n <
the O(n2)-time algorithm actually runs faster, and only when n ~ 100 is
O(nlogn)-time algorithm better. Explain why this scenario is possible.
may give numerical examples.
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Algorithm Analysis - | CS 4310 and more Assignments Algorithms and Programming in PDF only on Docsity!

50 Chapter 1. Algorithm Analysis

Creativity c.

c- .I What is the amortized running time of the operations in a sequenceof n opera- tions P = PIP2... Pn if the running time of Pi is 8(i) if i is a multiple of 3, and is constant otherwise? .2 Let P = PI P2 ...Pn be a sequenceof n operations,each either a red or blue oper- ation, with PI being a red operation and P2 being a blue operation. The running time of the blue operations is always constant. The running time of the first red operation is constant, but each red operation Pi after that runs in time that is twice as long as the previous red operation, P j (with j < i). What is the amortized time of the red and blue operations under the following conditions? a. There are always 8( I) blue operations betweenconsecutive red operations. b. There are always 8( Jii) blue operations between consecutive red opera-

tions.

c. The number of blue operations betweena red operation Pi and the previous red operation P j is always twice the number between P j and its previous red operation. c. .3 What is the total running time of counting from 1 to n in binary if the time needed to add 1 to the current number i is proportional to the number of bits in the binary expansion of i that must change in going from i to i + I? .4 Consider the following recurrence equation, defining a function T(n): c.

1 ifn=l

T(n-l) +n otherwise,

T(n) =

Show, by induction, that T(n) = n(n+ 1)/2. c .5 Consider the following recurrence equation, defining a function T(n):

1 if n = 0

T(n-l)+2n otherwise.

T(n) =

Show,by induction,that T(n) = 2n+l-I.

C-I.6 Considerthe following recurrenceequation,defininga function T(n)

1 ifn=O 2T(n -1) otherwise,

T(n) =

Show,by induction,that T(n) = 2ft.

C-l.7 Al and Bill are arguing aboutthe performanceof their sorting algorithms. AI claims thathis O(nlogn)-time algorithmis aLwaysfasterthanBilI's O(n2)-time algorithm. To settle the issue,they implementand run the many randomlygenerateddatasets. To AI's dismay,they find that if n < the O(n2)-time algorithm actually runs faster,and only when n ~ 100 is O(nlogn)-time algorithm better. Explain why this scenariois possible. maygive numericalexamples.

Analysis 1.7. Exercises^51 of n opera- of 3, and is 1rblue oper- The running the first red that is twice 1ortized time C-I.8 Communication secunty is extremely important in computer networks, and one way many network protocols achieve security is to encrypt messages. Typical cryptographic schemes for the secure transmission of messages over such net- works are based on the fact that no efficient algorithms are known for factoring large integers. Hence, if we can represent a secret message by a large prime number p, we can transmit over the network the number r = p' q, where q > p is another large prime number that acts as the encryption key. An eavesdropper who obtains the transmitted number r on the network would have to factor r in order to figure out the secret messagep. Using factoring to figure out a message is very difficult without knowing the encryption key q. To understand why, consider the following naive factoring

algorithm:

~doperations. ve red opera- For every integer p such that I < p < r, check if p divides r. If so, print "The secret messageis p'" and stop; if not, continue. d the previous d its previous he time needed [5 in the binary a. Supposethat the eavesdropperusesthe above algorithm and has a computer that can carry out in 1 microsecond (1 millionth of a second) a division between two integers of up to 100 bits each. Give an estimate of the time that it will take in the worst caseto decipher the secretmessageif r has 100 bits. b. What is the worst-case time complexity of the above algorithm? Since the input to the algorithm is just one large number r. assume that the input size n is the number of bytes needed to store r. that is. n = (log2 r) /8. and that each division takes.time O(n).

algorithms. AlJill's O(n2)-time,, algorithms on 1 that if n < 100, n n ~ 100 is the IS possible. You C-I.9 Give an example of a positive function f(n) such that f(n) is neither O(n) nor

.Q(n).

C-I.IO Showthatr-I=IP is O(n3). C-I.II Show that r-1=1i/2i < 2. Hint: Try to bound this sum term by term with a geometric progression. C-I.12 Show that logbf(n) is 0(logf(n)) if b > I is a constant. C-I.13 Describe a method for finding both the minimum and maximum of n numbers using fewer than 3n/2 comparisons. Hint: First construct a group of candidate minimums and a group of candidate

maximums.

C-I.14 Suppose you are given a set of small boxes, numbered I to n, identical in every respect except that each of the first i contain a pearl whereas the remaining n -i are empty. You also have two magic wands that can each test if a box is empty or pot in a single touch, except that a wand disappearsif you test it on an empty box. Show that, without knowing the value of i, you can use the two wands to determine all the boxes containing pearls using at most o(n) wand touches. Express, as a function of n, the asymptotic number of wand touches needed. C-I.15 Repeat the previous problem assuming that you now have k magic wands, with k> 2 and k < logn. Express, as a function of nand k, the asymptotic number of wand touches needed to identify all the magic boxes containing pearls.

P Lists 3.7. Exercises^213 " empty ~r each ,( of ele- 'I: results

,ious ex-

'd into an ~ a small tbout the

:. into the

from the :d using a R-3.14 For each of the following statements about red-black trees, determine whether it is true or false. If you think it is true, provide a justification. If you think it is false, give a counterexample. a. A subtree of a red-black tree is itself a red-black tree. b. The sibling of an external node is either external or it is red. c. Given a red-black tree T, there is a unique (2,4) tree T' associated with T. d. Given a (2,4) tree T, there is a unique red-black tree T' associated with T. R-3.IS Perform the following sequenceof operations in an initially empty splay tree and draw the tree after eachoperation. a. Insert keys 0,2,4,6,8, 10, 12, 14, 16, 18, in this order. b. Search for keys 1,3,S,7,9, 11, 13, IS, 17, 19, in this order. c. Delete keys 0,2,4,6,8, 10, 12, 14, 16, 18, in this order. R-3.16 What does a splay tree look like if its items are accessedin increasing order by their keys? R-3.17 How many trinode restructuring operations are needed to perform the zig-zig, zig-zag, and zig updates in splay trees? Use figures to explain your counting. R-3.18 Draw an example skip list resulting from performing the following sequence of operations on the skip list in Figure 3.S1: removeElement(38), insertltem(48,x), insertltem(24,y), removeElement(5S). Assume the coin flips for the first inser- tion yield two heads followed by tails, and those for the second insertion yield three heads followed by tails. R-3.19 Give a pseudo-code description of the removeElement dictionary operation, as- suming the dictionary is implemented by a skip-list structure. answer. () partition \ich of the

ill alwayse

inserted.

ti

tree using Creativity C-3.l Suppose we are given two ordered dictionaries S and T, each with n items, and that S and T are implemented by means of array-basedordered sequences. De- scribe an O(logn )-time algorithm for finding the kth smallest key in the union of the keys from S and T (assuming no duplicates). C-3.2 Describe how to perform the operation findAIIElements(k) in an ordered dictio- nary implemented with an ordered array, and show that it runs in time O(logn + s), where n is the number of elements in the dictionary and s is the number of items returned. :5;~:' C-3.3 Describe how to perform the operation findAIIElements(k) irian ordered dic- tionary implemented with a binary search tree T, and show that it runs in time O(h + s), where h is the height of T and s is the number of items returned. C-3.4 Describe how to perform the operation removeAIIElements(k) in an ordered dic- tionary implemented with a binary searchtree T, and show that this method runs in time O(h + s), where h is the height of T and s is the size of the iterator re- turned.

and

Selection (^) 4.9. Exercises sort that is spe- and return that ort, and quick- 'nceS for s (k.,k

:hm~mentsequence

whether A = B,erge algorithm,

. EBB, which is ,et using a bal- the methods in or converting A ling time of this C4." ~rs in the range ~ is by meansof

r] = true. Since

es referred to as on, intersection, ration. What are

stic

](; Ie of he total he oftri orne2,.. . lplets N~ ,kd). (- C-4.12 'show that any comparison-based sorting algorithm can be made to be stable, ~ without affecting the asymptotic running time of this algorithm.

Hint: Changethe way elementsarecomparedwith eachother.

, 2&;