CS 3343: Analysis of Algorithms

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
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
Analysis of Recursive Algorithms
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
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.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Introduction to Algorithms
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Analysis of Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Analysis of Algorithms
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Divide-And-Conquer-And-Combine
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
O-notation (upper bound)
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms and Data Structures Lecture III
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
CS 201 Fundamental Structures of Computer Science
Ch 4: Recurrences Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
CS 3343: Analysis of Algorithms
CS200: Algorithms Analysis
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
Trevor Brown CS 341: Algorithms Trevor Brown
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
Algorithms Recurrences.
Solving recurrence equations
Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Estimating Algorithm Performance
Divide-and-Conquer 7 2  9 4   2   4   7
Quicksort Quick sort Correctness of partition - loop invariant
Analysis of Algorithms
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:

CS 3343: Analysis of Algorithms Lecture 5: Solving recurrence by recursion-tree method 11/23/2018

Problem of the day How many multiplications do you need to compute 316? 316 =3 x 3 x 3 …. x 3 Answer: 15 316 =38 x 38 38 =34 x 34 34 =32 x 32 32 =3 x 3 Answer: 4 11/23/2018

Pseudo code int pow (b, n) // compute bn m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; 11/23/2018

Pseudo code int pow (b, n) m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; int pow (b, n) m = n >> 1; p = pow(b,m) * pow(b,m); if (n % 2) return p * b; else return p; 11/23/2018

Outline Review of last lecture – analyzing recursive algorithms Defining recurrence relation Today: analyzing recursive algorithms Solving recurrence relation 11/23/2018

Analyzing merge sort T(n) MERGE-SORT A[1 . . n] Θ(1) 2T(n/2) f(n) MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . “Merge” the 2 sorted lists Sloppiness: Should be T( n/2 ) + T( n/2 ) , but it turns out not to matter asymptotically. 11/23/2018

Recurrence for merge sort Divide: Trivial. Conquer: Recursively sort 2 subarrays. Combine: Merge two sorted subarrays T(n) = 2 T(n/2) + Θ(n) # subproblems Dividing and Combining subproblem size 11/23/2018

Binary Search BinarySearch (A[1..N], value) { if (N == 0) return -1; // not found mid = (1+N)/2; if (A[mid] == value) return mid; // found else if (A[mid] > value) return BinarySearch (A[1..mid-1], value); else return BinarySearch (A[mid+1, N], value) } 11/23/2018

Recursive Insertion Sort RecursiveInsertionSort(A[1..n]) 1. if (n == 1) do nothing; 2. RecursiveInsertionSort(A[1..n-1]); 3. Find index i in A such that A[i] <= A[n] < A[i+1]; 4. Insert A[n] after A[i]; 11/23/2018

Compute factorial Factorial (n) if (n == 1) return 1; return n * Factorial (n-1); Note: here we use n as the size of the input. However, usually for such algorithms we would use log(n), i.e., the bits needed to represent n, as the input size. 11/23/2018

Recurrence for computing power int pow (b, n) m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; int pow (b, n) m = n >> 1; p=pow(b,m)*pow(b,m); if (n % 2) return p * b; else return p; T(n) = ? T(n) = ? 11/23/2018

Recurrence for computing power int pow (b, n) m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; int pow (b, n) m = n >> 1; p=pow(b,m)*pow(b,m); if (n % 2) return p * b; else return p; T(n) = T(n/2)+(1) T(n) = 2T(n/2)+(1) 11/23/2018

What do they mean? Challenge: how to solve the recurrence to get a closed form, e.g. T(n) = Θ (n2) or T(n) = Θ(nlgn), or at least some bound such as T(n) = O(n2)? 11/23/2018

Solving recurrence Running time of many algorithms can be expressed in one of the following two recursive forms or Both can be very hard to solve. We focus on relatively easy ones, which you will encounter frequently in many real algorithms (and exams…) 11/23/2018

Solving recurrence Recursion tree or iteration method - Good for guessing an answer Substitution method - Generic method, rigid, but may be hard Master method - Easy to learn, useful in limited cases only - Some tricks may help in other cases 11/23/2018

Recurrence for merge sort T(n) = Q(1) if n = 1; 2T(n/2) + Q(n) if n > 1. We will usually ignore the base case, assuming it is always a constant (but not 0). 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n/2) dn 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn T(n/4) dn/2 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/2 dn/2 dn/4 dn/4 dn/4 dn/4 … Q(1) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/2 dn/2 h = log n dn/4 dn/4 dn/4 dn/4 … Q(1) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 h = log n dn/4 dn/4 dn/4 dn/4 … Q(1) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 dn h = log n dn/4 dn/4 dn/4 dn/4 … Q(1) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 dn h = log n dn/4 dn/4 dn/4 dn/4 dn … … Q(1) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 dn h = log n dn/4 dn/4 dn/4 dn/4 dn … … Q(1) #leaves = n Q(n) 11/23/2018

Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn dn/2 dn/2 dn h = log n dn/4 dn/4 dn/4 dn/4 dn … … Q(1) #leaves = n Q(n) Total Q(n log n) Later we will usually ignore d 11/23/2018

