Section IV.1: Recursive Algorithms and Recursion Trees



Similar documents
What Is Recursion? Recursion. Binary search example postponed to end of lecture

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Full and Complete Binary Trees

Math 55: Discrete Mathematics

Analysis of Binary Search algorithm and Selection Sort algorithm

Mathematical Induction. Lecture 10-11

3. Mathematical Induction

Fibonacci Numbers and Greatest Common Divisors. The Finonacci numbers are the numbers in the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

Patterns in Pascal s Triangle

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Sample Induction Proofs

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Figure 1: Graphical example of a mergesort 1.

Ordered Lists and Binary Trees

Dynamic Programming Problem Set Partial Solution CMPSC 465

Math 55: Discrete Mathematics

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Reading 13 : Finite State Automata and Regular Expressions

Data Structures Fibonacci Heaps, Amortized Analysis

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Positional Numbering System

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

6.2 Permutations continued

Data Structures and Algorithms

Pseudo code Tutorial and Exercises Teacher s Version

Sequential Data Structures

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

mod 10 = mod 10 = 49 mod 10 = 9.

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

8 Primes and Modular Arithmetic

Algorithms and Data Structures

8 Divisibility and prime numbers

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Solutions for Practice problems on proofs

Regular Languages and Finite Automata

Regression Verification: Status Report

ML for the Working Programmer

Algorithms are the threads that tie together most of the subfields of computer science.

Symbol Tables. Introduction

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MACM 101 Discrete Mathematics I

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Basic Proof Techniques

Analysis of a Search Algorithm

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Lecture 1: Course overview, circuits, and formulas

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures and Algorithms Written Examination

Data Structures. Algorithm Performance and Big O Analysis

Loop Invariants and Binary Search

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12

A Static Analyzer for Large Safety-Critical Software. Considered Programs and Semantics. Automatic Program Verification by Abstract Interpretation

Homework until Test #2

Lecture Notes on Linear Search

Binary Search Trees CMPSC 122

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Abstraction and Abstract Data Types

PES Institute of Technology-BSC QUESTION BANK

Lecture 13 - Basic Number Theory.

C H A P T E R Regular Expressions regular expression

COMPUTER SCIENCE. Paper 1 (THEORY)

Sample Questions Csci 1112 A. Bellaachia

Let s put together a Manual Processor

10CS35: Data Structures Using C

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

GRID SEARCHING Novel way of Searching 2D Array

This asserts two sets are equal iff they have the same elements, that is, a set is determined by its elements.

GCDs and Relatively Prime Numbers! CSCI 2824, Fall 2014!

1.2 Solving a System of Linear Equations

CBA Fractions Student Sheet 1

Session 6 Number Theory

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

THE BINARY NUMBER SYSTEM

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

The Assignment Problem and the Hungarian Method

n 2 + 4n + 3. The answer in decimal form (for the Blitz): 0, 75. Solution. (n + 1)(n + 3) = n lim m 2 1

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

k, then n = p2α 1 1 pα k

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

CSI 333 Lecture 1 Number Systems

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

11 Multivariate Polynomials

Binary search algorithm

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Transcription:

Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller input and (2) having a part (for the smallest instances) where the solution is computed directly without the algorithm making any calls to itself. Thus, a recursive algorithm is made of two parts a base part and a recursive part. The base part is where the solution is computed directly for smallest inputs. The recursive part is where the solution is computed by making one or more calls to the algorithm itself using smaller inputs That is, a recursive algorithm calls itself using a smaller version of the original input. Recursion is similar to mathematical induction. In mathematical induction, a statement can be proved to be true for a value by using the assumption that it is true for smaller values. In mathematical induction, we also must have a basis that can be proven directly. Definition IV.1.2: A recursion tree is a tree that is generated by tracing the execution of a recursive algorithm. A recursion tree shows the relationship between calls to the algorithm. Each item in the tree represents a call to the algorithm. Developing a Recursive Algorithm A recursive algorithm can be developed in two main steps. Step 1 is developing the base part and Step 2 is developing the recursive part. Step 1: Developing the base part. When developing the base part, define the solution for the smallest inputs. Write a statement giving the solution for the smallest input. This statement should be in the following form: if the input is the smallest, then here is the solution. The base part may consist of one or more statements. In many cases, defining the solution for the smallest input is adequate, but at times it may be appropriate to define the solution for the smallest two inputs, or smallest three, etc. Step 2: Developing the recursive part. To develop the recursive part, determine a way to define the solution for values greater than the smallest by using the solution for smaller inputs. The statement for this part should include calling the algorithm with smaller inputs. Example IV.1.1: This example finds the sum of N integers stored in an array. Solution (a) does not use recursion. Solution (b) uses a recursive algorithm. Solution (c) shows the recursion tree for tracing Solution (b). 1

