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

Unit 4 Mathematical Aspects and Analysis of Algorithms - 2: Structure

This document discusses mathematical analysis and empirical analysis of algorithms. It begins by introducing recursion and analyzing recursive algorithms mathematically. Recursion is defined as breaking large problems into smaller subproblems. Examples of recursive algorithms given include calculating Fibonacci numbers and binary search. Empirical analysis is also discussed as analyzing algorithm performance based on measuring real execution times. Algorithm visualization is presented as a way to understand algorithms through visual demonstration.

Uploaded by

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

Unit 4 Mathematical Aspects and Analysis of Algorithms - 2: Structure

This document discusses mathematical analysis and empirical analysis of algorithms. It begins by introducing recursion and analyzing recursive algorithms mathematically. Recursion is defined as breaking large problems into smaller subproblems. Examples of recursive algorithms given include calculating Fibonacci numbers and binary search. Empirical analysis is also discussed as analyzing algorithm performance based on measuring real execution times. Algorithm visualization is presented as a way to understand algorithms through visual demonstration.

Uploaded by

Raj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Analysis and Design of Algorithms Unit 4

Unit 4 Mathematical Aspects and


Analysis of Algorithms – 2
Structure:
4.1 Introduction
Objectives
4.2 Mathematical Analysis of Recursive Algorithms
Recursion
Recursive algorithms
4.3 Empirical Analysis of Algorithms
Plan for Empirical Analysis of Algorithms
Pros and Cons of Empirical Analysis
4.4 Algorithm Visualization
Need for algorithm visualization
4.5 Summary
4.6 Glossary
4.7 Terminal Questions
4.8 Answers

4.1 Introduction
From the previous units, we know that algorithm analysis is very important
for the efficient working of an algorithm. We analyze an algorithm for its time
and space complexities to resolve a problem easily.
In the previous unit, we analyzed non recursive algorithms. Recursive
algorithms form a better way of solving problems. Therefore, in this unit we
mathematically analyze recursive algorithms.
In this unit we will define recursion and recursive algorithm with examples.
We will also discuss the empirical analysis of algorithms and algorithm
visualization with examples.
Objectives:
After studying this unit, you should be able to:
 define ‘Recursion’ and ‘Recursive algorithms’
 analyze recursion with an example of Fibonacci numbers
 explain empirical analysis of algorithms
 describe algorithm visualization

Sikkim Manipal University B1480 Page No. 64


Analysis and Design of Algorithms Unit 4

4.2 Mathematical Analysis of Recursive Algorithms


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.
Let us first define recursion.
4.2.1 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.
Let us use a simple mathematics problem as given in equation Eq 4.1 to
study the concept of recursion.
Solve (((1 + 2) * (8 - 3)) + ((4 * 5) – (9 / 3))). Eq: 4.1
We can divide equation Eq 4.1 into sub parts and solve it separately.
(1 + 2) = 3
(8 – 3) = 5
(4 * 5) = 20
(9 / 3) = 3
Now let us combine the resolved sub parts and form the second level
answers.
(3 * 5) = 15
(20 - 3) = 17
Finally, we combine the second level answers to get the final answer.
(15 + 17) = 32

Sikkim Manipal University B1480 Page No. 65


Analysis and Design of Algorithms Unit 4

We can use the recursion method for problems that satisfy the following two
conditions:
 Each step of recursion has to break down the problem into smaller
problems of same type.
 Each step of recursion needs to reduce the problem significantly.
The two types of recursion are:
 Direct recursion – This is a form of recursion in which a procedure or a
function refers to itself.
 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.
Use of Recursion – We use recursion to make a complex code, simple.
Initially, the study of recursion and recursive algorithms seems to be
complicated but, once we identify the base process it becomes easy to
comprehend. Often, programming algorithms are written using recursion to
study it easily. In some cases recursion takes lots of programming time and
space. 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
Sorting is a real life example of recursion. We use lots of sorting algorithms
while programming. To enhance the efficiency of sorting algorithms we use
recursion. A sorting algorithm that includes recursion can have small
amount of coding in it and will decrease the man-hours required to write the
code.
Next, let us define recursive algorithms.
4.2.2 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

