A Gentle Introduction to Algorithms: Part II

Similar documents
Soving Recurrence Relations

In nite Sequences. Dr. Philippe B. Laval Kennesaw State University. October 9, 2008

Asymptotic Growth of Functions

SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES

Project Deliverables. CS 361, Lecture 28. Outline. Project Deliverables. Administrative. Project Comments

Lecture 4: Cauchy sequences, Bolzano-Weierstrass, and the Squeeze theorem

Incremental calculation of weighted mean and variance

Basic Elements of Arithmetic Sequences and Series

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies ( 3.1.1) Limitations of Experiments. Pseudocode ( 3.1.2) Theoretical Analysis

Infinite Sequences and Series

CS103A Handout 23 Winter 2002 February 22, 2002 Solving Recurrence Relations

Properties of MLE: consistency, asymptotic normality. Fisher information.

A probabilistic proof of a binomial identity

.04. This means $1000 is multiplied by 1.02 five times, once for each of the remaining sixmonth

I. Chi-squared Distributions

Convexity, Inequalities, and Norms

Example 2 Find the square root of 0. The only square root of 0 is 0 (since 0 is not positive or negative, so those choices don t exist here).

SAMPLE QUESTIONS FOR FINAL EXAM. (1) (2) (3) (4) Find the following using the definition of the Riemann integral: (2x + 1)dx

Chapter 7 Methods of Finding Estimators

CHAPTER 3 THE TIME VALUE OF MONEY

THE REGRESSION MODEL IN MATRIX FORM. For simple linear regression, meaning one predictor, the model is. for i = 1, 2, 3,, n

CS103X: Discrete Structures Homework 4 Solutions

Trigonometric Form of a Complex Number. The Complex Plane. axis. ( 2, 1) or 2 i FIGURE The absolute value of the complex number z a bi is

Sequences and Series

Output Analysis (2, Chapters 10 &11 Law)

Analyzing Longitudinal Data from Complex Surveys Using SUDAAN

Chapter 6: Variance, the law of large numbers and the Monte-Carlo method

5 Boolean Decision Trees (February 11)

Taking DCOP to the Real World: Efficient Complete Solutions for Distributed Multi-Event Scheduling

On Formula to Compute Primes. and the n th Prime

FIBONACCI NUMBERS: AN APPLICATION OF LINEAR ALGEBRA. 1. Powers of a matrix

3. Greatest Common Divisor - Least Common Multiple

NEW HIGH PERFORMANCE COMPUTATIONAL METHODS FOR MORTGAGES AND ANNUITIES. Yuri Shestopaloff,

Lecture 13. Lecturer: Jonathan Kelner Scribe: Jonathan Pines (2009)

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Note 13

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

S. Tanny MAT 344 Spring be the minimum number of moves required.

Repeating Decimals are decimal numbers that have number(s) after the decimal point that repeat in a pattern.

Section 8.3 : De Moivre s Theorem and Applications

Theorems About Power Series

Section 11.3: The Integral Test

Building Blocks Problem Related to Harmonic Series

Domain 1: Designing a SQL Server Instance and a Database Solution

Solutions to Selected Problems In: Pattern Classification by Duda, Hart, Stork

THE ABRACADABRA PROBLEM

MARTINGALES AND A BASIC APPLICATION

Sampling Distribution And Central Limit Theorem

Lecture 2: Karger s Min Cut Algorithm

Department of Computer Science, University of Otago

Concept: Types of algorithms

Convention Paper 6764

Confidence Intervals for One Mean

Annuities Under Random Rates of Interest II By Abraham Zaks. Technion I.I.T. Haifa ISRAEL and Haifa University Haifa ISRAEL.

1. C. The formula for the confidence interval for a population mean is: x t, which was

Definition. A variable X that takes on values X 1, X 2, X 3,...X k with respective frequencies f 1, f 2, f 3,...f k has mean

