Merge Sort Solving Recurrences The Master Theorem

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Advertisements

Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
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.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
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).
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
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.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
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.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Analysis of Recursive Algorithms October 29, 2014
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS. Definitions of Algorithm A mathematical relation between an observed quantity and a variable used in a step-by-step.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 9/8/2015 CS 332: Algorithms Review for Exam 1.
Analysis of Algorithms
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
CS 2133:Data Structures Merge Sort Solving Recurrences The Master Theorem.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Introduction to Algorithms Chapter 4: Recurrences.
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.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Analysis of Algorithms Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets.
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Spring 2015 Lecture 2: Analysis of Algorithms
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.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Introduction to Algorithms: Recurrences
Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Algorithms and Data Structures Lecture III
Ch 4: Recurrences Ming-Te Chi
Introduction to Algorithms
Divide and Conquer (Merge Sort)
Merge Sort Solving Recurrences The Master Theorem
Solving Recurrences Continued The Master Theorem
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Richard Anderson Lecture 12, Winter 2019 Recurrences
Algorithms and Data Structures Lecture III
Presentation transcript:

Merge Sort Solving Recurrences The Master Theorem CS 332: Algorithms Merge Sort Solving Recurrences The Master Theorem David Luebke 1 4/23/2017

Administrative Question Who here cannot make Monday-Wednesday office hours at 10 AM? If nobody, should we change class time? David Luebke 2 4/23/2017

Homework 1 Homework 1 will be posted later today (Problem with the exercise numbering, sorry) Due Monday Jan 28 at 9 AM Should be a fairly simple warm-up problem set David Luebke 3 4/23/2017

Review: Asymptotic Notation Upper Bound Notation: f(n) is O(g(n)) if there exist positive constants c and n0 such that f(n)  c  g(n) for all n  n0 Formally, O(g(n)) = { f(n):  positive constants c and n0 such that f(n)  c  g(n)  n  n0 Big O fact: A polynomial of degree k is O(nk) David Luebke 4 4/23/2017

Review: Asymptotic Notation Asymptotic lower bound: f(n) is (g(n)) if  positive constants c and n0 such that 0  cg(n)  f(n)  n  n0 Asymptotic tight bound: f(n) is (g(n)) if  positive constants c1, c2, and n0 such that c1 g(n)  f(n)  c2 g(n)  n  n0 f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n)) David Luebke 5 4/23/2017

Other Asymptotic Notations A function f(n) is o(g(n)) if  positive constants c and n0 such that f(n) < c g(n)  n  n0 A function f(n) is (g(n)) if  positive constants c and n0 such that c g(n) < f(n)  n  n0 Intuitively, o() is like < O() is like  () is like > () is like  () is like = David Luebke 6 4/23/2017

Merge Sort MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A // (how long should this take?) David Luebke 7 4/23/2017

Merge Sort: Example Show MergeSort() running on the array A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9}; David Luebke 8 4/23/2017

Analysis of Merge Sort Statement Effort So T(n) = (1) when n = 1, and 2T(n/2) + (n) when n > 1 So what (more succinctly) is T(n)? MergeSort(A, left, right) { T(n) if (left < right) { (1) mid = floor((left + right) / 2); (1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); (n) } David Luebke 9 4/23/2017

Recurrences The expression: is a recurrence. Recurrence: an equation that describes a function in terms of its value on smaller functions David Luebke 10 4/23/2017

Recurrence Examples David Luebke 11 4/23/2017

Solving Recurrences Substitution method Iteration method Master method David Luebke 12 4/23/2017

Solving Recurrences The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction to find the constants and show that solution works Examples: T(n) = 2T(n/2) + (n)  T(n) = (n lg n) T(n) = 2T(n/2) + n  ??? David Luebke 13 4/23/2017

Solving Recurrences The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction to find the constants and show that solution works Examples: T(n) = 2T(n/2) + (n)  T(n) = (n lg n) T(n) = 2T(n/2) + n  T(n) = (n lg n) T(n) = 2T(n/2 )+ 17) + n  ??? David Luebke 14 4/23/2017

Solving Recurrences The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction to find the constants and show that solution works Examples: T(n) = 2T(n/2) + (n)  T(n) = (n lg n) T(n) = 2T(n/2) + n  T(n) = (n lg n) T(n) = 2T(n/2+ 17) + n  (n lg n) David Luebke 15 4/23/2017

Solving Recurrences Another option is what the book calls the “iteration method” Expand the recurrence Work some algebra to express as a summation Evaluate the summation We will show several examples David Luebke 16 4/23/2017

s(n) = c + s(n-1) c + c + s(n-2) 2c + s(n-2) 2c + c + s(n-3) … kc + s(n-k) = ck + s(n-k) David Luebke 17 4/23/2017

So far for n >= k we have What if k = n? s(n) = ck + s(n-k) What if k = n? s(n) = cn + s(0) = cn David Luebke 18 4/23/2017

So far for n >= k we have What if k = n? So Thus in general s(n) = ck + s(n-k) What if k = n? s(n) = cn + s(0) = cn So Thus in general s(n) = cn David Luebke 19 4/23/2017

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) = n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) David Luebke 20 4/23/2017

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) = = n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) = David Luebke 21 4/23/2017

So far for n >= k we have David Luebke 22 4/23/2017

So far for n >= k we have What if k = n? David Luebke 23 4/23/2017

So far for n >= k we have What if k = n? David Luebke 24 4/23/2017

So far for n >= k we have What if k = n? Thus in general David Luebke 25 4/23/2017

T(n) = 2T(n/2) + c 2(2T(n/2/2) + c) + c 22T(n/22) + 2c + c … 2kT(n/2k) + (2k - 1)c David Luebke 26 4/23/2017

So far for n > 2k we have What if k = lg n? T(n) = 2kT(n/2k) + (2k - 1)c What if k = lg n? T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c = n T(n/n) + (n - 1)c = n T(1) + (n-1)c = nc + (n-1)c = (2n - 1)c David Luebke 27 4/23/2017