3.5 RECURSIVE ALGORITHMS



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

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

10CS35: Data Structures Using C

Section IV.1: Recursive Algorithms and Recursion Trees

Algorithms and Data Structures

ML for the Working Programmer

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

CS 103X: Discrete Structures Homework Assignment 3 Solutions

DATA STRUCTURES USING C

Lecture Notes on Linear Search

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

Pseudo code Tutorial and Exercises Teacher s Version

Sample Questions Csci 1112 A. Bellaachia

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

I PUC - Computer Science. Practical s Syllabus. Contents

PES Institute of Technology-BSC QUESTION BANK

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

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

Lecture 3: Finding integer solutions to systems of linear equations

Module 2 Stacks and Queues: Abstract Data Types

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

AP Computer Science AB Syllabus 1

The Mixed Binary Euclid Algorithm

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Mathematical Induction. Lecture 10-11

Curriculum Map. Discipline: Computer Science Course: C++

How To Understand The Theory Of Computer Science

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

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

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

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

Algorithm Design and Recursion

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

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

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

Computations in Number Theory Using Python: A Brief Introduction

Data Structures and Algorithms

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Chapter 15 Functional Programming Languages

Analysis of Binary Search algorithm and Selection Sort algorithm

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

RSA Question 2. Bob thinks that p and q are primes but p isn t. Then, Bob thinks Φ Bob :=(p-1)(q-1) = φ(n). Is this true?

U.C. Berkeley CS276: Cryptography Handout 0.1 Luca Trevisan January, Notes on Algebra

8 Divisibility and prime numbers

Unordered Linked Lists

Data Structures. Algorithm Performance and Big O Analysis

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Domains and Competencies

Objected oriented Programming: C++ Unit 1 1. Introduction 1.1. Introduction to Object Oriented Programming C++ fundamentals.

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

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

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

The finite field with 2 elements The simplest finite field is

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structure [Question Bank]

Specimen 2015 am/pm Time allowed: 1hr 30mins

Lecture 13 - Basic Number Theory.

5.4 Closest Pair of Points

Computer Science 281 Binary and Hexadecimal Review

Moving from CS 61A Scheme to CS 61B Java

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

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

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Generating Elementary Combinatorial Objects

1 Definition of a Turing machine

A simple criterion on degree sequences of graphs

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

8 Primes and Modular Arithmetic

WOLLONGONG COLLEGE AUSTRALIA. Diploma in Information Technology

Sources: On the Web: Slides will be available on:

First Steps in Magma. John J. Cannon Catherine Playoust. School of Mathematics and Statistics University of Sydney NSW 2006, Australia

COMPUTER SCIENCE. Paper 1 (THEORY)

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

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

Reading 13 : Finite State Automata and Regular Expressions

Chapter 3. if 2 a i then location: = i. Page 40

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

Computing Concepts with Java Essentials

[Refer Slide Time: 05:10]

def: An axiom is a statement that is assumed to be true, or in the case of a mathematical system, is used to specify the system.

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

Data Structure with C

The Set Data Model CHAPTER What This Chapter Is About

Unit 6. Loop statements

Converting a Number from Decimal to Binary

Basic Programming and PC Skills: Basic Programming and PC Skills:

BCS2B02: OOP Concepts and Data Structures Using C++

Data Structures Fibonacci Heaps, Amortized Analysis

Introduction to Java

Programming and Software Development CTAG Alignments

Transcription:

Section 3.5 Recursive Algorithms 3.5.1 3.5 RECURSIVE ALGORITHMS review : An algorithm is a computational representation of a function. Remark: Although it is often easier to write a correct recursive algorithm for a function, iterative implementations typically run faster, because they avoid calling the stack. RECURSIVELY DEFINED ARITHMETIC Example 3.5.1: recursive addition of natural numbers: succ = successor, pred = predecessor. Algorithm 3.5.1: recursive addition recursive function: sum(m, n) Input: integers m 0,n 0 Output: m + n If n =0then return (m) else return ( sum(succ(m),pred(n)) )

Chapter 3 MATHEMATICAL REASONING 3.5.2 Example 3.5.2: numbers Algorithm 3.5.2: iterative addition of natural iterative addition function: sum(m, n) Input: integers m 0,n 0 Output: m + n While n>0 do m := succ(m); n := pred(n); endwhile Return (m)

Section 3.5 Recursive Algorithms 3.5.3 Example 3.5.3: proper subtraction of natural numbers: succ = successor, pred = predecessor. Algorithm 3.5.3: proper subtraction recursive function: diff(m, n) Input: integers 0 n m Output: m n If n =0then return (m) else return ( diff(pred(m),pred(n)) ) Example 3.5.4: Algorithm 3.5.4: natural multiplication: natural multiplication recursive function: prod(m, n) Input: integers 0 n m Output: m n If n =0then return (0) else return ( prod(m, pred(n)) + m )

Chapter 3 MATHEMATICAL REASONING 3.5.4 Example 3.5.5: Algorithm 3.5.5: factorial function: factorial recursive function: factorial(n) Input: integer n 0 Output: n! If n =0then return (1) else return ( prod(n, factorial(n 1)) ) notation: Hereafter, we mostly use the infix notations +, -, *, and! to mean the functions sum, diff, prod, and factorial, respectively.