Lecture 3. denote the orthogonal complement of S k. Then. 1 x S k. n. 2 x T Ax = ( ) λ x. with x = 1, we have. i = λ k x 2 = λ k.

Factors of sums of powers of binomial coefficients

Lesson 15 ANOVA (analysis of variance)

Solving Logarithms and Exponential Equations

1. MATHEMATICAL INDUCTION

where: T = number of years of cash flow in investment's life n = the year in which the cash flow X n i = IRR = the internal rate of return

Class Meeting # 16: The Fourier Transform on R n

THE HEIGHT OF q-binary SEARCH TREES

Elementary Theory of Russian Roulette

The Binomial Multi- Section Transformer

The following example will help us understand The Sampling Distribution of the Mean. C1 C2 C3 C4 C5 50 miles 84 miles 38 miles 120 miles 48 miles

A Recursive Formula for Moments of a Binomial Distribution

A Note on Sums of Greatest (Least) Prime Factors

A gentle introduction to Expectation Maximization

1 Computing the Standard Deviation of Sample Means

Irreducible polynomials with consecutive zero coefficients

How To Solve The Homewor Problem Beautifully

INFINITE SERIES KEITH CONRAD

Stochastic Online Scheduling with Precedence Constraints

Lecture 4: Cheeger s Inequality

Your organization has a Class B IP address of Before you implement subnetting, the Network ID and Host ID are divided as follows:

TO: Users of the ACTEX Review Seminar on DVD for SOA Exam MLC

A Faster Clause-Shortening Algorithm for SAT with No Restriction on Clause Length

LECTURE 13: Cross-validation

Hypothesis testing. Null and alternative hypotheses

Modified Line Search Method for Global Optimization

Chatpun Khamyat Department of Industrial Engineering, Kasetsart University, Bangkok, Thailand

This document contains a collection of formulas and constants useful for SPC chart construction. It assumes you are already familiar with SPC.

5: Introduction to Estimation

CME 302: NUMERICAL LINEAR ALGEBRA FALL 2005/06 LECTURE 8

A Guide to the Pricing Conventions of SFE Interest Rate Products

Determining the sample size

Integer Factorization Algorithms

Partial Di erential Equations

Problem Solving with Mathematical Software Packages 1

1 Correlation and Regression Analysis

University of California, Los Angeles Department of Statistics. Distributions related to the normal distribution

Multiplexers and Demultiplexers

AP Calculus BC 2003 Scoring Guidelines Form B

THE LEAST COMMON MULTIPLE OF A QUADRATIC SEQUENCE

COMPARISON OF THE EFFICIENCY OF S-CONTROL CHART AND EWMA-S 2 CONTROL CHART FOR THE CHANGES IN A PROCESS

Chapter 5: Inner Product Spaces

. P. 4.3 Basic feasible solutions and vertices of polyhedra. x 1. x 2

THE ARITHMETIC OF INTEGERS. - multiplication, exponentiation, division, addition, and subtraction

SEQUENCES AND SERIES

Transcription:

A Getle Itroductio to Algorithms: Part II Cotets of Part I:. Merge: (to merge two sorted lists ito a sigle sorted list.) 2. Bubble Sort 3. Merge Sort: 4. The Big-O, Big-Θ, Big-Ω otatios: asymptotic bouds 5. Basics of aalysis of complexity: The two basic techiques of programmig are iteratios (such as for, while, repeat loops) ad recursios. I this sectio, we will study how to do the accoutig for the time-complexity of a algorithm that cotais iteratios or recursios (or both). If you are aalyzig a fairly complex algorithm, this is the portio that requires most traiig ad care. Therefore I ca oly go ito these topics at a very superficial level. If you eed to lear more about this topic, the best thig is to do a lot of examples from referece []. 5.. Iteratios: The mai tool i evaluatio of time-complexity of a iterative algorithm is the summatio of a series. I our aalysis of BUBBLE, we came up with a time-complexity fuctio that looked like: c ( ) f ( ) = ci = c i = 2 =Θ( for sum of a arithmetic series. Other useful summatio formulas iclude the followig: geometric series: 2 + x =, if x <, x x i i x = 0 0 ) ad was computed by the well kow formula. x harmoic series: = l + O(), where O() deotes a costat. i 0 Fially, due to the liearity of summatio, the followig is useful: Θ( f ( i)) = Θ f ( i) ; ote that the Q-otatio applies to f(i), while o the right side it applies to the fuctio of the parameter.

