You are on page 1of 71

ETCS – 301 – ALGORITHMS DESIGN AND ANALYSIS

Dr.AUDITHAN SIVARAMAN
Professor/CSE
Notation of an Algorithm
What is algorithm?
Algorithm is defined as a collection of unambiguous instructions
occurring in some specific sequence and such an algorithm should
produce output for given set of input in finite amount of time.

Notation of Algorithm
2
Properties of Algorithm
1. Non-ambiguity
2. Range of input
3. Multiplicity
4. Speed
5. Finiteness

Non-ambiguity:
• Each instruction should be clear and precise.
• The instruction in an algorithm should not denote any conflict
meaning.

3
Properties of Algorithm
Range of input :
• The range of input should be specified.
• Normally the algorithm is input driven and if the range of input is
not been specified then algorithm can go infinite state.

Multiplicity:
• Algorithm can be represented in several ways.
• For solving a problem we can write several different algorithms.
Example:
Problem: Searching a number from the given list.
Solution: Searching is a task and use of either a “ sequential
search “or “binary search”.
4
Properties of Algorithm
Speed:
• The algorithms are written using some specific ideas.
• Such algorithm should be efficient and should produce the output
with fast speed.

Finiteness :

• The algorithm should be finite.


• After performing required operations it should terminate.

5
How to write an Algorithm ?
Algorithm is basically a sequence of instructions written
in simple english language.

The algorithm is broadly divided into two sections:


1. Algorithm heading - It consists of name of algorithm, problem
description, input and output.

2. Algorithm body - It consists of logical body of the algorithm


by making use of various programming constructs and assignment
statement.
Algorithm name (p1,p2….,pn)

keyword name parameters


6
How to write an Algorithm ?
Example:
Write an algorithm to count the sum of n numbers.
Solution:
Algorithm sum(1,n)
//Problem description: This algorithm is for finding the sum of given n
numbers
// Input: 1 to n numbers
//Output: The sum of n numbers
result 0
for i 1 to n do i i+1
result result + 1
return result
7
How to write an Algorithm ?
Example:
Write an algorithm to check whether the given number is even or odd.
Solution:
Algorithm oddtest(val)
//Problem description: This algorithm is to check whether the given
number is even or odd
// Input: The number to be tested .
//Output: Appropriate message indicating even or odd.
if(val%2=0) then
write (“given number is even”)
else
write(“given number is odd”)
8
How to write an Algorithm ?
Example:
Write an algorithm to find factorial of n number.
Solution:
Algorithm fact(n)
//Problem description: This algorithm finds the factorial of given number
// Input: The number n of which the factorial is to be calculated.
//Output: Factorial value of given n number.
if(n=1) then
return 1
else
return n*fact(n-1)

9
Calculating GCD
Three methods are used for calculating GCD
1. GCD Using Euclid algorithm:
gcd(a,b) = gcd (b , a mod b) if a>b
gcd(a,0) then
GCD = a

Example: Find the Gcd(30,18)

A B Remainder a/b
30 18 12
18 12 6
12 6 0
6 0 GCD=6

10
Calculating GCD
2 . GCD using consecutive integer checking algorithm:
Example: Consider a=15 and b=10
t=min(15,10)
A B Description

15 mod 10 = 5 10 mod 10 =0 a mod t is not giving zero, t is not a GCD.


Set t=t-1, t=10-1=9, t=9
15 mod 9= 6 10 mod 9 =1 a mod t ,b mod t is not giving zero, t is not a GCD.
Set t=t-1, t=9-1=8, t=8
15 mod 8= 7 10 mod 8=2 a mod t ,b mod t is not giving zero, t is not a GCD.
Set t=t-1, t=8-1=7, t=7
15 mod 7= 1 10 Mod 7=3 a mod t ,b mod t is not giving zero, t is not a GCD.
Set t=t-1, t=7-1=6, t=6
15 mod 6= 3 10 Mod 6=4 a mod t ,b mod t is not giving zero, t is not a GCD.
Set t=t-1, t=6-1=5, t = 5
15 mod 5= 0 10 Mod 5=0 a mod t ,b mod t both are giving zero values, t = 5 is a
GCD value.