Section 3.5 Recursive Algorithms 3.5.5 RECURSIVELY DEFINED RELATIONS def: The (Iverson) truth function true assigns to an assertion the boolean value TRUE if true and FALSE otherwise. Example 3.5.6: Algorithm 3.5.6: order relation: order relation recursive function: ge(m, n) Input: integers m, n 0 Output: true (m n) If n =0then return (TRUE) elseif m =0then return (FALSE) else return ( ge(m 1,n 1) ) Time-Complexity: Θ(min(m, n)).

Chapter 3 MATHEMATICAL REASONING 3.5.6 OTHER RECURSIVELY DEFINED FUNCTIONS review Euclidean algorithm: Algorithm 3.5.7: Euclidean algorithm recursive function: gcd(m, n) Input: integers m>0,n 0 Output: gcd (m, n) If n =0then return (m) else return ( gcd(n, m mod n) ) Time-Complexity: O(ln n). Example 3.5.7: Iterative calc of gcd (289, 255) m 1 = 289 n 1 = 255 r 1 =34 m 2 = 255 n 2 =34 r 2 =17 m 3 =34 n 3 =17 r 3 =0

Section 3.5 Recursive Algorithms 3.5.7 def: The execution of a function exhibits exponential recursive descent if a call at one level can generate multiple calls at the next level. Example 3.5.8: Fibonacci function f 0 =1,f 1 =1,f n = f n 1 + f n 2 for n 2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... (( ) n ) 1+ 5 Time-Complexity: Θ. 2 Algorithm 3.5.8: Fibonacci function iterative speedup function: fibo(n) Input: integer n 0 Output: fibo (n) If n =0 n =1then return (1) else f n 2 := 1; f n 1 := 1; for j := 2 to n step 1 f n := f n 1 + f n 2 ; f n 2 := f n 1 ; f n 1 := f n ; endfor return ( f n ) Time-Complexity: Θ(n).

Chapter 3 MATHEMATICAL REASONING 3.5.8 RECURSIVE STRING OPERATIONS Σ=set, c Σ (object), s Σ (string) Σ 0 =Λ={λ} strings of length 0 Σ n =Σ n 1 Σ strings of length n Σ + =Σ 1 Σ 2 all finite non-empty strings Σ =Σ 0 Σ 1 Σ 2 all finite strings These three primitive string functions are all defined and implemented nonrecursively for arbitrary sequences, not just strings of characters. def: appending a character to a string append : Σ Σ Σ non-recursive (a 1 a 2 a n,c) a 1 a 2 a n c def: first character of a non-empty string first : Σ + Σ non-recursive a 1 a 2 a n a 1 def: trailer of a non-empty string trailer : Σ n Σ n 1 a 1 a 2 a n a 2 a n

Section 3.5 Recursive Algorithms 3.5.9 These four secondary string functions are all defined and implemented recursively. def: length of a string length : Σ N length(s) =0 ifs = λ = 1+length(trailer(s)) if s λ def: concatenation of two strings concat : Σ Σ Σ (s t) =s if t = λ = append(s, first(t)) trailer(t) ifs λ notation: It is customary to overload the concatenation operator so that it also appends. def: reversing a string reverse : Σ Σ s 1 = s if s = λ = trailer(s) 1 first(s) ifs λ def: last character of a non-empty string last: Σ + Σ last(s) =first(s 1 )

Chapter 3 MATHEMATICAL REASONING 3.5.10 RECURSIVE ARRAY OPERATIONS Algorithm 3.5.9: location recursive function: location(x, A[ ]) Input: target value x, sorted array A[ ] Output: 0ifx A; min{j x = A[j]} if x A If length (A) =1then return (true (x = A[1])) elseif x midval(a) return location(x, fronthalf(a)) else return location(x, backhalf(a)) function: midindex(a) Input: array A[ ] Output: middle location of array A midindex(a) = length(a)/2 function: midval(a) Input: array A[ ] Output: value at middle location of array A midval(a) =A[midindex(A)] continued on next page

Section 3.5 Recursive Algorithms 3.5.11 Algorithm 3.5.9: location, continuation function: fronthalf(a) Input: array A[ ] Output: front half-array of array A fronthalf(a) = A[1...midindex(A)] function: backhalf(a) Input: array A[ ] Output: back half-array of array A fronthalf(a) =A[midindex(A)+1...length(A)] Time-Complexity: Θ(log n). Algorithm 3.5.10: verify ascending order recursive function: ascending(a[ ]) Input: array A[ ] Output: TRUE if ascending; FALSE if not if length (A[ ]) 1 then return (TRUE) else return (a 1 a 2 ascending(trailer(a[ ]))) Time-Complexity: Θ(n).

Chapter 3 MATHEMATICAL REASONING 3.5.12 Algorithm 3.5.11: merge sequences recursive function: merge(s, t) Input: ascending sequences s, t Output: merged ascending sequence If length (s) =0then return t elseif s 1 t 1 return ( first(s) merge(trailer(s),t) ) else return ( first(t) merge(s, trailer(t)) ) Algorithm 3.5.12: mergesort recursive function: msort(a) Input: ascending array A[ ] Output: merged ascending array If length (A[ ]) 1 then return (A[ ]) else return ( ) merge(msort(fronthalf(a), msort(backhalf(a))