Introduction to Algorithms

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J
Advertisements

5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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.
Introduction to Algorithms 6.046J/18.401J L ECTURE 3 Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
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.
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.
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.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
File Organization and Processing Week 13 Divide and Conquer.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
CMPT 438 Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Lecture 2 Sorting.
分治法.
Introduction to Algorithms Prof. Charles E. Leiserson
UNIT- I Problem solving and Algorithmic Analysis
Divide-and-Conquer 6/30/2018 9:16 AM
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Algorithm Design & Analysis
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Divide and Conquer.
CS 3343: Analysis of Algorithms
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Recursion-tree method
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 3343: Analysis of Algorithms
CSE 2010: Algorithms and Data Structures
Introduction to Algorithms
Introduction to Algorithms
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
CMPS 3120: Computational Geometry Spring 2013
Divide & Conquer Algorithms
Solving recurrence equations
Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1.
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer Merge sort and quick sort Binary search
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

6.006- Introduction to Algorithms Introduction to Algorithms, Lecture 5 September 24, 2001 6.006- Introduction to Algorithms Lecture 8 Prof. Silvio Micali CLRS: chapter 4. © 2001 by Charles E. Leiserson

Menu Sorting! Insertion Sort Merge Sort Recurrences Master theorem

The problem of sorting How can we do it efficiently ? Input: array A[1…n] of numbers. Output: permutation B[1…n] of A such that B[1] £ B[2] £ … £ B[n] . e.g. A = [7, 2, 5, 5, 9.6] → B = [2, 5, 5, 7, 9.6] How can we do it efficiently ?

Insertion sort A: key sorted new location of key INSERTION-SORT (A, n) ⊳ A[1 . . n] for j ← 2 to n insert key A[j] into the (already sorted) sub-array A[1 .. j-1] by pairwise key-swaps down to its right position Illustration of iteration j A: 1 n i j key new location of key sorted

Example of insertion sort 8 2 4 9 3 6

Example of insertion sort 8 2 4 9 3 6 1 swap

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 1 swap

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 0 swap

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 3 swaps

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6 2 swaps

Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6 2 3 4 6 8 9 done Running time? O(n2) e.g. when input is A = [n, n − 1, n − 2, . . . , 2, 1]

Meet Merge Sort MERGE-SORT A[1 . . n] divide and conquer If n = 1, done (nothing to sort). Otherwise, recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . “Merge” the two sorted sub-arrays. Key subroutine: MERGE

Merging two sorted arrays 20 13 7 2 12 11 9 1 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 1 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1 2 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2 7 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7 9 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9 11 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 20 13 12 1 2 7 9 11 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 … output array

Merging two sorted arrays 20 13 7 2 12 11 9 1 … output array Time = Q(n) to merge a total of n elements (linear time).

Analyzing merge sort MERGE-SORT A[1 . . n] T(n) Q(1) If n = 1, done Q(n) If n = 1, done 2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] 3. “Merge” the two sorted lists T(n) = Q(1) if n = 1; 2T(n/2) + Q(n) if n > 1.

Recurrence solving Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. … Q(1) …

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. … Q(1) …

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. … Q(1) …

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. … Q(1) …

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. … … Q(1) …

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. h = lg n cn/4 cn/4 cn/4 cn/4 cn … … Q(1) … Q(n) #leaves = n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. h = lg n cn/4 cn/4 cn/4 cn/4 cn … … Q(1) … Q(n) Total = ? #leaves = n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. h = lg n cn/4 cn/4 cn/4 cn/4 cn … … Q(1) Q(n) Total = Q(n lg n) #leaves = n

The master method “One theorem for all recurrences” (sort of) It applies to recurrences of the form T(n) = a T(n/b) + f (n) , where a ³ 1, b > 1, and f is positive. e.g. Mergesort: a=2, b=2, f(n)=O(n) e.g.2 Binary Search: a=1, b=2, f(n)=O(1) Basic Idea: Compare f (n) with nlogba. size of each subproblem #subproblems time to split into subproblems and combine results

Idea of master theorem T(n) = a T(n/b) + f (n) Recursion tree: f (n) h = logbn a … f (n/b) f (n/b) f (n/b) a f (n/b) a … f (n/b2) f (n/b2) f (n/b2) a2 f (n/b2) … … T (1) nlogbaT (1) #leaves = ah = alogbn = nlogba ?

Idea of master theorem T(n) = a T(n/b) + f (n) Recursion tree: f (n) … f (n/b) f (n/b) f (n/b) a f (n/b) a h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 1: The weight increases geometrically from root to leaves. T (1) nlogbaT (1) ⇒ Leaves hold a constant fraction of total weight! Q(nlogba) ?

Idea of master theorem T(n) = a T(n/b) + f (n) Recursion tree: f (n) … f (n/b) f (n/b) f (n/b) a f (n/b) a h = logbn … f (n/b2) f (n/b2) f (n/b2) a2 f (n/b2) … … CASE 2: Weight approximately the same on each level. T (1) nlogbaT (1) ⇒ Total weight ≈ #levels × leaves’ weight! Θ( 𝑛 log 𝑏 𝑎 log 𝑏 𝑎) ?

Idea of master theorem T(n) = a T(n/b) + f (n) Recursion tree: f (n) … f (n/b) f (n/b) f (n/b) a f (n/b) a h = logbn … f (n/b2) f (n/b2) f (n/b2) a2 f (n/b2) … … CASE 3: Weight decreases geometrically from root to leaves. T (1) nlogbaT (1) ⇒ Root holds a constant fraction of total weight! Q( f (n)) ?

Three common cases Compare f (n) with nlogba: f (n) = Θ(nlogba – e) for some constant e > 0. I.e., f (n) grows polynomially slower than nlogba (by an ne factor). cost of level i = ai f (n/bi) = Q(nlogba-e  bi e ) (be)i so geometric increase of cost as we go deeper in the tree hence, leaf level cost dominates! Solution: T(n) = Q(nlogba) .

Three common cases (cont.) Compare f (n) with nlogba: f (n) = Q(nlogba log 𝑏 𝑘 𝑛 ) for some constant k ³ 0. I.e., f (n) and nlogba grow at similar rates. (cost of level i ) = ai f (n/bi) = Q(nlogba ⋅log⁡_𝑏k(n/bi)) so all levels have about the same cost Solution: 𝑇 𝑛 =Θ( 𝑛 log 𝑏 𝑎 log 𝑏 𝑘+1 𝑛)

Three common cases (cont.) Compare f (n) with nlogba: f (n) = Θ(nlogba + e) for some constant e > 0. I.e., f (n) grows polynomially faster than nlogba (by an ne factor). (cost of level i ) = ai f (n/bi) = Q(nlogba+e  b-i e ) so geometric decrease of cost as we go deeper in the tree hence, root cost dominates! Solution: T(n) = Q( f (n) ) .

Example1 Please don’t! T(n) = 2T(n/2) + 1 a = 2, b = 2  nlogba =n f (n) = 1 CASE 1: f (n) = O(n1 – e) for e = 1  T(n) = Q(n). Use Master Theorem: Compute a and b Compute nlogba and f (n) Compare

Example 2 T(n) = 2T(n/2) + n a = 2, b = 2  nlogba = n f (n) = n CASE 2: f (n) = Q(nlg0n), that is, k = 0  T(n) = Q(nlg n).

Example 3 T(n) = 4T(n/2) + n3 a = 4, b = 2  nlogba = n2 f (n) = n3 CASE 3: f (n) = W(n2 + e) for e = 1  T(n) = Q(n3).

Let’s go master the rest of the day!