Sikkim Manipal University B1480 Page No. 66


Analysis and Design of Algorithms Unit 4

define algorithms recursively, it becomes very easy to write, study and


check for errors. Recursive algorithms need very few lines of code as it
performs the same process again and again on different data.
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.
Direct implementation:
f(x) = 40x+7 Eq: 4.2
Recursive implementation:
f(0) = 7(Base Case); Eq: 4.3
f(x) = f(x-1)+40 Eq: 4.4
Equation Eq 4.2 gives the equation to find the value of f(x). Here the value is
found by substituting suitable values for x in the equation. In equation
Eq: 4.3 the function f(x) is solved recursively. The base case is a terminal
condition of the function f(x). In equation Eq: 4.4, f(x) calls itself with a lesser
value of x. this continues until the base case is met.
We can define recursive functions in terms of the following factors:
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.
Let us take the above example. Suppose you have a complex function f(x)
which cannot be defined directly. Compute the function for some input
values, and find a pattern. If you find that f(0)=7 and for each f(x) value there
is a difference of 40 between them, then you can write the function
recursively as f(x)=f(x-1)+40. Here you can see that f(0) is the base case
and f(x)=f(x-1)+40 is the recursive step.
Now we will discuss some examples of recursive algorithms.

Sikkim Manipal University B1480 Page No. 67


Analysis and Design of Algorithms Unit 4

Examples of recursive algorithms


Fibonacci numbers – The Fibonacci numbers are defined by F0=0, F1=1,
and for n  2, Fn = Fn-1 + Fn-2. The first few numbers in the Fibonacci series
is given 0, 1, 1, 2, 3, 5 ,8, 13, 21, 34…
By this definition we get
F2 = F1 + F0 = 1 + 0 = 1.
F3 = F2 + F1 = 1 + 1 = 2.
F4 = F3 +F2 = 2 + 1 = 3.
Few values for N and Fn are given below:

N 0 1 2 3 4 5 6 7 8 9
Fn 0 1 1 2 3 5 8 13 21 34

The recursive algorithm for Fibonacci numbers is given below:

Algorithm for Fibonacci Numbers


fib(interger;n)
{
if(n==0 or n==1)
{
return 1;
}
else
{
return fib(n-1)+fib(n-2);
}
}

We will next trace the algorithm for Fibonacci numbers.

Algorithm Tracing For Fibonacci numbers


n=2
fib(2)
{
if(n==0 or n==1)

Sikkim Manipal University B1480 Page No. 68


Analysis and Design of Algorithms Unit 4

{
return 1;
}
else
{
return fib(1)+fib(0) ;// return 1+1=2
}// this is a recursive function
}

We can see that the algorithm calls itself (fib()) to find the Fibonacci
numbers. It terminates when it reaches fib(0) and fib(1). The running time of
this recursive Fibonacci algorithm is given as T(n) =  (Fn+1).
We will now discuss binary search, another recursive algorithm.
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.
Initially, the target could be anywhere in the list, so we start with variables
low and high set to the first and last positions of the list, respectively.
We can use recursion to look for the middle value and then recursively
search either the lower half or the upper half of the list. The recursive loop
stops when either of the following conditions is met:
 When the searched value is found
 When there are no more values in the list to search
Examples for binary search are given below:
Example 1: Find 6 in [1, 2, 6, 17, 19, 25, 45, 76, 100, 112].
Step 1 (middle element is 19 > 6): 1 2 6 17 19 25 45 76 100 112
Step 2 (middle element is 2 < 6): 1 2 6 17
Step 3 (middle element is 6 == 6): 6 17

Sikkim Manipal University B1480 Page No. 69


Analysis and Design of Algorithms Unit 4

Let us now discuss the recursive algorithm for binary search.


