Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Advertisements

Analysis of Algorithms
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
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.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
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.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
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).
Analysis of Algorithms CS 477/677
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
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.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
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.
Algorithm Design and Analysis (ADA)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Analysis of Algorithms
Discrete Maths Objective to introduce mathematical induction through examples , Semester 2, Mathematical Induction 1.
Analysis of Algorithms CS 477/677
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
Chapter 7 Recursion 1CSCI 3333 Data Structures. 2 Recurrent Sequence A recursively defined sequence – First, certain initial values are specified  c.f.,
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.
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Discrete Maths: Induction/1 1 Discrete Maths Objective – –to introduce mathematical induction through examples , Semester
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Introduction to Algorithms Chapter 4: Recurrences.
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
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.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
COSC 3101A - Design and Analysis of Algorithms 3 Recurrences Master’s Method Heapsort and Priority Queue Many of the slides are taken from Monica Nicolescu’s.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 4: Recurrences.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
BY Lecturer: Aisha Dawood. A recurrence is a function is defined in terms of:  one or more base cases, and  itself, with smaller arguments. 2.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 2.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Analysis of Algorithms CS 477/677
Recursion Ali.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Lecture Outline for Recurrences
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Recurrences (Method 4) Alexandra Stefan.
Divide and Conquer (Merge Sort)
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Analysis of Algorithms
Algorithms Recurrences.
Big O notation f = O(g(n)) c g(n)
Quicksort Quick sort Correctness of partition - loop invariant
Analysis of Algorithms CS 477/677
Presentation transcript:

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3

CS 477/677 - Lecture 32 Mathematical Induction Used to prove a sequence of statements ( S(1), S(2), … S(n) ) indexed by positive integers Proof: –Basis step: prove that the statement is true for n = 1 –Inductive step: assume that S(n) is true and prove that S(n+1) is true for all n ≥ 1 Find case n “within” case n+1

More Examples CS 477/677 - Lecture 33

4 Recurrent Algorithms BINARY – SEARCH for an ordered array A, finds if x is in the array A[lo…hi] Alg.: BINARY-SEARCH (A, lo, hi, x) if (lo > hi) return FALSE mid   (lo+hi)/2  if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) mid lohi

CS 477/677 - Lecture 35 Example A[8] = {1, 2, 3, 4, 5, 7, 9, 11} –lo = 1hi = 8 x = 7 mid = 4, lo = 5, hi = 8mid = 6, A[mid] = x Found!

CS 477/677 - Lecture 36 Example A[8] = {1, 2, 3, 4, 5, 7, 9, 11} –lo = 1hi = 8 x = 6 mid = 4, lo = 5, hi = 8mid = 6, A[6] = 7, lo = 5, hi = mid = 5, A[5] = 5, lo = 6, hi = 5 NOT FOUND!

CS 477/677 - Lecture 37 Analysis of BINARY-SEARCH Alg.: BINARY-SEARCH (A, lo, hi, x) if ( lo > hi ) return FALSE mid   (lo+hi)/2  if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH ( A, lo, mid-1, x ) if ( x > A[mid] ) BINARY-SEARCH ( A, mid+1, hi, x ) T(n) = c + –T(n) – running time for an array of size n constant time: c 2 same problem of size n/2 constant time: c 1 constant time: c 3 T(n/2)

CS 477/677 - Lecture 38 Recurrences and Running Time Recurrences arise when an algorithm contains recursive calls to itself What is the actual running time of the algorithm? Need to solve the recurrence –Find an explicit formula of the expression (the generic term of the sequence)

CS 477/677 - Lecture 39 Example Recurrences T(n) = T(n-1) + nΘ(n 2 ) –Recursive algorithm that loops through the input to eliminate one item T(n) = T(n/2) + cΘ(lgn) –Recursive algorithm that halves the input in one step T(n) = T(n/2) + nΘ(n) –Recursive algorithm that halves the input but must examine every item in the input T(n) = 2T(n/2) + 1Θ(n) –Recursive algorithm that splits the input into 2 halves and does a constant amount of other work