11
Fundamentals of Algorithmic problem solving

Algorithmic analysis and design process

12
Fundamentals of Algorithmic problem solving
1. Understanding the problem:
• This is the first step in designing of algorithm.
• Read the problem’s description carefully to understand the problem statement completely.
• Ask questions for clarifying the doubts about the problem.
• Identify the problem types and use existing algorithm to find solution.
• The input to the algorithm is called instance of the problem . It is very important to decide the range
of the input so that boundary values of algorithms get fixed.

2. Decision Making :
Decision making is done on the following:
1. Capabilities of computational devices.
2. Choosing between Exact and Approximate Problem Solving.
3. Algorithm Design Techniques.

13
Fundamentals of Algorithmic problem solving
Capabilities of computational devices :
• In random-access machine (RAM), instructions are executed one after another. Accordingly,
algorithms designed to be executed on such machines are called sequential algorithms.
• In some newer computers, operations are executed concurrently are called parallel algorithm.
• Choice of computational devices like Processor and memory is mainly based on space and time
efficiency.

Choosing between Exact and Approximate Problem Solving:


• The next decision is to decide whether the problem is to be solved exactly or approximately.
• An algorithm used to solve the problem exactly and produce correct result is called an exact
algorithm.
• If the problem is so complex and not able to get exact solution, then we have to choose an algorithm
called an approximation algorithm.

14
Fundamentals of Algorithmic problem solving
Algorithm design Techniques:
• An algorithm design technique is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing.
Algorithm + data structure = Program
• Algorithms and Data Structures are independent, but they are combined together to develop
program.

Algorithm Techniques:
Brute Force - straightforward technique
Divide and Conquer - Problem is divided into smaller instance
Decrease and conquer - instance size is decreased to solve the problem
Dynamic Programming - results are smaller,reoccurring instances are obtained to

15
Fundamentals of Algorithmic problem solving
3. Design an algorithm:
There are three ways to specify an algorithm. They are:
• Natural language
• Pseudo code
• Flowchart
Natural Language
It is very simple and easy to specify an algorithm using natural language. But many times specification
of algorithm by using natural language is not clear and thereby we get brief specification.
Example: Write an algorithm to perform addition of two numbers.
Step 1: Read the first number, say a.
Step 2: Read the first number, say b.
Step 3: Add the above two numbers and store the result in c.
Step 4: Display the result from c.

16
Fundamentals of Algorithmic problem solving
Pseudo code:
Pseudo code is a combination of a natural language and programming language constructs.
Pseudo code is usually more precise than natural language.
Example:
ALGORITHM Sum(a,b)
//Problem Description: This algorithm performs addition of two numbers
//Input: Two integers a and b
//Output: Addition of two integers
C ← a+b
return c

17
Fundamentals of Algorithmic problem solving
Flow chart:
Flowchart is a graphical representation of an algorithm.

18
Fundamentals of Algorithmic problem solving
Proving correctness :
• Once an algorithm has been specified then its correctness must be proved.
• An algorithm must yields a required result for every legitimate input in a finite amount of time.
• A common technique for proving correctness is to use mathematical induction because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.
• The notion of correctness for approximation algorithms is less straightforward than it is for exact
algorithms. The error produced by the algorithm should not exceed a predefined limit.

19
Fundamentals of Algorithmic problem solving
Analysis of Algorithm:
• For an algorithm the most important is efficiency. In fact, there are two kinds of algorithm
efficiency. They are:
• Time efficiency, indicating how fast the algorithm runs, and
• Space efficiency, indicating how much extra memory it uses.
• The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency.
So factors to analyze an algorithm are:
• Time efficiency of an algorithm
• Space efficiency of an algorithm
• Simplicity of an algorithm
• Generality of an algorithm

20
Fundamentals of Algorithmic problem solving
Code the Algorithm:
• The coding / implementation of an algorithm is done by a suitable programming language like C,
C++, JAVA.

