# RECURSION (CH 5) A pattern for solving algorithm design problems

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 RECURSION (CH 5) A pattern for solving algorithm design problems Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley,

2 THE RECURSION PATTERN EXAMPLE Recursion: when a method calls itself Classic example the factorial function: n! = (n-1) n Recursive definition: As a Java method: 1 f ( n) = n f ( n 1) if n = 0 else 2

3 CONTENT OF A RECURSIVE METHOD Base case(s) Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). Every possible chain of recursive calls must eventually reach a base case. Recursive calls Calls to the current method. Each recursive call should be defined so that it makes progress towards a base case. Recursion 3

4 VISUALIZING RECURSION Recursion trace A box for each recursive call An arrow from each caller to callee An arrow from each callee to caller showing return value Example call recursivefactorial ( 4 ) call recursivefactorial ( 3 ) call recursivefactorial ( 2 ) call recursivefactorial ( 1 ) call return 4 * 6 = 24 recursivefactorial ( 0 ) return 3 * 2 = 6 return 2 * 1 = 2 return 1 * 1 = 1 return 1 final answer Recursion 4

5 VISUALIZING BINARY SEARCH We consider three cases: If the target equals data[mid], then we have found the target. If target < data[mid], then we recur on the first half of the sequence. If target > data[mid], then we recur on the second half of the sequence. INPUT: Values stored in sorted order within an array (sequence is sorted and indexable,) OUTPUT: Location of the target value. 5

6 BINARY SEARCH Search for an integer in an ordered list 6

7 ANALYZING BINARY SEARCH Runs in O(log n) time. The remaining portion of the list is of size high low + 1 After one comparison, this becomes one of the following: Thus, each recursive call divides the search region in half; hence, there can be at most log n levels 7

8 TYPES OF RECURSION Linear recursion : If a recursive call starts at most one other. Binary recursion: If a recursive call may start two others. Multiple recursion: If a recursive call may start three or more others. Terminology reflects the structure of the recursion trace, not the asymptotic analysis of the running time. 8

9 LINEAR RECURSION Test for base cases Begin by testing for a set of base cases (there should be at least one). Every possible chain of recursive calls must eventually reach a base case, and the handling of each base case should not use recursion. Recur once Perform a single recursive call This step may have a test that decides which of several possible recursive calls to make, but it should ultimately make just one of these calls Define each possible recursive call so that it makes progress towards a base case. 9

10 EXAMPLE OF LINEAR RECURSION Algorithm linearsum(a, n): Input: Array, A, of integers Integer n such that 0 n A Output: Sum of the first n integers in A. Recursion trace of linearsum(data, 5) called on array data = [4, 3, 6, 2, 8] 10

11 REVERSING AN ARRAY Algorithm reversearray(a, i, j): Input: An array A and nonnegative integer indices i and j Output: The reversal of the elements in A starting at index i and ending at Problem: Reverse the n elements of an array, so that the first element becomes the last, the second element becomes second to the last, and so on. if i < j then Swap A[i] and A[ j] reversearray(a, i + 1, j 1) return Terminates after a total of (1 + fffff n ) recursive calls. Because 2 each call involves a constant amount of work, the entire process runs in O(n) time. 11

12 DEFINING ARGUMENTS FOR RECURSION In creating recursive methods, it is important to define the methods in ways that facilitate recursion. This sometimes requires we define additional parameters that are passed to the method. For example, we defined the array reversal method as reversearray(a, i, j), not reversearray(a) 12

13 RECURSIVE ALGORITHMS FOR COMPUTING POWERS Problem: Raise a number x to an arbitrary nonnegative integer n. The power function, power(x,n)=x n, can be defined recursively: power( x, n) = x 1 power( x, n 1) if n = 0 else This leads to an power function that runs in O(n) time (for we make n recursive calls) We can do better than this, however 13