Solution: (a) - Iterative Solution Algorithm Array Sum Input: nonnegative integer N, and array A[1], A[2],, A[N] Output: sum of the N integers in array A. j :=1 sum := 0 while j<n sum := sum + A[j] j:= j+1 end while end Algorithm Array Sum Solution:(b) Recursive Solution using the steps in developing a recursive algorithm Step 1: Developing the base part The smallest input is where N=0. Thus, the output is zero, since the sum of an empty array is zero. In other words, if N is zero, then the sum is zero. Step 2: Developing the recursive part For N greater than zero, the sum of N integers is the N th integer plus the sum of the previous N-1 integers. Notice the sum with input N is based on finding the sum with input N-1. Algorithm Array Sum Input: non-negative integer N, and array A[1], A[2],, A[N] Output: sum of the N integers in array A. if N=0 then sum := 0 [using Step 1] else sum := A[N] + Array Sum of integers A[1], A[2],,A[N-1] [using Step 2] end Algorithm Array Sum 2

Solution:(c) The following shows of the recursion tree using algorithm Array Sum with the following input values: N=5, and A = [2, 4, 6, 3, 7], that is, A[1]=2, A[2]=4, A[3]=6, A[4]=3, A[5]=7. Array Sum with inputs 5 and [2, 4, 6, 3, 7] is 7 + Array Sum with inputs 4 and [2, 4, 6, 3] Array Sum with inputs 4 and [2, 4, 6, 3] is 3 + Array Sum with inputs 3 and [2, 4, 6] Array Sum with inputs 3 and [2, 4, 6] is 6 + Array Sum with inputs 2 and [2, 4] 4 + 2= 6, return 6 Array Sum with inputs 2 and [2, 4] is 4 + Array Sum with inputs 1 and [2] 2 + 0 = 2, return 2 Array Sum with inputs 1 and [2] is 2 + Array Sum with inputs 0 and [] 7 + 15 = 22, return 22 [solution] 3 + 12 = 15, return 15 6 + 6=12, return 12 Array Sum with inputs 0 and [] is 0 Return 0 The arrows going down show the order that calls are made to Array Sum. To add an array of five numbers, six calls are made to Array Sum. The last call is the one that satisfies the base part of the recursive algorithm. Once the base condition is satisfied (that is, N is zero), the algorithm starts returning values to be added. The arrows going up show the returns from calls to the algorithm. Zero is the first value that is returned, and it is added to 2. Then the sum 2 is returned and added to 4, the sum 6 is returned and added to 6, the sum 12 is returned and added to 3, the sum 15 is returned and added to 7, and, finally, the sum 22 is returned. Example IV.1.2: Compute the N th number in the Fibonacci series of numbers. (See Epp, page 431 for an explanation of Fibonacci numbers.) Solution (a) shows the recursive algorithm for this problem. Solution (b) show the recursion tree tracing the execution of Solution (a). Solution: (a) Recursive solution using the steps in developing a recursive algorithm Step 1: Developing the base part. The solutions for the smallest values are as follows. If N is zero, then the N th fibonacci number is one. If N is one, then the N th fibonacci number is one. 3

Step 2: Developing the recursive part. For N greater than one, the N th fibonacci number is the (N-1) st Fibonacci number plus the (N-2) nd Fibonacci number. Algorithm Fibonacci Input: non-negative integer N specifying the Fibonacci number that is to be computed. Output: f, the N th Fibonacci number. if N=0 or N=1 then f := 1 else f := Fibonacci with input N-1 + Fibonacci with input N-2 end Algorithm Fibonacci Solution: (b) Recursion tree using algorithm Fibonacci with N = 4. Fibonacci (4) 3 + 2 = 5 Fibonacci (3) Fibonacci (2) 2 + 1 = 3 1 + 1 = 2 Fibonacci (2) Fibonacci(1) Fibonacci(1) Fibonacci(0) 1+ 1 = 2 Fibonacci (1) Fibonacci (0) 1 1 1 1 1 Each unshaded box shows a call to the algorithm Fibonacci with the input value of N in parentheses. Each shaded box shows the value that is returned from the call. Calls to algorithm Fibonacci are made until a call is made with input value one or zero. When a call is made with input value one or zero, a one is returned. When a call is made with N > 1, two calls are made to algorithm Fibonacci, and the value that is returned is the sum of the values from the two calls. The final number that is returned is 5. Thus the 4 th number in the Fibonacci series is 5. 4

Exercises: (1) Develop a recursive algorithm to search an array of unordered integers for a specified value. The algorithm should return an array index value specifying the location of the value, if found, or return zero, if not found. The input values are N (the number of items in the array) and the array A[1], A[2],, A[N]. Show the recursion trees using N=5 and A= [12, 4, 6, 13, 14] when searching for 6, and when searching for 5. (2) Show the recursion tree for algorithm Fibonacci when N = 6. What is the 6 th number in the Fibonacci series? (3) Develop a recursive algorithm for the binary search given in Section II.6. Show the recursion trees using array A= [10, 20, 30, 40, 50, 60, 70] when searching for 60, and when searching for 80. 5