0% found this document useful (0 votes)
64 views37 pages

DAA - Unit 1 & 2

The document discusses algorithms and their analysis. It covers topics like the notion of an algorithm, important problem types, and analyzing algorithm efficiency using asymptotic notations. Example algorithms like the greatest common divisor, sieve of Eratosthenes, and sorting algorithms are provided.

Uploaded by

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

DAA - Unit 1 & 2

The document discusses algorithms and their analysis. It covers topics like the notion of an algorithm, important problem types, and analyzing algorithm efficiency using asymptotic notations. Example algorithms like the greatest common divisor, sieve of Eratosthenes, and sorting algorithms are provided.

Uploaded by

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

CS8451

Design and Analysis


of Algorithms [UNIT I - INTRODUCTION ]

UNIT I INTRODUCTION 9
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Important Problem
Types – Fundamentals of the Analysis of Algorithmic Efficiency –Asymptotic Notations and their
properties. Analysis Framework – Empirical analysis - Mathematical analysis for Recursive and
Non-recursive algorithms - Visualization.

1.1 Notion of an Algorithm


1.2 Fundamentals of Algorithmic Problem Solving
1.3 Important Problem Types
1.4 Fundamentals of the Analysis of Algorithm Efficiency
1.4.1 Analysis Framework
1.4.2 Asymptotic Notations and its properties
1.4.3 Mathematical analysis for Recursive algorithms
1.4.4 Mathematical analysis for Non-recursive algorithms
1.4.5 Empirical analysis of algorithm
1.4.6 Visualization

1.1NOTATION OF AN ALGORITHM
– Algorithm:
– An algorithm is a sequence of unambiguous instructions for solving a problem, i.e.,
for obtaining a required output for any legitimate input in a finite amount of time.

– Several important points of an algorithm:


– The nonambiguity requirement for each step of an algorithm cannot be compromised.
– The range of inputs for which an algorithm works has to be specified carefully.
– The same algorithm can be represented in several different ways.
– There may exist several algorithms for solving the same problem.
– Algorithms for the same problem can be based on very different ideas and can
solve the problem with dramatically different speeds.

– Characteristics of an algorithm:

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 1
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

– Input: Zero / more quantities are externally supplied.


– Output: At least one quantity is produced.
– Definiteness: Each instruction is clear and unambiguous.
– Finiteness: If the instructions of an algorithm is traced then for all cases the algorithm
must terminates after a finite number of steps.
– Efficiency: Every instruction must be very basic and runs in short time.

– Steps for writing an algorithm:


1) An algorithm is a procedure. It has two parts; the first part is head and the second part
is body.
2) The Head section consists of keyword Algorithm and Name of the algorithm with
parameter list. E.g. Algorithm name1(p1, p2,…,p3)
The head section also has the following:
//Problem Description:
//Input:
//Output:
3) In the body of an algorithm various programming constructs like if, for, while and
some statements like assignments are used.
4) The compound statements may be enclosed with {and} brackets. if, for, while can be
closed by endif, endfor, endwhile respectively. Proper indention is must for block.
5) Comments are written using // at the beginning.
6) The identifier should begin by a letter and not by digit. It contains alpha numeric letters
after first letter. No need to mention data types.
7) The left arrow “←” used as assignment operator. E.g. v←10
8) Boolean operators (TRUE, FALSE), Logical operators (AND, OR, NOT) and
Relational operators (<,<=, >, >=,=, ≠, <>) are also used.
9) Input and Output can be done using read and write.
10) Array [], if then else condition, branch and loop can be also used in algorithm.

Example:
The greatest common divisor(GCD) of two nonnegative integers m and n (not-both-zero),
denoted gcd(m, n), is defined as the largest integer that divides both m and n evenly, i.e.,
with a remainder of zero.

Euclid’s algorithm is based on applying repeatedly the equality


gcd(m, n) = gcd(n, m mod n),
where m mod n is the remainder of the division of m by n, until m mod n is equal to 0.
Since gcd(m,0) = m, the last value of m is also the greatest common divisor of the initial m
and n.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 2
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

gcd(60, 24) can be computed as follows: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.

Euclid’s algorithm for computing gcd(m, n) in simple steps


Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2.
Step 2 Divide m by n and assign the value of the remainder to r.
Step 3 Assign the value of n to m and the value of r to n. Go to Step 1.

Euclid’s algorithm for computing gcd(m, n) expressed in pseudocode


ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n
m←n
n←r
return m

• Consecutive integer checking algorithm for computing gcd(m, n)


– Step 1 Assign the value of min{m, n} to t.
– Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, go to
Step 4.
– Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the
answer and stop; otherwise, proceed to Step 4.
– Step 4 Decrease the value of t by 1. Go to Step 2.

• Middle-school procedure for computing gcd(m, n)


