And now for something completely different . . .

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Recursion Introduction to Computing Science and Programming I.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Building Java Programs Chapter 13 Searching reading: 13.3.
Problem Solving and Algorithms
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CSC 211 Data Structures Lecture 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion.
Recursion.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
Sort & Search Algorithms
Recursion Salim Arfaoui.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Introduction to Computing Science and Programming I
Decrease-and-Conquer Approach
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Chapter 15 Recursion.
And now for something completely different . . .
Adapted from slides by Marty Stepp and Stuart Reges
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CS 3343: Analysis of Algorithms
CSc 110, Spring 2017 Lecture 39: searching.
CS 201 Fundamental Structures of Computer Science
CSE 373 Data Structures and Algorithms
Linear Search Binary Search Tree
Recurrences (Method 4) Alexandra Stefan.
Recursion.
CS148 Introduction to Programming II
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Last Class We Covered Recursion Stacks Parts of a recursive function:
Running Time Exercises
Searching.
Presentation transcript:

And now for something completely different . . . 10-Apr-19 AHD c 2009

Part 12 Python 3 Introducing Recursion 10-Apr-19 AHD c 2009

Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm 10-Apr-19 AHD c 2009

Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm 10-Apr-19 AHD c 2009

Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself. 10-Apr-19 AHD c 2009

A recursive solution to a programming problem uses a function which calls itself AHD c 2009

Recursion Recursion is a very important concept in computer science, and is always studied along with sorting and searching data structures. 10-Apr-19 AHD c 2009

Recursion versus Repetition It's important to realize that most problems which have a recursive solution can also be solved by repetition. For example, the binary search algorithm can be implemented using either repetition or recursion. 10-Apr-19 AHD c 2009

Recursion versus Repetition There are times when repetition is the method of choice. . . 10-Apr-19 AHD c 2009

Recursion . . . but there are some problems which can only be solved elegantly by way of recursion. 10-Apr-19 AHD c 2009

Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm 10-Apr-19 AHD c 2009

Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself which can either be solved explicitly or can be solved recursively. . . 10-Apr-19 AHD c 2009

In recursion, the base case is the version of the problem that can be solved explicitly, the problem that we know the answer to. . . 10-Apr-19 AHD c 2009

Consider the problem of solving the factorial of any positive integer. The factorial of a number n is written as n! and is calculated as follows: For any number n, n! = n * n-1 * n-2 * n-3 ... * 1 10-Apr-19 AHD c 2009

Example: Calculation of 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 Example: Calculation of 4! 4! = 4 * 3 * 2 * 1 = 24 10-Apr-19 AHD c 2009

Example: Calculation of 3! 3! = 3 * 2 * 1 = 6 Example: Calculation of 2! 2! = 2 * 1 = 2 Example: Calculation of 1! 1! = 1 * 1 = 1 10-Apr-19 AHD c 2009

Note that 5! is the same as 5 * 4! Which means that any factorial can be expressed in terms of a smaller version of itself. 10-Apr-19 AHD c 2009

A recursive function 12-01.py def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) 12-01.py See all programs at: http://www.annedawson.net/Python3Programs.txt 10-Apr-19 AHD c 2009

Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm 10-Apr-19 AHD c 2009

Binary Search Binary search is a more specialized algorithm than a sequential search as it takes advantage of the fact that the data has been sorted. . . 10-Apr-19 AHD c 2009

Binary Search The underlying idea of binary search is to divide the sorted data into two halves and to examine the data at the point of the split. 10-Apr-19 AHD c 2009

2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 10-Apr-19 AHD c 2009

Binary Search Since the data is sorted, we can easily ignore one half or the other depending on where the value we're looking for lies in comparison to the value at the split. This makes for a much more efficient search than linear search. 10-Apr-19 AHD c 2009

Binary Search Algorithms A binary search algorithm involves breaking down the problem into a smaller version of itself, and hence is an ideal candidate for a recursive solution. . . 10-Apr-19 AHD c 2009

Binary Search Binary search involves binary decisions, decisions with two choices. At each step in the process you can eliminate half of the data you're searching. . . 10-Apr-19 AHD c 2009

Binary Search If you have an list of 16 numbers (N = 16), you can find any one number in at most, 4 steps: 16 -> 8 -> 4 -> 2 -> 1 Log216 = 4 10-Apr-19 AHD c 2009

Searching for number 7 is 7 <= 33? 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 33? 10-Apr-19 AHD c 2009

Searching for number 7 is 7 <= 10? 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 10? 10-Apr-19 AHD c 2009

Searching for number 7 is 7 <= 7? 7 found! 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 7? 7 found! 10-Apr-19 AHD c 2009

The recursive algorithm binarySearch(a, value, left, right) if right < left return not found mid = floor((left+right)/2) if a[mid] = value return mid else if value < a[mid] binarySearch(a, value, left, mid-1) else if value > a[mid] binarySearch(a, value, mid+1, right) 10-Apr-19 AHD c 2009

The iterative (repetitive) algorithm binarySearch(a, value, left, right) while left <= right mid = floor((left+right)/2) if a[mid] = value return mid else if value < a[mid] right = mid-1 else if value > a[mid] left = mid+1 return not found 10-Apr-19 AHD c 2009

10-Apr-19 AHD c 2009

This presentation uses the following program file: 12-01.py See all programs at: http://www.annedawson.net/Python3Programs.txt 10-Apr-19 AHD c 2009

End of Python3_Recursion.ppt 10-Apr-19 AHD c 2009

Last updated: Wednesday 2nd December 2009, 6:39 PT, AHD 10-Apr-19 AHD c 2009