Introduction to Algorithms Jiafen Liu Sept. 2013.

Slides:



Advertisements
Similar presentations
한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
Advertisements

Introduction to Algorithms
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 2 Prof. Erik Demaine.
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
Introduction to Algorithms 6.046J
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Introduction to Algorithms Jiafen Liu Sept
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Introduction to Algorithms Jiafen Liu Sept
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Recurrence Relations Reading Material –Chapter 2 as a whole, but in particular Section 2.8 –Chapter 4 from Cormen’s Book.
The master method The master method applies to recurrences of the form T(n) = a T(n/b) + f (n), where a  1, b > 1, and f is asymptotically positive.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
4.Recurrences Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Recurrences -- Substitution method Recursion-tree method Master method.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Analysis of Algorithms
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Chapter 4: Solution of recurrence relationships
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Algorithms Jiafen Liu Sept
Analysis of Algorithms
Analysis of Algorithms CS 477/677
MCA 202: Discrete Mathematics Instructor Neelima Gupta
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Analysis of Algorithms1 O-notation (upper bound) Asymptotic running times of algorithms are usually defined by functions whose domain are N={0, 1, 2, …}
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Introduction to Algorithms Lecture 2 Chapter 3: Growth of Functions.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
Spring 2015 Lecture 2: Analysis of Algorithms
David Meredith Growth of Functions David Meredith
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 4: Recurrences.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
BY Lecturer: Aisha Dawood. A recurrence is a function is defined in terms of:  one or more base cases, and  itself, with smaller arguments. 2.
Asymptotic Notation Faculty Name: Ruhi Fatima
Chapter 4: Solution of recurrence relationships Techniques: Substitution: proof by induction Tree analysis: graphical representation Master theorem: Recipe.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 2.
Equal costs at all levels
Recursion Ali.
Introduction to Algorithms: Recurrences
CS 3343: Analysis of Algorithms
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Recursion-tree method
Introduction to Algorithms
Assignment 1: due 1/9/19 Geometric sum: Prove by induction on integers that Give a structured proof using the technique if S(n-1) then S(n). Include the.
Algorithms and Data Structures Lecture III
Presentation transcript:

Introduction to Algorithms Jiafen Liu Sept. 2013

Today’s task Develop more asymptotic notations How to solve recurrences

Θ-notation Math: Θ(g(n)) = { f(n) : there exist positive constants c 1, c 2, and n 0 such that 0 ≤ c 1 g(n) ≤ f (n) ≤ c 2 g(n) for all n ≥ n 0 } Engineering: –Drop low-order terms; –ignore leading constants.

O-notation Another asymptotic symbol used to indicate upper bounds. Math: Ex : 2n 2 = O(n 3 ) (c= 1, n 0 = 2) “=” means “one-way” equality

Set definition of O-notation Math: Looks better? EX : 2n 2 ∈ O(n 3 ) O-notation corresponds roughly to “less than or equal to”.

Usage of O-notation Macro substitution: A set in a formula represents an anonymous function in the set, if O-notation only occurs on the right hand of formula. Ex: f(n) = n 3 + O(n 2 ) Means f(n) = n 3 + h(n) for some h(n) ∈ O(n 2 ), here we can think of h(n) as error term.

Usage of O-notation If O-notation occurs on the left hand of formula, such as n 2 + O(n) = O(n 2 ), which means for any f(n) ∈ O(n): there exists some h(n) ∈ O(n 2 ), makes n 2 + f(n) = h(n) O-notation is an upper-bound notation. –It makes no sense to say f(n) is at least O(n 2 ).

Ω-notation How can we express a lower bound? Ex:

Θ-notation O-notation is like ≤ Ω-notation is like ≥. And Θ-notation is like = Now we can give another definition of Θ- notation We also call Θ-notation as “tight bound”.

A Theorem on Asymptotic Notations Theorem 3.1 – For any two functions f(n) and g(n), we say f(n)=Θ(g(n)) if and only if f(n)=O(g(n)) and f(n)=Ω(g(n)). Ex:, how can we prove that?

Two More Strict Notations We have just said that: –O-notation and Ω-notation are like ≤and ≥. –o-notation and ω-notation are like. –Difference with O-notation : This inequality must hold for all c instead of just for 1.