21
Important problem types
The most important problem types are:
• Sorting
• Searching
• String processing
• Graph problems
• Combinatorial problems
• Geometric problems
• Numerical problems

22
Important problem types
Sorting:

• The sorting problem is to rearrange the items of a given list in non decreasing (ascending) order.

• Sorting can be done on numbers, characters, strings or records.

• To sort student records in alphabetical order of names or by student number or by student grade-
point average. Such a specially chosen piece of information is called a key.

• An algorithm is said to be in-place if it does not require extra memory, E.g., Quick sort.

• A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its
input.

23
Important problem types
Searching:

• The searching problem deals with finding a given value, called a search key, in a given set.
E.g., Ordinary Linear search and fast binary search.

String processing
• A string is a sequence of characters from an alphabet.

• Strings comprise letters, numbers, and special characters.

• Searching for a given word in a text is called string matching.

24
Important problem types
Graph problems :

• A graph is a collection of points called vertices, some of which are connected by line segments called
edges.

• Some of the graph problems are graph traversal, shortest path algorithm, topological sort, traveling
salesman problem and the graph-coloring problem and so on.

25
Important problem types
Combinatorial problems:

• These are problems that ask, explicitly or implicitly, to find a combinatorial object such as a
permutation, a combination, or a subset that satisfies certain constraints.

• A desired combinatorial object may also be required to have some additional property such s a
maximum value or a minimum cost.In practical, the combinatorial problems are the most difficult
problems in computing.

Example: The traveling salesman problem and the graph coloring problem.

26
Important problem types
Geometric problems:
• Geometric algorithms deal with geometric objects such as points, lines, and polygons.

• Geometric algorithms are used in computer graphics, robotics.

• The closest-pair problem and the convex-hull problem are comes under this category.

27
Important problem types
Numerical problems:
• Numerical problems are problems that involve mathematical equations, systems of equations,
computing definite integrals, evaluating functions, and so on.

• The majority of such mathematical problems can be solved only approximately.

28
Fundamentals of the analysis of algorithmic efficiency
The efficiency of an algorithm can be in terms of time and space.
The algorithm efficiency can be analyzed by the following ways.
• Analysis Framework.
• Asymptotic Notations and its properties.
• Mathematical analysis for Recursive algorithms.
• Mathematical analysis for Non-recursive algorithms.

Analysis Framework :
There are two kinds of efficiencies to analyze the efficiency of any algorithm. They are:
• Time efficiency, indicating how fast the algorithm runs, and
• Space efficiency, indicating how much extra memory it uses.

29
Fundamentals of the analysis of algorithmic efficiency
Space Complexity :
• The space complexity is defined as amount of memory required by an algorithm to run.
• To compute the space complexity we use two factors:
1. constant
2.Instance
S(p)=C + Sp
Where c is constant i.e fixed part and it denotes the space of input and outputs.

Example:
Algorithm add(a,b,c)
//problem description: This algorithm computes the addition of three elements
//Input: a,b,c are of floating type
//Output: addition is returned
return a+b+c

Assume that a , b and c occupy one word size then total size comes to be 3.

30
Fundamentals of the analysis of algorithmic efficiency
Time Complexity :
The space complexity of an algorithm is the amount of computer time required by an algorithm to
run to completion.

Example:
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j]
}
}

31
Fundamentals of the analysis of algorithmic efficiency
Time Complexity :

The frequency count can be computed


i=0 executes once
i<n executes for n+1 times
i++ executes for n times
J=0 executes for n * (1)= n times
J<n executes for n *(n+1)=n2 + n
J++ executes for n * n = n2
C[i][j]=a[i][j]+b[i][j] executes for
n*n = n2
Total frequency count= 3n2+4n+2

32
Fundamentals of the analysis of algorithmic efficiency

The algorithm analysis framework consists of the following:


• Measuring an Input’s Size
• Units for Measuring Running Time
• Orders of Growth
• Worst-Case, Best-Case, and Average-Case Efficiencies

Measuring an Input’s Size:


If the input size is longer, then usually algorithm runs for a longer time.
Example:
• while performing the product of two n × n matrices, we should know the order of matrices.
• Consider a spell-checking algorithm. If the algorithm examines individual characters of its input,
then the size is measured by the number of characters.

33
Fundamentals of the analysis of algorithmic efficiency
Units for Measuring Running Time :
• Some standard unit of time measurement such as a second, or millisecond, and so on can be
used to measure the running time of a program after implementing the algorithm.
Drawbacks :
• Dependence on the speed of a particular computer.
• Dependence on the quality of a program implementing the algorithm.
• The compiler used in generating the machine code.
• The difficulty of clocking the actual running time of the program.
• So, we need metric to measure an algorithm’s efficiency that does not depend on these extraneous
factors.
• One possible approach is to count the number of times each of the algorithm’s operations is
executed.

34
Fundamentals of the analysis of algorithmic efficiency
Orders of Growth :
Measuring the performance of an algorithm in relation with the input size n is called order
of growth.

n logn n logn n2 2n

1 0 0 1 2

2 1 2 4 4

4 2 8 16 16

8 3 24 64 256

16 4 64 256 65536

35
Asymptotic Notation and its properties
• Asymptotic notation is a notation, which is used to take meaningful statement about the
efficiency of a program.

• Asymptotic notation is a shorthand way to represent the time complexity.

• Various notations such as O,Ω and Θ used are called asymptotic notations.

There are mainly three asymptotic notations:

• Big-O notation

• Omega notation

• Theta notation

36
Asymptotic Notation and its properties
Big-O notation :
• A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded below by some positive
constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative
integer n0 such that t (n) ≥ c * g(n) for all n ≥ n0.
• Where t(n) and g(n) are nonnegative functions

37
Asymptotic Notation and its properties
Example:
f(n) = 2n + 2 and g(n)=n2
Solution :
n=1 f(n) = 2n + 2 = 2(1) +2 = 4
g(n)= n2 = (1)2 = 1
f(n) > g(n)
n=2
f(n) = 2n + 2 = 2(2) +2 = 6
g(n)= n2 = (2)2 = 2
f(n) > g(n)
n=3
f(n) = 2n + 2 = 2(3) +2 = 8
g(n)= n2 = (3)2 = 9
f(n) < g(n) is true

so we can conclude that for n>2, f(n) < g(n)

38
Asymptotic Notation and its properties
Big omega notation :
• A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded below by some
positive constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and
some nonnegative integer n0 such that t (n) ≥ c * g(n) for all n ≥ n0.
• Where t(n) and g(n) are nonnegative functions

39
Asymptotic Notation and its properties
Example:
f(n) = 2 n2 + 5 and g(n)= 7 n
Solution :
n=0 f(n) = 2 n2 + 5 = 2 (0)2 + 5 = 5
g(n)= 7n = 7(0) = 0 f(n) > g(n)
n=1
f(n) = 2 n2 + 5 = 2 (1)2 + 5 = 7
g(n)= 7n = 7(1) = 7 f(n) = g(n)
n=2
f(n) = 2 n2 + 5 = 2 (2)2 + 5 = 13
g(n)= 7n = 7(2) = 14 f(n) < g(n)
n=3
f(n) = 2 n2 + 5 = 2 (3)2 + 5 = 23
g(n)= 7n = 7(3) = 21 f(n) > g(n) is true

40
Asymptotic Notation and its properties
Big theta notation :
• A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded both above and
below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive
constants c1 and c2 and some nonnegative integer n0 such that c2g(n) ≤ t (n) ≤ c1g(n) for all n ≥ n0.
• Where t(n) and g(n) are nonnegative functions .

41
Asymptotic Notation and its properties
Example:

f(n) = 2 n + 8 and g(n)= 7 n


Solution :
f(n) = 2 n + 8
5n < 2n+8 < 7n n>=2
c1= 5 and c2= 7
The theta notation is more precise with both big oh and big omega notation.

42
Asymptotic Notation and its properties
Properties:
1. If f1(n) is order of g1(n) and f2(n) is order of g2(n) then
f1(n) + f2(n) ∈ O(max(g1(n) , g2(n) )

2. polynomials of degree m ∈ Θ(nm )

3. O(1) < O(log n) < O(n) < O (n2 ) < O(2n)

4. Exponential functions an ) have different orders of growth for different values of a

