Recursion James Atlas University of Delaware 3/4/2009.

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU.
Back to Sorting – More efficient sorting algorithms.
Divide and Conquer Yan Gu. What is Divide and Conquer? An effective approach to designing fast algorithms in sequential computation is the method known.
A simple example finding the maximum of a set S of n numbers.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
For Monday Read 10.3 Homework: –Chapter 10, exercise 10.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
Divide and Conquer. Divide and Conquer is a technique for designing the algorithms that consists of decomposing the instance to be solved into a number.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
Class 2: Recursion (1). cis 335 Fall 2001 Barry Cohen Iterative problem solving n “Begin at the beginning.” n “Continue until you come to the end.” n.
General Computer Science for Engineers CISC 106 Lecture 08 James Atlas Computer and Information Sciences 9/21/2009.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Fundamental in Computer Science Recursive algorithms 1.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
M180: Data Structures & Algorithms in Java
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
File Organization and Processing Week 13 Divide and Conquer.
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Today’s Material Recursive (Divide & Conquer) Algorithms Design
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Computer Science 101 A Survey of Computer Science QuickSort.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Divide and Conquer Strategy
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Basic Design Techniques iteration versus recursion divide-and-conquer an example: merge sort.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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:
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Lecture 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
CS201: Data Structures and Discrete Mathematics I
Algorithms and Data Structures Lecture III
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Lecture 27 CSE 331 Nov 3, 2017.
Searching: linear & binary
CSE 373 Data Structures and Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4.
Divide & Conquer Algorithms
Last Class We Covered Recursion Stacks Parts of a recursive function:
ITEC324 Principle of CS III
Lecture 27 CSE 331 Nov 4, 2016.
Divide-and-conquer approach
Presentation transcript:

Recursion James Atlas University of Delaware 3/4/2009

Overview Review Recursion –Real-world –Programming –Algorithms

Review (Conditional) Programming Control Mechanisms –conditional (if-else) if i == 0 disp(“zero”); else disp(“non-zero”);

Review (Iteration) Programming Control Mechanisms –iteration (while) i = 1; sum = 0; while i <= 100 sum = sum + i; i = i + 1; end;

Recursion Overview Solution depends on solutions to smaller instances of the same problem

Destroying a Rock Acknowledgement: original example credited to Bradley Kjell, Central Connecticut State University

Destroying a Rock (cont’) When do you stop using the hammer?

Real-world Recursion Technique Two Steps to Recursion: –Is the problem solved? –If not, divide problem into smaller problems, and then solve the smaller problems

Rock-destroying Procedure Two Steps to Recursion: –Is the problem solved? When a piece is small, stop hammering.

Rock-destroying Procedure (cont’) Two Steps to Recursion: –Divide problem into smaller problems To destroy a large rock, hit it with a hammer. The rock shatters, leaving small and large pieces.

Rock-destroying Procedure (cont’) Two Steps to Recursion: –Divide problem into smaller problems To destroy a large rock, hit it with a hammer. The rock shatters, leaving small and large pieces. –and then solve the smaller problems Repeat the Rock-destroying Procedure for each piece.

Search Lost diamond ring on the beach

Search Lost diamond ring on the beach

Search Lost diamond ring on the beach

Search Lost diamond ring on the beach

Search Lost diamond ring on the beach

Programming Recursive Data Recursive Function

Recursive Data Data Structure that refers to itself Example: Binary Tree class BinaryTree BinaryTree leftChild; BinaryTree rightChild;

Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);

Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);

Tracing Recursion Non-recursive, call to function a() function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion functionarglocal return value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion ab() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion ab() bc() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion ab() bc() c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion ab() b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

Tracing Recursion Recursive call to function sum(3) function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1)

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0)

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0) sumn=0 0

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=10 1 sumn=0 0

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0

Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0

Questions?

Computer Science Examples

File System DOS, Windows 9X, NT 4.0 –Folders contain files or sub-folders –Data is defined recursively Problem: How to search for a file?

Sorting Algorithms Problem: How to sort a list of numbers? Quicksort Mergesort Both use a divide and conquer approach

Quicksort “Divide and Conquer” as recursion Sort a list of numbers: quicksort(list) if list size <= 1 return list; else p = remove random number from list; first = all numbers <= p in list; second = all numbers > p in list; return quicksort(first) + p + quicksort(second);

Search (revisited) “Divide and Conquer” as recursion General form: solve(problem) if base case return easy solve; else return solve(smaller problem 1) operator solve(smaller problem 2);

Dividing a Sub Problem: 16 equal pieces