Master Theorem Yin Tat Lee

Slides:



Advertisements
Similar presentations
Linear-time Median Def: Median of elements A=a 1, a 2, …, a n is the (n/2)-th smallest element in A. How to find median? sort the elements, output the.
Advertisements

Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Cse 521: design and analysis of algorithms Time & place T, Th pm in CSE 203 People Prof: James Lee TA: Thach Nguyen Book.
Advanced Algorithms Piyush Kumar (Lecture 12: Parallel Algorithms) Welcome to COT5405 Courtesy Baker 05.
Closest Pair Given a set S = {p1, p2,..., pn} of n points in the plane find the two points of S whose distance is the smallest. Images in this presentation.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
See the demo slide on the website. 4 Example 1: Whole Process H G F E D C B A Divide H G F E D C B A Merge G H E F C D A B Merge E F G H A B.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.
Closest Pair of Points Computational Geometry, WS 2006/07 Lecture 9, Part II Prof. Dr. Thomas Ottmann Khaireel A. Mohamed Algorithmen & Datenstrukturen,
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
Analysis of Recursive Algorithms October 29, 2014
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 12.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
1 Closest Pair of Points (from “Algorithm Design” by J.Kleinberg and E.Tardos) Closest pair. Given n points in the plane, find a pair with smallest Euclidean.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Advanced Algorithms Analysis and Design
Introduction to Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
Algorithm Design & Analysis
Randomized Algorithms
Chapter 4 Divide-and-Conquer
Haim Kaplan and Uri Zwick
CSCE350 Algorithms and Data Structure
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Jordi Cortadella and Jordi Petit Department of Computer Science
Rank Rank of an element is its position in ascending key order.
CSE 421: Introduction to Algorithms
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Chapter 5 Divide and Conquer
Dr. Yingwu Zhu Chapter 9, p Linear Time Selection Dr. Yingwu Zhu Chapter 9, p
Randomized Algorithms
Punya Biswas Lecture 15 Closest Pair, Multiplication
עידן שני ביה"ס למדעי המחשב אוניברסיטת תל-אביב
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 11 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
CSE 421: Introduction to Algorithms
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
Lecture 15, Winter 2019 Closest Pair, Multiplication
Clustering.
Tutorial 6 Array Problem Solving
Richard Anderson Lecture 14 Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 12, Winter 2019 Recurrences
Given a list of n  8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
Richard Anderson Lecture 12 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Presentation transcript:

Master Theorem Yin Tat Lee CSE 421 Master Theorem Yin Tat Lee

Almost 1D Problem Partition each side of 𝐿 into 𝛿 2 × 𝛿 2 squares Claim: No two points lie in the same 𝛿 2 × 𝛿 2 box. Say we want to check if there is a point < 𝛿 close to point 31. Only need to check points with indices roughly 31. (coz we sorted). How many? Not too many coz every box only have 1 point.  29 30 31 28 26 25 49 27 >𝛿

Closest Pair (2 dimension) if(𝒏≤𝟐) return | 𝒑 𝟏 − 𝒑 𝟐 | Compute separation line 𝑳 such that half the points are on one side and half on the other side. 𝜹 𝟏 = Closest-Pair(left half) 𝜹 𝟐 = Closest-Pair(right half) 𝜹 = min( 𝜹 𝟏 , 𝜹 𝟐 ) Delete all points further than  from separation line L Sort remaining points p[1]…p[m] by y-coordinate. for 𝒊=𝟏,𝟐,⋯,𝒎 for k = 𝟏,𝟐,⋯,𝟏𝟏 if 𝒊+𝒌≤𝒎  = min(, distance(p[i], p[i+k])); return . }

