October 1, 20011 Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

Introduction to Algorithms 6.046J
A simple example finding the maximum of a set S of n numbers.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
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.
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
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.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
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).
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)
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 Algorithms Jiafen Liu Sept
Analysis of Algorithms
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
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
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.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
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.
Spring 2015 Lecture 2: Analysis of Algorithms
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
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.
Data Structures and Algorithms Roberto Sebastiani - Week 03 - B.S. In Applied Computer.
Equal costs at all levels
Analysis of Algorithms CS 477/677
Divide-and-Conquer 6/30/2018 9:16 AM
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS 332: Algorithms Quicksort David Luebke /9/2019.
Trevor Brown CS 341: Algorithms Trevor Brown
Analysis of Algorithms
Algorithms Recurrences.
Design and Analysis of Algorithms
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithms and Data Structures Lecture II
Presentation transcript:

October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University

October 1, Merge Sort Revisited To sort n numbers if n=1 done! recursively sort 2 lists of numbers  n/2  and  n/2  elements merge 2 sorted lists in  (n) time Strategy break problem into similar (smaller) subproblems recursively solve subproblems combine solutions to answer

October 1, Merge Sort Revisited Merge-Sort(A, p, r): if p < r then q  (p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Merge-Sort(A, p, r): if p < r then q  (p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r]. Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r].

October 1, Recurrences Running times of algorithms with Recursive calls can be described using recurrences A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example: Merge Sort

October 1, Solving Recurrences Substitution method guessing the solutions verifying the solution by induction Iteration (recursion-tree) method expansion of the recurrence drawing of the recursion-tree Master method templates for different classes of recurrences

October 1, Substitution method

October 1, Substitution Method Achieving tighter bounds

October 1, Substitution Method (2) The problem? We could not rewrite the equality as: in order to show the inequality we wanted Sometimes to prove inductive step, try to strengthen your hypothesis T(n)  (answer you want) - (something > 0)

October 1, Substitution Method (3) Corrected proof: the idea is to strengthen the inductive hypothesis by subtracting lower-order terms!

October 1, Iteration Method The basic idea is to expand the recurrence and convert to a summation!

October 1, Iteration Method (2) The iteration method is often used to generate guesses for the substitution method Should know rules and have intuition for arithmetic and geometric series Math can be messy and hard Focus on two parameters the number of times the recurrence needs to be iterated to reach the boundary condition the sum of the terms arising from each level of the iteration process

October 1, Recursion Tree A recursion tree is a convenient way to visualize what happens when a recurrence is iterated Construction of a recursion tree

October 1, Recursion Tree (2)

October 1, Recursion Tree (3)

October 1, Master Method The idea is to solve a class of recurrences that have the form a >= 1 and b > 1, and f is asymptotically positive! Abstractly speaking, T(n) is the runtime for an algorithm and we know that a subproblems of size n/b are solved recursively, each in time T(n/b) f(n) is the cost of dividing the problem and combining the results. In merge-sort

October 1, Master Method (2) Split problem into a parts at log b n levels. There are leaves

October 1, Master Method (3) Number of leaves: Iterating the recurrence, expanding the tree yields The first term is a division/recombination cost (totaled across all levels of the tree) The second term is the cost of doing all subproblems of size 1 (total of all work pushed to leaves)

October 1, MM Intuition Three common cases: Running time dominated by cost at leaves Running time evenly distributed throughout the tree Running time dominated by cost at root Consequently, to solve the recurrence, we need only to characterize the dominant term In each case compare with

October 1, MM Case 1 for some constant f(n) grows polynomially (by factor ) slower than The work at the leaf level dominates Summation of recursion-tree levels Cost of all the leaves Thus, the overall cost

October 1, MM Case 2 and are asymptotically the same The work is distributed equally throughout the tree (level cost)  (number of levels)

October 1, MM Case 3 for some constant Inverse of Case 1 f(n) grows polynomially faster than Also need a regularity condition The work at the root dominates

October 1, Master Theorem Summarized Given a recurrence of the form The master method cannot solve every recurrence of this form; there is a gap between cases 1 and 2, as well as cases 2 and 3

October 1, Strategy Extract a, b, and f(n) from a given recurrence Determine Compare f(n) and asymptotically Determine appropriate MT case, and apply Example merge sort

October 1, Examples Binary-search(A, p, r, s): q  (p+r)/2 if A[q]=s then return q else if A[q]>s then Binary-search(A, p, q-1, s) else Binary-search(A, q+1, r, s) Binary-search(A, p, r, s): q  (p+r)/2 if A[q]=s then return q else if A[q]>s then Binary-search(A, p, q-1, s) else Binary-search(A, q+1, r, s)

October 1, Examples (2)

October 1, Examples (3)

October 1, Next Week Sorting QuickSort HeapSort