Recursion Michael Ernst CSE 190p University of Washington To seal: moisten flap, fold over, and seal.

Slides:



Advertisements
Similar presentations
Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
Advertisements

Dr. Sharon Persinger October 30,  Recursion is a type of repetition used in mathematics and computing to create objects and to define functions.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Recursion Introduction to Computing Science and Programming I.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Csci1300 Introduction to Programming Recursion Dan Feng.
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.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring
BY MISS FARAH ADIBAH ADNAN IMK
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
(c) , University of Washington
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
CPSC 3730 Cryptography and Network Security
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Recursion in C Computer Organization I 1 September 2009 © McQuain, Feng & Ribbens Recursion Around the year 1900 the illustration of the "nurse"
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
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 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
Computer Science 101 A Survey of Computer Science QuickSort.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Recitation 8 Programming for Engineers in Python.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
MA/CSSE 473 Day 07 Euclid's Algorithm. MA/CSSE 473 Day 07 Student Questions Review topics not covered in class Euclid's algorithm (if there is time) extended.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
AF2. Turn off your phones Primes, gcd, some examples, reading.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Algorithms.
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSC 222: Object-Oriented Programming
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Teach A level Computing: Algorithms and Data Structures
Algorithmic complexity: Speed of algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Searching CSCE 121 J. Michael Moore.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
Recursion Spring 2015 UW CSE 160
Recursion UW CSE 160 Spring 2018
Recursion Winter 2014 UW CSE 140
Recursion UW CSE 160 Winter 2016
CS 1114: Sorting and selection (part two)
Application: Algorithms
Application: Algorithms
Algorithmic complexity: Speed of algorithms
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Recursion Michael Ernst CSE 190p University of Washington To seal: moisten flap, fold over, and seal

Three recursive algorithms Sorting GCD (greatest common divisor) Exponentiation Used in cryptography, which protects information and communication

Sorting a list Python’s sorted function returns a sorted version of a list. sorted([4, 1, 5, 2, 7])  [1, 2, 4, 5, 7] How could you implement sorted ? Idea (“quicksort”, invented in 1960): – Choose an arbitrary element (the “pivot”) – Collect the smaller items and put them on its left – Collect the larger items and put them on its right Sir Anthony Hoare

First version of quicksort def quicksort(thelist): pivot = thelist[0] smaller = [elt for elt in thelist if elt < pivot] larger = [elt for elt in thelist if elt > pivot] return smaller + [pivot] + larger print quicksort([4, 1, 5, 2, 7]) There are three problems with this definition Write a test case for each problem

Near-final version of quicksort def quicksort(thelist): if len(thelist) < 2: return thelist pivot = thelist[0] smaller = [elt for elt in thelist if elt < pivot] larger = [elt for elt in thelist if elt > pivot] return quicksort(smaller) + [pivot] + quicksort(larger) How can we fix the problem with duplicate elements?

Handling duplicate pivot items def quicksort(thelist): if len(thelist) < 2: return thelist pivot = thelist[0] smaller = [elt for elt in thelist if elt < pivot] pivots = [elt for elt in thelist if elt == pivot] larger = [elt for elt in thelist if elt > pivot] return quicksort(smaller) + pivots + quicksort(larger) def quicksort(thelist): if len(thelist) < 2: return thelist pivot = thelist[0] smaller = [elt for elt in thelist[1:] if elt <= pivot] larger = [elt for elt in thelist if elt > pivot] return quicksort(smaller) + [pivot] + quicksort(larger)

GCD (greatest common divisor) gcd(a, b) = largest integer that divides both a and b gcd(4, 8) = 4 gcd(15, 25) = 5 gcd(16, 35) = 1 How can we compute GCD?

Euclid’s method for computing GCD (circa 300 BC, still commonly used!) a if b = 0 gcd(a, b) = gcd(b, a) if a < b gcd(a-b, b) otherwise

Python code for Euclid’s algorithm def gcd(a, b): if b == 0: return a if a < b: return gcd(b, a) return gcd(a-b, b)

Exponentiation Goal: Perform exponentiation, using only addition, subtraction, multiplication, and division. (Example: 3 4 ) def exp(base, exponent): """Exponent is a non-negative integer""" if exponent == 0: return 1 return base * exp(base, exponent - 1) Example: exp(3, 4) 3 * exp(3, 3) 3 * (3 * exp(3, 2)) 3 * (3 * (3 * exp(3, 1))) 3 * (3 * (3 * (3 * exp(3, 0)))) 3 * (3 * (3 * (3 * 1)))

Faster exponentiation Suppose the exponent is even. Then, base exponent = (base*base) exponent/2 Examples: 3 4 = = = = New implementation: def exp(base, exponent): """Exponent is a non-negative integer""" if exponent == 0: return 1 if exponent % 2 == 0: return exp(base*base, exponent/2) return base * exp(base, exponent - 1)

Comparing the two algorithms Original algorithm: 12 multiplications * * 5 * * 5 * 5 * 5 9 … 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 1 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 25 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 125 … Fast algorithm: 5 multiplications 5 12 (5 * 5) (25 * 25) * * 625 * * 625 * 625 * * 625 * 625 * * 625 * * Speed matters: In cryptography, exponentiation is done with 600-digit numbers.

Recursion: base and inductive cases Recursion expresses the essence of divide and conquer – Solve a smaller subproblem, use the answer to solve the original problem A recursive algorithm always has: – a base case (no recursive call) – an inductive or recursive case (has a recursive call) What happens if you leave out the base case? What happens if you leave out the inductive case?

Recursion vs. iteration Any recursive algorithm can be re-implemented as a loop instead – This is an “iterative” expression of the algorithm Any loop can be implemented as recursion instead Sometimes recursion is clearer and simpler – Mostly for data structures with a recursive structure Sometimes iteration is clearer and simpler

More examples of recursion List algorithms: recursively process all but the first element of the list, or half of the list Map algorithms: search for an item in part of a map (or any other spatial representation) Numeric algorithms: Process a smaller value