CSI33 Data Structures

Similar documents
Outline. Simple Recursive Examples In-Class Work. 1 Chapter 6: Recursion. Recursive Definitions Simple Recursive Examples In-Class Work

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Algorithm Design and Recursion. Search and Sort Algorithms

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

Introduction to Computer Programming for Non-Majors

Python Programming: An Introduction to Computer Science

Introduction to Computer Programming for Non-Majors

CSI33 Data Structures

Outline: Search and Recursion (Ch13)

CSI33 Data Structures

CSI33 Data Structures

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

Search and Sorting Algorithms

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

CSI33 Data Structures

CS159. Nathan Sprague. November 9, 2015

Test #2 October 8, 2015

Recursion. Fundamentals of Computer Science

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

STA141C: Big Data & High Performance Statistical Computing

STA141C: Big Data & High Performance Statistical Computing

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

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


CSI33 Data Structures

DM536 Introduction to Programming. Peter Schneider-Kamp.

DM502 Programming A. Peter Schneider-Kamp.

CSI33 Data Structures

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Binary Search APRIL 25 TH, 2014

Lecture 8 Recursion. The Mirrors

Algorithm Analysis and Design

Logic & Algorithms Foundations of Computer Science Behrouz A. Forouzan, Brooks/Cole Thomson Learning, Pacific Grove, USA, 2003.

CSI33 Data Structures

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

a function that calls itself It doesn t do anything! Defining a recursive function 1) divide and conquer 2) base case 6/21/2018 chapter 15

CSCI 121: Searching & Sorting Data

Recursion Chapter 3.5

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

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

Recursion. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

Recursion. Chapter 11. Chapter 11 1

CSI33 Data Structures

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

CSC236H Lecture 5. October 17, 2018

Lecture 10: Recursion vs Iteration

CSI33 Data Structures

CSI33 Data Structures

CSL 201 Data Structures Mid-Semester Exam minutes

Lecture 2: Getting Started

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

PYTHON PROGRAMMING. John M.Zelle. Wartburg College AN INTRODUCTION TO COMPUTER SCIENCE SECOND EDITION

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Comp 249 Programming Methodology Chapter 11 Recursion

CSCE 2014 Final Exam Spring Version A

Basic Data Structures (Version 7) Name:

CSI32 Object-Oriented Programming

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

STUDENT LESSON A9 Recursion

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

ANS:

Unit #3: Recursion, Induction, and Loop Invariants

Recursive Algorithms. We would like to write an algorithm that computes the factorial of a given non-negative integer number n.

CS103 Unit 8. Recursion. Mark Redekopp

Module 08: Searching and Sorting Algorithms

Recursive Definitions

CS103 Unit 8. Recursion. Mark Redekopp

What is an algorithm?

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

Functions and Recursion

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

More Complicated Recursion CMPSC 122

CSI33 Data Structures

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Multiple-choice (35 pt.)

CS240 Fall Mike Lam, Professor. Quick Sort

Warmup: On paper, write a C++ function that takes a single int argument (n) and returns the product of all the integers between 1 and n.

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Stacks. Revised based on textbook author s notes.

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Solution for Data Structure

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

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

CS 331 Midterm Exam 2

ECE 2400 Computer Systems Programming Fall 2018 Topic 10: Sorting Algorithms

Data structure and algorithm in Python

CSI33 Data Structures

Stacks, Queues (cont d)

Data Structures and Algorithms CSE 465

CSC148 Week 2. Larry Zhang

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College October 10, 2018

Outline Outline 1 Chapter 6: Recursion

Outline Chapter 6: Recursion 1 Chapter 6: Recursion

Chapter 6: Recursion A Function Can Call Itself A recursive definition of a function is one which makes a function call to the function being defined. The function call is then a recursive function call. A definition is circular if it leads to an infinite sequence of function calls. To prevent this, the function must call itself with a parameter smaller than the one it is using. The function must test for when the parameter has reached the minimum size (the base case): this must be handled without a recursive call.

Chapter 6: Recursion The Call Stack The function call stack can handle recursive functions easily. There is no reason why a function can t push an activation record onto the call stack with variables for the current function while calling that same function. The earlier version of that function will resume when the recursive call is completed. When the base case is finally met, there will be no further recursive calls, and no further activation records will be pushed onto the stack. (Without a base case, the stack would overflow, producing a run-time error.)

Chapter 6: Recursion The Factorial Function def fact(n): if n == 0: return 1 else: return n * fact(n - 1)

Chapter 6: Recursion String Reversal Circular Definition def reverse(s): return reverse(s[1:]) + s[0]

Chapter 6: Recursion String Reversal Definition with Base Case def reverse(s): if s == "": return s else: return reverse(s[1:]) + s[0]

Anagrams Chapter 6: Recursion Anagrams An anagram of a word is another word spelled using the same letters but rearranged. Rearrangements are also called permutations. For example: TORSO is an anagram for ROOST. A recursive strategy to produce all anagrams of a given word is: remove the first letter from the word. for all anagrams of the smaller word, insert it in all possible positions.

Anagrams Chapter 6: Recursion Anagrams Using Recursion def anagrams(s): if s == "": return [s] else: ans = [] for w in anagrams(s[1:]): for pos in range(len(w)+1): ans.append(w[:pos]+s[0]+w[pos:1]) return ans

Chapter 6: Recursion Fast Exponentiation Naive Iteration is Θ(n) # power.py def looppower(a, n): ans = 1 for i in range(n): ans = ans * a return ans

Chapter 6: Recursion Fast Exponentiation Divide and Conquer Recursion is Θ(log n) # power.py def recpower(a, n): if n == 0: return 1 else: factor = recpower(a, n // 2) if n % 2 == 0: return factor * factor else: return factor * factor * a

Binary Search Chapter 6: Recursion Iteration def search(items, target): low = 0 high = len(items) - 1 while low <= high: mid = (low + high) // 2 item = nums[mid] if target == item: return mid elif target < item: high = mid - 1 else: low = mid + 1 return -1

Binary Search Chapter 6: Recursion Pseudocode Using Recursion Algorithm: binary search -- search for x in nums[low]...nums[high] if low > high x is not in nums mid = (low + high) // 2 if x== nums[mid]: x is at mid position elif x < nums[mid] binary search for x in nums[low]...nums[mid-1] else binary search for x in nums[mid+1]...nums[high]

Binary Search Chapter 6: Recursion Python Code Using Recursion def search(items, target): return recbinsearch(target, items, 0, len(items)-1) def recbinsearch(x, nums, low, high): if low > high: return -1 mid = (low + high) // 2 item = nums[mid] if x == item: return mid elsif x < item: return recbinsearch(x, nums, low, mid-1) else: return recbinsearch(x, nums, mid+1, high)