– Step 1 Find the prime factors of m.
– Step 2 Find the prime factors of n.
– Step 3 Identify all the common factors in the two prime expansions found in Step 1 and
Step 2. (If p is a common factor occurring pm and pn times in m and n, respectively, it
should be repeated min{pm, pn } times.)
– Step 4 Compute the product of all the common factors and return it as the greatest
common divisor of the numbers given.

Thus, for the numbers 60 and 24, we get


– 60 = 2 . 2 . 3 . 5
– 24 = 2 . 2 . 2 . 3

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 3
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

– gcd(60, 24) = 2 . 2 . 3 = 12
Sieve of Eratosthenes
• Generating consecutive primes not exceeding any given integer n > 1.
• As an example, consider the application of the algorithm to finding the list of
• primes not exceeding n = 25:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Divisible by 2
23 5 7 9 11 13 15 17 19 21 23 25
Divisible by 3
23 5 7 11 13 17 19 23 25
Divisible by 5
23 5 7 11 13 17 19 23

ALGORITHM Sieve(n)
• //Implements the sieve of Eratosthenes
• //Input: A positive integer n > 1
• //Output: Array L of all prime numbers less than or equal to n

for p←2 to n do A[p]←p


for p←2 to √n do //see note before pseudocode
if A[p] =0 //p hasn’t been eliminated on previous passes
j←p∗p
while j ≤ n do
A[j ]←0 //mark element as eliminated
j ←j + p
//copy the remaining elements of A to array L of the primes
i ←0
for p←2 to n do
if A[p] = 0
L[i]←A[p]
i ←I + 1
return L

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 4
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Example problem 1:
1) Write an algorithm to find the maximum element in an array
Algorithm:
Algorithm max(A,N)
// A is an array of size N
result=A[0]
for i=0 to n-1 do
ifA[i]>result
then
result=A[i]
return result

2) Write an algorithm to perform selection sort


Algorithm:
Algorithm selectionsort(a,n)
//sort the array a[1:n] into increasing order
for i=1 to n do
j=i
for k=i+1 to n do
if (a[k]<a[j])
then
j=k
t=a[i]
a[i]=a[j]
a[j]=t

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 5
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

1.2FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING

(i) 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.
 Input (instance) to the problem and range of the input get fixed.
(ii) Decision making

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 6
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

The Decision making is done on the following:


(a) Ascertaining the Capabilities of the Computational Device
 In random-access machine (RAM), instructions are executed one after another (The
central assumption is that one operation at a time). Accordingly, algorithms designed to be
executed on such machines are called sequential algorithms.
 In some newer computers, operations are executed concurrently, i.e., in parallel.
Algorithms that take advantage of this capability are called parallel algorithms.
 Choice of computational devices like Processor and memory is mainly based on
Space and time efficiency
(b) Choosing between Exact and Approximate Problem Solving
 The next principal decision is to choose between solving the problem exactly or solving it
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. i.e., produces an approximate answer.
E.g., extracting square roots, solving nonlinear equations, and evaluating definite
integrals.
(c) Algorithm Design Techniques
 An algorithm design technique (or “strategy” or “paradigm”) is a general approach to
solving problems algorithmically that is applicable to a variety of problems from different
areas of computing.
 Algorithms+ Data Structures = Programs
 Though Algorithms and Data Structures are independent, but they are combined together
to develop program. Hence the choice of proper data structure is required before designing
the algorithm.
 Implementation of algorithm is possible only with the help of Algorithms and Data
Structures
 Algorithmic strategy / technique / paradigm are a general approach by which many
problems can be solved algorithmically. E.g., Brute Force, Divide and Conquer, Dynamic
Programming, Greedy Technique and so on.

(iii) Methods of Specifying an Algorithm


There are three ways to specify an algorithm. They are:
a. Natural language
b. Pseudocode
c. Flowchart

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 7
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Pseudocode and flowchart are the two options that are most widely used nowadays for
specifying algorithms.

a. 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: 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.
Such a specification creates difficulty while actually implementing it. Hence many
programmers prefer to have specification of algorithm by means of Pseudocode.

b. Pseudocode
* Pseudocode is a mixture of a natural language and programming language
constructs. Pseudocode is usually more precise than natural language.
*For Assignment operation left arrow “←”, for comments two slashes “//”,if
condition, for, while loops are used.
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

This specification is more useful for implementation of any language.

c. Flowchart
In the earlier days of computing, the dominant method for specifying algorithms was a
flowchart, this representation technique has proved to be inconvenient.

Flowchart is a graphical representation of an algorithm. It is a a method of expressing an


algorithm by a collection of connected geometric shapes containing descriptions of the
algorithm’s steps.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 8
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

(iv) Proving an Algorithm’s 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.
 For example, the correctness of Euclid’s algorithm for computing the greatest common divisor