43
Asymptotic Notation and its properties
Properties of Big oh:

44
Recurrence Equation
The recurrence equation is an equation that defines a sequence recursively.
It is normally in the form of
T(n) = T(n-1)+n for n > 0 -----------(1)
T(0) = 0 -----------(2)

Here equation 1 is called recurrence relation and equation 2 is called initial condition

45
Recurrence Equation
Solving recurrence equations:
The recurrence equations can be solved by following methods
1. substitution method
2. Tree method
3.Mater’s method
Substitution Method:
The substitution method is a kind of method which a guess for the solution is made.
Types:
• Forward substitution
• Backward substitution

46
Recurrence Equation
Forward substitution :

T(n) = T(n-1) + n , with initial condition T(0) = 0


T(0) = 0, Initial condition
T(1) = T(n-1) + n
= T(1-1)+1 T(3) = T(n-1) + n
= T(0)+1 = T(3-1)+3
=0+1 = T(2)+3
= 3+3
=1 =6
T(2) = T(n-1) + n T(n) = 𝑛(𝑛+1) / 2
= T(2-1)+2
=
= T(1)+2
=1+2 = O( )
=3
47
Recurrence Equation
Backward substitution :

T(n) = T(n-1) + n
T(0) = 0, Initial condition
T(n-1) = T(n-1-1) + n-1
= T(n-2)+(n-1)+n
Let
T(n-2) =T(n-2-1)+(n-2)
T(n) =T(n-3)+(n-2)+(n-1)+n

= T(n-k)+(n-k+1)+(n-k+2)+………n
Put k=n then
T(n)= T(0)+1+2+…+n
T(n)= 0+1+2+3+……+n
T(n)=n(n+1)/2
48
Recurrence Equation
Master theorem:
T(n) = aT(n/b) + f(n) where, n>=d and d is some constant.
If f(n) is Θ(nd) where d>=0 then recurrence relation then
1.T(n) = Θ(nd) if a> bd
2. T(n) = Θ(nd log n) if a = b
3. T(n) = Θ(nlogb a) if a< bd

49
Recurrence Equation
Master theorem:
Solve the following recurrence relation:
T(n)=4T(n/2)+n
Solution:
T(n) = a T(n/b)+f(n)
a=4 and b=2 and
a> bd i.e. 4> 21
T(n) = Θ(nlogb a)
= Θ(nlog2 4)
= Θ(n2 )

50
Recurrence Equation
Master theorem:
Solve the following recurrence relation:
X(n)=X(n-1)+5 for n>1 X(1)=0
Solution:

51
Empirical Analysis
Empirical analysis of algorithm means observing behavior of an algorithm for certain set of
input.
In Empirical analysis actual
General Plan for the Empirical Analysis of Algorithm (Time Efficiency):
1. Understand the experiment’s purpose.
2. Decide on the efficiency metric M to be measured and the measurement unit (an operation count
vs. a time unit).
3. Decide on characteristics of the input sample (its range, size,and so on).
4. Prepare a program implementing the algorithms for the experimentation.
5. Generate a sample of inputs.
6. Run the algorithm (or algorithms) on the sample’s inputs and record the data
observed.
7. Analyze the data obtained.

52
Empirical Analysis
General Plan for the Empirical Analysis of Algorithm (Time Efficiency):
• Algorithm’s efficiency is to be measured by two ways:
(i) insert a counter into a program to count the number of times the algorithm’s basic operation is
executed.
(ii) The second alternative is to time the program by using a system’s command (using any
programming languages such as C, C++, Java etc.,)

• Profiling is an important resource in the empirical analysis of an algorithm’s running time

• Pseudorandom - Generating random numbers on a digital computer and the algorithm to generate
such numbers is linear congruential method

• To predict the algorithm’s performance using extrapolation and interpolation