14 RECURSIVE SQUARING OBSERVATION: We consider the expression x k 2, where k = fffff( n 2 ). When n is odd, fffff n, 2 2 xk 2 = x n 1 and therefore x n = x k 2 x. When n is even, fffff( n ) = n and therefore x k 2 = x n 2 2 = x n. 2 2 = n 1 We can derive a more efficient linearly recursive algorithm by using repeated squaring: For example, 2 4 = 2 (4/2)2 = (2 4/2 ) 2 = (2 2 ) 2 = 4 2 = = 2 1+(4/2)2 = 2(2 4/2 ) 2 = 2(2 2 ) 2 = 2(4 2 ) = = 2 (6/ 2)2 = (2 6/2 ) 2 = (2 3 ) 2 = 8 2 = = 2 1+(6/2)2 = 2(2 6/2 ) 2 = 2(2 3 ) 2 = 2(8 2 ) =

15 RECURSIVE SQUARING METHOD Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value x n if n = 0 then return 1 if n is odd then y = Power(x, (n - 1)/ 2) return x y y else y = Power(x, n/ 2) return y y O(logn) recursive calls. Each time we make a recursive call we halve the value of n; hence, we make log n recursive calls. That is, this method runs in O(log n) time. It is important that we use a variable twice here rather than calling the method twice. 15

16 RECURSIVE SQUARING METHOD Example: power(2, 13) 16

17 BINARY RECURSION Binary recursion occurs whenever there are two recursive calls for each non-base case. Example from before: the drawinterval method for drawing ticks on an English ruler. 18

18 EXAMPLE: DRAWING ENGLISH RULER Print the ticks and numbers like an English ruler: 2-inch ruler with major tick length 4; 1-inch ruler with major tick length 5; 3-inch ruler with major tick length 3. Recursion 19

19 USING RECURSION Slide by Matt Stallmann included with permission. drawinterval(length) Input: length of a tick Output: ruler with tick of the given length in the middle and smaller rulers on either side drawinterval(length) if( length > 0 ) then drawinterval ( length 1 ) draw line of the given length drawinterval ( length 1 ) 20

20 RECURSIVE DRAWING METHOD Recursion The drawing method is based on the following recursive definition An interval with a central tick length L >1 consists of: An interval with a central tick length L 1 An single tick of length L An interval with a central tick length L 1 21

21 A RECURSIVE METHOD FOR DRAWING TICKS ON AN ENGLISH RULER Note the two recursive calls 22

22 ANOTHER BINARY RECURSIVE METHOD Problem: Add all the numbers in an integer array A Solution strategy: Recursively compute the sum of the first half, and the sum of the second half, and add those sums together. Algorithm BinarySum(A, i, n): Input: An array A and integers i and n Output: The sum of the n integers in A starting at index i if n = 1 then return A[i] return BinarySum(A, i, n/ 2) + BinarySum(A, i + n/ 2, n/ 2) Example trace: linearsum 0, 2 method. 2, 2 0, 1 BinarySum(data, 0, 8) 0, 8 Space: binarysum uses O(logn) amount of additional space, which 0 is, 4 a big improvement 4, 4 over the O(n) space used by the 1, 1 2, 1 3, 1 4, 1 4, 2 5, 1 6, 1 6, 2 Time : However, the running time of is O(n). 7, 1 23

23 MULTIPLE RECURSION Motivating example: summation puzzles pot + pan = bib dog + cat = pig boy + girl = baby Multiple recursion: makes potentially many recursive calls not just one or two Multiple recursion : a process in which a method may make more than two recursive calls. To solve such a puzzle, we need to assign a unique digit (that is, 0,1,...,9) to each letter in the equation, in order to make the equation true. 24

24 ALGORITHM FOR MULTIPLE RECURSION If the number of possible configurations is not too large, however, we can use a computer to simply enumerate all the possibilities and test each one. 25

25 Recursion trace for an execution of PuzzleSolve(3, S, U), where S is empty and U ={a,b,c}. 26

26 EXAMPLE Slide by Matt Stallmann included with permission. cbb + ba = abc = 997 [a] {b,c} a=7 [] {a,b,c} [b] {a,c} b=7 a,b,c stand for 7,8,9; not necessarily in that order [c] {a,b} c=7 [ab] {c} a=7,b=8 c=9 [ac] {b} a=7,c=8 b=9 [ba] {c} b=7,a=8 c=9 [ca] {b} c=7,a=8 b=9 [bc] {a} b=7,c=8 a=9 [cb] {a} c=7,b=8 a=9 might be able to stop sooner 27