stems from the correctness of the equality gcd(m, n) = gcd(n, m mod n).
 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.

(v) Analyzing an 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

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 9
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

(vi) Coding an Algorithm


 The coding / implementation of an algorithm is done by a suitable programming language
like C, C++, JAVA.
 The transition from an algorithm to a program can be done either incorrectly or very
inefficiently. Implementing an algorithm correctly is necessary. The Algorithm power
should not reduced by inefficient implementation.
 Standard tricks like computing a loop’s invariant (an expression that does not change its
value) outside the loop, collecting common subexpressions, replacing expensive
operations by cheap ones, selection of programming language and so on should be known
to the programmer.
 Typically, such improvements can speed up a program only by a constant factor, whereas
better algorithm can make a difference in running time by orders of magnitude. But once
an algorithm is selected, a 10–50% speedup may be worth an effort.
 It is very essential to write an optimized code (efficient code) to reduce the burden of
compiler.

1.3 IMPORTANT PROBLEM TYPES


The most important problem types are:
(i). Sorting
(ii). Searching
(iii). String processing
(iv). Graph problems
(v). Combinatorial problems
(vi). Geometric problems
(vii). Numerical problems

(i) Sorting
 The sorting problem is to rearrange the items of a given list in nondecreasing (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.

(ii) 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.

(iii) String processing


 A string is a sequence of characters from an alphabet.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 10
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

 Strings comprise letters, numbers, and special characters; bit strings, which comprise zeros
 and ones; and gene sequences, which can be modeled by strings of characters from the four
character alphabet {A, C, G, T}. It is very useful in bioinformatics.
 Searching for a given word in a text is called string matching

(iv) 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.

(v) 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.
 The traveling salesman problem and the graph coloring problem are examples of
combinatorial problems.

(vi) Geometric problems


 Geometric algorithms deal with geometric objects such as points, lines, and polygons.
 Geometric algorithms are used in computer graphics, robotics, and tomography.
 The closest-pair problem and the convex-hull problem are comes under this category.

(vii) 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.

1.4FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY


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

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 11
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

1.4.1 ASYMPTOTIC NOTATIONS AND THEIR PROPERTIES


Asymptotic notation is a notation, which is used to take meaningful statement about the
efficiency of a program.
The efficiency analysis framework concentrates on the order of growth of an algorithm’s basic
operation count as the principal indicator of the algorithm’s efficiency.
To compare and rank such orders of growth, computer scientists use three notations, they
are:
 O- Big oh notation
 Ω - Big omega notation
 Θ - Big theta notation
Let t(n) and g(n) can be any nonnegative functions defined on the set of natural numbers.
The algorithm’s running time t(n) usually indicated by its basic operation count C(n), and
g(n),some simple function to compare with the count.

(i) O - Big oh notation

DEFINITION
A function t (n) is said to be in O(g(n)), denoted t (n) ∈ O(g(n)), if t (n) is bounded
above by some 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) ≤ cg(n) for all n ≥ n0.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 12
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Example : Prove the assertions 100n + 5 ∈ O(n2)


Proof: 100n + 5 ≤ 100n + n (for all n ≥ 5)
= 101n
≤ 101n2 (n<= n2)

If n=1 then 105>101


If n=2 then 205>202
If n=3 then 305>303
If n=4 then 405>404
If n=5 then 505=505 [n0=5]
If n=6 then 605<606 f(n) < O(g(n))
If n=7 then 705<707 f(n) < O(g(n))

Since, the definition gives us a lot of freedom in choosing specific values for constants c
and n0. We have c=101 and n0=5

(ii) Ω - Big omega notation


DEFINITION
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

t (n) ≥ cg(n) for all n ≥ n0.

Example: Prove the assertions n3+10n2+4n+2 ∈ Ω (n2).

Proof: n3+10n2+4n+2 ≥ n2 (for all n ≥ 0)

If n=1 then 17>1


i.e., by definition t(n) ≥ cg(n), where c=1 and n0=0

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 13
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

(iii) Θ - Big theta notation


DEFINITION
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

Example 1:
F(n) = 2n2+8 g1(n)=5n g2(n)=7n
C1=5 and c2=7
If 5n 2n2+8 7n
n=1 5 < 10 > 7
n=2 10 < 12 >14
n=3 15 < 27 >21
Therefore, f(n) εθ(g(n)) for all n> 2

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 14
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Example 2:

Properties of asymptotic notation:


Property 1: MAXIMUM PROPERTY

If t1(n) ∈ O[g1(n)] and t2(n) ∈ O[g2(n)] then


t1(n) + t2(n) ∈ O(max{ g1(n) + g2(n)})
let
a1, a2, b1, b2, arbitrary numbers a1 + a2 < 2{b1, b2 }

t1(n) ∈ O[g1(n)]
 t1(n) < C1[g1(n)] for all n>n1