Closest Pair Analysis Let 𝐷(𝑛) be the number of pairwise distance calculations in the Closest-Pair Algorithm 𝐷 𝑛 ≤ 1 if 𝑛=1 2𝐷 𝑛 2 +11 𝑛 o.w. ⇒𝐷 𝑛 =O(𝑛log 𝑛) BUT, that’s only the number of distance calculations What if we counted running time? 𝑇 𝑛 ≤ 1 if 𝑛=1 2𝑇 𝑛 2 +𝑂(𝑛 log 𝑛) o.w. ⇒𝐷 𝑛 =O(𝑛 log 2 𝑛)

Closest Pair (2 dimension) Improved if(𝒏≤𝟐) return | 𝒑 𝟏 − 𝒑 𝟐 | Compute separation line 𝑳 such that half the points are on one side and half on the other side. ( 𝜹 𝟏 , 𝒑 𝟏 ) = Closest-Pair(left half) ( 𝜹 𝟐 , 𝒑 𝟐 ) = Closest-Pair(right half) 𝜹 = min( 𝜹 𝟏 , 𝜹 𝟐 ) 𝒑 𝒔𝒐𝒓𝒕𝒆𝒅 = merge( 𝒑 𝟏 , 𝒑 𝟐 ) (merge sort it by y-coordinate) Let 𝒒 be points (ordered as 𝒑 𝒔𝒐𝒓𝒕𝒆𝒅 ) that is 𝜹 from line L. for 𝒊=𝟏,𝟐,⋯,𝒎 for k = 𝟏,𝟐,⋯,𝟏𝟏 if 𝒊+𝒌≤𝒎  = min(, distance(q[i], q[i+k])); return  and 𝒑 𝒔𝒐𝒓𝒕𝒆𝒅 . } 𝑇 𝑛 ≤ 1 if 𝑛=1 2𝑇 𝑛 2 +𝑂 𝑛 o.w. ⇒𝐷 𝑛 =𝑂(𝑛 log 𝑛)

Master Theorem Suppose 𝑇 𝑛 =𝑎 𝑇 𝑛 𝑏 +𝑐 𝑛 𝑘 for all 𝑛>𝑏. Then, If 𝑎< 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 If 𝑎= 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 log 𝑛 If 𝑎> 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 log 𝑏 𝑎 Works even if it is 𝑛 𝑏 instead of 𝑛 𝑏 . We also need 𝑎≥1,𝑏>1 , 𝑘≥0 and 𝑇 𝑛 =𝑂(1) for 𝑛≤𝑏.

Master Theorem Suppose 𝑇 𝑛 =𝑎 𝑇 𝑛 𝑏 +𝑐 𝑛 𝑘 for all 𝑛>𝑏. Then, If 𝑎< 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 If 𝑎= 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 log 𝑛 If 𝑎> 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 log 𝑏 𝑎 Example: For mergesort algorithm we have 𝑇 𝑛 =2𝑇 𝑛 2 +𝑂 𝑛 . So, 𝑘=1,𝑎= 𝑏 𝑘 and 𝑇 𝑛 =Θ(𝑛 log 𝑛)

Proving Master Theorem 𝑛 Problem size 𝑛/𝑏 𝑛/ 𝑏 2 𝑏 1 𝑇 𝑛 =𝑎𝑇(𝑛/𝑎)+𝑐 𝑛 𝑘 # probs 𝑎2 𝑎 1 𝑎𝑑 cost 𝑐 𝑛 𝑘 𝑎⋅𝑐 𝑛/𝑏 𝑘 𝑎 2 ⋅𝑐 𝑛/ 𝑏 2 𝑘 𝑎 𝑘 ⋅𝑐 𝑛/ 𝑏 𝑘 𝑘 𝑑= log 𝑏 𝑛 𝑎 𝑇 𝑛 = 𝑖=0 𝑑= log 𝑏 𝑛 𝑎 𝑖 𝑐 𝑛 𝑏 𝑖 𝑘