53
Mathematical Analysis for recursive algorithms
General Plan for Analyzing the Time Efficiency of Recursive Algorithms
• Decide on a parameter (or parameters) indicating an input’s size.
• Identify the algorithm’s basic operation.
• Check whether the number of times the basic operation is executed can
vary on different inputs of the same size; if it can, the worst-case,
average-case, and best-case efficiencies must be investigated separately.
• Set up a recurrence relation, with an appropriate initial condition, for
the number of times the basic operation is executed.
• Solve the recurrence or, at least, ascertain the order of growth of its
solution.

54
Mathematical Analysis for recursive algorithms
Compute the factorial of some number n.
If n= 5 then
1. n!= 5! =5*4*3*2*1

ALGORITHM F(n)
//Problem description: This algorithm Computes n! Using recursive function
//Input: A nonnegative integer n
//Output: returns the factorial value
if (n = 0)
return 1
else
return F(n − 1) * n

55
Mathematical Analysis for recursive algorithms
Mathematical analysis
• The factorial algorithm works for input size n.
• The basic operation in computing factorial is multiplication.
• The recursive function call can be formulated as
F(n) = F(n −1)* n where n > 0.
• The number of multiplications M(n) needed to compute it must satisfy the equality

• The recurrence relation and initial condition for the algorithm’s number of multiplications M(n):
M(n) = M(n − 1) + 1 ,M(0)=0
Forward substitution:
M(1) = M(1 − 1) + 1 =M(0)+1=1
M(2) = M(2− 1) + 1 =M(1)+1=1+1=2

56
Mathematical Analysis for recursive algorithms
backward substitutions
M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1
= [M(n − 2) + 1]+ 1
= M(n − 2) + 2substitute M(n − 2) = M(n − 3) + 1
= [M(n − 3) + 1]+ 2
= M(n − 3) + 3

= M(n − i) + i

= M(n − n) + n
= n.

57
Mathematical Analysis for recursive algorithms
Example: Towers of Hanoi
The problem “towers of hanoi” is a classic example of recursive function.

There are three rods as shown in the figure above. One of the rods has the discs arranged in
the order of their size. The discs are arranged upwards in the decreasing-size manner. Our job is to
shift whole set of discs to any other rod following the following rules.
• Only one disc can be moved at a time.
• Never can a larger disc sit above smaller disc.
• Only uppermost disc can be moved from the set of discs.

58
Mathematical Analysis for recursive algorithms
Example: Towers of Hanoi
ALGORITHM TOH(n, A, C, B)
//Move disks from source to destination recursively
//Input: n disks and 3 pegs A, B, and C
//Output: Disks moved to destination as in the source order.
if n=1 then
{
Write(“peg moved from A to C”)
return
}
else
{
move top n-1 disks from A to B using C
TOH(n - 1, A, B, C)
move top n-1 disks from B to C using A
TOH(n - 1, B, C, A)
}
}
59
Mathematical Analysis for recursive algorithms
Mathematical Analysis:
The number of moves M(n) depends on n only, and we get the following recurrence equation for it:
M(n) = M(n − 1) + 1 + M(n − 1) for n > 1.With the obvious initial condition M(1) = 1,
we have the following recurrence relation for the number of moves M(n):
M(n) = 2M(n − 1) + 1 for n > 1, M(1) = 1.
Backward substitutions:
M(n) = 2M(n − 1) + 1 sub. M(n − 1) = 2M(n − 2) + 1
= 2[2M(n − 2) + 1]+ 1
= 22M(n − 2) + 2 + 1 sub. M(n − 2) = 2M(n − 3) + 1
= 22[2M(n − 3) + 1]+ 2 + 1
= 23M(n − 3) + 22 + 2 + 1 sub. M(n − 3) = 2M(n − 4) + 1
= 24M(n − 4) + 23 + 22 + 2 + 1

= 2iM(n − i) + 2i−1
The towers of hanoi has time complexity =ϴ( 2n ).

60
Mathematical Analysis for non-recursive algorithms
General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms

• Decide on a parameter (or parameters) indicating an input’s size.

• Identify the algorithm’s basic operation (in the innermost loop).