t2(n) ∈ O[g2(n)]
 t2(n) < C2[g2(n)] for all n>n2

Let C3 = max{ C1, C2 } and n > max(n1, n2)

t1(n) + t2(n) < C1[g1(n)] + C2[g2(n)]


< C3[g1(n)] + C3[g2(n)]
< 2C3 max {g1(n) , g2(n)}
t1(n) + t2(n) < 2C3 max {g1(n) , g2(n)}

therefore t1(n) + t2(n) ∈ O[ max {g1(n) , g2(n)}]

Property 2: TRANSITIVE PROPERTY


f(n) = O[g(n)] and g(n) = O[h(n)]
therefore f(n) = O[h(n)] [like A-> B , B-> C => A->C]

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 15
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Property 3:
If f(n) = O [g(n)]
Then
af(n) is O [g(n)] for any a.

Property 4:
f(n) = O [g(n)], h(n) = O [gl (n)]
then
f(n) + h(n) = O [g(n) + O [gl (n)]

f(n) . h(n) = O [g(n) . O [gl (n)]

Property 5:
If f(n) ∈ O [g(n)]
Then g(n) ∈ Ω [f(n)]

Property 6:
If there exists a function f1 such that f1 (n) = f2 (n) * c
Where c is the constant then f1 (n) and f2 (n) are
Equivalent that is

O[f1 (n) + f2 (n) ] = O[f1 (n)] ≠ O[f2 (n)]

Property 7:
In a polynomial the highest power term dominates other terms,
For example:
If we obtain 3n3 + 2n2 +10 then its time complexity is O(n3)

Property 8:
F(n) and g(n) are functions

If lim f(n) = 0 => f(n) ∈ O [g(n)]


n->α g(n) but f(n) not∈ O [g(n)]

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 16
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

1.4.2 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.

The algorithm analysis framework consists of the following:


(i) Measuring an Input’s Size
(ii) Units for Measuring Running Time
(iii)Orders of Growth
(iv) Worst-Case, Best-Case, and Average-Case Efficiencies

(i) Measuring an Input’s Size


 An algorithm’s efficiency is defined as a function of some parameter n indicating the
algorithm’s input size. In most cases, selecting such a parameter is quite straightforward.
For example, it will be the size of the list for problems of sorting, searching.
 For the problem of evaluating a polynomial p(x) = anxn + . . . + a0 of degree n, the size of the
parameter will be the polynomial’s degree or the number of its coefficients, which is larger
by 1 than its degree.
 In computing the product of two n × n matrices, the choice of a parameter indicating an
input size does matter.
 Consider a spell-checking algorithm. If the algorithm examines individual characters of its
input, then the size is measured by the number of characters.
 In measuring input size for algorithms solving problems such as checking primality of a
positive integer n. the input is just one number.
 The input size by the number b of bits in the n’s binary representation is b=(log2 n)+1.

(ii) 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.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 17
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

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. This approach is excessively difficult.

The most important operation (+, -, *, /) of the algorithm, called the basic operation. Computing the
number of times the basic operation is executed is easy. The total running time is determined by basic
operations count.

(iii) Orders of Growth


 A difference in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones.
 For example, the greatest common divisor of two small numbers, it is not immediately clear
how much more efficient Euclid’s algorithm is compared to the other algorithms, the
difference in algorithm efficiencies becomes clear for larger numbers only.
 For large values of n, it is the function’s order of growth that counts just like the Table, which
contains values of a few functions particularly important for analysis of algorithms.

TABLE Values (approximate) of several functions important for analysis of algorithms

(iv) Worst-Case, Best-Case, and Average-Case Efficiencies


Consider Sequential Search algorithm some search key K

ALGORITHM SequentialSearch(A[0..n - 1], K)


//Searches for a given value in a given array by sequential search

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 18
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

//Input: An array A[0..n - 1] and a search key K


//Output: The index of the first element in A that matches K or -1 if there are no
// matching elements
i ←0
while i < n and A[i] ≠ K do
i ←i + 1
if i < n return i
else return -1

Clearly, the running time of this algorithm can be quite different for the same list size n.

Worst-case efficiency
 The worst-case efficiency of an algorithm is its efficiency for the worst case input of size n.
 The algorithm runs the longest among all possible inputs of that size.
 For the input of size n, the running time is Cworst(n) = n.

Best case efficiency


 The best-case efficiency of an algorithm is its efficiency for the best case input of size n.
 The algorithm runs the fastest among all possible inputs of that size n.
 In sequential search, If we search a first element in list of size n. (i.e. first element equal to

a search key), then the running time is Cbest(n) = 1

Average case efficiency


 The Average case efficiency lies between best case and worst case.
 To analyze the algorithm’s average case efficiency, we must make some assumptions about
possible inputs of size n.
 The standard assumptions are that
o The probability of a successful search is equal to p (0 ≤ p ≤ 1) and
o The probability of the first match occurring in the ith position of the list is the same
for every i.

– Cavg = p(1+2+3+…..+n)/n + n(1-p)


= p(n(n+1)/2)/n + n(1-p)
= (p(n+1))/2 + n(1-p)

For successful search p=0 and Cavg = (n+1)/2


For unsuccessful search p=0 and Cavg = n

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 19
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

1.4.3 MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

General Plan for Analyzing the Time Efficiency of Recursive Algorithms

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


2. Identify the algorithm’s basic operation.
3. 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.
4. Set up a recurrence relation, with an appropriate initial condition, for the number of times
the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.

EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary nonnegative


integer n.
– n!= 1 . . . . . (n − 1) . n = (n − 1)! . n for n ≥ 1
– 0!= 1 by definition
– F(n) = F(n − 1) . n
– Algorithm:
• //Computes n! recursively
• //Input: A nonnegative integer n
• //Output: The value of n!
• if n = 0 return 1
• else return F(n − 1) ∗ n
Algorithm analysis:
– Input size :n
– Basic operation : multiplication
– Number of execution : M(n)
– Computed : F(n) = F(n − 1) * n for n > 0,
– The number of Multiplication: M(n) = M(n − 1)+ 1 for n > 0.
– M(n-1) => to compute factorial(n-1)
– 1 => to multiply factorial (n-1) times

Backward substitution:
M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1
= [M(n − 2) + 1]+ 1
= M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 20
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

= [M(n − 3) + 1]+ 2
= M(n − 3) + 3

General formula for the pattern: M(n) = M(n − i) + i


Specified for n = 0, we have to substitute i = n
M(n) = M(n − 1) + 1= . . .
= M(n − i) + i = . . .
= M(n − n) + n
= n.
Thus the time complexity of factorial of n numbers is O(n).

EXAMPLE 2: Tower of Hanoi

– Algorithm:
• //if only one disk has to be moved
• If(n=1) then
write (“the disk is moved from A to C
return
else
//move top n-1 disk from A to B using a
TOH (n-1, A,B,C)
//move remaining disk from B to C using a
TOH(n-1,B,C,A)

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 21
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Algorithm analysis:
– number of disks :n
– basic operation : 1 disk move
– number of moves : M(n)
– M(n) = M(n − 1) + 1+ M(n − 1) for n > 1
– initial condition M(1) = 1

M(n) = M(n − 1) + 1+ M(n − 1) for n > 1


M(1) = 1
M(n) = 2M(n − 1) + 1 for n > 1
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
= 2 [2M(n − 3) + 1]+ 2 + 1
2

= 23M(n − 3) + 22 + 2 + 1

– General pattern will be


M(n) = 2iM(n − i) + 2i−1 + 2i−2 + . . . + 2 + 1
= 2iM(n − i) + 2i − 1
When i=n-1
We get
M(n) = 2n-1M(n-(n-1)+2n-1-1
= 2n-1M(1)+2n-1-1
= 2n-1+2n-1-1
= 2n-1

EXAMPLE 3:
An investigation of a recursive version of the algorithm which finds the number of
binary digits in the binary representation of a positive decimal integer.

ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec(Ln/2J)+ 1

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 22
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Algorithm analysis:

the initial condition is A(1) = 0


The standard approach to solving such a recurrence is to solve it only for n = 2k
A(2k) = A(2k−1) + 1 for k > 0,
A(20) = 0

backward substitutions
A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1
k−2 k−2
= [A(2 ) + 1]+ 1= A(2 ) + 2 substitute A(2k−2) = A(2k−3) + 1
k−3 k−3
= [A(2 ) + 1]+ 2 = A(2 ) + 3 . . .
...
= A(2k−i) + i
...
= A(2k−k) + k.

Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original variable n = 2k and
hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).

1.4.4 MATHEMATICAL ANALYSIS OF NON RECURSIVE ANALYSIS


General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation (in the innermost loop).
3. 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.
4. Set up a sum expressing the number of times the algorithm’s basic operation is executed.
5. 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.

Two basic rules of sum manipulation

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 23
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Two summation formulas

EXAMPLE 1:
Consider the problem of finding the value of the largest element in a list of n
numbers. Assume that the list is implemented as an array for simplicity.

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


//Determines 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
maxval←A[i]
return maxval

Algorithm analysis
– Input size :n
– Operation that is most performed : for loop
– 2 operation in the loop body : comparison and assignment
– Comparison is considered as the basic operation
– Number of comparison will be same for all size of array
– No need to distinguish among the worst, average & best cases
– Let C(n) the number of times this comparison is executed
– algorithm makes one comparison on each execution of the loop, which is repeated for
each value of the loop’s variable i within the bounds 1 and n − 1

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 24
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

EXAMPLE 2:
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])


//Determines 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

Algorithm analysis
– Input :n
– Basic operation: comparison of two elements
– limit our investigation to the worst case only
– 2 worst case scenario - arrays with no equal elements and arrays in which the last two
elements are the only pair of equal elements.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 25
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

EXAMPLE 3
Given two n × n matrices A and B, find the time efficiency of the definition-based algorithm for
computing their product C = AB. By definition, C is an n × n matrix whose elements are
computed as the scalar (dot) products of the rows of matrix A and the columns of matrix B:

ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])


