Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
CIT 590 Recursion. A function calling itself Factorial def factorial(n): if n == 0: return 1 return n * factorial(n-1)
Advertisements

1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Recursion.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
IB Computer Science Unit 5 – Advanced Topics Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
ALGORITHMS.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Introduction to Recursion
Introduction to Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 17 Recursion.
Chapter 15 Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Algorithm Analysis CSE 2011 Winter September 2018.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
MSIS 655 Advanced Business Applications Programming
Quicksort and Mergesort
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
“Human Sorting” It’s a “Problem Solving” game:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS201: Data Structures and Discrete Mathematics I
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Divide and Conquer Algorithms Part I
Basics of Recursion Programming with Recursion
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Search,Sort,Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS210- Lecture 3 Jun 6, 2005 Announcements
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
“Human Sorting” It’s a “Problem Solving” game:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 6 - Recursion.
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Wrapping up Recursion Intro to Computer Science CS1510 Dr. Sarah Diesburg

Recursion Recursive functions are functions that call themselves We can create recursive functions by breaking them up into two smaller parts

Recursion – one way A class of methods exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases), and A set of rules which reduce all other cases toward the base case. (recursive step)

Recursion – another way A recursive function is: One that calls itself With “smaller” inputs (a recursive step) Until those inputs reach a base case

1) Call same function with something “smaller” Recursion is a natural outcome of a divide and conquer approach to problem solving. A recursive function defines how to break a problem down (divide) and how to reassemble (conquer) the sub-solutions into an overall solution.

2) Base Case Recursion is a process not unlike loop iteration. You must define how long (how many iterations) recursion will proceed until it stops. The base case defines this limit. Without the base case, recursion will continue infinitely (just like an infinite loop).

How does this work in a computer?

The Stack A Stack is a data structure, like a List or a Dictionary, but with a few different characteristics. A Stack is a sequence. A Stack only allows access to one end of its data, the top of the stack.

Operations pop: remove top of stack. Stack is one element smaller. push (val): add val to the stack. Val is now the top. Stack is one element larger. top: Reveals the top of the stack. No modification to stack.

Stack of Function Calls Python maintains a stack of function calls. If a function calls another function recursively, the new function is pushed onto the calling stack and the previous function waits. The top is always the active function. When a pop occurs, the function below becomes active.

Some examples of things that can be done recursively Summation of numbers – sum(lower,upper) Exponents - power(base,exp) Reverse a string – reverse(string) Merge Sort – mergeSort(lyst)

Summation Usually with a summation, we have a lower bound and upper bound We want to sum from the lower bound to the upper bound summation(1,5) would yield 1+2+3+4+5=15 Write a summation function summation(lower,higher)

Summation def summation(lower,higher): if lower==higher: return lower else: return lower + summation(lower+1,higher)

Power Write a function that takes a base and a power and returns the result For example, we know that 24 is 16 2 is the base 4 is the power We break it up as 2 x 2 x 2 x 2 = 16 Same as 2 x 23 = 16

Power def power(base,exp): if (exp==0): return 1 else: return base*power(base,exp-1)

Reverse a String def reverse(string): if (len(string))==0: return "" else: return string[-1] + reverse( string[0:-1] )

Merge Sort Let’s say I have 16 programming assignments, and I need to sort them in alphabetical order I’m lazy, so I hand off half of the assignments to one student to sort, and the other half to another student to sort

What if everyone hands off the work? 16 B C

What if everyone hands off the work? 16 B C 8 8 D E F G

What if everyone hands off the work? 16 B C 8 8 D E F G 4 4 4 4 H 2 I 1

Merge Sort At some point, the last students will only have 1 paper. They can then hand those single papers back to the student that delegated the work in the first place. The delegating student can then sort the two piles (of 1) papers by performing a merge.

Merge Sort Students I and J hand each of their sorted, 1 item stacks to H. H performs a merge sort on the two stacks of items H 1 1 I J

What is a merge? Start with two sorted piles (pile A and pile B) Take the smallest item off the top of pile A or B and place it in the finished pile. Repeat the previous step until no more items in either pile. Resulting finished pile will be sorted.

Finishing the merge sort All students hand off sorted piles to delegating students, each of which performs a merge sort Finally the student that started everything (A) will merge sort two large piles of items and created the final sorted output pile

Pseudocode for Merge Sort Has to be some sort of recursive algorithm What is the base case? If you have pile of size 1, it is sorted and you are done. What is the recursive case? This is trickier We can merge two sorted piles, but we want the two piles to be sorted first Each pile is of size n/2, where n is the total number of items we have