• Check whether the number of times the basic operation is executed depends only on the size of an
input. If it also depends on some additional property, the worst-case, average-case, and, if necessary,
best-case efficiencies have to be investigated separately.

• Set up a sum expressing the number of times the algorithm’s basic operation is executed.

• Using standard formulas and rules of sum manipulation either find a closed form formula for the
count or at the least, establish its order of growth.

61
Mathematical Analysis for non- recursive algorithms
Example:
Consider the problem of finding the value of the largest element in a list of n numbers.

ALGORITHM MaxElement(A[0..n − 1])


//Problem Description: This algorithm is for finding the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
//Output: The value of the largest element in A
maxval ←A[0]
for i ←1 to n − 1 do
if (A[i]>maxval) then
maxval←A[i]
return maxval

62
Mathematical Analysis for non- recursive algorithms
Example:
Consider the element uniqueness problem: check whether all the Elements in a given array of n
elements are distinct.

ALGORITHM UniqueElements(A[0..n − 1])


//Problem description: this algorithm finds whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false” otherwise
for i ←0 to n − 2 do
for j ←i + 1 to n − 1 do
if A[i]= A[j ] return false
return true

63
Mathematical Analysis for non- recursive algorithms
• Algorithm analysis
• The natural measure of the input’s size here is again n (the number of elements in the array).
• Since the innermost loop contains a single operation (the comparison of two elements).

• The number of element comparisons depends not only on n but also on whether there are equal
elements in the array and, if there are, which array positions they occupy. We will limit our
investigation to the worst case only.

• One comparison is made for each repetition of the innermost loop, i.e., for each value of the loop
variable j between its limits i + 1 and n − 1; this is repeated for each value of the outer loop, i.e.,
for each value of the loop variable i between its limits 0 and n − 2.

64
Mathematical Analysis for non- recursive algorithms
• Algorithm analysis

65
Mathematical Analysis for non-recursive algorithms
Example:
The following algorithm finds the number of binary digits in the binary representation of a positive
decimal integer.

ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation count ←1
while n > 1 do
count ←count + 1 n←[n/2]
return count
.

66
Mathematical Analysis for non-recursive algorithms

Algorithm analysis
• An input’s size is n.
• The loop variable takes on only a few values between its lower and upper limits.
• Since the value of n is about halved on each repetition of the loop, the answer should be
about log2 n.
• The exact formula for the number of times.
• The comparison n > 1 will be executed is actually [log2 n] + 1.

67
Visualization
Algorithmic visualization is a technique in which images are used to convey the information about
the algorithm.
Two approaches of algorithmic visualization:
1. static algorithmic visualization
2. Dynamic algorithm visualization
Static algorithm visualization:
Static algorithm visualization shows an algorithm’s progress through a series of still images.
The behaviour of an algorithm on different inputs should be highlighted. Not only this, it is always
effective to represent the comparison made with different algorithms solving same problem.
Dynamic algorithm visualization:
The dynamic representation of algorithmic visualization is called algorithm animation.

68
Visualization
Example:
The following snapshot shows selection sort method for random input.
similarly the elements can be sorted in ascending order.

69
Visualization
Features of algorithmic visualization:
1.Visualization should be consistent.
2. It should be interactive so that ordinary user should understand it easily.
3.It should be clear and concise.
4.There should be user friendliness with the developed system.
5.while developing such visualization first the developer should understand the knowledge
level of the user and then accordingly the system should be designed.
6.Emphasis should be on visual component rather than producing textual information.
7.Such system should keep the user interested.
8.The symbolic and iconic representation should be incorporated.
9.The algorithmic analysis as well as comparison with different algorithms solved the same
problem should be included in such animated systems.
10.The execution history should be included in such systems.
70
Visualization
Applications of algorithmic visualization:
1. Research:
Many uncovered features of algorithm can be effectively shown by visual systems and
researcher can get the benefit of such systems for further development and studies.
2. Education:
Students can learn algorithms very easily using animated systems. Various visual
components included in the animated systems help the student to understand and analyze the
algorithm in simplest manner.

71

You might also like