5.2. Recurreces: computig performace of recursive algorithms. Oe of the most powerful techiques for programmig is recursio; a particularly useful algorithmic techique usig recursio is divide-ad-coquer. We saw its power i the desig of the Mergesort, which was much more efficiet tha the more obvious Bubblesort. However, it is sometimes ot as easy to aalyze the performace of recursive algorithms. Our aalysis of Mergesort used a rather brute-force techique, sometimes called buildig a recursio tree. The performace of most recursive methods (ad especially divide-ad-coquer) ca be writte i the followig form: f () if = T ( ) = at ( / b) + D( ) + C( ) if > Here, f() is the cost of computig the base-case of the recursio. At each step of the recursio, we divide the problem ito b equal sized sub-problems, ad it takes us D() uits of time to divide the problem ito these smaller parts. Fially, it takes us C() uits of time to merge, i.e. combie the solutios of the b sub-problems ito the solutio of the orgial problem. The expressio above is called a recurrece, ad there are three stadard methods for solvig recurreces. 5.2.. The Substitutio Method We first guess the solutio, ad the we prove that our guess is correct usig mathematical iductio. Example: T() = 2 T( /2 ) + This looks quite similar to the recurrece for our Mergesort, so we are tempted to guess that the asymptotic upper boud here is O( lg ). This is reiforced by the observatio that as icreases i size, the value of /2 will totally domiate the costat b. We ow use iductio to prove it: We must show that T() c lg for some properly chose value of c. Base case: Note that if we choose =, the c lg = c lg = 0, for all c. Therefore we shall use a larger value of, e.g. = 2. This is allowed sice the asymptotic otatio oly requires that T() c lg is true for all > 0 of our choosig. Thus, at = 2, c lg = 2c, ad we ca easily fid a c that guaratees that the iequality

will hold. Assume that the claim holds for /2, i.e. T( /2 ) c /2 lg( /2 ). The T() = 2 T( /2 ) + 2 (c /2 lg( /2 ) ) + c lg c lg 2 + = c lg (c ), which is true wheever c >. Obviously, good guesswork is eeded, alog with some cleveress to successfully apply this method. For example, cosider: Example: T() = 2 T(/2 + b) + This looks similar to the previous example, except for the additio of the costat b. We may guess that T() is O( lg ) sice asymptotically, b is egligible compared to /2, ad ay additioal computatioal burde due to it ca be absorbed by a suitably chose c i our O-otatio. You ca use the substitutio method to prove that this is ideed the case, but it will require some trickery (see hit i the exercises). A variatio of the substitutio method is by usig trasformatios (chage of variables). The followig is a example: T() = 2T( ) + lg The square-root term makes is difficult to guess a solutio to this recurrece; we try a substitutio: m = lg, ad get: T( 2 m ) = 2 T( 2 m/2 ) + m Reamig S(m) = T( 2 m ), we get: S(m) = 2 S(m/2) + m, ad we kow, from the proof earlier, that S(m) = O(m lg m) = O( lg lg lg ). 5.2.2. The Iteratio method This method does ot require us to guess a solutio. Here, we repeatedly expad the recurrece, ad attempt to idetify a patter that ca allow us to use algebraic techiques to simplify the solutio. The formulas for summatio of series (arithmetic, geometric, harmoic) are very useful i this method. I the followig example, I use the idetity /4 / 4 = /6 (prove it!). Example: T() = 3 T( /4 ) +

