Recursion: The Beginning

Similar documents
Recursion: The Beginning

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Binary Search and Worst-Case Analysis

k-selection Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong

Binary Search and Worst-Case Analysis

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

RAM with Randomization and Quick Sort

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Priority Queues and Binary Heaps

Linked Lists, Stacks, and Queues

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Binary Search to find item in sorted array

CSE Winter 2015 Quiz 2 Solutions

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

The RAM Computation Model

SORTING AND SEARCHING

ECE 122. Engineering Problem Solving Using Java

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

CSC 273 Data Structures

CS2351 Data Structures. Lecture 1: Getting Started

Dynamic Arrays and Amortized Analysis

lecture17: AVL Trees

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Test 1 Review Questions with Solutions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #19. Loops: Continue Statement Example

(Refer Slide Time: 01.26)

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

(Refer Slide Time: 00:50)

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Sorting and Searching

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Lecture Notes: External Interval Tree. 1 External Interval Tree The Static Version

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

We can use a max-heap to sort data.

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Reading for this lecture (Goodrich and Tamassia):

THE UNIVERSITY OF WESTERN AUSTRALIA

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Sorting Pearson Education, Inc. All rights reserved.

Active Learning: Sorting

Midterm solutions. n f 3 (n) = 3

W4231: Analysis of Algorithms

Unit-2 Divide and conquer 2016

Suffix Trees and Arrays

COMP250: Loop invariants

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

CSC Design and Analysis of Algorithms

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Question And Answer.

Selection (deterministic & randomized): finding the median in linear time

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Algorithms and Data Structures

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Binary Search APRIL 25 TH, 2014

Intro to Algorithms. Professor Kevin Gold

Solutions to the Second Midterm Exam

Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Programming II (CS300)

Heaps. Heaps Priority Queue Revisit HeapSort

Lecture 9: Sorting Algorithms

UNIT 5B Binary Search

Programming II (CS300)

Greedy Algorithms CHAPTER 16

Searching. Algorithms and Data Structures. Marcin Sydow. Divide and Conquer. Searching. Positional Statistics. Summary

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Inference rule for Induction

Q1 Q2 Q3 Q4 Q5 Q6 Total

Randomized Algorithms, Hash Functions

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

COMP 250 Fall recurrences 2 Oct. 13, 2017

Solutions to Problem Set 1

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

CSE 21 Spring 2016 Homework 4 Answer Key

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

C/C++ Programming Lecture 18 Name:

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Clustering: Centroid-Based Partitioning

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Searching Algorithms/Time Analysis

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Searching, Sorting. Arizona State University 1

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

Space Efficient Linear Time Construction of

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Algorithms. Lecture Notes 5

L14 Quicksort and Performance Optimization

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Lecture Notes: Range Searching with Linear Space

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Transcription:

Department of Computer Science and Engineering Chinese University of Hong Kong

This lecture will introduce a useful technique called recursion. If used judiciously, this technique often leads to elegant algorithms that are easy to understand and analyze. To make our point, we will first re-discover the binary search algorithm by way of recursion. Then, we will use the technique to obtain an algorithm for solving another problem called sorting. This will pave the way for more advanced applications of recursion in later lectures.

Array An array of length n is a sequence of n elements such that they are stored consecutively in memory (i.e., the first element is immediately followed by the second, and then by the third, and so on); every element occupies the same number of memory cells.... 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88 array of length 16 array of length 12

With the concept of array, we now redefine the dictionary search problem: The Dictionary Search Problem (Redefined) Problem Input: A set S of n integers has been arranged in ascending order in an array of length n. You are given the value of n and another integer v inside the CPU. Goal: Design an algorithm to determine whether v exists in S.

Recursion in General The idea of recursion can be summarized as: 1 [Reduce] Show that if we can solve the same problem but with a size smaller than n, then we can solve the original problem (of size n). 2 [Base] When the problem size has become sufficiently small, solve it trivially.

