Iteration and Recursion Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Dynamic Programming.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Introduction to Algorithms
Dividing Polynomials.
Recursion.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
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. Binary search example postponed to end of lecture.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Solving systems using matrices
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
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.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
M180: Data Structures & Algorithms in Java
Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
Getting Started Introduction to Algorithms Jeff Chastine.
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.
IB Computer Science Unit 5 – Advanced Topics Recursion.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Decimals Day 2 Multiply and Divide Decimals. DO NOT line up decimals Multiply with first number, then continue with next number Place decimal point in.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Decrease-and-Conquer Approach
Recursion CSC 202.
Control Structure Senior Lecturer
2-4 Solving Multi-Step Equations
Recursion Taken from notes by Dr. Neil Moore
Divide & Conquer Algorithms
Dr. Sampath Jayarathna Cal Poly Pomona
ITEC324 Principle of CS III
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Iteration and Recursion Tonga Institute of Higher Education

Iteration Iteration - Repeating a set of steps. Most programs contain loops where the body of the loop is executed over and over again. The computer iterates (repeats) through the loop, which means that it repeatedly executes the loop.

Simple Example of Iteration Name: Sione Salary: 400 Name: Semisi Salary: 430 Name: Ana Salary: 300 Name: Mele Salary: 410 Name: Poli Salary: 900 We have an array that stores employee objects for the employees in your company. We want to give each employee a 40 pa’anga raise. We could try to find each employee in the array and give them a raise but that takes a long time. It is better to iterate through each employee and give each one a raise. First add 40 here Then add 40 here Then add 40 here Then add 40 here Finally, add 40 here

Triangular Numbers Pythagoras was a famous mathematician in ancient Greece. He made the Pythagorean theorem. He had some people who worked under him who called themselves the Pythagorians. The believed that this series of numbers was magical: 1, 3, 6, 10, 15, 21, 28 … Can you see the pattern in these numbers?

The Pattern of Triangular Numbers NumberCalculationTriangle Number 1By Definition

Why are they “Triangular” Numbers? They can be seen as a triangular arrangement of objects, shown as little squares below.

Triangular Number Calculation with Iteration – 1 To find 4 th Triangular Number can add the number of squares in each column. Subtract one every time we add to the total

Triangular Number Calculation with Iteration – 2 Subtract one every time we add to the total Change this to be the Triangular Number you want to find Used to calculate and store the Triangular Number

Demonstration Triangular Numbers Iteration Project: TriangularNumber

Recursion Recursion - A programming method in which a method calls itself. Recursion is an extremely powerful concept, but it can strain a computer's memory resources. Recursive algorithms follow a divide-and-conquer approach. 1. Divide the problem into a number of subproblems. 2. Conquer the subproblems. If the subproblem is too hard, divide it again until it is small enough to solve easily. 3. Combine the solutions to the subproblems into a solution for the original problem.

Triangular Number Calculation with Recursion – 1 To find 4 th Triangular Number can add: 1. The tallest column, which has a value of The sum of all the remaining columns, which is 6.

Triangular Number Calculation with Recursion – 2 To find 4 th Triangular Number can add: 1. The tallest column. 2. The sum of all the remaining columns. Adds the tallest column to The sum of the columns Adds the tallest column to The sum of the remaining columns The sumAllColumns method does the same thing as triangle. So replace sumAllColumns with triangle 1 2 3

Triangular Number Calculation with Recursion – 3 This approach works like this:  Someone tells me to find the 4 th Triangular number.  I know: (4 th Triangular number) = (3 rd Triangular number) + 4.  So I tell Sione to find the 3 rd Triangular number.  Sione knows: (3 rd Triangular number) = (2 nd Triangular number) + 3  So Sione tells Ana to find the 2 nd Triangular number.  Ana knows: (2 nd Triangular number) = (1 st Triangular number) + 2  So Ana tells Taniela to get the 1 st Triangular number.  Taniela knows that the 1 st Triangular number so he tells Ana 1  Ana tells Sione the 2 nd Triangular number is = 3  Sione tells me that the 3 rd Triangular number is = 6  So I know that the 4 th Triangular number is = 10 At some point, we stop asking others and provide an answer. If we never provide an answer, we will be asking questions forever.

Triangular Number Calculation with Recursion – 4 At some point, we stop asking others and provide an answer. We stop asking here and give an answer 3 4 Previously coded step

Triangular Number Calculation with Recursion – 5 We stop asking here and give an answer We call this method over and over again Change this to be the Triangular Number you want to find Begins the recursive process

Demonstration Triangular Numbers Recursion Project: TriangularNumber

Demonstration Triangular Numbers Recursion w/ Outputs Project: TriangularNumber

Characteristics of Recursive Methods It calls itself When it calls itself, it does so to solve a smaller problem At some point, the problem is simple enough so the method can return something without calling itself.

Is Recursion Efficient? Recursion is not as efficient as iteration  Methods must be called over and over again  All the variables in all the methods must be stored while methods calls are repeated

Why Use Recursion? Recursion simplifies problems conceptually.

Factorial Calculation with Recursion - 1 NumberCalculationFactorial 1By Definition1 21 * * 2 * * 2 * 3 * * 2 * 3 * 4 * * 2 * 3 * 4 * 5 * * 2 * 3 * 4 * 5 * 6 * 75040

Class Competition Develop the program to find a factorial using the triangular number program.

Demonstration Factorials Project: Factorial

Factorial Calculation with Recursion - 2 Multiply instead of Add Change all words that say triangular to factorial