Algorithm for Recursive Binary Search
def recBinSearch(x, nums, low, high)
if low > high( No place left to look, return -1)
return -1
mid = (low + high) / 2
item = nums[mid]
if item == x(Found the item. Return the index)
return mid
else if x < item (Search lower half)
return recBinSearch(x, nums, low, mid-1)
else(Search upper half)
return recBinSearch(x, nums, mid+1, high)

We can then recursively call the first function to start the search between
0 and len(nums)-1. Let us see the algorithm tracing for recursive binary
search.

Algorithm Tracing for Recursive Binary Search


low=1,high=3, X=3;
nums[ ]=[1,2,3,4];
def recBinSearch(x, nums, low, high)// this is a recursive function
if 1> 4//No place left to look, return -1
return -1
mid = (1 + 3) / 2 // the value will be 2/2=2
item = nums[2]
if item == 3//Found the item. Return the index
return mid
else if x < 3//Search lower half
return recBinSearch(3, nums, 1, 1)
else(Search upper half)
return recBinSearch(3, nums, 3, 3)
def search(x, nums):
return recBinSearch(x, nums, 0, len(nums)-1)

Sikkim Manipal University B1480 Page No. 70


Analysis and Design of Algorithms Unit 4

This recursive binary search is easy to write and execute, but the binary
search using loops is a bit faster than this algorithm. The recursive versions
of binary search use a divide and conquer structure. Here, the efficiency of
algorithm lies in the way of searching an item. Instead of searching each
item individually, the binary search algorithm cuts the list into half every time
it calls itself again.
The Towers of Hanoi puzzle also uses a recursive algorithm. In the previous
unit we had discussed the non-recursive algorithm for the ‘Towers of Hanoi’
problem. Let us now discuss the recursive algorithm for this problem.
Towers of Hanoi – ‘Towers of Hanoi’ is a mathematical puzzle, which has
three towers or rods and a set of disks with different diameters. Initially the
disks are arranged one above the other, so that a smaller disc is placed
above a larger one. All the discs are to be transferred from the first tower
(Tower A) to the third tower (Tower C) with the help of the second tower
(Tower B) as a temporary storage.
The basic rules to be followed while transferring the discs are:
 We can move only one disk at a time.
 We cannot set aside the disk; it should be kept in another tower.
 We cannot keep a larger disk on a smaller one.
 The task is to move the disks from the first tower to the third tower.
Consider a simple example of this puzzle with three disks as shown in
figure 4.1. Let us label the towers as A, B, C. So the problem here is to
move the disks from A to C.

Figure 4.1: Towers of Hanoi Puzzle with Three Disks

Sikkim Manipal University B1480 Page No. 71


Analysis and Design of Algorithms Unit 4

To solve this puzzle, we have to seven moves as shown in the figure 4.2.

Figure 4.2: Solution of Towers of Hanoi Puzzle with Three Disks

The steps are:


Step 1 – Move the smallest disc from A to C
Step 2 – Move the second largest disc from A to B
Step 3 – Move the smallest disc from C to B
Step 4 – Move the largest disc from A to C
Step 5 – Move the smallest disc from B to A

Sikkim Manipal University B1480 Page No. 72


Analysis and Design of Algorithms Unit 4

Step 6 – Move the second largest disc from B to C


Step 7 – Move the smallest disc from A to C
The steps to solve the towers of Hanoi puzzle with 5 disks arranged
randomly on the three towers is given in the table 4.1.
Table 4.1: Towers of Hanoi Puzzle with Five Disks
Step No: Tower A Tower B Tower C
Initial case Disc 3 Disc 4 Discs 1,2,5
1 3 1, 4 2, 5
2 2, 3 1, 4 5
3 1, 2, 3 4 5
4 1, 2, 3 – 4, 5
5 2, 3 – 1, 4, 5
6 3 2 1, 4, 5
7 3 1, 2 4, 5
8 – 1, 2 3, 4, 5
9 1 2 3, 4, 5
10 1 – 2, 3, 4, 5
11 – – 1, 2, 3, 4, 5

