0% found this document useful (0 votes)
7 views

ch04 - ADA

This chapter covers the concepts of recursion and recursive algorithms, defining recursion as a process that simplifies problems by breaking them down into smaller parts. It discusses the analysis of recursive algorithms, including empirical analysis and algorithm visualization, highlighting their applications and advantages. Examples such as Fibonacci numbers and binary search are provided to illustrate these concepts.

Uploaded by

qx54x4q55g
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

ch04 - ADA

This chapter covers the concepts of recursion and recursive algorithms, defining recursion as a process that simplifies problems by breaking them down into smaller parts. It discusses the analysis of recursive algorithms, including empirical analysis and algorithm visualization, highlighting their applications and advantages. Examples such as Fibonacci numbers and binary search are provided to illustrate these concepts.

Uploaded by

qx54x4q55g
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

CH04

Mathematical Aspects
and Analysis of
Algorithms-2
Eng. Jibril Hassan (Msc IT)

Design and Analysis of Algorithms - Chapter 4 1


Objectives

• After studying this chapter you


should able to;
• Define ‘Recursion’ and ‘Recursive
algorithms’
• Analyze recursion with an example of
Fibonacci numbers
• Explain Empirical Analysis of
algorithms
• Describe Algorithm Visualization
Design and Analysis of Algorithms - Chapter 4 2
Introduction
• We can classify the algorithm analysis into
three types. They are:
•  Mathematical analysis
•  Empirical analysis
•  Algorithm visualization
• In mathematical analysis of recursive
algorithms, we analyze the algorithms
using mathematical and logical operations.
• Usually we use mathematical induction to
prove the correctness of the algorithms.

Design and Analysis of Algorithms - Chapter 4 3


Recursion
• Definition – Recursion is defined as
the process that refers itself to
simplify a problem.
• More precisely, recursion is the
process of simplifying large and
complex problems into small and
easily solvable problems.
• In recursion, we divide large problems
into its sub parts and again recur the
sub parts to solve the problem
Design and Analysis of Algorithms - Chapter 4 4
Recursion
• Let us use a simple mathematics problem
as given in equation Eq 4.1 to study the
concept of recursion

• We can divide equation Eq 4.1 into sub


parts and solve it separately.

Design and Analysis of Algorithms - Chapter 4 5


Recursion
• Now let us combine the resolved sub parts
and form the second level answers.

• Finally, we combine the second level


answers to get the final answer.

Design and Analysis of Algorithms - Chapter 4 6


Recursion
• We can use the recursion method for problems that
satisfy the following two conditions:
1. Each step of recursion has to break down the
problem into smaller problems of same type.
2. Each step of recursion needs to reduce the problem
significantly.
• The two types of recursion are:
a.Direct recursion – This is a form of recursion in
which a procedure or a function refers to itself.
b.Indirect recursion – This is a form of recursion in
which a function P calls another function Q, and Q
in turn calls P in its body.
Design and Analysis of Algorithms - Chapter 4 7
Use of Recursion
• We use recursion to make a complex code,
simple. Initially,
• We use recursion only if the problem is
recursively defined. Usually, we use direct
recursion more than indirect recursion.
Some practical uses of recursion are:
•  Disk directory trees navigation
•  Binary tree parsing
•  Searching
•  Sorting
Design and Analysis of Algorithms - Chapter 4 8
Recursive algorithms
• Definition – An algorithm defined at least
partially in terms of the same algorithm is
known as a recursive algorithm.
• Recursive algorithms play a huge role in all
the programming we do.
• It is not necessary to use recursion for
simple computational problems, but if we
define algorithms recursively, it becomes
very easy to write, study and check for
errors.

Design and Analysis of Algorithms - Chapter 4 9


Recursive algorithms
• Before explaining recursive algorithms
using examples, let us study what recursive
functions mean.
• Recursive functions are functions defined
recursively.
• We can write functions directly or
recursively.
• The example shown below explains the
difference.

Design and Analysis of Algorithms - Chapter 4 10


Recursive algorithms
• Base case – The base case of a function is
the non-recursive part of the problem that we
already know.
• This base case is also known as an escape
clause, as we can use it to terminate the
algorithm.
• A recursive step – Recursive step or steps
are used to compute the current value of a
function by calling the function repeatedly.
• Eventually this will lead the function to a
stopping condition.
Design and Analysis of Algorithms - Chapter 4 11
Examples of recursive
algorithms

Design and Analysis of Algorithms - Chapter 4 12


Algorithm for Fibonacci
Numbers
• The recursive algorithm for Fibonacci
numbers is given below:

Design and Analysis of Algorithms - Chapter 4 13


Binary search
• Binary search is a recursive searching algorithm
used to look for an item in a sorted list of items.
• Binary means two, and at each step, we are
dividing the remaining list into two parts.
• The basic idea of binary search is that we use two
variables to keep track of the endpoints of the
range in the list where the item could be.

Design and Analysis of Algorithms - Chapter 4 14


Algorithm for Recursive
Binary Search

Design and Analysis of Algorithms - Chapter 4 15


Design and Analysis of Algorithms - Chapter 4 16
Analyzing efficiency of
recursive algorithms
1) Decide the size of the input based on a parameter n.
2) Identify and analyze the basic operations of the
recursive algorithm.
3) Determine the number of times the basic operations
are used. Check whether the basic operations require
more size than the decided input size n.
4) Determine the best, worst and average cases for the
input size n. We have to analyze the cases separately
if the basic operations depend on it. To solve the basic
operation, set a recurrence relation.
5) Solve the recurrence relation using the forward and
backward substitution method. We can prove the
solution using mathematical induction.

