Prof. Dan Barrish-Flood Algorithms (su04)

Slides:



Advertisements
Similar presentations
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Advertisements

Analysis of Algorithms
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
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
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.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
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.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
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.
Analysis of Recursive Algorithms October 29, 2014
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Merge Sort Solving Recurrences The Master Theorem
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Asymptotic Analysis (based on slides used at UMBC)
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.
Foundations II: Data Structures and Algorithms
Spring 2015 Lecture 2: Analysis of Algorithms
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recursion Ali.
Analysis of Algorithms CS 477/677
COMP108 Algorithmic Foundations Divide and Conquer
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
Mergesort Based on divide-and-conquer strategy
CS201: Data Structures and Discrete Mathematics I
Algorithms and Data Structures Lecture III
Data Structures Review Session
CS200: Algorithms Analysis
Recurrences (Method 4) Alexandra Stefan.
Divide and Conquer (Merge Sort)
Divide & Conquer Algorithms
Analysis of Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II
Algorithms Recurrences.
Recurrences.
Presentation transcript:

Prof. Dan Barrish-Flood Algorithms (su04) Bounding Facts Prof. Dan Barrish-Flood Algorithms (su04)

More Bounding Facts If f(n) = o(g(n)), then f(n) ¹ Q(g(n)) Proof: Assume

More Bounding Facts (continued) So eventually ( for n large enough)

Summations

Common Summations

In general: divide by (r – 1)

Running Times for Some Simple Iterative Algorithms CLEAR-MATRIX(A[1 ¼ n,1¼n]) 1 for i ¬ 1 to n 2 do for j ¬ 1 to n 3 do A[i,j]=0

Running Times for Some Simple Iterative Algorithms (continued) IDENTITY-MATRIX(A[1 ¼ n,1¼n]) 1 CLEAR-MATRIX(A) 2 for i ¬ 1 to n do 3 do A[i,i]=1 same asymptotic running time as CLEAR-MATRIX

CLEAR-LOWER-TRIANGLE(A[1¼n,1¼n]) (including diagonal) 1 for i ¬ 1 to n 2 do for j ¬ 1 to i 3 do A[i,j]=0

CLEAR-UPPER-TRIANGLE(A[1¼n,1¼n]) (including diagonal) 1 for i ¬ 1 to n 2 do for j ¬ i to n 3 do A[i,j]=0

{ AVERAGE-NEIGHBORS(A[1¼n]) 1 for i ¬ 2 to n-1 do 2 sum ¬ 0 3 for j ¬ i-1 to i+1 do 4 sum ¬ sum+A[j] 5 A[i] ¬ sum/3 { T(n)=q(n) LINEAR!

Mergesort To sort a list of n items: Sorting 1 or 0 items is trivial. sort the first half of the list sort the second half merge the sorted halves Sorting 1 or 0 items is trivial. Merging two sorted lists is easily done in linear (q(n)) time. (Just take the smallest item from each list and put it on the merged list.)

MERGESORT(A[1¼n]) 1 if n £ 1 then 2 return A 3 else 4 B ¬MERGESORT(A[1 ¼ ën/2û) 5 C ¬MERGESORT(A[ën/2û+1¼n]) 6 return MERGE(B,C)

Mergesort Example sorted sequence 1 2 2 3 4 5 6 6 2 4 5 6 1 2 3 6 2 5 1 2 2 3 4 5 6 6 merge 2 4 5 6 1 2 3 6 merge merge 2 5 4 6 1 3 2 6 merge merge merge merge 5 2 4 6 1 3 2 6 The operation of mergesort on the array A= á5,2,4,6,1,3,2,6 ñ. The lengths of the sorted sequences being merged increases as the algorithm progresses from bottom to top.

The Call Tree for Mergesort MS(n) MS(n/2) MS(n/4) ...MS(1) ...................................... MS(0)........... MS(n/4)

What is the running time of Mergesort? Let’s call it T(n). time taken by recursive calls T(n) = 2T(n/2) + n T(1) = 1 This is a recurrence equation, or recurrence for short. T(n) = + time to merge

Solving Recurrences Method 1: Know the answer. Not recommended as a general technique. Method 2: Recursion Trees Good for intuition Method 3: Telescoping + Transformations Fairly general

Recursion Trees 1. Draw the call tree for the algorithm. 2. Label each node with the work done at that invocation only. 3. Determine the total work done at each level of the tree. 4. Sum the values at each level to get the total running time.

Recursive Factorial T(1) = 1 FACT(n) T(n) = T(n-1) + 1, n ≥ 2 1 if n £ 1 then Call tree: 2 return 1 FACT(n) 3 else | 4 return n*FACT(n-1) FACT(n-1) | FACT(n-2) FACT(1)

Recursion Tree for FACT levels total work at each level 1 1 n

Recursion Tree for Mergesort total work at each level lgn + 1 levels n n n/2 n/2 n n/4 n/4 n/4 n/4 n n n(lgn+1) n(lgn+1) = nlgn + n = Θ(nlgn)

Telescoping We can solve it like this: Consider T(n) - T(n-1) = 1 T(1) - T(0) = 1 T(n) - T(0) = n T(n) = n+1 Consider T(n) = T(n-1) + 1, n≥1 T(0) = 1 (The recurrence for FACT(n).) “world’s simplest recurrence”

Generalizing ... The RHS’s can be different from each other - as long as they don’t contain T. Check

Also... The LHS doesn’t have to be T(n). All we need is that expr(n)-expr(n-1) = ... E.g. nT(n) = (n-1)T(n-1) + 1, T(1) = 1 nT(n) - (n-1)T(n-1) = 1 (n-1)T(n-1) - (n-2)T(n-2) = 1 2T(2) - T(1) = 1 nT(n) - T(1) = n-1 nT(n) = n, T(n) = 1

Finally... It doesn’t matter whether the “base case” is T(0), T(1), T(2), etc. - as long as it’s some constant. Our goal is to get all recurrences into telescoping form.

Domain Transformations Consider: T(n) = T(n/2) + 1 , n ³ 2 T(1) = 1 (The recurrence for binary search) Not in the form we want, which is T(n) = T(n-1) + ... But let’s do the following:

Our recurrence T(n) = T(n/2) + 1, n ≥ 2 T(1) = 1 has become S(k) = S(k-1) + 1, k ≥ 1 S(0) = 1 which telescopes, giving S(k) = k+ 1. Back-substituting: T(n) = S(k) = k+1 = lgn+ 1 (since we defined k = lgn.)

Another recurrence: T(n) = T(n/2) + n, T(1) = 1

Range Transformations (recurrence for “Towers of Hanoi “ puzzle.) T(n) = 2T(n-1) + 1, T(1) = 1. The “2” multiplying T(n-1) is the problem. So let’s multiply through by (Trust me.) We call this a summing factor. It’s in telescoping form!

Now we are ready to tackle MERGESORT’s recurrence. T(n) = 2T(n/2) + n, n > 1 T(1) = 1 First, a domain transformation: world’s simplest recurrence!

We can solve that: R(k) = k + 1 Back-substituting:

The Telescope/Transformation Method: A Summary 1. Do domain transformations to deal with the argument to T. E.g. T(n/2) ® S(k-1). 2. Do range transformations to handle multipliers of T. E.g. 2T(n-1) ®2 T(n-1). 3. Telescope. 4. Check. -(n-1)