Master Theorem Suppose 𝑇 𝑛 =𝑎 𝑇 𝑛 𝑏 +𝑐 𝑛 𝑘 for all 𝑛>𝑏. Then, If 𝑎< 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 If 𝑎= 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 𝑘 log 𝑛 If 𝑎> 𝑏 𝑘 then 𝑇 𝑛 =Θ 𝑛 log 𝑏 𝑎 # of problems increases slower than the decreases of cost. First term dominates. # of problems increases faster than the decreases of cost Last term dominates.

A Useful Identity Theorem: 1+𝑥+ 𝑥 2 +…+ 𝑥 𝑑 = 𝑥 𝑑+1 −1 𝑥−1 Proof: Let 𝑆=1+𝑥+ 𝑥 2 +…+ 𝑥 𝑑 Then, 𝑥𝑆=𝑥+ 𝑥 2 +…+ 𝑥 𝑑+1 So, 𝑥𝑆−𝑆= 𝑥 𝑑+1 −1 i.e., 𝑆 𝑥−1 = 𝑥 𝑑+1 −1 Therefore, 𝑆= 𝑥 𝑑+1 −1 𝑥−1 Corollary: 1+𝑥+ 𝑥 2 +…+ 𝑥 𝑑 = 𝑂 𝑥 1 if 𝑥<1 𝑑+1 if 𝑥=1 𝑂 𝑥 𝑥 𝑑+1 if 𝑥>1 𝑂 𝑥 means the hidden constant depends on 𝑥

Solve: 𝑇 𝑛 =𝑎𝑇 𝑛 𝑏 +𝑐 𝑛 𝑘 Corollary: 1+𝑥+ 𝑥 2 +…+ 𝑥 𝑑 = Θ 𝑥 1 if 𝑥<1 Θ 𝑑 if 𝑥=1 Θ 𝑥 𝑥 𝑑+1 if 𝑥>1 Going back, we have 𝑇 𝑛 = 𝑖=0 𝑑= log 𝑏 𝑛 𝑎 𝑖 𝑐 𝑛 𝑏 𝑖 𝑘 =𝑐 𝑛 𝑘 𝑖=0 𝑑= log 𝑏 𝑛 𝑎 𝑏 𝑘 𝑖 Hence, we have 𝑇 𝑛 =Θ 𝑛 𝑘 1 if 𝑎< 𝑏 𝑘 log 𝑏 𝑛 if 𝑎= 𝑏 𝑘 𝑎 𝑏 𝑘 log 𝑏 𝑛 if 𝑎> 𝑏 𝑘 constant depends on 𝑎,𝑏,𝑐 When we can hide constant?

Solve: 𝑇 𝑛 =𝑎𝑇 𝑛 𝑏 +𝑐 𝑛 𝑘 𝑇 𝑛 =Θ 𝑛 𝑘 1 if 𝑎< 𝑏 𝑘 log 𝑏 𝑛 if 𝑎= 𝑏 𝑘 𝑎 𝑏 𝑘 log 𝑏 𝑛 if 𝑎> 𝑏 𝑘 For 𝑎< 𝑏 𝑘 , we simply have 𝑇 𝑛 =Θ 𝑛 𝑘 . For 𝑎= 𝑏 𝑘 , we have 𝑇 𝑛 =Θ 𝑛 𝑘 log 𝑏 𝑛 =Θ( 𝑛 𝑘 log 𝑛 ). For 𝑎> 𝑏 𝑘 , we have 𝑇 𝑛 =Θ 𝑛 𝑘 𝑎 𝑏 𝑘 log 𝑏 𝑛 =Θ( 𝑛 log 𝑏 𝑎 ). 𝑎 log 𝑏 𝑛 = (𝑏 log 𝑏 𝑎 ) log 𝑏 𝑛 = (𝑏 log 𝑏 𝑛 ) log 𝑏 𝑎 = 𝑛 log 𝑏 𝑎 𝑏 𝑘 log 𝑏 𝑛 = 𝑏 log 𝑏 𝑛 𝑘 = 𝑛 𝑘

Example