Design and Analysis of Algorithms - Chapter 4 17


Example 1
• We can find the factorial of a number n by
performing repeated multiplication.

Design and Analysis of Algorithms - Chapter 4 18


Empirical Analysis of
Algorithms
• What is empirical analysis?
• Empirical analysis of algorithm means
analyzing the behavior of the algorithm
with a specific set of inputs.
• We apply a set of input values on the
algorithm and analyze the corresponding
outputs

Design and Analysis of Algorithms - Chapter 4 19


Empirical Analysis of
Algorithms
• The general plan for empirical analysis of
algorithms is given below:
1)Comprehend the purpose of the given operation.
2)Create an efficiency metric M and decide the unit
of measurement.
3)Decide the type and range of inputs.
4)Write a program for the algorithm.
5)Generate a sample set of input values.
6)Run the program for this sample set of inputs and
record the resulting outputs.
7)Analyze the outputs.

Design and Analysis of Algorithms - Chapter 4 20


Pros and Cons of Empirical
Analysis
• Advantages of empirical analysis
•  We can solve the problem without using any
complicated mathematics in it.
•  We can always apply empirical analysis to any
algorithm easily.
•  The pictorial analysis of the algorithm makes it
easy to study.
• Disadvantage of empirical analysis
•  It depends upon the sample set of inputs.
•  It depends upon the computer on which it is
executed.
Design and Analysis of Algorithms - Chapter 4 21
Algorithm Visualization
• Algorithm visualization is defined as a
technique which uses images to convey the
information about algorithms.
• In algorithm visualization, we use some
images or animation to illustrate the
operations of the algorithm.
• We can highlight the different behavior of
algorithms on different set of inputs on these
images.
• We can compare different algorithms using
this method
Design and Analysis of Algorithms - Chapter 4 22
Algorithm Visualization
• The two types of algorithm visualization are
as follows: Static algorithm visualization:
• This type of visualization uses still images
to illustrate the algorithm. Dynamic
algorithm visualization:
• This type of visualization uses animations
to illustrate the algorithm. This is also
known as algorithm animation.

Design and Analysis of Algorithms - Chapter 4 23


Algorithm Visualization
• The two fields for which algorithm visualization is
applied are:
• Education – algorithm visualization is widely
used in the field of education. Students can
easily understand animated algorithms. For
example, we can study sorting and searching
algorithms using bar charts and line graphs.
• Research – Researches use algorithm
visualization to study the uncovered features of
different algorithms. This leads to further
development and progress in the particular field.

Design and Analysis of Algorithms - Chapter 4 24


Examples of Algorithm
Visualization

Design and Analysis of Algorithms - Chapter 4 25


Bubble Sort

Design and Analysis of Algorithms - Chapter 4 26


Example 2: Quick sort

Design and Analysis of Algorithms - Chapter 4 27


Quick sort

Design and Analysis of Algorithms - Chapter 4 28


Self Assessment Questions
1. Recursion is defined as the process that
refers itself to simplify a problem.
2. Recursive algorithms need very few lines of
code as it performs the same process
again and again on different data.
3. In the towers of Hanoi problem, if the
numbers of disks is n, the number of
steps will be 2n-1.
4. _Empirical analysis of algorithm means
analyzing the behavior of the algorithm
with a specific set of inputs.
Design and Analysis of Algorithms - Chapter 4 29
Self Assessment Questions
5. We can measure efficiency of
algorithms using . Counters, and system
clocking __ methods.
6. The Factorial analysis of the algorithm
makes it easy to study.

Design and Analysis of Algorithms - Chapter 4 30


Self Assessment Questions
7. _Algorithm visualization__ is defined as a
technique which uses images to convey the
information about algorithms.
8. Static algorithm __ visualization is the type of
visualization which uses still images to illustrate the
algorithm.
9. Dynamic algorithm visualization is the type of
visualization which uses animations to illustrate the
algorithm. This is also known as algorithm animation.

Design and Analysis of Algorithms - Chapter 4 31


Summary
• In this unit, we have defined recursion and
recursive algorithms.
• We defined recursion as something that calls itself
to solve the problem easily. Recursive function is
any function that uses recursion in it. Next we
discussed the examples for recursive algorithms,
namely. Fibonacci numbers, binary search.
• We also studied the steps for analyzing efficiency
of recursive algorithm using the example of
finding the factorial of a number. Recursion should
not be used beyond a limit. We have also
discussed when to avoid recursion.
Design and Analysis of Algorithms - Chapter 4 32
Summary
• We have discussed empirical analysis of
algorithms which uses a set of inputs to
solve the problem. We explained the
steps for empirical analysis and its pros
and cons.
• We have analyzed the process of
algorithm visualization which is the
method of representing algorithms using
images and animations.
• We have also studied some illustrated
examples of sorting algorithms.
Design and Analysis of Algorithms - Chapter 4 33
Terminal Questions
1. What are the rules for analyzing the
efficiency of recursive algorithms?
2. What are the basic conditions to avoid
recursion?
3. What are the steps for analyzing an
algorithm empirically?
4. What are the guidelines followed while
performing algorithm visualization?

Design and Analysis of Algorithms - Chapter 4 34


Activities
1. Determine a constant p for a given
function F(n)≤p*h(n) where F(n)=2n+3
and h(n)=n2 .
2. Write an algorithm for counting even
number of bits in an integer

Design and Analysis of Algorithms - Chapter 4 35


END

Design and Analysis of Algorithms - Chapter 4 36

You might also like