CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Slides:



Advertisements
Similar presentations
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Advertisements

Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
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 Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Recursion. Binary search example postponed to end of lecture.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Recursion Chapter 7.
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.
Recursion!. Can a method call another method? YES.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Fundamental in Computer Science Recursive algorithms 1.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
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.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive Algorithms Section 5.4.
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Supplement: Recursion with Java 1.5
Lesson #6 Modular Programming and Functions.
ITEC324 Principle of CS III
Presentation transcript:

CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Recursion – Definition – Examples Math Functions Algorithms Data – Computability

CS-2852 Data Structures, Andrew J. Wozniewicz What Is Recursion? Recursion, n. see Recursion Some functional programming languages don’t have loops (equivalent to imperative looping constructs) A function “calls itself” – directly or not Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. DEFINITION

CS-2852 Data Structures, Andrew J. Wozniewicz Visual Recursion Source: Wikipedia

CS-2852 Data Structures, Andrew J. Wozniewicz Recursion in Mathematics By this base case and recursive rule, one can generate the set of all natural numbers 1 is a natural number, and each natural number has a successor, which is also a natural number. SET THEORY DEFINITION FOR NATURAL NUMBERS

CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Function fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) Fibonacci Series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… Leonardo Pisano Bigollo c – c Liber Abaci (The Book of Calculation) (the growth of a population of rabbits under idealized assumptions) Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, Fibonacci – Golden Ratio

CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Numbers BINARY REPRESENTATION The first 511 terms of the Fibonacci sequence represented in binary.

CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Function 1: int fib(int n) { 2: if (n==0) 3: return 0; 4: if (n==1) 5: return 1; 6: return fib(n-1) + fib(n-2); 7: } DEMO!

CS-2852 Data Structures, Andrew J. Wozniewicz Fibonacci Numbers - Plotted

CS-2852 Data Structures, Andrew J. Wozniewicz Factorial Function ! 1: int fact(int n) { 2: if (n==0) 3: return 0; 6: return n*fact(n-1); 7: } 0! = 1 n! = n * (n-1)

CS-2852 Data Structures, Andrew J. Wozniewicz Greatest Common Divisor long gcd(long a, long b) { // a > b, b != 0 if (b==0) return a; return gcd(b, a % b); } EUCLID’S ALGORITHM

CS-2852 Data Structures, Andrew J. Wozniewicz Recursively Count From n Downto 0 void countDown(int n) { for (int i = n; i >= 0; i--) System.out.print(i); } void countDown(int n) { System.out.print(i); countDown(n-1); } void countDown(int n) { System.out.print(n); if (n == 0) return; countDown(n-1); }

CS-2852 Data Structures, Andrew J. Wozniewicz Other Examples of Recursive Algorithms Linear search through an array Binary search in an array

CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Algorithm Design Identify the base case – the small problem that can be solved directly Split the big problem into smaller subproblems – Divide-and-Conquer Combine the solutions to the smaller subproblems

CS-2852 Data Structures, Andrew J. Wozniewicz Tail Recursion Occurs when the recursive call is at the “end” of the recursive function. The algorithm can usually be rewritten to use (pure) iteration instead. Computational Equivalence: – f1() { while(C) { S; }; return Q; } – f2() { if (C) then { S; return f(); } else { return Q; } }

CS-2852 Data Structures, Andrew J. Wozniewicz Recursion versus Iteration Both allow to repeat an operation You can always write a recursive solution to a problem solvable by iteration. The reverse is not necessarily true: – Some recursion problems (non-tail recursion) cannot be solved by iteration alone.

CS-2852 Data Structures, Andrew J. Wozniewicz “Proving” Correctness Verify that the base case is recognized and solved correctly. Verify that each recursive case makes progress towards the base case. Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly.

CS-2852 Data Structures, Andrew J. Wozniewicz Recursive Data Structures Linked-List: – Node – Node, Linked-List Tree – Node – Left-(Sub) Tree – Right-(Sub)Tree

CS-2852 Data Structures, Andrew J. Wozniewicz Summary Recursion – Recursive Functions Fibonacci Factorial Greatest Common Divisor – Recursive Algorithms Designing Recursive Algorithms Recursion versus Iteration Proving Correctness – by Induction Tail Recursion – Recursive Data Structures

Questions? Image copyright © 2010 andyjphoto.com