What does it mean? “for any constant c ” With o-notation, we mean that: no matter what constant we put in front of g(x), f(x) will be still less than cg(x) for sufficiently large n. No matter how small c is. Ex: 2n 2 = o(n 3 ) An counter example: 1/2n 2 = Ω (n 2 ) does not hold for each c. (n 0 = 2/c)

Two More Strict Notations We have just said that: –O-notation and Ω-notation are like ≤and ≥. –o-notation and ω-notation are like. –Difference with Ω-notation : This inequality must hold for all c instead of just for 1.

Solving recurrences The analysis of merge sort from Lecture 1 required us to solve a recurrence. Lecture 3: Applications of recurrences to divide- and-conquer algorithms. We often omit some details inessential while solving recurrences: –n is supposed to be an integer because the size of input is an integer typically. –Ignore the boundary conditions for convenience.

Substitution method The most general method : Substitution method –Guess the form of the solution. –Verify by induction. –Solve for constants. Substitution method is used to determine the upper or lower bounds of recurrence.

Example of Substitution EXAMPLE: T(n) = 4T(n/2) + n (n≥1) –(Assume that T(1) = Θ(1) ) Can you guess the time complexity of it? Guess O(n 3 ). (Prove O and Ω separately.) –We will prove T(n) ≤ cn 3 by induction. –First,we assume that T(k) ≤ ck 3 for k < n – T(k) ≤ ck 3 holds while k=n/2 by assumption.

Example of Substitution residual desired All we need is this holds as long as c ≥ 2 for n ≥ 1

Base Case in Induction We must also handle the initial conditions, that is, ground the induction with base cases. Base: T(n) = Θ(1) for all n < n 0, where n 0 is a suitable constant. For 1 ≤ n< n 0, we have “Θ(1)” ≤ cn 3, if we pick c big enough. Here we are! BUT this bound is not tight !

A tighter upper bound? We shall prove that T(n) = O(n 2 ). Assume that T(k) ≤ ck 2 for k < n. Anybody can tell me why? Now we need –n ≥ 0 But it seems impossible for n ≥ 1 residual desired

A tighter upper bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k) ≤ c 1 k 2 – c 2 k for k< n. Now we need (c 2 -1) n ≥0, it holds if c 2 ≥ 1 residual desired

For the Base Case We have proved now that for any value of c 1, and provided c 2 ≥ 1. Base: We need T(1) ≤ c 1 – c 2 Assumed T(1) is some constant. We need to choose c 1 to be sufficiently larger than c 2, and c 2 has to be at least 1. To handle the initial conditions, just pick c 1 big enough with respect to c 2.

About Substitution method We have worked for upper bounds, and the lower bounds are similar. –Try it yourself. Shortcomings: –We had to know the answer in order to find it, which is a bit of a pain. –It would be nicer to just figure out the answer by some procedure, and that will be the next two techniques.

Recursion-tree method A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion-tree method can be unreliable, just like any method that uses dot,dot,dots (…). The recursion-tree method promotes intuition, however. The recursion tree method is good for generating guesses for the substitution method.

Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n 2 :

Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n 2 :

Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n 2 :

Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n 2 : ? How many leaves? We just need an upper bound. <n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n 2 : < 2n 2 So T(n) = Θ(n 2 ) ? ? ? ? Recall 1+1/2+1/4+1/8+…

The Master Method It looks like an application of the recursion tree method but with more precise. The sad part about the master method is it is pretty restrictive. It only applies to recurrences of the form: T(n) = a T(n/b) + f(n), where a ≥1, b >1, and f(n) is asymptotically positive. aT(n/b) means every problem you recurse on should be of the same size.

Three Common Cases

Case 1: –

Three Common Cases Case 1: – Case 2: –

Three Common Cases Case 3: –

Examples Ex: T(n) = 4T(n/2) + n –a = 4, b= 2 – and – –

Examples Ex: T(n) = 4T(n/2) + n 2 –a = 4, b= 2 – and – –

Examples Ex: T(n) = 4T(n/2) + n 3 –a = 4, b= 2 – and – –

Homework Read Chapter 3 and 4 to be prepared for applications of recurrences.

Proof of Master Method Height = ? log b n #(leaves) = ?

Proof of Master Method Height = ? log b n #(leaves) = ? CASE1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight.

Proof of Master Method Height = ? log b n #(leaves) = ? CASE2(k= 0) :The weight is approximately the same on each of the log b n levels.

Proof of Master Method Height = ? log b n #(leaves) = ? CASE3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight.