




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An analysis of heap sort algorithm and its implementation, including the build-max-heap and max-heapify procedures. Additionally, it covers related algorithms such as merge sort, binary search algorithm (recursive), and longest common subsequence using dynamic programming. The document also includes examples and explanations of each algorithm.
What you will learn
Typology: Study notes
1 / 105
This page cannot be seen from the preview
Don't miss anything!
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
An algorithm is any well-defined computational procedure that takes some values or set of values as
input and produces some values or set of values as output.
An algorithm is a sequence of computational steps that transform the input into the output.
An algorithm is a set of rules for carrying out calculation either by hand or on a machine.
An algorithm is an abstraction of a program to be executed on a physical machine (model of
Computation).
All the algorithms should satisfy following five properties,
Input: There is zero or more quantities supplied as input to the algorithm.
Output: By using inputs which are externally supplied the algorithm produces at least one quantity
as output.
operations must be clear and unambiguous. This implies that each of these operations must be
definite; clearly specifying what is to be done.
Finiteness: Algorithm must terminate after some finite number of steps for all cases.
Effectiveness: The instructions which are used to accomplish the task must be basic i.e. the human
being can trace the instructions by using paper and pencil in every way.
Unordered collection of distinct elements. Can be represented either by property or by value.
The number of elements in a set is called cardinality or size of the set, denoted |S| or sometimes n(S).
The two sets have same cardinality if their elements can be put into a one-to-one correspondence.
If we do want to take the number of occurrences of members into account, we call the group a
multiset.
For example, {7} and {7, 7} are identical as set but {7} and {7, 7} are different as multiset.
A set contains infinite elements. For example, set of negative integers, set of integers, etc …
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
For two sets A and B, we say that A is a subset of B, written A B, if every member of set A is also a
Set A is a proper subset of B, written A B, if A is a subset of B and not equal to B. That is, a set A is
proper subset of B if A B but A B.
The sets A and B are equal, written A = B, if each is a subset of the other.
Let A and B be sets. A = B if A B and B A.
Let A be the set. The power of A, written P(A) or 2
A
, is the set of all subsets of A. That is, P (A) = {B: B
For example, consider A= {0, 1}.
The power set of A is {{}, {0}, {1}, {0, 1}}.
The union of A and B, written A B, is the set we get by combining all elements in A and B into a
single set.
Let A and B be sets. A and B are disjoint if A ∩ B =.
The intersection of set A and B, written A ∩ B, is the set of elements that are both in A and in B. That
For example, let A = {1, 2, 3} and B = {2, 4, 6, 8}. The set difference A - B = {1, 3} while B-A = {4, 6, 8}.
All set under consideration are subset of some large set U called universal set.
Given a universal set U, the complement of A, written A', is the set of all elements under
consideration that are not in A.
Formally, let A be a subset of universal set U.
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
So that by using this concept we can say all functions are considered as relation also but not vice
versa.
Different relations can observe some special properties namely reflexive, symmetric , transitive and
Anti symmetric.
When for all values is true then relation R is said to be reflexive.
E.g. the equality (=) relation is reflexive.
When for all values of x and y, is true. Then we can say that relation R is symmetric.
Equality (=) relation is also symmetric.
When for all values of x, y and z, x R y and y R z then we can say that x R z, which is known as transitive
property of the relation.
E.g. the relation grater then > is transitive relation.
If x>y and y>z then we can say that x>z i.e. x is greater than y and y is greater than z then x is also
greater than z.
When for all values of x and y if x R y and y R x implies x=y then relation R is Anti - symmetric.
Anti-symmetric property and symmetric properties are lookalike same but they are different.
E.g. consider the relation greater than or equal to if x y and y x then we can say that y = x.
A relation is Anti-symmetric if and only if x X and (x, x) R.
Equivalence Relation plays an important role in discrete mathematics.
Equivalent relation declares or shows some kind of equality or say equivalence.
The relation is equivalent only when it satisfies all following property i.e. relation must be reflexive,
symmetric and transitive then it is called Equivalence Relation.
E.g. Equality ‘=’ relation is equivalence relation because equality proves above condition i.e. it is
reflexive, symmetric and transitive.
o Reflexive: x=x is true for all values of x. so we can say that ’=’ is reflexive.
o Symmetric: x=y and y=x is true for all values of x and y then we can say that ‘=’ is symmetric.
o Transitive: if x=y and y=z is true for all values then we can say that x=z. thus’ =’ is transitive.
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
There are two basic quantifiers.
P (a) is the preposition, if P (a) is give expected result for all values of a in the universe of
discourse. The universal quantification of P (a) is denoted by, ( ). So is called as for all i.e. it
is the Universal Quantifier.
P (a) is the preposition, if there exits an element a in the universe of discourse such that P (a) is
giving expected result. Here the Existential Quantification of P (a) is represented by
( )
called as for some, i.e. it is Existential Quantifier.
A vector, u, means a list (or n-tuple) of numbers:
u = (u 1 , u 2 ,... , un)
Where ui are called the components of u. If all the ui are zero i.e., ui = 0, then u is called the zero
vector.
Given vectors u and v are equal i.e., u = v, if they have the same number of components and if
corresponding components are equal.
If two vectors, u and v, have the number of components, their sum, u + v, is the vector obtained by
adding corresponding components from u and v.
u + v = (u 1 , u 2 ,... , un) + (v 1 , v 2 ,... , vn)
= (u 1 + v 1 + u 2 + v 2 ,... , un + vn)
The product of a scalar k and a vector u i.e., ku, is the vector obtained by multiplying each component
of u by k:
ku = k(u 1 , u 2 ,... , un)
= ku 1 , ku 2 ,... , kun
It is not difficult to see k(u + v) = ku + kv where k is a scalar and u and v are vectors
The dot product or inner product of vectors u = (u 1 , u 2 ,... , un) and v = (v 1 , v 2 ,... , vn) is denoted by
u.v and defined by
u.v = u 1 v 1 + u 2 v 2 +... + unvn
The norm or length of a vector, u, is denoted by ||u|| and defined by
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
iv. A + (-A) = (-A) + A = 0
v. k(A + B) = kA + kB
vi. (k + I)A = kA + I A
vii. (k I)A = k(I A)
viii. I A = A
Suppose A and B are two matrices such that the number of columns of A is equal to number of rows
of B. Say matrix A is an m×p matrix and matrix B is a p×n matrix. Then the product of A and B is the
m×n matrix whose ij-entry is obtained by multiplying the elements of the i
th
row of A by the
corresponding elements of the j
th
column of B and then adding them.
It is important to note that if the number of columns of A is not equal to the number of rows of B,
then the product AB is not defined.
Let A, B, and C be matrices and let k be a scalar. Then
i. (AB)C = A(BC)
ii. A(B+C) = AB + AC
iii. (B+C)A = BA + CA
iv. k(AB) = (kB)B = A(kB)
The transpose of a matrix A is obtained by writing the row of A, in order, as columns and denoted by
AT. In other words, if A - (Aij), then B = (bij) is the transpose of A if bij - aji for all i and j.
It is not hard to see that if A is an m×n matrix, then AT is an n×m matrix.
For example if
A = then AT =
If the number of rows and the number of columns of any matrix are same, we say matrix is a square
matrix, i.e., a square matrix has same number of rows and columns. A square matrix with n rows and
n columns is said to be order n and is called an n-square matrix.
The main diagonal, or simply diagonal, of an n-square matrix A = (aij) consists of the elements a ( 11 ), a
( 22 ), a ( 33 )... a (mn).
The n-square matrix with 1's along the main diagonal and 0's elsewhere is called the unit matrix and
usually denoted by I.
The unit matrix plays the same role in matrix multiplication as the number 1 does in the usual
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
multiplication of numbers.
A I = I A = A for any square matrix A.
The term inequality is applied to any statement involving one of the symbols <, >, ≤, ≥.
Examples of inequalities are:
i. x≥ 1
ii. x + y + 2z > 16
iii. p
2
2
iv. a
2
For example, - 3 ≤-1 implies - 3+4≤-1 + 4.
For example, 2≤3 implies 2(4) ≤3(4).
For example, 3≤9 implies 3(-2) ≥9(-2).
For example, - 1/2≤2 and 2≤8/3 imply - 1/2≤8/3.
By solution of the one variable inequality 2x + 3≤ 7 we mean any number which substituted for x yields
a true statement.
For example, 1 is a solution of 2x + 3≤7 since 2(1) + 3 = 5 and 5 is less than and equal to 7.
By a solution of the two variable inequality x - y≤5 we mean any ordered pair of numbers which when
substituted for x and y, respectively, yields a true statement.
For example, (2, 1) is a solution of x - y≤5 because 2-1 = 1 and 1≤5.
By a solution of the three variable inequalities 2x - y + z≥3 we means an ordered triple of number
which when substituted for x, y and z respectively, yields a true statement.
For example, (2, 0, 1) is a solution of 2x - y + z≤3.
A solution of an inequality is said to satisfy the inequality. For example, (2, 1) is satisfy x - y≤5.
One Unknown
A linear equation in one unknown can always be stated into the standard form
ax = b
Where x is an unknown and a and b are constants. If a is not equal to zero, this equation has a unique
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
Measuring Space complexity
Measuring Time complexity
Input size
Computing Best case, Average case, Worst case
Computing order of growth of algorithm.
Divide and Conquer
Greedy Approach
Dynamic Programming
Branch and Bound
Backtracking
Randomized Algorithm
When we have a problem to solve, there may be several suitable algorithms available. We would
obviously like to choose the best.
Analyzing an algorithm has come to mean predicting the resources that the algorithm requires.
Generally, by analyzing several candidate algorithms for a problem, a most efficient one can be easily
identified.
Analysis of algorithm is required to decide which of the several algorithms is preferable.
There are two different approaches to analyze an algorithm.
techniques and trying them on various instances with the help of computer.
resources needed by each algorithm as a function of the size of the instances considered. The
resources of most interest are computing time (time complexity) and storage space (space
complexity). The advantage is this approach does not depend on programmer, programming
language or computer being used.
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
Analysis of algorithm is required to measure the efficiency of algorithm.
Only after determining the efficiency of various algorithms, you will be able to make a well informed
decision for selecting the best algorithm to solve a particular problem.
We will compare algorithms based on their execution time. Efficiency of an algorithm means how
fast it runs.
If we want to measure the amount of storage that an algorithm uses as a function of the size of the
instances, there is a natural unit available Bit.
On the other hand, is we want to measure the efficiency of an algorithm in terms of time it takes to
arrive at result, there is no obvious choice.
This problem is solved by the principle of invariance , which states that two different
implementations of the same algorithm will not differ in efficiency by more than some multiplicative
constant.
Suppose that the time taken by an algorithm to solve an instance of size n is never more than cn
seconds, where c is some suitable constant.
Practically size of instance means any integer that in some way measures the number of components
in an instance.
Sorting problem: size is no. of items to be sorted.
Graph: size is no. of nodes or edges or both involved.
We say that the algorithm takes a time in the order of n i.e. it is a linear time algorithm.
If an algorithm never takes more than cn
2
seconds to solve an instance of size n, we say it takes time
in the order of cn
2
i.e. quadratic time algorithm.
Some algorithms behave as Polynomial : (
n
or n
k
) , Exponential : (c
n
or n!), Cubic : (n
3
or 5n
3
+n
2
Logarithmic: (log n or n log n)
In the worst case analysis, we calculate upper bound on running time of an algorithm.
We must know the case that causes maximum number of operations to be executed.
e.g. For Linear Search, the worst case happens when the element to be searched (x in the above code) is not
present in the array.
When x is not present, the search () functions compares it with all the elements of arr[] one by one.
In average case analysis, we take all possible inputs and calculate computing time for all of the inputs.
Sum all the calculated values and divide the sum by total number of inputs. We must know (or predict)
distribution of cases.
For the linear search problem, let us assume that all cases are uniformly distributed (including the case of x
not being present in array).
So we sum all the cases and divide the sum by (n+1)
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
It is similar to the accounting method.
Instead of representing prepaid work as credit it represents prepaid work as “Potential Energy”.
Suppose initial data structure is D
0
for n operations D
0,
1,
2
n
be the data structures let C
1,
2
n
denotes actual cost.
φ is potential function that maps Data structure Di to a real number φ (Di)
Asymptotic notation is used to describe the running time of an algorithm.
It shows order of growth of function.
For a given function g(n), we denote by Θ( g ( n )) the set of functions
Θ( g ( n )) = { f(n) : there exist positive constants c
1
, c
2
and n
0
such that
0 ≤ c
1
g(n) ≤ f (n) ≤ c
2
g(n) for all n ≥ n
0
Because Θ( g ( n )) is a set, we could write f(n) € Θ( g ( n )) to indicate that f(n) is a member of Θ( g ( n )).
This notation bounds a function to within constant factors. We say f ( n ) = Θ( g ( n )) if there exist
positive constants n
0
, c
1
and c
2
such that to the right of n
0
the value of f ( n ) always lies between c
1
g ( n )
and c
2
g ( n ) inclusive.
Figure a gives an intuitive picture of functions f(n) and g(n). For all values of n to the right of n
0
, the
value of f(n) lies at or above c
1
g(n) and at or below c
2
g(n). In other words, for all n ≥ n
0
, the value of
f(n) is equal to g(n) to within a constant factor.
We say that g(n) is an asymptotically tight bound for f(n).
For a given function g(n), we denote by Ο( g ( n )) the set of functions
Ο( g ( n )) = { f(n) : there exist positive constants c and n
0
such that
0 ≤ f (n) ≤ cg(n) for all n ≥ n
0
We use Ο notation to give an upper bound on a function, to within a constant factor. For all values of
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
n to the right of n 0
, the value of the function f(n) is on or below g(n).
This notation gives an upper bound for a function to within a constant factor. We write f ( n ) = O ( g ( n ))
if there are positive constants n 0
and c such that to the right of n 0
, the value of f ( n ) always lies on or
below cg ( n ).
We say that g(n) is an asymptotically upper bound for f(n).
Example:
Let f(n)=n
2
and g(n)=
n
n f(n)=n
2
g(n)=
n
1 1 2 f(n) < g(n) Here for n ≥ 4 we have
behavior f (n) ≤ g(n)
Where n 0
f(n) = g(n)
f(n) > g(n)
f(n) = g(n)
f(n) < g(n)
f(n) < g(n)
f(n) < g(n)
For a given function g(n), we denote by Ω( g ( n )) the set of functions
Ω ( g ( n )) = { f(n) : there exist positive constants c and n 0
such that
0 ≤ cg(n) ≤ f (n)for all n ≥ n 0
Ω Notation provides an asymptotic lower bound. For all values of n to the right of n 0
, the value of
the function f(n) is on or above cg(n).
This notation gives a lower bound for a function to within a constant factor. We write f ( n ) = Ω( g ( n )) if
there are positive constants n
0
and c such that to the right of n
0
, the value of f ( n ) always lies on or
above cg ( n ).
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
for i=1 to n
for j=1 to n
b = a * c
end
end
C1: n+
C2: n+
n
C3: n
T(n) =C1+C2 +C
=n+1+n(n+1)+n(n) = 2n
2
+2n+
=O(n
2
Insertion Sort works by inserting an element into its appropriate position during each iteration.
Insertion sort works by comparing an element to all its previous elements until an appropriate
position is found.
Whenever an appropriate position is found, the element is inserted there by shifting down remaining
elements.
The running time of an algorithm on a particular input is the number of primitive operations or
"steps" executed.
A constant amount of time is required to execute each line of our pseudo code. One line may take a
different amount of time than another line, but we shall assume that each execution of the i
th
line
takes time c
i
, where c
i
is a constant.
The running time of the algorithm is the sum of running times for each statement executed; a
statement that takes c i
steps to execute and is executed n times will contribute c i
n to the total
running time.
Let the time complexity of selection sort is given as T(n), then
T(n) = C
1
n+ C
2
(n-1)+ C
3
(n-1)+ C
4
5
6
7
(n-1).
Procedure insert (T[1…. n ]) cost times
for i ← 2 to n do C1 n
x ← T[i] C2 n- 1
j ← i - 1 C3 n- 1
while j > 0 and x < T[j] do C4 ∑
T[j+1] ← T[j] C5 ∑
j ← j - 1
end
C ∑ - 1
T[j + 1] ← x
end
C7 n- 1
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
Where,
Take j = 1
T(n) = C 1
n + C 2
(n-1) + C 3
(n-1) + C 4
(n-1) + C 5
(n-1)+ c 7
(n-1)
1
2
3
4
7
) n (C 2
3
4
7
= an b
Thus, T(n) = Θ(n)
T (n) = C
1
n+ C
2
(n-1)+ C
3
(n-1)+ C
4
5
6
7
(n-1).
1
n+ C 2
n+ C 3
n+ C 4
5
6
4
5
6
7
n-C 2
3
= n
2
4
5
6
)+n(C
1
2
3
7
4
5
6
2
3
7
= an
2
Thus, T(n) = Θ(n
2
2
Time complexity of insertion sort
Best case Average case Worst case
O(n) O (n
2
) O (n
2
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
In the bubble sort, the consecutive elements of the table are compared and if the keys of the two
elements are not found in proper order, they are interchanged.
It starts from the beginning of the table and continue till the end of the table. As a result of this the
element with the largest key will be pushed to the last element’s position.
After this the second pass is made. The second pass is exactly like the first one except that this time
the elements except the last are considered. After the second pass, the next largest element will be
pushed down to the next to last position.
T(n)=C1 (n+1) + C2 ∑ ( ) + C3 ∑ ( ) + C4 ∑ ( )
Take i = 1
T(n) = C
1
n + C
1
2
n + C
3
n – C
3
4
n – C
4
1
2
3
4
) n (C
2
3
4
7
= an b
Thus, T(n) = Θ(n)
= C 1
n+ C 1
n+ C 2
(
( )
) + C 3
n – C 3
(
( )
) +C 4
n – C 4
(
( )
)
2
/n
2
3
/n
2
4
/n
2
2
/n–C 3
/n–C 4
/n]+C 1
2
3
4
= an
2
+bn+c
= Θ (n
2
2
Consider the following numbers are stored in an array:
Original Array: 32,51,27,85,66,23,13,
Pass 1 : 32,27,51,66,23,13,57,
Pass 2 : 27,33,51,23,13,57,66,
Pass 3 : 27,33,23,13,51,57,66,
Pass 4 : 27,23,13,33,51,57,66,
Procedure bubble (T[1…. n ]) cost times
for i ← 1 to n do C1 n+
for i ← 1 to n-i do C
∑ ( )
if T[i] > T[j] C
∑ ( )
T[i] ↔ T[j] C
∑ ( )
end
end
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 – Analysis and Design of Algorithms
Pass 5 : 23,13,27,33,51,57,66,
Pass 6 : 13,23,27,33,51,57,66,
Selection Sort works by repeatedly selecting elements.
The algorithm finds the smallest element in the array first and exchanges it with the element in the first
position.
Then it finds the second smallest element and exchanges it with the element in the second position and continues
in this way until the entire array is sorted.
Procedure select ( T [1….n] ) Cost times
for i←1 to n- 1 do C1 n
minj ← i ; minx ← T[i] C2 n- 1
for j←i+1 to n do C
∑ ( )
if T[j] < minx then minj ← j C4 ∑
minx ← T[j] C5 ∑
T[minj] ← T[i] C6 n- 1
T[i] ← minx C7 n- 1
T(n)=C
1
n+ C
2
(n-1)+ C
3
4
5
6
(n-1)+ C
7
(n-1)
1
n+ C
2
n+ C
6
n+ C
7
n+ C
3
4
5
3
4
5
2
6
7
=n(C 1
2
6
7
3
4
5
) + n
2
3
4
5
2
6
7
= an
2
+bn+c
Time complexity of insertion sort
Best case Average case Worst case
O(n
2
) O (n
2
) O (n
2