27 PITFALLS OF RECURSION Recursion can easily be misused in various ways. 28

28 ELEMENT UNIQUENESS PROBLEM, REVISITED RROBLEM: Given an array with n elements, are all the elements of that collection are distinct from each other? O(n 2 ) brute force method O(nlogn) sorting based O(2 n ) Recursive unique3 for testing element uniqueness 29

29 ANALYSIS OF RECURSIVE UNIQUE3 unique3 is a terribly inefficient use of recursion!! Let n denote the number of entries under consideration: n = 1 + high low Base case (n = 1): running time of unique3 is O(1) since there are no recursive calls and the nonrecursive part of each call uses O(1) time. General case (n>1): a single call to unique3 for a problem of size n may result in two recursive calls on problems of size n 1, and so on. Thus, in the worst case, the total number of method calls is given by the geometric summation: n 1 = 2 n 1 = O(2 n ) 30

30 COMPUTING FIBONACCI NUMBERS Fibonacci numbers are defined recursively: F 0 = 0 F 1 = 1 F i = F i-1 + F i-2 for i > 1. Recursive algorithm (inefficiently): Algorithm BinaryFib(k): Input: Nonnegative integer k Output: The kth Fibonacci number F k if k = 1 then return k else return BinaryFib(k 1) + BinaryFib(k 2) 31

31 ANALYSIS Let n k be the number of recursive calls by BinaryFib(k) n 0 = 1 n 1 = 1 n 2 = n 1 + n = = 3 n 3 = n 2 + n = = 5 n 4 = n 3 + n = = 9 n 5 = n 4 + n = = 15 n 6 = n 5 + n = = 25 n 7 = n 6 + n = = 41 n 8 = n 7 + n = = 67. Note that n k at least doubles every other time That is, n k > 2 k/2. It is exponential! 32

32 A BETTER FIBONACCI ALGORITHM Use linear recursion instead Algorithm LinearFibonacci(k): Input: A nonnegative integer k Output: Pair of Fibonacci numbers (F k, F k 1 ) if k = 1 then return (k, 0) else (i, j) = LinearFibonacci(k 1) Each invocation 1) makes only one recursive call and 2) decreases the argument n by 1. return (i +j, i) runs in O(n) time. 33

### Recursion. The Recursion Pattern. Recursion 3/16/14

Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Recursion Recursion 1 The Recursion Pattern

### Using Recursion. Linear Recursion ( 4.1.1) Recall the Recursion Pattern ( 2.5) A Simple Example of Linear Recursion

Using Recursion Re the Recursion Pattern ( 2.5) Recursion: when a method s itself Classic example- - the factorial function: n! = 1 2 3 (n-1) n Recursive definition: Using Recursion 1 As a Java method:

### Linked Lists. Outline. COMP9024: Data Structures and Algorithms. Algorithms versus Heuristics. Data Structures and Algorithms. Abstract Data Types

COMP9024: Data Structures and Algorithms Week Three: Linked Lists and Recursion Outline Singly Linked Lists Doubly Linked Lists Recursions Hui Wu Session 1, 2014 http://www.cse.unsw.edu.au/~cs9024 1 2

### CSC 344 Algorithms and Complexity. What is Recursion?

CSC 344 Algorithms and Complexity Lecture #9 Recursive Algorithms What is Recursion? Recursion - when a method calls itself Classic example - the factorial function: n! = 1 2 3 (n-1) n Recursive definition:

### Recursion Chapter 3.5

Recursion Chapter 3.5-1 - Outline Induction Linear recursion Example 1: Factorials Example 2: Powers Example 3: Reversing an array Binary recursion Example 1: The Fibonacci sequence Example 2: The Tower

### Parallel Sorting Algorithms

