Handout: How to calculate time complexity? CSE 101 Winter 2014

Similar documents
Soving Recurrence Relations

SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES

Incremental calculation of weighted mean and variance

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

Infinite Sequences and Series

Lecture 2: Karger s Min Cut Algorithm

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

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

Sequences and Series

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

Chapter 5: Inner Product Spaces

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

5 Boolean Decision Trees (February 11)

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

Basic Elements of Arithmetic Sequences and Series

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

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

Convexity, Inequalities, and Norms

1. MATHEMATICAL INDUCTION

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.

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

3. Greatest Common Divisor - Least Common Multiple

CHAPTER 3 THE TIME VALUE OF MONEY

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

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

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).

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

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

Chapter 7 Methods of Finding Estimators

Solving Logarithms and Exponential Equations

CS103X: Discrete Structures Homework 4 Solutions

Lesson 17 Pearson s Correlation Coefficient

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

Department of Computer Science, University of Otago

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

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

hp calculators HP 12C Statistics - average and standard deviation Average and standard deviation concepts HP12C average and standard deviation

1 Computing the Standard Deviation of Sample Means

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

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

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

Solving equations. Pre-test. Warm-up

Solutions to Exercises Chapter 4: Recurrence relations and generating functions

A probabilistic proof of a binomial identity

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

BENEFIT-COST ANALYSIS Financial and Economic Appraisal using Spreadsheets

DAME - Microsoft Excel add-in for solving multicriteria decision problems with scenarios Radomir Perzina 1, Jaroslav Ramik 2

2-3 The Remainder and Factor Theorems

Building Blocks Problem Related to Harmonic Series

Approximating Area under a curve with rectangles. To find the area under a curve we approximate the area using rectangles and then use limits to find

Concept: Types of algorithms

Determining the sample size

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

INVESTMENT PERFORMANCE COUNCIL (IPC)

Lesson 15 ANOVA (analysis of variance)

Laws of Exponents. net effect is to multiply with 2 a total of = 8 times

Learning objectives. Duc K. Nguyen - Corporate Finance 21/10/2014

Bond Valuation I. What is a bond? Cash Flows of A Typical Bond. Bond Valuation. Coupon Rate and Current Yield. Cash Flows of A Typical Bond

THE ABRACADABRA PROBLEM

Confidence Intervals. CI for a population mean (σ is known and n > 30 or the variable is normally distributed in the.

1 Correlation and Regression Analysis

Maximum Likelihood Estimators.

Math C067 Sampling Distributions

How To Solve The Homewor Problem Beautifully

Modified Line Search Method for Global Optimization

Finding the circle that best fits a set of points

Chapter 5 Unit 1. IET 350 Engineering Economics. Learning Objectives Chapter 5. Learning Objectives Unit 1. Annual Amount and Gradient Functions

5.3. Generalized Permutations and Combinations

Now here is the important step

Chair for Network Architectures and Services Institute of Informatics TU München Prof. Carle. Network Security. Chapter 2 Basics

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

Present Value Factor To bring one dollar in the future back to present, one uses the Present Value Factor (PVF): Concept 9: Present Value

INFINITE SERIES KEITH CONRAD

Our aim is to show that under reasonable assumptions a given 2π-periodic function f can be represented as convergent series

A Guide to the Pricing Conventions of SFE Interest Rate Products

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

3 Energy Non-Flow Energy Equation (NFEE) Internal Energy. MECH 225 Engineering Science 2

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

Asymptotic Growth of Functions

Section 8.3 : De Moivre s Theorem and Applications

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

Vladimir N. Burkov, Dmitri A. Novikov MODELS AND METHODS OF MULTIPROJECTS MANAGEMENT

GCSE STATISTICS. 4) How to calculate the range: The difference between the biggest number and the smallest number.

CHAPTER 3 DIGITAL CODING OF SIGNALS

GCE Further Mathematics (6360) Further Pure Unit 2 (MFP2) Textbook. Version: 1.4

Heat (or Diffusion) equation in 1D*

Class Meeting # 16: The Fourier Transform on R n

Output Analysis (2, Chapters 10 &11 Law)

Cooley-Tukey. Tukey FFT Algorithms. FFT Algorithms. Cooley

The Fundamental Forces of Nature

Theorems About Power Series

Elementary Theory of Russian Roulette

Simple Annuities Present Value.

AP Calculus BC 2003 Scoring Guidelines Form B

CHAPTER 7: Central Limit Theorem: CLT for Averages (Means)

Terminology for Bonds and Loans