CS 477/677 - Lecture 310 Methods for Solving Recurrences Iteration method Substitution method Recursion tree method Master method

CS 477/677 - Lecture 311 The Iteration Method T(n) = c + T(n/2) = c + c + T(n/4) = c + c + c + T(n/8) Assume n = 2 k T(n) = c + c + … + c + T(1) = clgn + T(1) = Θ(lgn) k times T(n/2) = c + T(n/4) T(n/4) = c + T(n/8)

CS 477/677 - Lecture 312 Iteration Method – Example T(n) = n + 2T(n/2) = n + 2(n/2 + 2T(n/4)) = n + n + 4T(n/4) = n + n + 4(n/4 + 2T(n/8)) = n + n + n + 8T(n/8) … = in + 2 i T(n/2 i ) = kn + 2 k T(1) = nlgn + nT(1) = Θ(nlgn) Assume: n = 2 k T(n/2) = n/2 + 2T(n/4)

CS 477/677 - Lecture 313 Iteration Method – Example T(n) = n + T(n-1) = n + (n-1) + T(n-2) = n + (n-1) + (n-2) + T(n-3) … = n + (n-1) + (n-2) + … T(1) = n(n+1)/ T(1) = n 2 + T(1) = Θ(n 2 )

CS 477/677 - Lecture 314 The substitution method 1.Guess a solution 2.Use induction to prove that the solution works

CS 477/677 - Lecture 315 Substitution method Guess a solution – T(n) = O(g(n)) –Induction goal: apply the definition of the asymptotic notation T(n) ≤ d g(n), for some d > 0 and n ≥ n 0 –Induction hypothesis: T(k) ≤ d g(k) for all k < n Prove the induction goal –Use the induction hypothesis to find some values of the constants d and n 0 for which the induction goal holds

CS 477/677 - Lecture 316 Example: Binary Search T(n) = c + T(n/2) Guess: T(n) = O(lgn) –Induction goal: T(n) ≤ d lgn, for some d and n ≥ n 0 –Induction hypothesis: T(n/2) ≤ d lg(n/2) Proof of induction goal: T(n) = T(n/2) + c ≤ d lg(n/2) + c = d lgn – d + c ≤ d lgn if: – d + c ≤ 0, d ≥ c

CS 477/677 - Lecture 317 Example 2 T(n) = T(n-1) + n Guess: T(n) = O(n 2 ) –Induction goal: T(n) ≤ c n 2, for some c and n ≥ n 0 –Induction hypothesis: T(n-1) ≤ c(n-1) 2 Proof of induction goal: T(n) = T(n-1) + n ≤ c (n-1) 2 + n = cn 2 – (2cn – c - n) ≤ cn 2 if: 2cn – c – n ≥ 0  c ≥ n/(2n-1)  c ≥ 1/(2 – 1/n) –For n ≥ 1  2 – 1/n ≥ 1  any c ≥ 1 will work

CS 477/677 - Lecture 318 Example 3 T(n) = 2T(n/2) + n Guess: T(n) = O(nlgn) –Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n 0 –Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2) Proof of induction goal: T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n = cn lgn – cn + n ≤ cn lgn if: - cn + n ≤ 0  c ≥ 1

CS 477/677 - Lecture 319 Changing variables –Rename: m = lgn  n = 2 m T ( 2 m ) = 2T(2 m/2 ) + m –Rename: S(m) = T(2 m ) S(m) = 2S(m/2) + m  S(m) = O(mlgm) (demonstrated before) T(n) = T(2 m ) = S(m) = O(mlgm)=O(lgnlglgn) Idea: transform the recurrence to one that you have seen before T(n) = 2T( ) + lgn

CS 477/677 - Lecture 320 The recursion-tree method Convert the recurrence into a tree: –Each node represents the cost incurred at that level of recursion –Sum up the costs of all levels Used to “guess” a solution for the recurrence

CS 477/677 - Lecture 321 Readings Chapter 4