Binary Search (Re-discovered) Reduce (Problem Size n > 1) 1. Compare v to the middle element e of the array. If v = e, return yes and done. 2. Otherwise: 2.1 If v < e, solve the problem in the part of the array before e; 2.2 If v > e, solve the problem in the part of the array after e.

Binary Search (Re-discovered) Base Case (n = 0 or 1) Trivial: If n = 0, then simply return no. If n = 1, compare v to the (only) element in the array in O(1) time (i.e., constant time without caring about what the constant is). Our algorithm description is now complete!

Analysis of Binary Search Next we will see how recursion allows us to analyze the running time in a neat way. Define f (n) to be the worst-case running time of binary search. From the base case, we know: From the inductive case, we know: f (1) = O(1) f (n) O(1) + f (n/2) where the O(1) term is the cost of Step 1 (of Slide 6), and the f (n/2) term is the cost of Step 2.1 or 2.2, noticing that each of the two steps runs binary search on a problem of size at most n/2.

Analysis of Binary Search So it remains to solve the recurrence (c 1, c 2 are constants whose values we do not care): f (1) = c 1 f (n) c 2 + f (n/2) An easy way of doing so is the expansion method, which simply expands f (n) all the way down: f (n) c 2 + f (n/2) c 2 + c 2 + f (n/2 2 ) c 2 + c 2 + c 2 + f (n/2 3 ) c 2 +... + c }{{} 2 +f (1) log 2 n of them = c 2 log 2 n + c 1 = O(log n).

Analysis of Binary Search Technically speaking, our previous analysis holds only when n is a power of 2 (otherwise, some n/2 i along the way is a non-integer, making f (n/2 i ) undefined). We can account for this easily using a rounding approach. Suppose that n is not a power of 2. Let n be the least power of 2 that is larger than n. It thus holds that n < 2n (otherwise, n is not the least). We then have: f (n) f (n ) c 2 log 2 n + c 1 (proved earlier) < c 2 log 2 (2n) + c 1 = c 2 (1 + log 2 n) + c 1 = c 2 log 2 n + c 1 + c 2 = O(log n).

Next, we switch our attention to the sorting problem, which is a classical problem in computer science, and is worth several lectures discussion.

The Sorting Problem Problem Input: A set S of n integers is given in an array of length n. The value of n is inside the CPU (i.e., in a register). Goal: Design an algorithm to store S in an array where the elements have been arranged in ascending order.

Example Input: 16... 38 28 88 17 26 41 72 83 69 47 12 68 5 52 35 9 Output: 16... 5 9 12 17 26 28 35 38 41 47 52 68 69 72 83 88

We will use recursion to design our first sorting algorithm, called selection sort.

Selection Sort Reduce(Problem Size n > 1) 1. Scan all the elements in the array to identify the largest one e max. 2. Swap the positions of e max and the last (i.e., n-th) element of the array (after which e max is at the end of the array). 3. Sort the first n 1 elements.

Selection Sort Base (n = 1) Trivial. Nothing to sort. Return directly.

Example Input: 16... 38 28 88 17 26 41 72 83 69 47 12 68 5 52 35 9 After the induction step at n = 16: 16... 38 28 9 17 26 41 72 83 69 47 12 68 5 52 35 88 sort these 15 elements recursively

Analysis of Selection Sort Let f (n) be the worst-case running time of selection sort when the problem size is n. From the base case, we know: From the inductive case, we have: f (n) = O(1) f (n) O(n) + f (n 1) where the O(n) term captures the cost of Steps 1 and 2, and f (n 1) is the cost of Step 3.

Analysis of Selection Sort So it remains to solve the recurrence (c 1, c 2 are constants whose values we do not care): f (1) = c 1 f (n) c 2 n + f (n 1) Using the expansion method, we get: f (n) c 2 n + f (n 1) c 2 n + c 2 (n 1) + f (n 2) c 2 n + c 2 (n 1) + c 2 (n 2) + f (n 3) c 2 n + c 2 (n 1) +... + c 2 2 + f (1) c 2 n(n + 1)/2 + c 1 = O(n 2 ). We now conclude that the selection sort algorithm solves the sorting problem in O(n 2 ) worst-case time.