//Multiplies two square matrices of order n by the definition-based algorithm
//Input: Two n × n matrices A and B
//Output: Matrix C = AB
for i ←0 to n − 1 do
for j ←0 to n − 1 do
C[i, j ]←0.0
for k←0 to n − 1 do
C[i, j ]←C[i, j ]+ A[i, k] ∗ B[k, j]
return C

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 26
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

Algorithm analysis
– An input’s size is matrix order n.
– There are two arithmetical operations (multiplication and addition) in the innermost
loop. But we consider multiplication as the basic operation.
– Let us set up a sum for the total number of multiplications M(n) executed by the
algorithm.
– Since this count depends only on the size of the input matrices, we do not have to
investigate the worst-case, average-case, and best-case efficiencies separately.
– There is just one multiplication executed on each repetition of the algorithm ’ s
innermost loop, which is governed by the variable k ranging from the lower bound 0 to
the upper bound n − 1.
– Therefore, the number of multiplications made for every pair of specific values of
variables i and j is

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 27
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

EXAMPLE 4
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

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.

1.4.5 EMPIRICAL ANALYSIS OF ALGORITHM


The principal alternative to the mathematical analysis of an algorithm’s efficiency is its empirical
analysis.
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 algorithm (or 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.

– several different goals one can pursue in analyzing algorithms empirically

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 28
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

– They include checking the accuracy of a theoretical assertion about the algorithm’s
efficiency
– comparing the efficiency of several algorithms for solving the same problem or different
implementations of the same algorithm
– developing a hypothesis about the algorithm’s efficiency class, and ascertaining the
efficiency of the program implementing the algorithm on a particular machine
– goal of the experiment – how the algorithm’s efficiency is to be measured
- insert a counter (or counters) into a program implementing the algorithm to
count the number of times the algorithm’s basic operation is executed
- time the program implementing the algorithm in question - use a system’s
command, such as the time command in UNIX
– It is important to keep several facts in mind,
(1) First, a system’s time is typically not very accurate, and you might get somewhat
different results on repeated runs of the same program on the same inputs. An
obvious remedy is to make several such measurements and then take their average
(or the median) as the sample’s observation point.
(2) Second, given the high speed of modern computers, the running time may fail to
register at all and be reported as zero. The standard trick to overcome this obstacle
is to run the program in an extra loop many times, measure the total running time,
and then divide it by the number of the loop’s repetitions.
(3) Third, on a computer running under a time-sharing system such as UNIX, the
reported time may include the time spent by the CPU on other programs,
which obviously defeats the purpose of the experiment.
– Profiling—is an important resource in the empirical analysis of an algorithm’s running time;
the data in question can usually be obtained from the system tools available in most
computing environments.
– That is - measuring time spent on different segments of a program can pinpoint a bottleneck
in the program’s performance that can be missed by an abstract deliberation about the
algorithm’s basic operation. Getting such data—called profiling
– make decisions about the sample size
– The principal advantage of size changing according to a pattern is that its impact is easier to
analyze.
– important issue concerning sizes in an experiment’s sample is whether several instances of the
same size should be included
– Generating random numbers on a digital computer is known to present a difficult problem
because, in principle, the problem can be solved only approximately. This is the reason
computer scientists prefer to call such numbers pseudorandom

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 29
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

– Alternatively, you can implement one of several known algorithms for generating
(pseudo)random numbers. The most widely used and thoroughly studied of such algorithms is
the linear congruential method.

ALGORITHM Random(n, m, seed, a, b)


//Generates a sequence of n pseudorandom numbers according to the linear
// congruential method
//Input: A positive integer n and positive integer parameters m, seed, a, b
//Output: A sequence r1, . . . , rn of n pseudorandom integers uniformly
// distributed among integer values between 0 and m − 1
//Note: Pseudorandom numbers between 0 and 1 can be obtained
// by treating the integers generated as digits after the decimal point
r0←seed
for i ←1 to n do
ri←(a ∗ ri−1 + b) mod m

Empirical data obtained for the algorithm:


- Data can be presented numerically in a table or graphically in a scatterplot
- The principal advantage of tabulated data lies in the opportunity to manipulate it easily. For
example, one can compute the ratios M(n)/g(n) where g(n) is a candidate to represent the
efficiency class of the algorithm in question.
- On the other hand, the form of a scatterplot may also help in ascertaining the algorithm’s
probable efficiency class.
 For a logarithmic algorithm, the scatterplot will have a concave shape
 For a linear algorithm, the points will tend to aggregate around a straight line or, more
generally, to be contained between two straight lines
 Scatterplots of functions in θ(n log n) and θ(n2) will have a convex shape

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 30
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

1.4.6 VISUALIZATION
 In addition to the mathematical and empirical analyses of algorithms, there is yet a third way to
study algorithms. It is called algorithm visualization and can be defined as the use of images to
convey some useful information about algorithms
 That information can be a visual illustration of an algorithm’s operation, of its performance on
different kinds of inputs, or of its execution speed versus that of other algorithms for the same
problem.
 There are two principal variations of algorithm visualization:
o Static algorithm visualization
o Dynamic algorithm visualization, also called algorithm animation
 Static algorithm visualization shows an algorithm’s progress through a series of still images.
 Algorithm animation, on the other hand, shows a continuous, movie-like presentation of an
algorithm’s operations. Animation is an arguably more sophisticated option, which, of course, is
much more difficult to implement.
 There are two principal applications of algorithm visualization: research and education.
Potential benefits for researchers are based on expectations that algorithm visualization may help
uncover some unknown features of algorithms. For example, Tower of Hanoi algorithm in which
odd- and even-numbered disks was colored in two different colors. He noticed that two disks of

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 31
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

the same color never came in direct contact during the algorithm’s execution. This observation
helped him in developing a better nonrecursive version of the classic algorithm.
 The application of algorithm visualization to education seeks to help students learning algorithms.
The available evidence of its effectiveness is decisively mixed.

Question bank
UNIT – 1
2 marks
1. Why is the need of studying algorithms?
From a practical standpoint, a standard set of algorithms from different areas of computing
must be known, in addition to be able to design them and analyze their efficiencies. From a
theoretical standpoint the study of algorithms is the cornerstone of computer science.

2. What is algorithmic?
The study of algorithms is called algorithmic. It is more than a branch of computer science. It
is the core of computer science and is said to be relevant to most of science, business and technology.

3. What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for
obtaining a required output for any legitimate input in finite amount of time.
An algorithm is step by step procedure to solve a problem.

4. Give the diagram representation of Notion of algorithm.

5. What is the formula used in Euclid’s algorithm for finding the greatest common divisor of
two numbers?
Euclid‘s algorithm is based on repeatedly applying the equality
Gcd(m,n)=gcd(n,m mod n) until m mod n is equal to 0, since gcd(m,0)=m.

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 32
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

6. What are the three different algorithms used to find the gcd of two numbers?
The three algorithms used to find the gcd of two numbers are
 Euclid‘s algorithm
 Consecutive integer checking algorithm
 Middle school procedure

7. What are the fundamental steps involved in algorithmic problem solving?


The fundamental steps are
 Understanding the problem
 Ascertain the capabilities of computational device
 Choose between exact and approximate problem solving
 Decide on appropriate data structures
 Algorithm design techniques
 Methods for specifying the algorithm
 Proving an algorithms correctness
 Analyzing an algorithm
 Coding an algorithm

8. What is an algorithm design technique?


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.

9. What is pseudocode?
A pseudocode is a mixture of a natural language and programming language constructs to
specify an algorithm. A pseudocode is more precisethan a natural language and its usage often yields
more concise algorithm descriptions.

10. What are the types of algorithm efficiencies?


The two types of algorithm efficiencies are
 Time efficiency: indicates how fast the algorithm runs
 Space efficiency: indicates how much extra memory the algorithm needs

11. Mention some of the important problem types?


Some of the important problem types are as follows
 Sorting
 Searching
 String processing
 Graph problems
 Combinatorial problems
 Geometric problems
 Numerical problems

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 33
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

12. What are the classical geometric problems?

INTRODUCTION ]
The two classic geometric problems are
 The closest pair problem: given n points in a plane find the closest pair among them
 The convex hull problem: find the smallest convex polygon that would include all the points