Transcription:

Hadout: How to calculate time complexity? CSE 101 Witer 014 Recipe (a) Kow algorithm If you are usig a modied versio of a kow algorithm, you ca piggyback your aalysis o the complexity of the origial algorithm For example, if you use a modied versio of DFS or Dijkstra's algorithm, as log as your modicatios do ot aect the origial ruig time, you ca use the complexity of these other algorithms However, you must state (ad be correct) about why your chages do ot aect the origial algorithm's ruig time For example: The algorithm performs DFS ad marks each vertex with a color whe it is visited This is a costat amout of additioal work per vertex ad so the ruig time remais the same as DFS: O(jV j + jej) (b) Master Theorem If the time complexity equatio ca be writte as follow, the we ca use the Master Theorem T () = T () = a T b + O( d ) If log b a < d; T () = O( d ) If log b a = d; T () = O( d log ) If log b a > d; T () = O( log b a ) (c) Else, we have to evaluate T () to be able to take the big-o out of it (see Evaluatig sums ad Examples) There are 3 ways of doig this: Guess by lookig at the rst terms the prove, Guess by urollig the equatio the prove, Ad look at the recursio tree Importat trick Whe aalysig recurreces, for coveiece you ca 1 assume is a power of a umber by givig the followig explaatio: We assume without loss of geerality that is power of [your umber] This will ot iuece the al boud i ay importat way, after all, is at most a multiplicative factor away from some power of [your umber] 1 See the proof of Master Theorem i the book, page 49 1

Hadout: How to calculate time complexity? CSE 101 Witer 014 Evaluatig sums Arithmetic progressio Geometric progressio T = T 1 + c T = T 1 c We have: +c +c +c +c T 0 T 1 T 1 T T = rst + c (# of times c has bee added) rst + last T 0 + T 1 + + T = (# of terms) Example: 1 + + 3 + + = 1 + c c c c T 0 T 1 T 1 T We have (c 6= 1): (# of times c has bee multiplied) T = rst c T 0 + T 1 + + T = rst c(# of terms) 1 c 1 Example (c 6= 1): 1 + c + c + + c = c+1 1 c 1 Examples A) Fast multiplicatio Algorithm The idea is to square a to get a (oe multiplicatio) ad whe is odd, evaluate a 1 the same way ad the multiply it by a to get a (oe multiplicatio) Pseudo-code procedure mult(a, ): if == 1: retur a else if is eve: b = mult(a, / ) retur b * b else retur a * mult(a, - 1) Time complexity equatio Let T () be the umber of multiplicatios We assume without loss of geerality that is power of This will ot iuece the al boud i ay importat way, after all, is at most a multiplicative factor away from some power of At each step is divided by T () = T + 1

Hadout: How to calculate time complexity? CSE 101 Witer 014 Evaluatig T () Total work for step Total work for step Work at step (1 multiplicatio) The Master Theorem gives us a = 1 b = d = 0 so log b a = log 1 = 0 = d so T () = O(log ) B) Tower of Haoi: Lookig at the recursio tree Algorithm The algorithm requires that to move disks, we move rst 1 disks, the 1 ad ally 1 agai Time complexity equatio Let T () be the umber of moves T () = T ( 1) + 1 + T ( 1) Total work for disks Total work for 1 disks Move 1 disk Total work for 1 disks We ca't apply Master Theorem o the complexity equatio T () = T ( evaluate T () to be able to give a big-o boud 1) + 1 so we have to The recursio tree Let's cosider the recurrece sub-problem tree: Level 0 : 1 problem Level 1 : problems 1 1 Level : 4 problems Level k: k Level 1 problems k k k k 1 1 1 1 1 1 At the k-th level, there are k sub-problems that all require 1 move to get to level k 1 So, there are w(k) = k moves at level k (for level k oly) Google "tower of haoi recursive solutio" if you do't remember the solutio 3