The recursive algorithm for solving the towers of Hanoi puzzle with n
number of disks is given below:
Step 1 – Move n-disk tower from source to destination via resting place
Step 2 – Move n-1 disk tower from source to resting place
Step 3 – Move 1 disk tower from source to destination
Step 4 – Move n-1 disk tower from resting place to destination
The base case for this algorithm is when there is only one disk. Here we use
two recursive moves of n-1 disks and an additional move in step 3. As the
algorithm proceeds, the solvable number of disks in the puzzle is reduced.
Let us now define the recurrence relation for the towers of Hanoi puzzle.
The total number of moves required to solve the towers of Hanoi puzzle T[N]
is given in the equation 4.5.
T[N] ≤ T[N-1]+1+T[N-1] = 2T[N-1]+1 Eq: 4.5

Sikkim Manipal University B1480 Page No. 73


Analysis and Design of Algorithms Unit 4

Now let us solve this recurrence relation in Eq: 4.5.


T[N] = 2T[N-1]+1
= 2 [2 T[N-2]+1]+1
= 2 [2 [2 T[N-3]+1]+1]+1
= 2[2 [2 [2 T[N-4]+1]+1]+1]+1
T[N]= 24 T[N-4] +15
For N number of moves, we can derive T[N] as in equation Eq 4.6.
T[N]= 2x T[N-x] + 2x -1 Eq: 4.6
The number of moves, N is considered as finite. Therefore we can derive
T[N] as given in equation 4.7.

T[N] x N = 2N-1 Eq: 4.7


The complexity of this algorithm is given as O(2N). This efficiency of the
puzzle depends upon the number of disks and the initial case of the puzzle.
If there are many disks, then it will take exponential time to solve the puzzle.
If the initial case is close to the final case then the complexity of the puzzle
is reduced and it becomes more efficient. The recursive tree for the towers
of Hanoi puzzle with 4 disks is given in figure 4.3.

Figure 4.3: Recursive Tree for Towers of Hanoi Puzzle with Four Disks

The number of steps for solving the puzzle increases with the number of
disks. The table 4.2 given below shows the number of steps required for
disks up to a number of 5. This is calculated using equation 4.2.

Sikkim Manipal University B1480 Page No. 74


Analysis and Design of Algorithms Unit 4

Table 4.2: Number of Steps for Different Number of Disks

Disks 1 2 3 4 5
Steps 1 3 7 15 31

Even though we can write a simple algorithm to solve the towers of Hanoi
puzzle, it is considered as an intractable problem. This puzzle requires too
much of computing resources such as time and memory.
Analyzing efficiency of recursive algorithms
Let us see the general plan for analyzing the efficiency of recursive
algorithms. The steps are as follows:
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.
5) To solve the basic operation, set a recurrence relation.
6) Solve the recurrence relation using the forward and backward
substitution method. We can prove the solution using mathematical
induction.
These steps will be clearer if we discuss it using some examples.
Example 1: Computing factorial of a number n.
We can find the factorial of a number n by performing repeated
multiplication.
Consider n=5, then n factorial (n!) is computed by following steps:
1) n!= 5!
2) 4! * 5
3) 3! * 4 * 5
4) 2! * 3 * 4 * 5
5) 1! * 2 * 3 * 4 * 5
6) 0! * 1 * 2 * 3 * 4 * 5 * 6
7) 1 * 1 * 2 * 3 * 4 * 5 * 6

Sikkim Manipal University B1480 Page No. 75


Analysis and Design of Algorithms Unit 4

The recursive algorithm for finding factorial of a number is given as:


Algorithm to Find the Factorial of a Number
factorial(n)
{
//Input: A non negative integer, n
//Output: Factorial value of n
If(n=0)
return 1
else return factorial(n-1) * n
Let us see the algorithm tracing to find the factorial of a number.
Algorithm Tracing to Find the Factorial of a Number
n=2
factorial(2)// this is a recursive function which traces to itself
{
If(2=0)//this condition is not met
{
return 1
}
else return factorial(2-1) * 2 // recursively calls factorial (1)
Now let us see the mathematical analysis of factorial algorithm.
Step 1: The algorithm works for input n.
Step 2: The basic operation of computing the factorial is multiplication.
Step 3: The recursive function call can be formulated as:
F(n) = F(n-1) *n where n >0
The basic operation, multiplication is given as M(n).
M(n) = M(n-1) + 1
Where the term M(n-1) is the multiplication which is used to calculate the
factorial (n-1).
The last term 1 represents the product of (n-1) and n.

Sikkim Manipal University B1480 Page No. 76


Analysis and Design of Algorithms Unit 4

Step 4: The obtained recurrence relation M(n) = M(n-1) +1, can be solved
using forward and backward substitutions.
Forward substitution
M(1) = M(0) + 1 Eq: 4.8
M(2) = M(1) + 1= 1 + 1= 2 Eq: 4.9
M(3) = M(2) + 1 = 2 + 1=3 Eq: 4.10
In equations Eq: 4.8, Eq: 4.9, and Eq: 4.10, we are substituting value of n
and directly finding the value for M(n).
Backward substitution
M(n) = M(n-1) + 1
= [M(n-2)+1]+1 = M(n-2) + 2
= [M(n-3)+1]+1+1 = M(n-3) +2 Eq: 4.11
From the substitution method shown in equation Eq: 4.11, we can establish
a general formula as given in equation Eq: 4.12.
M(n) = M(n-i) + i Eq: 4.12
Let us now prove the correctness of the formula using mathematical
induction.
Prove: M(n)= n By mathematical induction
Let n=0 then
M(n) = 0
i.e. M(0) = 0 = n
Induction: if we assume M(n-1) = n-1 then
M(n) = M(n-1) + 1
= n -1+1
=n
i.e. M(n) = n

Thus the time complexity of factorial function is  (n).


Even if we know the concepts of recursion, we need to know when and
where to use it. We will next discuss the precautions to be kept in mind
while using recursion.

Sikkim Manipal University B1480 Page No. 77


Analysis and Design of Algorithms Unit 4

Precautions on recursion
In all cases recursion is not the only best way to solve a problem. We can
use recursion only when the problem is recursively defined. Any function
that is defined using recursion can also be defined using iteration. The time
taken to define a recursive function is more and some recursive method
calls can be difficult. Sometimes, recursion uses lots of space to solve the
problem. In such cases, a direct implementation of the problem works
better. Recursion should be used only if this direct implementation is very
complex to study.
The rules to be kept in mind while deciding on using recursion are:
 We can use recursion if the problem definition requires a recursive
solution, provided it follows the next point.
 We have to analyze the time and memory space of the recursive
solution. If we can solve it using a non recursive way, with lesser time
and space then go for it.
 Do not use lots of recursion to solve a single problem. It becomes
complex to track every sub solution.
 If we get a small and elegant recursive solution, then go for it.
 A recursive function with its last line as a recursive call, does not waste
lots of memory. We can use this optimized way to write a recursive
function

Activity 1
Write an algorithm to calculate xn for different values and analyze its
efficiency.

Self Assessment Questions


1. _________ is defined as the process that refers itself to simplify a
problem.
2. _____________ 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 ___________.

Sikkim Manipal University B1480 Page No. 78


Analysis and Design of Algorithms Unit 4

4.3 Empirical Analysis of Algorithms


The analyses discussed in the previous units are mathematical analysis of
algorithms. We can also analyze algorithms empirically.
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.
4.3.1 Plan for 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.
Let us discuss the steps in detail.
1. Comprehend the purpose of the given operation
We perform the empirical analysis of algorithm for the following reasons:
 To check the accuracy of the algorithm
 To compare the efficiencies of different algorithms working to solve
the same problem
 To analyze the efficiency of a particular algorithm on a specific
machine.
 To develop the algorithm’s efficiency class
2. Create an efficiency metric M and decide the unit of measurement
We can measure the efficiency of algorithms using the following
methods:
 We have to insert a counter in the algorithm to count the number of
times the operation is computed.
Consider that we are writing a function to calculate the sum of n
numbers in an array, the efficiency of the algorithm can be found by

Sikkim Manipal University B1480 Page No. 79


Analysis and Design of Algorithms Unit 4

inserting a frequency count. We can write a count to know the number of


times a statement is executed.

int sum(int b[20], int n)


{
int i,sum=0
for(i=0;i<n;i++)
{
sum=sum+b[i];
}
return sum;
}

We can leave the declaration part while measuring and analyze the
loop.
Case 1: if i = 0; frequency count is 1
Case 2: if i<n, frequency count= (n+1); Here the statement executes n
times for the condition i<n and executes once for the condition
i>n
Case 3: i++; frequency count is n
Case 4: sum= sum+b[i]; frequency count is n
Case 5: Return sum; frequency count is 1
Therefore, the total frequency count = (3n+3).After neglecting the
constant part, we can see the efficiency of the algorithm is O(n).
 We can measure the time taken by the execution time of an algorithm
using the system clock.
But it is very difficult to calculate the system time for the following
reasons:
o It might not be accurate.
o It depends upon the type of computer. We can solve a problem in
limited time on a modern computer.
o If it is a time sharing system, it may include CPU execution time for
execution of the problem.

Sikkim Manipal University B1480 Page No. 80


Analysis and Design of Algorithms Unit 4

3. Decide the type and range of inputs


It is necessary to decide a set of inputs, even if we measure the
efficiency by frequency count or by time clocking methods. Observe the
behavior of the algorithm for various set of inputs. For example, we may
measure the ratio R(2*n)/R(n), where 2*n is the doubled set of inputs.
Thus we can find the efficiency of the algorithm.
4. Write a program for the algorithm.
Write a suitable program to solve the problem. We should write the
program in a simple way to comprehend each step easily.
5. Generate a sample set of input values.
We have to generate a sample set of input values for the program.
6. Run the program for this sample set of inputs and record the resulting
outputs.
Execute the program written in any suitable language. Collect the output
obtained from it and record it accordingly.
7. Analyze the outputs
Analyze the output values using a graph or any other suitable pictorial
representation.
4.3.2 Pros and Cons of Empirical Analysis
Empirical analysis is used for algorithms for which the mathematical
analysis is difficult. Let us study the advantages and disadvantages 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.

Self Assessment Questions


4. _________________________ of algorithm means analyzing the
behavior of the algorithm with a specific set of inputs.

Sikkim Manipal University B1480 Page No. 81


Analysis and Design of Algorithms Unit 4

5. We can measure efficiency of algorithms using ________________ and


_______________ methods.
6. The __________ analysis of the algorithm makes it easy to study.

4.4 Algorithm Visualization


So far we have analyzed algorithms mathematically and empirically. Now let
us study how to analyze algorithms visually. Certain algorithms are so
complex that it becomes difficult for us to track the steps and find a solution
to the problem. For this reason, algorithm visualization is used.
Algorithm visualization is defined as a technique which uses images to
convey the information about algorithms.
4.4.1 Need for algorithm visualization
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.
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.
Dynamic visualization is difficult to represent, but is used for better study of
algorithms. This is inspired from the color sound film created by University of
Toronto about sorting algorithm.
We need to implement the following measures for algorithm visualization:
 Create a consistent visualization.
 Create an interactive visualization, so that any ordinary user can study it.
 Create clear and crisp visualizations.
 Adopt friendliness while developing the system.
 Have a clear idea about the system, before presenting it to the users.
 Reduce the use of text and include more graphics.
 Incorporate all the symbolic and ionic representations.

Sikkim Manipal University B1480 Page No. 82


Analysis and Design of Algorithms Unit 4

 Include the detail algorithm analysis and comparisons with other


algorithms.
 Include the execution history in such systems.
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.
The concept of algorithm visualization will be clear with the following
examples.
Examples of Algorithm Visualization
Example 1: Bubble sort

Figure 4.4: Algorithm Visualization of Bubble Sort (n)

Figure 4.4 shows the algorithm visualization of bubble sort. The guidelines
for performing this visualization are as follows:
 We can see the sorted portion of the list as a triangular block in the
bottom-right part of the image. We can use this to measure the
percentage of time taken.
 We can track each element in the sort as the start and end of the
element is visible.
 We can measure the percentage of list sorted at 20% and 80% of the
process.

Sikkim Manipal University B1480 Page No. 83


Analysis and Design of Algorithms Unit 4

 Here we also see that the curve of the sorted elements is not linear, but
it is close to n2.
Example 2: Quick sort
Figure 4.5 shows the algorithm visualization of Quick Sort. The basic idea of
this algorithm visualization is to choose one element that we call pivot
(which is shown as the lines crossing in the centre) and to place all the
elements lower than the pivot on the left side and all the elements higher
than the pivot on the right side. This way the pivot is placed on the right
place and we repeat the same procedure for the two remaining sub lists and
sort the list recursively.

Figure 4.5: Algorithm Visualization of Quick Sort (n)

Activity 2
Write an algorithm to search an element from a list and illustrate using
the algorithm visualization features.

Self Assessment Questions


7. ______________________ is defined as a technique which uses
images to convey the information about algorithms.
8. ___________ visualization is the type of visualization which uses still
images to illustrate the algorithm.
9. ____________ visualization is the type of visualization which uses
animations to illustrate the algorithm. This is also known as algorithm
animation.

Sikkim Manipal University B1480 Page No. 84


Analysis and Design of Algorithms Unit 4

4.5 Summary
Let us summarize what we have discussed in this unit.
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 and the towers of Hanoi puzzle.
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.
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.

4.6 Glossary

Terms Description
Recurrence Recurrence relation is an equation that recursively defines a list
relation where each term of the list is defined as a function of the
preceding terms.
Induction Induction is a method of mathematical proof used to establish
that a given statement is true for all natural numbers.

4.7 Terminal Questions


1. What are the basic rules of the towers of Hanoi puzzle?
2. What are the rules for analyzing the efficiency of recursive algorithms?
3. What are the basic conditions to avoid recursion?
4. What are the steps for analyzing an algorithm empirically?
5. What are the guidelines followed while performing algorithm
visualization?

Sikkim Manipal University B1480 Page No. 85


Analysis and Design of Algorithms Unit 4

4.8 Answers
Self Assessment Questions
1. Recursion
2. Recursive algorithms
3. 2n-1
4. Empirical analysis
5. Counters, system clocking
6. Pictorial
7. Algorithm visualization
8. Static algorithm
9. Dynamic algorithm

Terminal Questions
1. Refer section 4.2.2 – Towers of Hanoi
2. Refer section 4.2 – Analyzing efficiency of recursive algorithms
3. Refer section 4.2 – Precautions on recursion
4. Refer section 4.4.1 – Plan for empirical analysis of algorithms
5. Refer section 4.5 – Need for algorithm visualization
References
 Corman. Thomas (1990). Introduction to algorithms. McGraw-Hill Book
Company
 Liang, Daniel (2009). Introduction to Java Programming. Pearson
Eduction Ltd
 Putembekar, A. A (2009). Design & Analysis of Algorithms. Technical
Publications
E-References
 http://www.algolist.net
 http://www.cargalmathbooks.com/5%20Recursive%20Algorithms.pdf
 http://cse.unl.edu/~dsadofs/RecursionTutorial/index.php?s=intro#recfunc
 http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion
 http://www.corte.si/posts/visualizingsorting/index.html
 http://mila.cs.technion.ac.il/~yona/c/lecture12/slide19.html

Sikkim Manipal University B1480 Page No. 86

You might also like