T() = + 3 T( /4 ) = + 3( /4 + 3 T( /6 ) = + 3( /4 + 3 ( /6 + 3 T( /64 ))) = + 3 /4 + 9 /6 + 27 T( /64 ) = + j= (i-) 3 j / 4 j + 3 i T( /4 i ) Which will hit the boudary coditio for T() whe = 4 i, that is, log 4 = i that is, after log 4 steps. Fially, usig / 4 j j, we get: T() 0 (3/4) i + 3 log 4 T() = 0 (3/4) i + 3 log 4 Θ() = 0 (3/4) i + log 43 Θ() [ote: 3 log 4 = log 43 (prove it!)] = 4 + O() = O() Note that whe we evaluated the complexity of the merge sort, we did a similar type of bookkeepig. I some cases, it really helps to make a picture of the effort ivolved i each step i the form of a tree (sometimes called a recursio tree). 5.2.3. The master method All recurreces of the form T() = at(/b) + f() ca be solved by the applicatio of a powerful theorem, called the master theorem. We will preset the theorem, but ot its proof, ad the show a example. Master Theorem: Give two costats, a recurrece: T() a T(/b) + f(), where /b ca be either / b, or / b. The T() ca be boud asymptotically by: (a) If f() = O( log b a-ε ) for some costat ε > 0, the T() = Θ( log b a ) (b) If f() = Θ( log b a ), the T() = Θ( log b a lg ) (c) f() = Ω( log b a+ε ) for some costat ε > 0, ad if af(/b) for some costat c < ad all sufficietly large, the T() = Θ( f()) The ituitive idea about the theorem is that we ca estimate the asymptotic boud o the recurrece by comparig f() with log b a : if f() is smaller, as i case (a), the the

recurrece is boud by log b a ; if the two are equal, as i case (b), the T() is O( f() lg ); fially, if f() is larger tha log b a, as i case (c), the T() is boud by f(). Example: T() = 9T(/3) + Here, a = 9, b = 3, f() = ; log 3 9 = 2 ; settig ε =, f() = = O( 2- ). Thus T() = Θ( 2 ). Example: T() = 2T(/2) + lg Here a = 2, b = 2, thus log b a =. f() = lg, so clearly case (a) ad (b) do ot apply. How about case (c)? It ca be show that for ay positive ε, however small, +ε will grow faster tha lg. Therefore oe of the three cases for the master theorem ca be applied, ad so some other meas must be foud to solve this recurrece. Exercises: (a) Provide a tight asymptotic boud to T() = 2T( /2 ) + 5. (b) Prove usig (i) substitutio, ad (ii) iteratio that T() = 2T( /2 + b) + is O( lg ). [Solutio for (i): Showig that T() 2b) lg ( 2b) is sufficiet to show that T() < c lg ; Assume that the statemet is true for /2 + b, that is: T( /2 + b) - b) lg (/2 - b). Now, T() = 2 T(/2 + b) + 2c (/2 b) lg (/2 b) + = c( - 2b) lg ( /2 b) + = c ( 2b) lg ( 2b) c ( 2b) lg 2 + = c ( 2b) lg ( 2b) (c ) + 2 b c c ( 2b) lg ( 2b) as log as 2 bc (c ) < 0, which is certaily true whe > 2b, ad c > 2. Note that we show that the iductive step is true for if it was true for /2+b, so we must also be certai that > /2 + b, which is true whe > b. To complete the solutio, we eed the boudary case, which i this case will probably be at =b, sice whe < b, T() = T(/2 + b) + has RHS larger tha LHS].

(c) Fid a upper boud for T() = T(/2 + ) +. (d) Usig the iteratio method (draw the recursio tree), provide a tight boud to the recurrece T() = 4 T( /2 ) +. (e) Use the master theorem to give bouds to: (i) T() = 4T(/2) + (ii) T() = 4T(/2) + 2 (iii) T() = 4T(/2) + 3