Recurrence for computing power int pow (b, n) m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; int pow (b, n) m = n >> 1; p=pow(b,m)*pow(b,m); if (n % 2) return p * b; else return p; T(n) = T(n/2)+(1) T(n) = 2T(n/2)+(1) Which algorithm is more efficient asymptotically? 11/23/2018

Time complexity for Alg1 Solve T(n) = T(n/2) + 1 T(n) = T(n/2) + 1 = T(n/4) + 1 + 1 = T(n/8) + 1 + 1 + 1 = T(1) + 1 + 1 + … + 1 = Θ (log(n)) log(n) Iteration method 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. T(n) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. T(n/2) 1 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 T(n/4) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 1 1 1 … Q(1) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 h = log n 1 1 1 1 … Q(1) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 h = log n 1 1 1 1 … Q(1) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 2 h = log n 1 1 1 1 … Q(1) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 2 h = log n 1 1 1 1 4 … … Q(1) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 2 h = log n 1 1 1 1 4 … … Q(1) #leaves = n Q(n) 11/23/2018

Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. 1 1 1 1 2 h = log n 1 1 1 1 4 … … Q(1) #leaves = n Q(n) Total Q(n) 11/23/2018

More iteration method examples T(n) = T(n-1) + 1 = T(n-2) + 1 + 1 = T(n-3) + 1 + 1 + 1 = T(1) + 1 + 1 + … + 1 = Θ (n) n - 1 11/23/2018

More iteration method examples T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n = T(1) + 2 + 3 + … + n = Θ (n2) 11/23/2018

3-way-merge-sort 3-way-merge-sort (A[1..n]) If (n <= 1) return; 3-way-merge-sort(A[n/3+1..2n/3]); 3-way-merge-sort(A[2n/3+1.. n]); Merge A[1..n/3] and A[n/3+1..2n/3]; Merge A[1..2n/3] and A[2n/3+1..n]; Is this algorithm correct? What’s the recurrence function for the running time? What does the recurrence function solve to? 11/23/2018

Unbalanced-merge-sort ub-merge-sort (A[1..n]) if (n<=1) return; ub-merge-sort(A[1..n/3]); ub-merge-sort(A[n/3+1.. n]); Merge A[1.. n/3] and A[n/3+1..n]. Is this algorithm correct? What’s the recurrence function for the running time? What does the recurrence function solve to? 11/23/2018

More recursion tree examples (1) T(n) = 3T(n/3) + n T(n) = 2T(n/4) + n T(n) = 3T(n/2) + n T(n) = 3T(n/2) + n2 T(n) = T(n/3) + T(2n/3) + n 11/23/2018

More recursion tree examples (2) T(n) = T(n-2) + n T(n) = T(n-2) + 1 T(n) = 2T(n-2) + n T(n) = 2T(n-2) + 1 11/23/2018

Ex 1: T(n) = 3T(n/3) + n n T(n/3) T(n/3) T(n/3) T(n/3) = 3T(n/9) + n/3 11/23/2018

T(n) = 3T(n/3) + n n n/3 T(n/3) T(n/3) T(n/9) T(n/9) T(n/9) 11/23/2018

T(n) = 3T(n/3) + n n n/3 T(n/3) T(n/3) n/9 T(n/9) T(n/9) T(n/27) 11/23/2018

T(n) = 3T(n/3) + n n n/3 T(n/3) T(n/3) n/9 T(n/9) T(n/9) n/27 T(n/27) 11/23/2018

T(n) = 3T(n/3) + n n n/3 n/3 n/3 T(n/3h)= T(1) h = log3n n/9 n/9 n/9 11/23/2018

Ex 2: T(n) = 2T(n/4) + n n T(n/4) T(n/4) T(n/4) = 2T(n/16) + n/4 11/23/2018

T(n) = 2T(n/4) + n n n/4 T(n/4) T(n/16) T(n/16) 11/23/2018

