Cinda Heeren / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Advertisements

Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Algorithms.
Insertion sort Loop invariants Dynamic memory
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Fundamental Data Structures and Algorithms
ADT description Implementations
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Top 50 Data Structures Interview Questions
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion notes Chapter 8.
Recitation 13 Searching and Sorting.
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
COMP 53 – Week Seven Big O Sorting.
Cinda Heeren / Geoffrey Tien
More Computational Theory
Review: recursion Tree traversals
Cinda Heeren / Geoffrey Tien
Chapter 4 Divide-and-Conquer
Cinda Heeren / Geoffrey Tien
COSC160: Data Structures Linked Lists
Search by Hashing.
Chapter 16 Tree Implementations
Chapter 13: Searching and Sorting
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
Hash functions Open addressing
Quadratic probing Double hashing Removal and open addressing Chaining
CS 3343: Analysis of Algorithms
ADT Heap data structure
Divide And Conquer Distinguish between small and large instances.
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Quicksort analysis Bubble sort
CS 3343: Analysis of Algorithms
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Hassan Khosravi / Geoffrey Tien
MSIS 655 Advanced Business Applications Programming
Algorithms and Data Structures Lecture III
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 373 Data Structures and Algorithms
Divide and Conquer (Merge Sort)
2019/4/10 chapter25.
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
Algorithms: Design and Analysis
DATA STRUCTURE.
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Searching/Sorting/Searching
Divide And Conquer Distinguish between small and large instances.
Tree traversals BST properties Search Insertion
The Selection Problem.
CSE 332: Parallel Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-conquer approach
Analysis of Algorithms
CMPT 225 Lecture 10 – Merge Sort.
DIVIDE AND CONQUER.
Presentation transcript:

Cinda Heeren / Geoffrey Tien Recurrence relations March 02, 2018 Cinda Heeren / Geoffrey Tien

Analyzing recursive functions In a previous adventure in CPSC 221: Geoff's hand-wavy intuition for running time of recursive functions e.g. Geoff says: Merge sort running time = number of recursion levels times work at each level Not a solid proof, Geoff gets: Recall: recursive functions are defined in terms of themselves The running time of some particular recursive call can also be defined in terms of the running time of its own recursive call(s) e.g 𝑇 𝑛 =something+𝑇 a smaller 𝑛 + … Actually, recursion tree analysis is a valid technique, but let's learn about a more precise method March 02, 2018 Cinda Heeren / Geoffrey Tien

Recurrence relations Analysis 𝑇 𝑛 ∈𝑂 𝑛 Example: Recursive max in an array double arrMax(double arr[], int size, int start) { if (start == sz – 1) return arr[start]; else return max( arr[start], arrMax(arr, size, start + 1) ); } 𝑇 1 ≤𝑏 𝑇 𝑛 ≤𝑐+𝑇 𝑛−1 Analysis 𝑇 𝑛 ≤𝑐+𝑐+𝑇 𝑛−2 (by substitution) 𝑇 𝑛 ≤𝑐+𝑐+𝑐+𝑇 𝑛−3 (by substitution, again) 𝑇 𝑛 ≤𝑘∙𝑐+𝑇 𝑛−𝑘 (extrapolating, 0<𝑘≤𝑛) 𝑇 𝑛 ≤ 𝑛−1 ∙𝑐+𝑇 1 = 𝑛−1 ∙𝑐+𝑏 for 𝑘=𝑛−1 𝑇 𝑛 ∈𝑂 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Merge sort analysis Now with more math! Merge sort algorithm Split list in half, sort first half, sort second half, merge together 𝑇 1 ≤𝑏 𝑇 𝑛 ≤2∙𝑇 𝑛/2 +𝑐∙𝑛 Analysis ≤2 2∙𝑇 𝑛/4 +𝑐 𝑛/2 +𝑐𝑛 =4∙𝑇 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 ≤4 2∙𝑇 𝑛/8 +𝑐 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 =8∙𝑇 𝑛/8 +𝑐∙𝑛+𝑐∙𝑛+𝑐∙𝑛 ≤ 2 𝑘 ∙𝑇 𝑛/ 2 𝑘 +𝑘∙𝑐∙𝑛 extrapolating, 1<𝑘≤𝑛 ≤𝑛∙𝑇 1 +𝑐∙𝑛 log 𝑛 for 2 𝑘 =𝑛, or 𝑘= log 2 𝑛 𝑇 𝑛 ∈𝑂 𝑛 log 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien A problem with trees... int fun(Node* curr) { if (curr != NULL) { int ret1 = fun(curr->left); int ret2 = fun(curr->right); return 1 + ret1 + ret2; } else return 0; Assume this function is only used on perfect binary trees! This function performs: 1 recursive call to the left subtree 1 recursive call to the right subtree 1 addition (and return) 𝑇 𝑛 =2∙𝑇 𝑛 2 +𝑐 𝑇 1 =𝑏 March 02, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien A fun problem 𝑇 𝑛 =2∙𝑇 𝑛 2 +𝑐 𝑇 𝑛 =2∙ 2∙𝑇 𝑛 4 +𝑐 +𝑐 𝑇 𝑛 =4∙𝑇 𝑛 4 +2𝑐+𝑐 𝑇 𝑛 =4∙ 2∙𝑇 𝑛 8 +𝑐 +2𝑐+𝑐 𝑇 𝑛 =8∙𝑇 𝑛 8 +4𝑐+2𝑐+𝑐 𝑇 𝑛 = 2 𝑘 ∙𝑇 𝑛 2 𝑘 +𝑐 𝑖=0 𝑘−1 2 𝑖 𝑇 𝑛 = 2 log 2 𝑛 ∙𝑇 1 +𝑐∙ 𝑖=0 log 2 𝑛 −1 2 𝑖 𝑇 𝑛 =𝑛∙𝑏+𝑐∙ 2 log 2 𝑛 −1 =𝑛∙𝑏+𝑐∙ 𝑛−1 ∈𝑂 𝑛 𝑇 𝑛 2 =2∙𝑇 𝑛 4 +𝑐 𝑇 𝑛 4 =2∙𝑇 𝑛 8 +𝑐 𝑛 2 𝑘 =1 𝑘= log 2 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien

In the numeric domain 𝑇 𝑛 =2∙𝑇 𝑛−1 +1, 𝑇 0 =0 Solving for exact closed forms 𝑇 𝑛 =2∙𝑇 𝑛−1 +1, 𝑇 0 =0 March 02, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise We know that binary search on an ordered array has a running time 𝑂 log 2 𝑛 . Do a recurrence analysis to show this Start by writing the base case and general recurrence relation Suppose we implemented Merge sort, to split the array into 3 (roughly) equally-sized subarrays, and the Merge function repeatedly copies in the smallest of the three values in each subarray index Perform a recurrence analysis to see how the (asymptotic) running time will be affected. March 02, 2018 Cinda Heeren / Geoffrey Tien

Readings for this lesson Epp Chapter 5.6 – 5.7 (Recurrence relations) Next class: Carrano & Henry, Chapter 18.4 (Hash tables) March 02, 2018 Cinda Heeren / Geoffrey Tien