of a given set.

Prepared By: Mr.


13. What are the steps involved in the analysis framework?
The various steps are as follows
 Measuring the input‘s size
 Units for measuring running time
 Orders of growth

K.Kamaraj, Assistant
 Worst case, best case and average case efficiencies

14. What is the basic operation of an algorithm and how is it identified?


 The most important operation of the algorithm is called the basic operation of the algorithm,
the operation that contributes the most to the total running time.

Professor / CSE
 It can be identified easily because it is usually the most time consuming operation in the
algorithms innermost loop.

15. What is the running time of a program implementing the algorithm?


The running time T(n) is given by the following formula

Department, T(n) ≈copC(n)


cop is the time of execution of an algorithm‘s basic operation on a particular computer and
C(n) is the number of times this operation needs to be executed for the particular algorithm.

16. What are exponential growth functions?

KPRIET,
The functions 2n and n! are exponential growth functions, because these two functions grow
so fast that their values become astronomically large even for rather smaller values of n.

17. What is worst-case efficiency?


The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n,

Coimbatore. Page
which is an input or inputs of size n for which the algorithm runs the longest among all possible
inputs of that size.

18. What is best-case efficiency?


The best-case efficiency of an algorithm is its efficiency for the best-case input of size n,

size.
34
which is an input or inputs for which the algorithm runs the fastest among all possible inputs of that