T(n) = 2T(n/4) + n n n/4 T(n/4) n/16 T(n/16) T(n/64) T(n/64) 11/23/2018

T(n) = 2T(n/4) + n n n/4 T(n/4) n/16 T(n/16) n/64 T(n/64) T(n/256) 11/23/2018

T(n) = 2T(n/4) + n … … … … … … n n/4 n/4 T(n/4h)= T(1) h = log4n n/16 11/23/2018

Ex 3: T(n) = 3T(n/2) + n n T(n/2) T(n/2) T(n/2) T(n/2) = 3T(n/4) + n/2 11/23/2018

T(n) = 3T(n/2) + n n n/2 T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) 11/23/2018

T(n) = 3T(n/2) + n n n/2 T(n/2) T(n/2) n/4 T(n/4) T(n/4) T(n/8) T(n/8) 11/23/2018

T(n) = 3T(n/2) + n n n/2 T(n/2) T(n/2) n/4 T(n/4) T(n/4) n/8 T(n/8) 11/23/2018

T(n) = 3T(n/2) + n … … … … … … … … … … … … n T(n/2h)= T(1) h = log2n 11/23/2018

Ex 4: T(n) = 3T(n/2) + n2 n2 T(n/2) T(n/2) T(n/2) 11/23/2018

T(n) = 3T(n/2) + n2 n2 (n/2)2 T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) 11/23/2018

T(n) = 3T(n/2) + n2 n2 (n/2)2 T(n/2) T(n/2) (n/4)2 T(n/4) T(n/4) 11/23/2018

T(n) = 3T(n/2) + n2 n2 (n/2)2 T(n/2) T(n/2) (n/4)2 T(n/4) T(n/4) 11/23/2018

T(n) = 3T(n/2) + n2 … … … … … … … … … … … … n2 T(n/2h)= T(1) h = log2n 11/23/2018

Ex 5: T(n) = T(n/2) + T(n/3) + n 11/23/2018

T(n) = T(n) = T(n/2) + T(n/3) + n 11/23/2018

T(n) = T(n) = T(n/2) + T(n/3) + n 11/23/2018

h = number of iterations Ex 6: T(n) = T(n-2) + 1 T(n) = T(n-2) + 1 = T(n-4) + 1 + 1 = T(n-6) + 1 + 1 + 1 = T(n-8) + 1 + 1 + 1 + 1 = h =  (n/2) =  (n) h = number of iterations T(n-2h) = T(0) h = n/2 11/23/2018

h = number of iterations Ex 7: T(n) = T(n-2) + n T(n) = T(n-2) + n = T(n-4) + (n-2) + n = T(n-6) + (n-4) + (n-2) + n = T(n-8) + (n-6) + (n-4) + (n-2) + n = h*(0+n)/2 =  (nh/2) =  (n2) h = number of iterations h = n/2 11/23/2018

Ex 8: T(n) = 2T(n-3) + 1 1 T(n-3) T(n-3) T(n-3) = 2T(n-6) + 1 11/23/2018

T(n) = 2T(n-3) + 1 1 1 T(n-3) T(n-6) T(n-6) T(n-6) = 2T(n-9) + 1 11/23/2018

T(n) = 2T(n-3) + 1 1 1 T(n-3) 1 T(n-6) T(n-9) T(n-9) 11/23/2018

T(n) = 2T(n-3) + 1 1 1 T(n-3) 1 T(n-6) 1 T(n-9) T(n-12) T(n-12) 11/23/2018

T(n) = 2T(n-3) + 1 … … … … … … 1 1 1 T(n-3h) = T(0) h = n/3 1 1 1 1 11/23/2018

Ex 9: T(n) = 2T(n-3) + n n T(n-3) T(n-3) T(n-3) = 2T(n-6) + (n-3) 11/23/2018

T(n) = 2T(n-3) + n n n-3 T(n-3) T(n-6) T(n-6) T(n-6) = 2T(n-9) + (n-6) 11/23/2018

T(n) = 2T(n-3) + n n n-3 T(n-3) n-6 T(n-6) T(n-9) T(n-9) 11/23/2018

T(n) = 2T(n-3) + n n n-3 T(n-3) n-6 T(n-6) n-9 T(n-9) T(n-12) T(n-12) 11/23/2018

Correct answer is same as in ex 7. T(n) = 2T(n-3) + n n n-3 n-3 T(n-3h) = T(0) h = n/3 … … n-6 n-6 … … n-9 n-9 … … T(n-12) T(n-12) Correct answer is same as in ex 7. 11/23/2018