Hadout: How to calculate time complexity? CSE 101 Witer 014 Summig to evaluate T () As T () is the sum of the moves of each level, we get: T () = 1 + + + 3 + + 1 We recogise here the sum of the rst terms of a geometric progressio whose ratio is ad rst term is 1 so we get: Therefore, T () = O( ) T () = 1 1 = 1 C) Tower of Haoi: Guessig with the first terms the prove Whe you have the recursio equatio T () = T ( by lookig at the rst terms: 1) + 1, you ca try to guess the value of T () T (0) = 0; T (1) = 1; T () = 3; T (3) = 7; T (4) = 15; T (5) = 31; T (6) = 63; : : : We ca guess it may be T () = 1, but we have to prove it! Let's do it by recursio o : Base case T (0) = 0 OK! Iductive hypothesis Let's suppose T () = 1 for a 0 Iductive step We have: T ( + 1) = T () + 1 = ( 1) + 1 (id hyp) = +1 1 Coclusio We have proved that T () = 1 for all 0 Therefore, T () = O( ) D) Tower of Haoi: Guessig by urollig the prove Aother way we ca guess the solutio is by urollig the recurrece, by substitutig it ito itself: T () = T ( 1) + 1 = ( T ( ) + 1) + 1 = 4 T ( ) + 3 = 4( T ( 3) + 1) + 3 = T ( 3) + 7 : : : Here we ca guess a ew recurrece: T () = k T ( k) + ( k 1) But we have also to prove it! Let's do it by recursio o k: 4

Hadout: How to calculate time complexity? CSE 101 Witer 014 Base case For k = 0, we have 0 T ( 0) + ( 0 1) = T () Ok! Iductive hypothesis Let's suppose T () = k T ( k) + ( k 1) for a k 0 Iductive step We have: T () = k T ( k) + ( k 1) = k ( T ( k 1) + 1) + ( k 1) itial recursio = k+1 T ( (k + 1)) + ( k+1 1) Coclusio We have proved that T () = k T ( k) + ( k 1) for k 0 Therefore, for k = we get T () = 1 so T () = O( ) E) Closest Pair of Poits i a Plae Let's evaluate the time complexity of the closest pair of poits i a plae solutio usig Divide ad Coquer is a power of We assume without loss of geerality that is power of This will ot iuece the al boud i ay importat way, after all, is at most a multiplicative factor away from some power of Sortig the poits before solvig First, before startig to solve the problem, we sort twice: Oce by the x-coordiate ad oce by the y-coordiate No further sortig is required at subsequet recursive steps Usig a stadard sortig algorithm requires O( log ), for example via mergesort Solvig For a set of poits i the plae (let T () be the time complexity): We partitio the curret set ito sub-sets deed by media x-coordiate As the poits have bee sorted by x-coordiate, dig the media requires oly a costat time operatio Recursively compute the closest pair distaces for the sub-sets T ( ) + T ( ) This requires a complexity of Deal with the middle ribbo Worst case sceario: all poits from the left side sub-problem ( poits) are i the ribbo ad for each of them, we have to check a maximum of 6 poits i the right side As the poits are sorted by y-coordiates ad we step through at most, checkig the middle ribbo takes at most a liear amout of time Fially take the miimum distace amog: the smallest of the right side, the smallest of the left side, ad the smallest of the middle ribbo That takes a costat time: O(1) 5

Hadout: How to calculate time complexity? CSE 101 Witer 014 We get: T () = O() + T + T + O() + O(1) Work for poits Splittig the plae (x-coord media) Work for the left side Work for the right side Work for the ribbo Combiig results (mi of 3 umbers) It ca be simplied i: Master Theorem gives us a = b = d = 1 T () = T + O() so log b a = log 1 = 1 = d so T () = O( log ) Coclusio We did steps: before solvig, we sorted the poits with a complexity of O( log ), ad we solved the problem with a complexity of T () = O( log ) So the overall time complexity is O( log ) F) Recursio tree agai We are give: T () = T + log We assume without loss of geerality that is power of This will ot iuece the al boud i ay importat way, after all, is at most a multiplicative factor away from some power of We ca't use the Master Theorem so we have to evaluate T () to be able to give the complexity i big-o terms Let's cosider the recurrece sub-problem tree: Level 0 : 1 problem Level 1 : problems = = Level : 4 problems =4 =4 =4 =4 Level k: k problems = k = k = k = k Level log 1 1 1 1 1 1 6

Hadout: How to calculate time complexity? CSE 101 Witer 014 As we kow the size of the sub-problems at level k is k accordig to the complexity equatio: The work for oe sub-problem of level k is, k log k = 1 k log() k As there is k sub-problems at level k, the total work made at level k is: k 1 k log() k = log() k # of problems at level k work for each problem of level k total work made at level k Therefore: T () = (work at level 0) + (work at level 1) + + (work at level log() - 1) = log() + log() 1 + + + 1 = 1 + 1 + 1 3 + + 1 log() 1 + 1! log() We saw i the homework that we had: 1 + 1 + 1 3 + + 1 m = O(log m) I our previous sum, m = log so we get T () = O( log(log )) 7