Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views35 pages

Recursion

The document outlines a lecture on recursion and algorithms, emphasizing the importance of recursion in solving complex problems by breaking them down into smaller, manageable tasks. It defines algorithms, provides examples, and discusses the differences between recursive and non-recursive definitions, particularly in the context of mathematical sequences. The lecture also covers the implementation of recursive methods in programming, specifically in Java, and compares recursive and iterative approaches.

Uploaded by

paddyk688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views35 pages

Recursion

The document outlines a lecture on recursion and algorithms, emphasizing the importance of recursion in solving complex problems by breaking them down into smaller, manageable tasks. It defines algorithms, provides examples, and discusses the differences between recursive and non-recursive definitions, particularly in the context of mathematical sequences. The lecture also covers the implementation of recursive methods in programming, specifically in Java, and compares recursive and iterative approaches.

Uploaded by

paddyk688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

COMP H2026

Information Technology
Mathematics

Recursion
Lecture Outline
• Introduction to recursion

• Algorithms!

• Solving problems using recursive functions


• Writing recursive algorithms
• Recursive functions and programming
Recursion
• The aim of this lecture is to introduce recursion
• Recursion and recursive algorithms are important in programming
and computing

What is recursion?
• Recursion is a simple yet powerful and enormously useful concept
in developing algorithms for solving complex problems
• Recursive style - can describe recursion as having two parts
– Solve easy problems in one step.
– Divide hard problems into smaller problems, and then solve the smaller
problems.

• In programming: it is the ability of a subroutine (method) to call itself.


– Why use it? - It is helpful for writing routines that solve problems by
repeatedly processing the output of the same process (breaking a hard
problem into smaller problems!)
– Breaks a large problem/task into smaller easier tasks
Algorithms
• Firstly lets look at algorithms (you will have worked on and used
algorithms in Java but lets refresh our memories)

What is an algorithm?
• A central theme in computing is the design of a process
for carrying out a task, for example
– Sorting names into alphabetical order
– The best way of linking a set of computing sites into a network
– Converting a number to its representation in the binary system
– Encrypting a message for secure transmission over the internet
– and so on

• Given a problem task – ask how can this task be


performed by a person or a computer?
– The answer takes the form of a precise set of steps known as an
algorithm
Algorithms
• Example
– The algorithm provided by the steps you carry out when you
withdraw money from an ATM

1. Insert your card into the slot


2. Key in your pin
3. Select ‘withdraw cash’
4. Enter the amount you want to withdraw
5. Take your card, cash and transaction slip

– Steps 1-5 are a sequence of instructions to be carried out one


after another and make up the algorithm for given task of
withdrawing cash from an ATM
Algorithms

Definition of an algorithm:
• An algorithm is a finite sequence of steps for performing
a task such that
– each step is a clear and unambiguous instruction that can be
executed in a finite time
– the sequence in which the steps are to be executed is clearly
defined
– the process is guaranteed to stop after a finite number of steps
have been executed
Algorithms
• A program contains the implementation of an algorithm
in a particular programming language (you are familiar with
Java)
• Designing an algorithm is one of the steps in writing a
program

• Example
– Problem: write an algorithm to calculate the areas of a circle,
given the radius
– Solution: the areas of a circle with radius r is pi*r2 The
algorithm would be as follows:

1. Input r {r is the radius of the circle}


2. area ← pi*r2
3. Output = area
Algorithms
• Can you tell me anything about the language used in the
previous example?

Pseudocode: is a form of structured English


Note: At this stage in the design : Using pseudocode when designing a
program or algorithm allows the programmer concentrate on the structure
of the algorithm itself without getting side tracked by the details of a
programming language

Note in this example the steps are numbered for easy reference,
comments are included but written in curly braces {}.
The symbol ← denotes assignment thus in step 2 pi*r2 is calculated and
the result is assigned to area (In computing terms, the result is stored in
the memory at an address specified by the identifier area)
Algorithms
• Example - with control structures…
– Problem: Find the smallest number in a list of numbers
– Solution: The smallest number can be found by looking at
each number in turn, keeping track at each step of the
smallest number so far.

1. Input the number of values n


2. Input the list of numbers x1, x2, …, xn
3. min ← x1
4. For I = 2 to n do
4.1. If xi < min then
4.1.1. min ← xi
5. Output min
Algorithms
• Having designed an algorithm Step min i xi Output
to perform a particular task –
should find out whether it does
3 5 - - -
the task correctly
– Can use tracing
4 5 2 4 -
• Tracing: choose a particular set
of inputs and record the values 4.1 5 2 4 -
of all the variables at each step
of the algorithm. 4.1.1 4 2 4 -

– Problem: trace the algorithm in 4 4 3 8 -


the previous slide, to find the
smallest number in a list of 4.1 4 3 8 -
numbers, with the inputs
n = 3, x1 = 5, x2 = 4, x3 = 8 5 4 - 8 4

The trace confirms that the


algorithm gives the correct output
for this set of inputs
Algorithms
• Given the specification of problem how is an algorithm
developed?
• Example
– Problem: Design an algorithm to evaluate xn, where x is any real
number and n is a positive integer. (Assume that the operation of
multiplication is available but raising to a power is not; which
may be the case in some languages)
– Solution: If n is a positive integer, xn can be evaluated using the
formula xn = x*x*…*x n times
– In order to carry out this process we will need a variable answer,
which is initially assigned the variable x and which will be
multiplied by x the required number of times. Need a control
structure also – for loop
Algorithms
Resulting algorithm in pseudocode:
1. Input x, n
2. answer = x
3. For i = 1 to n-1 do
4. answer = answer * x
5. Output answer

• Is this sequence of steps/instruction an algorithm?

• If your answer is yes – how did you come to that


answer?
Algorithms
Summary

• Introduction to algorithms and pseudocode


• Algorithms play a central role in (discrete) mathematics and
computing
– You will have used them already in your programming classes
• Help devise a structure for solution to a given problem
• Which can be tested and then implemented in a programming
language

• Now we are ready to consider recursion and recursive algorithms!


Questions?
Recursion
• Extremely useful mechanism in developing algorithms
for solving complex problems

There are two parts to recursion:


• If the problem is easy, solve it immediately.
• If the problem can't be solved immediately, divide it
into smaller problems, then:
– Solve the smaller problems by applying this procedure to each
of them.
• The over-all strategy is to find some easily applied
action that breaks the problem into smaller pieces.
Some of the pieces can be dealt with immediately;
others need to be further broken up.
Recursion
• Say you wish to divide a line into 16 equal parts

• What is the first thing you would do?

• A good answer might be: to divide the line in half…

• Now the original problem "divide the line into 16 equal


pieces" has been transformed into two smaller problems:
"divide each of two lines into 8 equal pieces”.
Recursion
• How do you solve the smaller problems?
– By applying the same process to them.

– Keep applying the process to each of the quarter sized lines

– And one more time to get the 16 pieces

– Stop when you have 16 pieces.

• So what are the two parts in terms of recursion?


– 1. If the line is small enough don’t divide it
– 2. Divide the line into 2 pieces
• Apply this process to each of the two pieces
Recursive definition
• Lets take a mathematical view of this…

• Consider an infinite sequence of numbers


– A sequence is a list of numbers in a particular order
– A general sequence can be written as
t(1), t(2), t(3), t(4), …
Terms of the
sequence

• There is a definite pattern to the numbers in the


sequence
– E.g. the positive even natural numbers 2,4,6,8,10,12,…
Recursive definition
– the positive even natural numbers 2,4,6,8,10,12,…
• In this example t(1) = 2, t(2) = 4, t(3) = 6 and so on…

• A sequence can be defined by giving a formula for the


general term t(n) in terms of the variable n. So for the
above sequence
– t(n) = 2n
– E.g. let n = 1; t(1) = 2(1) = 2
– Let n = 2; t(2) = 2(2) = 4

– So we can think of a sequence as a particular kind of function


• Consider the sequence 3,5,7,9,11,13,…
– can defined by the function t(n) = 2n +1
– let n = 1; t(1) = 2(1) + 1 = 3
– let n = 2; t(2) = 2(2) + 1 = 5
– let n = 3; t(3) = 2(3) + 1 = 7….

• Consider the sequence 1,4,9,16,25,36,…


– Can be defined as t(n) = n2
– Let n = 1; t(1) = 1(1) = 1
– Let n = 2; t(2) = 2(2) = 4
– Let n = 3; t(3) = 3(3) = 9 and so on
Each is defined by stating the nth term t(n) as a math formula
containing n
Recursive Definition
• Definitions of the kind in the previous case are non-
recursive
• A non-recursive definition can form the basis for an
algorithm for generating the first m terms of a sequence
• e.g. this algorithm generates the first m terms of the
sequence 2,4,6,8,10,12,…
The first term is 2 &
1. Input m 1. Input meach subsequent term
2. for n = 1 to m do Or 2. t = 2 is obtained by adding
2 to the previous term
2.1 t = 2n 3. Output t
2.2 Output t 4. for n = 2 to m do
4.1 t = t + 2
4.2 Output t
Recursive Definition
• Mathematically we can write this as …

t(1) = 2

t(n) = t(n-1) + 2 (n > 1)

These two equations together


i.e. t(2) = t(1) + 2 = 2+2 = 4
form what is called a recursive
t(3) = t(2) + 2 = 4 +2 = 6 definition.
t(4) = t(3) + 2 = 6 + 2 = 8 What makes it recursive?
Its recursive because the formula
What is t(5)? for t(n) contains the previous term
t(n-1) (or could say its defined in
terms of itself!)
Recursive definitions
• Recursive and non-recursive definitions are merely two
different ways of defining the same sequence.

