Recursion Gerry Howser, April 23, 2015.

Slides:



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

Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Factorial Recursion stack Binary Search Towers of Hanoi
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
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. 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)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Recursion.
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)
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Recursion.
Searching and Sorting Searching algorithms with simple arrays
Fundamentals of Algorithms MCS - 2 Lecture # 11
Recursion Salim Arfaoui.
Recursion Topic 5.
Chapter 15 Recursion.
Analysis of Algorithms
Recursion: The Mirrors
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Recursion: The Mirrors
Recursive Objects (Part 4)
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Chapter 15 Recursion.
Analysis of Algorithms
Recursion
Introduction to Computer Science - Alice
Java Programming: Program Design Including Data Structures
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
ITEC 2620M Introduction to Data Structures
Divide and Conquer – and an Example QuickSort
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Discrete Maths 11. Recursion Objective
Analysis of Algorithms
Recursion When performing a repetitive task either: a loop recursion
Recursion: The Mirrors
Recursion.
And now for something completely different . . .
CSC 143 Recursion.
ITEC324 Principle of CS III
Recursive Thinking.
Searching.
Analysis of Algorithms
Presentation transcript:

Recursion Gerry Howser, April 23, 2015

Example: Linear Search of an unordered list Iteration Usually do a series of sequential steps on a piece of data and then repeat on a new piece of data Often involve loops Example: Linear Search of an unordered list

Iteration vs Recursion Some problems have both iterative and recursive solutions Some problems lend themselves to recursive solutions Some problems do not lend themselves to recursion

Recursive Solutions Only specific types of problems can have recursive solutions The problem can be broken into sub-problems that exactly resemble the original problem Often the problem can be split in half Binary search Merge sort The solutions to the sub-problems combine to form a solution to the original problem

Two Quotes to Remember “Begin at the beginning”, the King said, very gravely, “and go on till you come to the end: then stop.” Lewis Carroll “If all you have is a hammer, everything looks like a nail.” Maslow’s Law

Problems with Recursive Solutions The Tower of Hanoi Merge Sort Matrix Problems Factorials Others…..

Tower of Hanoi

Tower of Hanoi Pseudo Code Solve(N, Src, Aux, Dst) if N is 0 exit else Solve(N - 1, Src, Dst, Aux) Move from Src to Dst Solve(N - 1, Aux, Src, Dst)

Recursive Merge Sort Merge_sort(A[ ],low, high) if low < high q = floor((low+high)/2.0) Merge_sort(A[ ], low, q) Merge_sort(A[ ], q+1, high) Merge(A[ ], low, q, high) return

Binary Tree of Pool Balls Given 15 pool balls, build a tree such that: Any parent is larger than its left child Any parent is less than its right child Why does this hold for 7?

Pool Ball Binary Tree

Binary Tree of Pool Balls Problem: Write an algorithm to list the pool balls in ascending order (In-order tree walk) Difficult to do with loops But if we choose wisely….. Can break the problem into identical sub-problems RECURSION!!!!

Pseudo Code to List Tree In Order list_in_order(Tree, node){ if (node.left exists) list_in_order(Tree, node.left); list(node); if(node.right exists) list_in_order(Tree, node.right); return;}

Listing the Tree All we need now is a way to start this at the root: in_order(Tree,root){ list_in_order(Tree, root); return;}

Factorials n!=n(n-1)(n-2)…1 (n-1)!=(n-1)(n-2)…1 n!=n * (n-1)! All sub-problems are identical to original problem … and contribute to the solution RECURSION!!!

Pseudo Code for n! int fact(int i) { int result, n; n=i; if (n<0) result = -1; else result = 1; if(n>1) result = n * fact(n-1); return result; }

Recursion and n! (continued…) Is this the only solution? Is this the best solution? What does “best” mean? Is there a better solution? Loop version

Loops and n! int fact(int n) { int result; int i=n; result=1; if(i<0) result = -1; while (i>0){ result = result * I; i--; }; return result; }

Generic Recursion recurse(A[ ],n) If(not base_case) recalculate n recurse(new_n) //or multiple times smash_together_answer return

Conclusions Many problems have recursive solutions Must be able to break the problem into sub-problems with exactly the same solution method Must be able to smash sub-solutions together to form the complete solution Not all recursive solutions are best Not all problems have recursive solutions