CSC 391/691: GPU Programming Fall 015 Parallel Sorting Algorithms Copyright 015 Samuel S. Cho Sorting Algorithms Review Bubble Sort: O(n ) Insertion Sort: O(n ) Quick Sort: O(n log n) Heap Sort: O(n log

### Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 2015 Goodrich and Tamassia

### 6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Recursive void Methods Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University A recursive method is a method that includes a call to itself Recursion is based on the general problem

### Algorithm Design and Recursion. Search and Sort Algorithms

Algorithm Design and Recursion Search and Sort Algorithms Objectives To understand the basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms

### Finite Math - J-term Homework. Section Inverse of a Square Matrix

Section.5-77, 78, 79, 80 Finite Math - J-term 017 Lecture Notes - 1/19/017 Homework Section.6-9, 1, 1, 15, 17, 18, 1, 6, 9, 3, 37, 39, 1,, 5, 6, 55 Section 5.1-9, 11, 1, 13, 1, 17, 9, 30 Section.5 - Inverse

### Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 8: Recursion Presentation slides for Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem Java Software Solutions for AP* Computer Science

### DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

### Fast Sorting and Selection. A Lower Bound for Worst Case

Lists and Iterators 0//06 Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 0 Fast Sorting and Selection USGS NEIC. Public domain government

### Classic Data Structures Introduction UNIT I

ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero

### Algorithms and Data Structures

Algorithm Analysis Page 1 - Algorithm Analysis Dr. Fall 2008 Algorithm Analysis Page 2 Outline Textbook Overview Analysis of Algorithm Pseudo-Code and Primitive Operations Growth Rate and Big-Oh Notation

Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Bucket-Sort and Radix-Sort B 0 1 2 3 4 5 6

### Sorting race. https://www.toptal.com/developers/sortingalgorithms

Sorting race https://www.toptal.com/developers/sortingalgorithms CS 2230 CS II: Data structures Comparison sorting Brandon Myers University of Iowa Today s Learning Objectives Execute several comparison

### Recursive Definitions

Recursion Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of

### 11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

RECURSION Chapter 5 Recursive Thinking Section 5.1 1 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that are difficult

Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Bucket-Sort and Radix-Sort 1, c 3, a 3, b

### CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,

### Priority Queue Sorting

Priority Queue Sorting We can use a priority queue to sort a list of comparable elements 1. Insert the elements one by one with a series of insert operations 2. Remove the elements in sorted order with

### Recursion Problems. Warm Ups. Enumeration 1 / 7

1 / 7 Recursion Problems Warm Ups 1. Write a recursive implementation of the factorial function. Recall that n! = 1 2 n, with the special case that 0! = 1. 2. Write a recursive function that, given a number

### 15.4 Longest common subsequence

15.4 Longest common subsequence Biological applications often need to compare the DNA of two (or more) different organisms A strand of DNA consists of a string of molecules called bases, where the possible

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

Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms

### Complexity of Algorithms

Complexity of Algorithms Time complexity is abstracted to the number of steps or basic operations performed in the worst case during a computation. Now consider the following: 1. How much time does it

### UNIT 1 ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS Analysis of Algorithms Structure Page Nos. 1.0 Introduction 7 1.1 Objectives 7 1.2 Mathematical Background 8 1.3 Process of Analysis 12 1.4 Calculation of Storage Complexity

### Lecture 4. Hashing Methods

Lecture 4 Hashing Methods 1 Lecture Content 1. Basics 2. Collision Resolution Methods 2.1 Linear Probing Method 2.2 Quadratic Probing Method 2.3 Double Hashing Method 2.4 Coalesced Chaining Method 2.5

### 15.4 Longest common subsequence

15.4 Longest common subsequence Biological applications often need to compare the DNA of two (or more) different organisms A strand of DNA consists of a string of molecules called bases, where the possible

### 1 The range query problem

CS268: Geometric Algorithms Handout #12 Design and Analysis Original Handout #12 Stanford University Thursday, 19 May 1994 Original Lecture #12: Thursday, May 19, 1994 Topics: Range Searching with Partition

### CH. 8 PRIORITY QUEUES AND HEAPS

CH. 8 PRIORITY QUEUES AND HEAPS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

### Algorithm Analysis. Big Oh

Algorithm Analysis with Big Oh Data Structures and Design with Java and JUnit Chapter 12 Rick Mercer Algorithm Analysis w Objectives Analyze the efficiency of algorithms Analyze a few classic algorithms

### 142

Scope Rules Thus, storage duration does not affect the scope of an identifier. The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned

### Lecture 12 Hash Tables

Lecture 12 Hash Tables 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons Dictionaries, also called associative arrays as well as maps, are data structures that are

### Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Section 5.3 1 Recursion 2 Recursion Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences Previously sequences were defined using a specific

1 19 Sorting 2 19.1 Introduction (Cont.) Sorting data Place data in order Typically ascending or descending Based on one or more sort keys Algorithms Insertion sort Selection sort Merge sort More efficient,

### 1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

1 Recursion Recursion is a powerful tool for solving certain kinds of problems. Recursion breaks a problem into smaller problems that are identical to the original, in such a way that solving the smaller

### Algorithms and Applications

Algorithms and Applications 1 Areas done in textbook: Sorting Algorithms Numerical Algorithms Image Processing Searching and Optimization 2 Chapter 10 Sorting Algorithms - rearranging a list of numbers

### Algorithmic Analysis. Go go Big O(h)!

Algorithmic Analysis Go go Big O(h)! 1 Corresponding Book Sections Pearson: Chapter 6, Sections 1-3 Data Structures: 4.1-4.2.5 2 What is an Algorithm? Informally, any well defined computational procedure

### TREES. Trees - Introduction

TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

### csci 210: Data Structures Stacks and Queues in Solution Searching

csci 210: Data Structures Stacks and Queues in Solution Searching 1 Summary Topics Using Stacks and Queues in searching Applications: In-class problem: missionary and cannibals In-class problem: finding

### Data Structures Lecture 3 Order Notation and Recursion

Data Structures Lecture 3 Order Notation and Recursion 1 Overview The median grade.cpp program from Lecture 2 and background on constructing and using vectors. Algorithm analysis; order notation Recursion

### AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 0 AVL Trees v 6 3 8 z 0 Goodrich, Tamassia, Goldwasser

### CS100: DISCRETE STRUCTURES

CS: DISCRETE STRUCTURES Computer Science Department Lecture : Set and Sets Operations (Ch2) Lecture Contents 2 Sets Definition. Some Important Sets. Notation used to describe membership in sets. How to

### Recursion. Example R1

Recursion Certain computer problems are solved by repeating the execution of one or more statements a certain number of times. So far, we have implemented the repetition of one or more statements by using

### Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

### Range Queries. Kuba Karpierz, Bruno Vacherot. March 4, 2016

Range Queries Kuba Karpierz, Bruno Vacherot March 4, 2016 Range query problems are of the following form: Given an array of length n, I will ask q queries. Queries may ask some form of question about a

### HASH TABLES. Goal is to store elements k,v at index i = h k

CH 9.2 : HASH TABLES 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND

### SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

Bucket-Sort and Radix-Sort SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT 1, c 3, a 3, b 7, d 7, g 7, e B 0 1 2 3 4 5 6 7 8 9 Presentation for use with the textbook Data Structures and Algorithms in

### Divide-and-Conquer. Dr. Yingwu Zhu

Divide-and-Conquer Dr. Yingwu Zhu Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently

### HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES 2 5 6 9 7 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley, 2014

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

Assignment 1 Stefano Guerra October 11, 2016 1 Problem 1 Describe a recursive algorithm to reconstruct an arbitrary binary tree, given its preorder and inorder node sequences as input. First, recall that

### Lecture Notes on Hash Tables

Lecture Notes on Hash Tables 15-122: Principles of Imperative Computation Frank Pfenning Lecture 13 February 24, 2011 1 Introduction In this lecture we introduce so-called associative arrays, that is,

### Analysis of algorithms

Analysis of algorithms Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing a

### CH 21 CONSECUTIVE INTEGERS

201 CH 21 CONSECUTIVE INTEGERS Introduction An integer is either a positive whole number, or zero, or a negative whole number; in other words it s the collection of numbers:... 4, 3, 2, 1, 0, 1, 2, 3,

### Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

RMIT School of Computer Science and Information Technology Programming 2 Topic 8: Linked Lists, Basic Searching and Sorting Lecture Slides COPYRIGHT 2008 RMIT University. Original content by: Peter Tilmanis,

### Graphs and Puzzles. Eulerian and Hamiltonian Tours.

Graphs and Puzzles. Eulerian and Hamiltonian Tours. CSE21 Winter 2017, Day 11 (B00), Day 7 (A00) February 3, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Exam Announcements Seating Chart on Website Good

### We can use a max-heap to sort data.

Sorting 7B N log N Sorts 1 Heap Sort We can use a max-heap to sort data. Convert an array to a max-heap. Remove the root from the heap and store it in its proper position in the same array. Repeat until

### Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Recursion EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Recursion: Principle Recursion is useful in expressing solutions to problems that can be recursively defined: Base Cases:

### CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x);

### Outline: Search and Recursion (Ch13)

Search and Recursion Michael Mandel Lecture 12 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture12final.ipynb

### Object-Oriented Programming

Object-Oriented Programming 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Object-Oriented

### Lec-5-HW-1, TM basics

Lec-5-HW-1, TM basics (Problem 0)-------------------- Design a Turing Machine (TM), T_sub, that does unary decrement by one. Assume a legal, initial tape consists of a contiguous set of cells, each containing

### Merge Sort

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conuer Divide-and conuer is a general algorithm design paradigm: n Divide: divide the input data S in two disjoint subsets S 1 and

### CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE 250 Final Exam Fall 2013 Time: 3 hours. Dec 11, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra

### Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

### Discrete Structures. Fall Homework3

Discrete Structures Fall 2015 Homework3 Chapter 5 1. Section 5.1 page 329 Problems: 3,5,7,9,11,15 3. Let P(n) be the statement that 1 2 + 2 2 + +n 2 = n(n + 1)(2n + 1)/6 for the positive integer n. a)

### Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Page 1 of 6 Template no.: A Course Name: Computer Programming1 Course ID: Exam Duration: 2 Hours Exam Time: Exam Date: Final Exam 1'st Semester Student no. in the list: Exam pages: Student's Name: Student

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

Table ADT and Sorting Algorithm topics continuing (or reviewing?) CS 24 curriculum A table ADT (a.k.a. Dictionary, Map) Table public interface: // Put information in the table, and a unique key to identify

### (D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

Part : Multiple Choice Enter your answers on the Scantron sheet. We will not mark answers that have been entered on this sheet. Each multiple choice question is worth. marks. Note. when you are asked to

### CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CS 2230 CS II: Data structures Meeting 21: trees Brandon Myers University of Iowa Today s learning objectives Define tree (a data structure that can store data hierarchically) and describe trees in terms

### Priority Queues (Heaps)

Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the

### Subject: Computer Science

Subject: Computer Science Topic: Data Types, Variables & Operators 1 Write a program to print HELLO WORLD on screen. 2 Write a program to display output using a single cout statement. 3 Write a program

### (Refer Slide Time 04:53)

Programming and Data Structure Dr.P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 26 Algorithm Design -1 Having made a preliminary study

### Solving Linear Recurrence Relations (8.2)

EECS 203 Spring 2016 Lecture 18 Page 1 of 10 Review: Recurrence relations (Chapter 8) Last time we started in on recurrence relations. In computer science, one of the primary reasons we look at solving

