CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

A simple example finding the maximum of a set S of n numbers.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
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.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Analysis of Recursive Algorithms
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
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).
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Introduction to Algorithm design and analysis
Analysis of Recursive Algorithms October 29, 2014
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
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.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
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.
Project 2 due … Project 2 due … Project 2 Project 2.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
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.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Foundations II: Data Structures and Algorithms
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.
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.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Analysis of Algorithms CS 477/677
Mathematical Foundations (Solving Recurrence)
Unit 1. Sorting and Divide and Conquer
Algorithm Design & Analysis
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide and Conquer (Merge Sort)
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Divide & Conquer Algorithms
Introduction To Algorithms
Algorithms Recurrences.
nalysis of lgorithms A A Universidad Nacional de Colombia
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Recurrences

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Recurrences Coming up Merge Sort (Chap 2.3) Recurrences (Chap )

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort Merge Sort : divide-and-conquer approach Insertion Sort: Incremental approach The Divide-and-Conquer Paradigm: Divide: Divide the problem into a number of subproblems. Conquer: If the subproblem sizes are small enough, solve them directly. Otherwise solve them recursively. Combine: Combine the solutions to the subproblems into the solution for the original problem.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort Suppose there are some people called Mr. MergeSort. They are identical. They don’t know how to do sorting. But each of them has a secretary called Mr. Merge, who can merge 2 sorted sequences into one sorted sequence.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort At the beginning, a Mr. MergeSort is called to sort: Then 2 other Mr. MergeSorts are called to sort: Both of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 4 other Mr. MergeSorts are called to sort: All of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 8 other Mr. MergeSorts are called to sort: “So complicated!!, I’ll split them and call other Mr. MergeSorts to handle.” All of them say ‘This is easy. No need to do anything.’

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort Then the first Mr. MergeSort succeeds and returns. Then each of the 2 Mr. MergeSorts returns the merged numbers. Then the 4 Mr. MergeSorts returns the merged numbers. Then the 8 Mr. MergeSorts return All of them say ‘This is easy. No need do anything.’ Both Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The 4 Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The first Mr. MergeSort calls his secretary Mr. Merge to merge the returned numbers

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort MERGE-SORT (A,p,r) 1if p < r 2Then q   (p+r)/2  3MERGE-SORT(A,p,q) 4MERGE-SORT(A,q+1,r) 5MERGE (A,p,q,r) The MERGE-SORT(A,p,r) procedure sorts the elements A[p,..r]: A = p th r th  x  : “Floor” The least integer greater than x  x  :“Ceiling” The greatest integer less than x MERGE-SORT (A,p,r) 1if p < r 2Then q   (p+r)/2  3MERGE-SORT(A,p,q) 4MERGE-SORT(A,q+1,r) 5MERGE (A,p,q,r)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort The MERGE(A,p,q,r) procedure merges 2 sorted sequences: A[p..q] and A[q+1..r] A = p th r th q th Step 1 : Copy A[p..q], A[q+1..r] to 2 temporary arrays L and R. Step 2 :Adds a largest value,  (a sentinel: ending condition), to each of L and R. Step 3 :Continuously remove the smallest one from L and R back to A until finished L= R=  L=  R= A = p th r th  L=  R= A = p th r th  L=  R= A = p th r th  L=  R= A = p th r th  L= 3 6  R= A = p th r th  L= 6  R= A = p th r th 5 7  L= 6  R= A = p th r th 7  L= 6  R= A = p th r th 7  L=  R= A = p th r th  L=  R=

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Merge Sort MERGE (A,p,q,r) 1n 1  q-p+1 2n 2  r-q 3create L[1..n 1 +1], R[1..n 2 +1] 4for i  1 to n 1 5do L[i]  A[p+i-1] 6for j  1 to n 2 7do R[j]  A[q+j] 8L[n 1 +1]   9R[n 2 +1]   10i  1 11j  1 12for k  p to r 13do if L[i]  R[j] 14then A[k]  L[i] 15i  i+1 16else 17j  j+1 Step 1 : Copy A[p..q], A[q+1..r] to 2 temporary arrays L and R. Step 2 :Adds a largest value,  (a sentinel: ending condition), to each of L and R. Step 3 :Continuously remove the smallest one from L and R back to A until finished. The MERGE(A,p,q,r) procedure merges 2 sorted sequences: A[p..q] and A[q+1..r]

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Analysis of MERGE Procedure Let n=n 1 + n 2 Line 1-3 and 8-11 takes constant time. So,  (1). Line 4-5 and Line 6-7 takes  (n 1 +n 2 ) =  (n) time. In line 12-17, the loop iterates n times, each of which takes constant time. So,  (n). Conclusion: The MERGE procedure runs in  (1) +  (n) +  (n) =  (n) time. MERGE (A,p,q,r) 1n 1  q-p+1 2n 2  r-q 3create L[1..n 1 +1], R[1..n 2 +1] 4for i  1 to n 1 5do L[i]  A[p+i-1] 6for j  1 to n 2 7do R[j]  A[q+j] 8L[n 1 +1]   9R[n 2 +1]   10i  1 11j  1 12for k  p to r 13do if L[i]  R[j] 14then A[k]  L[i] 15i  i+1 16else 17j  j+1

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Analysis of Merge Sort MERGE-SORT has a recursive call to itself, plus a call to MERGE. The Running time: Recurrence Equation MERGE-SORT (A,p,r) 1if p < r 2Then q   (p+r)/2  3MERGE-SORT(A,p,q) 4MERGE-SORT(A,q+1,r) 5MERGE (A,p,q,r)  (1)if n=1 T  n/2  + T  n/2  +  (n)if n>1 T(n) = Or simplified: cif n=1 2T(n/2)+cnif n>1 T(n) = Then, what is the complexity of Merge Sort? To sort A[1..n] using Merge Sort, we call MERGE-SORT(A,1,n)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences cif n=1 2T(n/2)+cnif n>1 T(n) = Solving Recurrences When an algorithm contains recursive call(s) to itself, (eg. Divide-and-conquer approaches), its running time can often be described by a recurrence equation. 3 methods to solve recurrences: Recursion-tree method Can directly prove or help to guess MI Substitution method guess a bound + prove by mathematical induction Master method Based on the Master Theorem => T(n) =  (?)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Recursion-tree method) T(n) Expanding the recursion tree: Recursion-tree method cn T(n/2) cn cn/2 T(n/4) cn/2 T(n/4) cif n=1 2T(n/2)+cnif n>1 T(n) =

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Recursion-tree method) Fully Expanded recursion tree: cn cn/2 cn/4 cn/2 cn/4 c*1 cn n Total: cn lg n + cn ie. T(n) =  (n lg n)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Substitution method) Step 1. Guess the form of the solution Example. Given: Substitution method 1if n=1 2T(  n/2  )+nif n>1 T(n) = We guess that T(n)=O(n lg n) To prove this we need to show “There exists positive constants c and n 0 such that...T(n)  cn lg n, for all n  n 0 ” Step 2 MI

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Substitution method) Step 2. Use mathematical induction to find the constants and prove. Assume it holds for  n/2 , ie. There exists positive constants c and n 0 such that T(  n/2  )  c  n/2  lg (  n/2  ). By substitution: T(n)  2 (c  n/2  lg (  n/2  )) + n  cn lg (n/2) + n = cn (lg(n)-lg(2))+n = cn (lg(n)-1)+n = cn lg n -cn + n  cn lg n T(n)=O(n lg n) “There exists a positive constants c and n 0 such that T(n)  cn lg n, for all n  n 0 ” 1if n=1 2T(  n/2  )+nif n>1 T(n) = This holds as long as c  1 MI

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Substitution method) Step 2 (cont’d). Then we need to find n 0. Try n 0 =1When n=1,T(n)= 1 cn lg n = (c*1) lg (1) = c * 0 = 0 => T(n)  cn lg n ? Try n 0 =2When n=2,T(n) = 2T(1) + 2 = 2(1) + 2 = 4 cn lg n = (c*2) lg (2) = c*2*1 = 2c => T(n)  cn lg n ? Try n 0 =3When n=3,T(n) = 2T(1) + 3 = 2(1) + 3 = 5 cn lg n = (c*3) lg (3) = 3*1.585*c = 4.755*c => T(n)  cn lg n ? Since n 0 =2 and n 0 =3 form the base cases for all n>=2, we thus completed the prove: T(n)  cn lg n is true when c>=2 and n>=2. T(n)=O(n lg n) “There exists a positive constants c and n 0 such that T(n)  cn lg n, for all n  n 0 ” 1if n=1 2T(  n/2  )+nif n>1 T(n) = MI

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Master method) A cookbook method For solving recurrences of the form: T(n) = a T(n/b) + f(n) Where a >= 1 and b > 1 and f(n) is an asymptotically positive function These algorithms work recursively by dividing a problem of size n into a subproblems, each of size n/b. f(n) = cost of dividing the problem and combining the results. We omit floors and ceilings (eg. interpret n/b to mean either  n/b  or  n/b . ) Master method

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Master method) Master Theorem Let T(n) be defined as T(n) = a T(n/b) + f(n) where a >=1 and b > 1. Then If f(n) = O(n log b a-  ) for some constant  >0, then T(n) =  (n log b a ) If f(n) =  (n log b a ), then T(n) =  (n log b a lg n) If f(n) =  (n log b a+  ) for some constant  >0, and if a f (n/b) <= c f(n) for some constant c <1 and all sufficiently large n, then T(n) =  (f(n)) The master method can be used only if f(n) satisfies any of the 3 cases.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Master method) T(n) = 9T(n/3) + n a = 9, b = 3 => n log b a = n log 3 9 = n 2 Sincef(n)= n = n (2-1) = O(n log  ) Where  =1 => Case 1 => T(n) =  (n log b a ) => T(n) =  (n 2 ) Example 1: T(n) = T(2n/3) + 1 a = 1, b = 3/2 => n log b a = n log 3/2 1 = n 0 = 1 Since f(n) = 1 =  (n log b a ) => Case 2 => T(n) =  (n log b a lg n) => T(n) =  (lg n) Example 2: Master Theorem Master Theorem For T(n) = a T(n/b) + f(n) where a >=1 and b > 1. If f(n) = O(n log b a-  ) for some constant  >0, then T(n) =  (n log b a ) If f(n) =  (n log b a ), then T(n) =  (n log b a lg n) If f(n) =  (n log b a+  ) for some constant  >0,.. then T(n) =  (f(n))

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Solving Recurrence (Master method) Example 3: T(n) = 3T(n/4) + n lg n a=3, b=4 => n log b a = n log 4 3 = n Since f(n) = n lg n When n  2, f(n)  n lg 2 = n = n 1 = n , where  =0.201 => f(n) =  (n log b a+  ) a f(n/b) = a (n/b * lg(n/b)) = 3 (n/4 * lg (n/4)) = 3/4 n lg(n/4) = 3/4 (n lg(n)) - 3/4 (n lg(4))  3/4 f(n) => a f(n/b)  c f(n) where c=3/4. => Case 3. ie. T(n) =  (f(n)) =  (n lg n) Master Theorem Master Theorem For T(n) = a T(n/b) + f(n) where a >=1 and b > 1. If f(n) = O(n log b a-  ) for some constant  >0, then T(n) =  (n log b a ) If f(n) =  (n log b a ), then T(n) =  (n log b a lg n) If f(n) =  (n log b a+  ) for some constant  >0, and if a f (n/b) <= c f(n) for some constant c <1 and all sufficiently large n, then T(n) =  (f(n))

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences Recurrences Summary The Divide-and-Conquer Paradigm Merge Sort ( MERGE-SORT Procedure + MERGE Procedure ) Analysis of MERGE:  (n) Analysis of MERGE-SORT: A recurrence problem  (n lg n)) 3 Methods to solve recurrences: Recursion-tree method Substitution method Master method