• Either can be used and both have advantages


depending on the circumstances.

t(4) = t(3) + 2 t(n) = 2n


= t(2) + 2 + 2 t(4) = 2 x 4
= t(1) + 2+ 2+ 2 Non-
=2+2+2+2 recursive
=8 Recursive definition
definition
Recursion
• Evaluate t(2) and t(3) for the following recursively defined sequence
t(1) = 3
t(n) = 2t(n-1) + n (n>1)

• Solution t(2) = 2t(2-1)+2 (notice we are using the value


= 2t(1) + 2 of t(1) given in the definition to evaluate
t(2))
= 2 x 3 +2
=8

t(3) = 2t(3-1) + 3 (notice we are using the value of t(2)


= 2t(2) + 3 we have already obtained to evaluate
t(3))
= 2 x 8 +3
= 19
Recursion
• An algorithm to output the first m terms of the
sequence defined in the previous example would be

1. Input m
2. t=3 Some terminology: Although this
3. Output t algorithm is based on a recursive
definition of the sequence, it is not a
4. For n = 2 to m Do
recursive algorithm but an iterative
4.1 t = 2t + n algorithm.
4.2 Output t
Iterate means to apply the same
process repeatedly, as in a For loop
Recursion
• Find a recursive and non-recursive
definition for the sequence:
– 2, 5, 8, 11, 14, 17…
Recursion
• In the examples of recursive definitions (& algorithms)
we’ve seen so far the expression for t(n) contains only
the immediately preceding term, t(n-1)
• A recursive formula for t(n) can contain any of the earlier
terms of the sequence
• For example the Fibonacci sequence: each term F(n)
from the third onwards is the sum of the two immediately
preceding terms:
Check that the first few
terms of the Fibonacci
F(1) = 1, F(2) = 1 Sequence are:
F(n) = F(n-2) + F(n-1) (n>2)1,1,2,3,5,6,13,21, …
Recursive algorithms
• A recursive algorithm is one that solves a problem by
solving one or more smaller instances of the same
problem.
– Recursive methods are used to implement recursive algorithms
in Java
– A recursive method is one that calls itself (with smaller values of
its arguments)
– Recursive methods in Java correspond to recursive definitions of
mathematical functions
Iteration v. Recursion
Example: Factorial function (recursive implementation)
O! = 1
N! = N(N-1)! (N>0)

int factorial (int n)


Recursive because
{ it contains a call to
if (N == 0) itself!
return 1;
else
return N * factorial (N-1); How does an algorithm
} written in this way work?
(on board)!
This recursive method computes the function N!, using the standard
recursive definition. N must be nonnegative and sufficiently small that
N! can be represented by an int.
Iteration v. Recursion
• Example: Factorial function (non-recursive
implementation)

int factorial (int N)


{
int product = 1;
for (int j = 1; j <=N; j++)
product *= j;
return product;
}
Recursive Algorithms
• Recursive algorithms allow us to express complex
algorithms in a compact form. The goal is be efficient.
– For example, the recursive implementation of the factorial
function does away with the need for local variables…
– Although it is easy to write an inefficient recursive
implementation also…

– Recursive algorithms should satisfy two basic properties:


• They must solve a base case
• Each recursive call must involve smaller values of the argument

Factorial program
Recursive algorithms
• Example: an algorithm to find the smallest number in a list of
number.
– Problem: Find the smallest number in a list of numbers
– Solution: The smallest number can be found by looking at each
number in turn, keeping track at each step of the smallest
number so far.

1. Input the number of values n


2. Input the list of numbers x1, x2, …, xn
3. min ← x1 Can you construct a
recursive algorithm to
4. For i = 2 to n do do this?
4.1. If xi < min then
4.1.1. min ← xi
5. Output min
Recursive algorithms

Function FindMinimum(n, min)


1. Input xn
2. If n = 1 then
2.1 min = x1
else
2.2 FindMinimum (n-1, min)
2.3 if xn < min then
2.3.1 min = xn
Summary
• The concept of recursion is fundamental in mathematics and
computing
• The simple definition:
– In maths is a function that is defined in terms of itself
– In programming is a program (method) that invokes itself
• A recursive algorithm then is one that solves one or more smaller
instances of the same problem.
• We use recursive methods to implement recursive algorithms in
Java
– The recursive methods in Java correspond to recursive definitions of
mathematical functions
• It is always possible to transform a recursive program into an non-
recursive (iterative) one that performs the same computation.
• It is always possible to use recursion to express without loops
(move from iteration) any computation that involves loops
• Recursion can be an elegant way to express complex algorithms in
a compact form (note: in your programming classes you will earn
more on the use of recursion)
Questions?

You might also like