### Part II. C. M. Bishop PATTERN RECOGNITION AND MACHINE LEARNING CHAPTER 8: GRAPHICAL MODELS

Part II C. M. Bishop PATTERN RECOGNITION AND MACHINE LEARNING CHAPTER 8: GRAPHICAL MODELS Converting Directed to Undirected Graphs (1) Converting Directed to Undirected Graphs (2) Add extra links between

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

University of Toronto Department of Electrical and Computer Engineering Midterm Examination ECE 345 Algorithms and Data Structures Fall 2010 Print your name and ID number neatly in the space provided below;

### Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

### Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Sorting The sorting problem is defined as follows: Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

### Dynamic Programming Algorithms

CSC 364S Notes University of Toronto, Fall 2003 Dynamic Programming Algorithms The setting is as follows. We wish to find a solution to a given problem which optimizes some quantity Q of interest; for

### Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Recursion: Factorial (1) Recall the formal definition of calculating the n factorial: 1 if n = 0 n! = n (n 1) (n 2) 3 2

### Recitation 9. Prelim Review

Recitation 9 Prelim Review 1 Heaps 2 Review: Binary heap min heap 1 2 99 4 3 PriorityQueue Maintains max or min of collection (no duplicates) Follows heap order invariant at every level Always balanced!

### Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics

### Time and space behaviour. Chapter 20

Time and space behaviour Chapter 20 Algorithmic complexity How many steps does it take to execute an algorithm given an input of size n? Complexity of functions Consider: f n = 2 n 2 + 4 n + 13 This function

### Introduction to Algorithms

Introduction to Algorithms 6.046J/18.401 Lecture 17 Prof. Piotr Indyk Computational Geometry ctd. Segment intersection problem: Given: a set of n distinct segments s 1 s n, represented by coordinates of

### Q1 Q2 Q3 Q4 Q5 Q6 Total

Name: SSN: Computer Science Foundation Exam May 5, 006 Computer Science Section 1A Q1 Q Q3 Q4 Q5 Q6 Total KNW KNW KNW ANL,DSN KNW DSN You have to do all the 6 problems in this section of the exam. Partial

### Computer Science Foundation Exam

Computer Science Foundation Exam August 6, 017 Section I A DATA STRUCTURES SOLUTIONS NO books, notes, or calculators may be used, and you must work entirely on your own. Question # Max Pts Category Passing

### Lecture 2: Algorithm Analysis

ECE4050/CSC5050 Algorithms and Data Structures Lecture 2: Algorithm Analysis 1 Mathematical Background Logarithms Summations Recursion Induction Proofs Recurrence Relations 2 2 Logarithm Definition: 3

### Lines and Angles. Chapter INTRODUCTION

LINES AND ANGLES 9 3 Lines and Angles Chapter 5 5.1 INTRODUCTION You already know how to identify different lines, line segments and angles in a given shape. Can you identify the different line segments

### The Running Time of Programs

The Running Time of Programs The 90 10 Rule Many programs exhibit the property that most of their running time is spent in a small fraction of the source code. There is an informal rule that states 90%

### Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Recursion Comp Sci 1575 Data Structures Outline 1 2 3 4 Definitions To understand, you must understand. Familiar of recursive definitions Natural numbers are either: n+1, where n is a natural number 1

### 8. Quicksort for Contiguous Lists. 10. Review: Comparison of Methods

Chapter 8 SORTING 1. Introduction and Notation 2. Insertion Sort 3. Selection Sort 4. Shell Sort 5. Lower Bounds 6. Divide-and-Conquer Sorting 7. Mergesort for Linked Lists 8. Quicksort for Contiguous

### Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Dynamic Programming Deliverables Dynamic Programming basics Binomial Coefficients Weighted Interval Scheduling Matrix Multiplication /1 Knapsack Longest Common Subsequence 6/12/212 6:56 PM copyright @

### Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Orders of Growth of Processes Today s topics Resources used by a program to solve a problem of size n Time Space Define order of growth Visualizing resources utilization using our model of evaluation Relating

### CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms Lecture 5: Hash Tables Instructor: Olga Veksler 0 1 2 3 025-612-0001 981-101-0002 4 451-229-0004 2004 Goodrich, Tamassia Outline Hash Tables Motivation Hash functions

### CSE 230 Intermediate Programming in C and C++ Recursion

CSE 230 Intermediate Programming in C and C++ Recursion Fall 2017 Stony Brook University Instructor: Shebuti Rayana What is recursion? Sometimes, the best way to solve a problem is by solving a smaller

### Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Recursion Chapter 8 CS 3358 Summer II 2013 Jill Seaman What is recursion?! Generally, when something contains a reference to itself! Math: defining a function in terms of itself! Computer science: when