19. What is average case efficiency?


The average case efficiency of an algorithm is its efficiency for an average case input of size

12. What are the


n. It provides information about an algorithm behavior on a ―typical‖ or ―random‖ input.

classical geometric
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 34
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

20. What is amortized efficiency?


In some situations a single operation can be expensive, but the total time for the entire
sequence of n such operations is always significantly better that the worst case efficiency of that
single operation multiplied by n. this is called amortized efficiency.

21. Define O-notation?


A function t(n) is said to be in O(g(n)), denoted by t(n) ε O(g(n)), if t(n) is bounded above by
some constant multiple of g(n) for all large n, i.e., if there exists some positive constant c and some
non-negative integer n0 such that

T (n) <=cg (n) for all n >= n0

22. Define Ω-notation?


A function t(n) is said to be in Ω (g(n)), denoted by t(n) ε Ω (g(n)), if t(n) is bounded below by
some constant multiple of g(n) for all large n, i.e., if there exists some positive constant c and some
non-negative integer n0 such that
T (n) >=cg (n) for all n >=n0

23. Define θ-notation?


A function t(n) is said to be in θ (g(n)), denoted by t(n) ε θ (g(n)), if t(n) is bounded both
above & below by some constant multiple of g(n) for all large n, i.e., if there exists some positive
constants c1 & c2 and some nonnegative integer n0 such that
c2g (n) <= t (n) <= c1g (n) for all n >= n0

24. Mention the useful property, which can be applied to the asymptotic notations and its use?
If t1(n) ε O(g1(n)) and t2(n) ε O(g2(n)) then t1(n)+t2(n) ε max {g1(n),g2(n)} this property is
also true for Ω and θ notations. This property will be useful in analyzing algorithms that comprise of
two consecutive executable parts.

25. What are the basic asymptotic efficiency classes?


The various basic efficiency classes are
o Constant : 1
o Logarithmic : log n
o Linear : n
o N-log-n : nlog n
o Quadratic : n2
o Cubic : n3
o Exponential : 2n
o Factorial : n!

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 35
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

26. What is algorithm visualization?


Algorithm visualization is a way to study algorithms. It is defined as the use of images to
convey some useful information about algorithms. That information can be a visual illustration of
algorithm‘s operation, of its performance on different kinds of inputs, or of its execution speed versus
that of other algorithms for the same problem.

27. What are the two variations of algorithm visualization?


o The two principal variations of algorithm visualization‖.
o Static algorithm visualization: It shows the algorithm‘s progress through a series of
still images
o Dynamic algorithm visualization: Algorithm animation shows a continuous movie like
presentation of algorithms operations

28. What is order of growth?


Measuring the performance of an algorithm based on the input size n is called order of
growth.

13 marks

1. Explain about algorithm with suitable example (Notion of algorithm).


2. Write short note on Fundamentals of Algorithmic Problem Solving.
3. Discuss important problem types that you face during Algorithm Analysis.
4. Discuss Fundamentals of the analysis of algorithm efficiency elaborately.
5. Explain Asymptotic Notations in detail.
6. List out the Steps in Mathematical Analysis of non recursive Algorithms.
7. Explain in detail about linear search and derive the mathematical efficiency.
8. Explain in detail about Tower of Hanoi and its efficiency.
9. Derive the worst case analysis of merge sort using suitable illustrations.
10. Derive a loose bound on the following equation: f(x) = 35x8 – 22x7 + 14x5 – 2x4 – 4x2 + x -15
11. Give the algorithm to check whether all the elements in a given array of n elements are
distinct. Find worst case complexity of the same.
12. Give the recursive algorithm for finding the number of binary digits in n’s binary
representation, where n is appositive integer. Find the recurrence relation and complexity.
13. Find the closest asymptotic tight bound by solving the recurrence equation T(n) = 8T(n/2) +n2
with (T(1) = 1) using recursion tree methos. [Assume that T(1) belongs to θ (1)]

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 36
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]

14. Suppose W satisfies the following recurrence equation and base case (where c is a constant:
W(n) =c.n + W(n/2) and W(1) = 1. What is the asymptotic order of W(n).
15. Solve the following recurrence relations

Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 37

You might also like