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

Design and Analysis of Algorithms-1

The document discusses the basics of algorithms. It defines an algorithm as a set of steps to solve a problem and lists key properties like having a finite number of inputs, definite steps, and producing an output. Algorithms are made up of three basic building blocks: sequencing, selection, and iteration. The document uses Euclid's algorithm for finding the greatest common divisor as an example algorithm and walks through the steps. It also covers analyzing algorithms by considering their time and space complexity.

Uploaded by

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

Design and Analysis of Algorithms-1

The document discusses the basics of algorithms. It defines an algorithm as a set of steps to solve a problem and lists key properties like having a finite number of inputs, definite steps, and producing an output. Algorithms are made up of three basic building blocks: sequencing, selection, and iteration. The document uses Euclid's algorithm for finding the greatest common divisor as an example algorithm and walks through the steps. It also covers analyzing algorithms by considering their time and space complexity.

Uploaded by

Tejas Dhage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Basic of An Algorithm

Unit 1 Basics of an Algorithm and its properties

1.0 Introduction 1
1.1 Objectives 2
1.2 Example of an Algorithm 3
1.3 Basics Building Blocks of Algorithms 4
1.3.1 Sequencing, Selection and Iteration 5
1.3.2 Procedure and Recursion 6
1.4 A Survey of Common Running Times 8
1.5 Analysis & Complexity of Algorithms 14
1.6 Types of Computational Problems 17
1.7 Problem Solving Techniques 20
1.8 Deterministic and Stochastic Algorithms 22
1.9 Summary 23
1.10 Solution to Check Your Progress 24
1.11 Further Readings 26

1.0 Introduction

Studying algorithms is an exciting subject in computer science discipline. We


come across a large number of interesting problems and techniques to solve
these problems. Not every problem can be solved with the existing techniques
but majority of them can be. But let us define what is an algorithm first.
According to the mathematician of the ninth century Abdullah Jafar
Muhammad ibn Musa Al-khowarizmi. :

 An algorithm is a set of rules for carrying out calculation either by hand


or on a machine.
 An algorithm is a well-defined computational procedure that takes
input and produces output.
 An algorithm is a finite sequence of instructions or steps (i.e. inputs) to
achieve some particular output.

An algorithm is not a coding instruction rather it is a sequence of tasks written


in common language, if executed produces certain output within a time frame.
An algorithm is completely independent of programming language.

For a good algorithm, it must satisfy the following characteristics or properties:

1. Input: There must be a finite number of inputs for the algorithm.

2. Output: There must be some output produced as a result of execution


of the algorithm.

3. Definiteness: There must be a definite sequence of operations for


transformation of input into output.

1
Introduction to Algorithm 4. Effectiveness: Every step of the algorithm should be basic and
essential.

5. Finiteness: The transformation of input to output must be achieved in


finite steps, i.e. the algorithm must stop, eventually! Stopping may
mean that it should produce the expected output ora response that no
solution is possible.

Following are desirable characteristics of an algorithm:

 The algorithm should be general and is able to solve several cases.


 The algorithms should use resources efficiently, i.e. takes less time
and memory in producing the result.
 The algorithms should be understandable so that anyone can
understand and apply it to own problem.
 The algorithm should follow the uniqueness such that each
instruction of the algorithm is unambiguous and clear.

In this unit, the basics of the algorithms and its designing process will be
discussed. Section 1.3 will define the algorithm and its uses with suitable
example. An algorithm is designed with three basic building blocks, namely
sequencing, selection, and iteration. A detailed discussion about these building
blocks of an algorithm is presented in Section 1.4.

The solution of a problem can be achieved through a number of algorithms. To


check which algorithm is better than the others, a parameter, known as time
complexity, is used. Therefore, time complexity is one of the important
concepts related to algorithm which are discussed in Section 1.5. Section 1.6
deals with the analysis of Algorithms. To compare Algorithms, complexity is
the parameter to be considered. Computing problems are categorized according
to their solving approach. These are discussed in section 1.7. Section 1.8
comprises the solving techniques of various computing problems. In section
1.9 Deterministic and Stochastic Algorithm are discussed. An algorithm is
deterministic if the next output can be predicted/ determined from the input
and the state of the program, whereas stochastic algorithms are random in
nature.

Chapter is summarized in section 1.10. In section 1.11 review questions of the


chapter are covered for check pointing purpose. In Section 1.12 a list of
reference material is enlisted for further readings.

1.1 Objectives

After studying this unit, you should be able to:

 Define and list properties of an algorithm.


 List basics building blocks of algorithms.
 Explain fundamental techniques to design an algorithm.
 Define the time and space complexity of an algorithm.

2
 Differentiate between deterministic and stochastic algorithms. Basic of An Algorithm

1.2 Example of an Algorithm

Let us consider a well-known algorithm to find the Greatest Common Divisor


(GCD) of two given integers.To find GCD of two given integers, we are using
an efficient Euclid's algorithm which is named after the ancient
Greek mathematician Euclid.

The pseudocode for computing GCD (a,b) by Euclid’s method is as follows:

Algorithm GCD-Euclid (a, b)


Input : Two integers a and b
Output: GCD of a and b
begin [start of Algorithm]

{
while b ≠ 0 do
{
r ← a mod b;
a ←b;
b← r;
} [end of while loop]

return (b)
// a and b are two positive numbers where a is dividend and b is a divisor
} [end of algorithm]
1. If b=0, return a and exit
2. else go to step 3
3. Divide a by b and assign remainder to r
4. Assign the value of b to a and the value of r to b and go back to step 1

The above algorithm has two inputs and one output. The algorithm is finite as
it terminates in finite steps and produces the desired result. To observe the
same, let us find the GCD of a = 1071 and b = 462 using Euclid’s algorithm.

Iteration 1:

1. Divide a=1071 by b=462 and store the remainder in r.


r= 1071 % 462 (here, % represents the remainder operator)
r = 147
2. If r = 0, the algorithm terminates and b is the GCD. Otherwise, go to
Step 3.

3
Introduction to Algorithm Here, r is not zero, so we will go to Step 3.
3. The integer will get the current value of integer b and the new value of
integer b will be the current value of r.
Here, a=462 and b=147
4. Go back to Step 1.
Iteration 2:

1. Divide a= 462 by b= 147 and store the remainder in r.


r= 462 % 147 (here, % represents the remainder operator)
r = 21
2. If r = 0, the algorithm terminates and b is the GCD. Otherwise, go to
Step 3.
Here, r is not zero, so we will go to Step 3.
3. The integer a will get the current value of integer b and the new value
of integer b will be the current value of r.
Here, a= 147 and b= 21
4. Go back to Step 1.
Iteration 3:

1. Divide a=147 by b=21 and store the remainder in r.


r= 147 % 21 (here, % represents the remainder operator)
r=0
2. If r = 0, the algorithm terminates and b is the GCD. Otherwise, go to
Step 3.
Here, r is zero, so the algorithm terminates and b is the answer, i.e. 21.

1.3 Basics Building Blocks of Algorithms

An algorithm follows a procedure to write the solution of a problem. It is


designed with five basic building blocks, namely: sequencing, selection,
iteration and recursion. In the first subsection we discuss sequencing, iteration
and selection and in the subsequent subsections procedure & recursion will be
explained.
Sr. N. Building Block Action
1. Sequencing Step by step actions
2. Selection Decision
3. Iteration Repetition or Loop
4. Procedure Set of instructions
5. Recursion Function calling itself

4
1.3.1 Sequencing, Selection and Iteration Basic of An Algorithm

A solution of a problem mainly comprises these three basic blocks only. In an


Algorithm there may be actions to be performed linearly or sequentially as
they written in text. At some times the next action/ statement to be executed is
decided based on some condition called the selection of next action to be
performed. Some set of actions/statements are to be executed more than once
called repetition or the loop.

Let’s consider the example of finding GCD of (a, b) with Euclidian method to
understand the basic building blocks of an algorithm(Algorithm 1)
Sequencing: A problem can be solved by performing some actions in a
sequence [called algorithm], and the order of execution of those actions is
important to ensure the correctness of an algorithm.
If the order of steps of algorithm changes and does not follow the steps as
specified, it will not produce the correct output as expected.

Correct sequence of steps of an algorithm is required to follow


for solving the problem.
Selection: As an algorithm has to be generalized to solve many cases, there
may be situations where the sequence of execution of actions may depend on
some condition. That is, some of the instructions will only be executed if a
given condition satisfies.

It is like the next action to be performed is dependent upon some Boolean


expression. So, using selection, next step to be executed is determined.
Iteration: While solving a problem certain actions may be required to execute
a certain number of times or until a certain condition is met.
Let us re-visit the algorithm to find GCD of (a, b) with Euclidian Method to
understand these concepts:
Algorithm GCD-Euclid (a, b)
Line 1: begin [start of Algorithm]
Line 2: {
Line 3: while b ≠ 0 do
Line 4: {

Line 5: r ← a mod b;
Line 6: a ←b;
Line 7: b← r;
Line 8: } [end of while loop]
Line 9: return (b)

5
Introduction to Algorithm Line 10: } [end of algorithm]
In above algorithm line 5, line 6 and line 7 are example of sequencing, as these
statements are always executed in sequence as written the text.

Line 3 is a selection step as it decides which next step is to be executed among


line 4 and Line 9 according to the condition of the loop.

Line 3 also acts as iteration or the looping statements. Based on the while loop
condition, the Line 4 to line 8 are executed in repeatedly manner.

1.3.2 Procedure & Recursion

Though the above-mentioned three control structures, viz., sequencing,


selection and repetition, are sufficient to express any algorithm, yet the
following two advanced control structures have proved to be quite useful in
facilitating the expression of complex algorithms viz.

(i) Procedure
(ii) Recursion

Procedure
Among a number of terms that are used in stead of procedure are: subprogram
and even function. These terms may have shades of differences in their usage
in different programming languages. However, the basic idea behind these
terms is the same, and is explained next.
It may happen that a sequence of statements frequently occur either in the
same algorithm repeatedly in different parts of the algorithm or may occur in
different algorithms. In such cases, writing repeatedly the same sequence of
statements is a wasteful activity. Procedure is a mechanism to avoid it. For
example we can define GCD(a,b) as a procedure/function only once and can
call it a number of times in a main function with different values of a and b

The general format for defining a procedure might look like this:
Procedure <Name> (<parameter-list>) [: < type>]
<declarations>
<sequence of instructions expected to be occurred repeatedly>
end;

In cases of procedures which pass a value to the calling program another basic
construct (in addition to assignment, read and write) viz., return (x) is used,
where x is a variable used for the value to be passed by the procedure.

Recursion

Next, we consider another important control structure namely recursion. In


order to facilitate the discussion, we recall from Mathematics, one of the ways
in which the factorial of a natural number n is defined:

factorial (1) = 1

factorial (n) = n* factorial (n─1).

6
For those who are familiar with recursive definitions like the one given above Basic of An Algorithm
for factorial, it is easy to understand how the value of (n!) is obtained from the
above definition of factorial of a natural number. However, for those who are
not familiar with recursive definitions, let us compute factorial (4) using the
above definition.
By definition
factorial (4) = 4 * factorial (3).
Again by the definition
factorial (3) = 3 * factorial (2)
Similarly
factorial (2) = 2* factorial (1)
And by definition
factorial (1) = 1
Substituting back values of factorial (1), factorial (2) etc., we get factorial (4) =
4.3.2.1=24, as desired.

This definition suggests the following procedure/algorithm for computing the


factorial of a natural number n:

In the following procedure factorial (n), let fact be the variable which is used
to pass the value by the procedure factorial to a calling program. The variable
fact is initially assigned value 1, which is the value of factorial (1).

Procedure factorial (n)

fact: integer;
begin

fact  1

if n equals 1 then return fact

else begin

fact  n * factorial (n ─ 1)

return (fact)

end;

end;

In order to compute factorial (n ─1), procedure factorial is called by itself, but


this time with (simpler) argument (n ─1). The repeated calls with simpler
arguments continue until factorial is called with argument 1. Successive
multiplications of partial results with 2,3, ….. upto n finally deliver the desired
result.

Definition: A procedure, which can call itself, is said to be recursive


procedure/algorithm. For successful implementation of the concept of
recursive procedure, the following conditions should be satisfied.

7
Introduction to Algorithm (i) There must be in-built mechanism in the computer system that
supports the calling of a procedure by itself, e.g, there may be in-built
stack operations on a set of stack registers.
(ii) There must be conditions within the definition of a recursive procedure
under which, after finite number of calls, the procedure is terminated.
(iii) The arguments in successive calls should be simpler in the sense that
each succeeding argument takes us towards the conditions mentioned in
(ii).
Recursion is an important construct which will be used extensively to solve
sorting algorithms, searching algorithm, matrix multiplications, etc.

Check Your Progress- 1


Q1. What is an Algorithm? What are the important characteristics of an
algorithm?

Q2.What are the building blocks of an Algorithm?

Q3. How to judge an algorithm, whether it is efficient or not?

1.4 A Survey of Common Running Times

For a given problem, more than one algorithm can be designed. However, one
algorithm may be better than the other. To compare two algorithms for a
problem, running time is generally used which is defined as the time taken by
an algorithm in generating the output. An algorithm is better if it takes less
running time. However, this measure should be invariant to any hardware used.
Therefore, the running time of an algorithm can be represented in terms of the
number of operations executed for a given input. More the number of
operations, the larger the running time of an algorithm. So, if we can find the
number of operations required for a given input in an algorithm then we can
measure the running time. This running time of an algorithm for producing the
output is also known as time complexity.

Therefore, two algorithms can be compared in terms of time complexity. an


Algorithm is better compared to others having smaller running time (time
complexity).

Running time of an algorithm is represented as a function T(n), where n is the


input size. Let, an algorithm has a running time T(n) = cn, where c is a
constant. The running time for this algorithm is linearly dependent on the size
of the input. The unit of T(n) is unspecified.

Following are the generalized form of running time for the algorithms:

1. Constant Time (O(k)): If the running time does not depend on the
input size (n) then it is known as constant running time. It can be
represented as

8
where k is a constant Basic of An Algorithm

T(n)=k
k

Input size (n)


Figure (a): T(n) = O(k)

2. Linear Time O(kn): If the time complexity is at most a constant factor


times the size of the input, then it is known as linear time complexity
and is presented as T(n) = O(kn), where k is a constant or O(n). An
algorithm of this type of complexity generally completes the execution
in a single pass. For example to search for minimum value of a given n
numbers in an array the processing can be completed just in one pass.
The following program fragment demonstrates. In this way we perform
constant amount of work in processing each element of an array.

minimum = a[1]
for ί = 2 to n
if a[ί] < minimum
minimum = a[ί]
end
end if

T(n)=kn
k

T(n)=kn

Input size (n)


Figure (b) : T(n) = O(n)

3. Logarithmic Time( log(n)): If the time complexity of an algorithm is


proportional to the logarithm of the input size, i.e. every time the size

9
Introduction to Algorithm of input remains half of that of previous iteration, then it is known as
logarithmic time complexity and depicted as O(log n) time. For
example running time of binary search algorithm is O(log n). O(n log
n) is a very common running time for many algorithms which are
solved through divide and conquer technique such as Merge sort ,
Quick sort algorithms, etc., The common operations among all these
problems are in splitting of the array in equal sized sub-arrays and then
solve it recursively.

T(n)=log(n)

T(n)=log(n)
Figure (c) T(n) = log(n)

Input size (n)


Figure (c) T(n) = log(n)

Quadratic Time: (T(n)= O(n)2)- It occurs when the algorithm is having a pair
of nested loops. The outer loop iterates O(n) time and for each iteration the
inner loop takes O(n) time so we get O(n2) by multiplying these two factors of
n. Practically this is useful for problem for small input size or elementary
sorting algorithms. The worst case time complexity for Bubble sort, Insertion
sort, Selection sort and insertion sort running time complexities are O(n2)

T(n)=n2

Input size (n)

Figure d T(n) =O(n2)

4. Cubic Time: (T(n)= O(n3)): It often occurs when the algorithm is


having there nested loops and each loop has a maximum n iterations.
Let us have one interesting example which requires cubic time
complexity. Suppose we are given n sets: 𝑆1 , 𝑆2 ,…. 𝑆𝑛 . Size of each set

10
is n (ie each set is having n elements). The problem is to find whether Basic of An Algorithm

some pairs of these sets are disjoint, i.e there are no common elements
in these pairs and what is the time complexity ?

T(n)=

Input size (n)


Figure (e) T(n) =O(n3)

Pseudo-code for finding common elements in pair of sets:


for each set 𝑆𝑖 of n elements
for each other set 𝑆𝑗 of n elements
for each element x of 𝑆𝑖
check whether x also belongs to 𝑆𝑗
endfor
if x belongs to both 𝑆𝑖 and 𝑆𝑗
print “ 𝑆𝑗 and 𝑆𝑗 are not disjoint”
endif
endfor
endfor

Time Complexity- The innermost loop takes O(n) time because of n elements.
The second inner loop over 𝑆𝑗 also takes O(n) iterations around the innermost
loop and finally O(n) over 𝑆𝑖 around 𝑆𝑗 iterations. Multiplying all the three
iterations we obtain O(𝑛3 ) time complexity.

5. Polynomial Time: (0(nk)):- This running time is obtained when the


search over all subsets of a set of a size k in performed. To understand
the complexity of running time we have to find how many distinct
subsets of a size k of n elements of a set can be chosen. For that we
have to take a combination of n elements taken k at a time .As an

11
Introduction to Algorithm example let us consider a problem to find an independent set in a graph
which can be defined as a set of nodes which are not joined by any
edge. Let us formulate the independent set problem in the following
way: given a constant k and a graph G having n nodes (vertices) find
out an independent set of a size k.
The brute force method to solve this problem would require searching
for all subsets of k nodes and for each subset it would examine whether
there is an edge connecting any two nodes for each subset s of a size k .
Below is a pseudo-code for finding an independent set.
Pseudo-code
for each subset s of a size k in a graph G
check whether s is an independent set
if yes, print “ s is an independent set
else stop
In this case the outer loop will iterate O(nk) times and it selects all k-
node subsets of n node of the graph. In the inner loop within each
subset it loops for each pair of nodes to find out whether there is an
edge between the pair which will require O( 2 out of k)
pairs of search i.e.O(k2 ) search. Therefore the total time now is O(k2
nk). Since k is a constant, it can be dropped, finally it is O(nk).

T(n)=nk

Input size (n)

Figure (f) :T(n)


=O(nk)
6. Exponential Time: (O( kn)) Beyond the polynomial time complexity
there are other two types of bounds :exponential time O(2N) and
factorial time O(n!): let us refine the independent set problem that we
are given a graph of a size n and want to find out an independent of a
maximum value instead of some constant k which is less than n. The
modified version of the pseudo-code is presented below.

12
Pseudo-code : Basic of An Algorithm

Input G(V,E)
{
for each subset s of n number of nodes
verify whether s is an independent set
if s is the largest among all the subsets examined so for
print “s is the largest independent set ”
end if
end for
}end of code fragment

Figure : Pseudo-code for finding an Independent Set of a graph


In this case the total number of subsets of n elements would be 2𝑛 , so
the outer loop will execute 2𝑛 times instead of 𝑛𝑘 times

Verification of all pairs of subsets i.e. (2n) whether these subsets are
having edges or not and then selecting the maximum will be O(n2) i.e
the total number of pair of subsets. The total running time would be
O(n2*2n) or O(2n ) .O(2n) running time complexity arises when a search
algorithm considers all subsets of n elements.

Factorial time (O(n!): In comparison to the growth of exponential


running time, the growth of factorial time (n!) is more rapid. The
running time of this type of complexity arises in two types of problems:
(i) matching type of problem, for example bipartite matching problem.
Suppose there are n number boys and n number of girls. To find perfect
matching between n number of boys & n number of girls, the first boy
will be compared with n numbers of girls
T(n)=kn

Input size (n)


(d) T(n) =O(kn)

The second boy will be left with (n-1) choices among girls for comparison.
There will be only (n-2) options for matching for the third boy, and so forth.

13
Introduction to Algorithm after array girls Multiplying all these options for n boys we obtain n! ie. n(n-
1) (n-2) .......(2) (1)

(ii) O(n!) also occurs where the problem requires arranging n elements into
a particular order (i.e. a permutation of n numbers). A classic example
is travelling salesman problem. Given a n number of cities with
distance between all pairs of cities with the following conditions (i) the
salesman can start the tour with any city but must conclude the tour
with the starting city only (ii) all cities must be visited only once
except the one where from the tour starts. The problem is to find out
the shortest tour covering all n cities. Applying a brute force approach
to find out the solution, a salesman has to explore n! searches which
will take O(n!). Note that a salesman can pick up any city among n
cities to start the tour. Next it will have (n-1) cities to pickup the second
city on the tour. There will be (n-2) cities to pick up the third city at the
next stage and so forth. Multiplying all these choices we get n! i.e. n (n-
1) (n-2) ....(2) (1)

1.5 Analysis & Complexity of Algorithm

The term "analysis of algorithms" was introduced by Donald Knuth. It has


become now an important computer science discipline whose overall objective
is to understand the complexity of an algorithm in terms of time complexity
and storage requirement.
System performance is directly dependent on the efficiency of algorithm in
terms of both the time complexity as well the memory. An algorithm designed
for time sensitive application takes too long to run can render its results of no
use.

Suppose M is an algorithm with n the input data size. The time and space used
by the algorithm M are the two main measures for the efficiency of M. The
time is measured by counting the number of key operations, for example, in
case of sorting and searching algorithms, the number of comparisons is the
number of key operations. That is because key operations are so defined that
the time for the other operations is much less than or at most proportional to
the time for the key operations. The space is measured by counting the
maximum of memory needed by the algorithm.

The complexity of an algorithm M is the function f(n), which give the running
time and/or storage space requirement of the algorithm in terms of the size n of
the input data. Frequently, the storage space required by an algorithm is simply
a multiple of the data size n. In general the term “complexity” given anywhere
simply refers to the running time of the algorithm. There are 3 cases, in
general, to find the complexity function f(n):
 Worst-case − The maximum number of steps taken on any instance of
size a.
 Best-case − The minimum number of steps taken on any instance of
size a.
 Average case − An average number of steps taken on any instance of
size a.
14
Basic of An Algorithm
Average case: The value of which is in between maximum and minimum
for any possible input. Generally the Average case implies the expected value
of
The analysis of the average case assumes a certain probabilistic distribution
for the input data; one such assumption might be that all possible
permutations of an input data set are equally likely. The Average case also
uses the concept of probability theory. Suppose the numbers ……
occur with respective probabilities 𝑝1 , 𝑝2 , … … 𝑝𝑘, Then the expectation or
average value of E is given by

To understand the Best, Worst and Average cases of an algorithm, consider a


linear array , where the array A contains n-elements. Students may
you are having some problem in understanding. Suppose you want either to
find the location LOC of a given element (say ) in the given array A or to
send some message, such as LOC=0, to indicate that does not appear in A.
Here the linear search algorithm solves this problem by comparing given ,
one-by-one, with each element in A. That is, we compare with A[1], then A[2],
and so on, until we find LOC such that 𝑥 = 𝐴[𝐿𝑂𝐶].

Algorithm: (Linear search)


/* Input: A linear list A with n elements and a searching element .
Output: Finds the location loc of 𝑥 in the array A (by returning an index )
or return loc=0 to indicate 𝑥 is not present in A. */

1. [Initialize]: Set K=1 and loc=0.


2. Repeat step 3 and 4 while (loc = = 0 & & K < n)
3. If (𝑥 = = A[K])
4. {
5. loc=K
6. K=K+1;
7. }
8. If (loc = = 0)
9. Print (“ 𝑥 is not present in the given array A);
10. Else
11. Print f(“𝑥 is present in the given array A at location A [loc]);
12. Exit [end of algorithm]

Analysis of linear search algorithm

The complexity of the search algorithm is given by the number C of


comparisons between x and array elements A[K].

Best case: Clearly the best case occurs when x is the first element in the array
A. That is . In this case

Worst case: Clearly the worst case occurs when x is the last element in the
array A or is not present in given array A (to ensure this we have to search

15
Introduction to Algorithm entire array A till last element). In this case, we have
.

Average case: Here we assume that searched element appear array A, and it
is equally likely to occur at any position in the array. Here the number of
comparisons can be any of numbers 1,2,3,…,n, and each

number occurs with the probability then

It means the average number of comparisons needed to find the location of x is


approximately equal to half the number of elements in array A. From above
discussion, it may be noted that the complexity of an algorithm in the average
case is much more complicated to analyze than that of worst case. Unless
otherwise stated or implied, we always find and write the complexity of an
algorithm in the worst case.

There are three basic asymptotic notations which are used to


express the running time of an algorithm in terms of function, whose domain is
the set of natural numbers N={1,2,3,…..}. These are:

 0(Big – ‘Oh’) [This notation is used to express Upper bound (maximum


steps) required to solve a problem]
 Ω0(Big – ‘Oh’) [This notation is used to express Lower bound i.e.
minimum (at least) steps required to solve a problem]
 Θ (‘Theta’) Notations.[ Used to express both Upper & Lower bound, also
called tight bound]

Asymptotic notation gives the rate of growth, i.e. performance, of the run time
for “sufficiently large input sizes” and is not a measure of the
particular run time for a specific input size (which should be done
empirically). O-notation is used to express the Upper bound (worst case); Ω-
notation is used to express the Lower bound (Best case) and Θ- Notations is
used to express both upper and lower bound (i.e. Average case) on a function.

We generally want to find either or both an asymptotic lower bound and upper
bound for the growth of our function. The lower bound represents the best
case growth of the algorithm while the upper bound represents the worst case
growth of the algorithm.

Check Your Progress-2

Q1 What are the common running times for the algorithms?


Q2 Define independent set problem.

16
Basic of An Algorithm
1.6 Types of Computational Problems

Although the types of problems in computing are infinite but can be


categorized into a few areas to make it easy for researchers to address the
problem categories by applying the existing algorithms in those categories.
Following are the some commonly known problem types:
 Sorting
 Searching
 Graph problems
 Combinatorial problems
 Geometric problems
 Numerical problems
For the above-mentioned categories, certain standard input sets are defined as
benchmarking sets to analyse the algorithms.

Sorting
The sorting is the process to arrange the given set of items in a certain order,
assuming that the nature of the items allow such an ordering. For example,
sorting a set of numbers in increasing or decreasing order and sorting the
character strings, like names, in an alphabetical order.

Researchers have published a large number of different sorting algorithms,


targeting various types of items. A sorting algorithm does not necessarily work
optimally for all types of items list. Some of them are good in terms of
resource usage, while some are fast in terms of computing. The efficiency of a
sorting algorithm also depends on the type of input, some work well on
randomly ordered inputs, whereas others perform better on already almost-
sorted lists. Some of the sorting algorithms perform well for lists residing in
the memory, while others perform optimally for sorting large files stored on a
secondary disk.

As of now in common, the best sorting algorithm takes nlogn comparisons to


sort an item list of n items.

For any sorting algorithm following two characteristics are desirable:

1. Stability
2. In-place.

A sorting algorithm is called stable if it does not change the relative positions
of any two equal items of input list. Say, in an input list, there are two equal
items at positions i and j where i< j, then the final position of these items in the
sorted list should also be k and l respectively, such that k<l. That is there
should not be any swapping among these equal items and should not
interchange their position with each other.

17
Introduction to Algorithm A sorting algorithm needs extra memory space to store elements during the
swapping process. For small set of items in a list, this constraint is not
observable but, for an input list of large elements the required storage space is
considerable large. An algorithm is said to be in-place if the required extra
memory is not markable.

Searching
Searching is finding an element, referred as search key, in a given set of items
(may have the redundant value). Searching is one of the most important and
frequently performed operation on any dataset/database.

Searching is one of the most favourite areas of researches in the field of


algorithm analysis. No single searching performs optimally to all situations.
Some algorithms are faster but consume more memory; some are very fast but
only with specific input set; and so on.

While designing an algorithm for searching problem, it is highly influenced by


the nature of underlying data. The data, static in nature, has to be addressed
differently than the dynamic one in nature with addition or deletion from the
data set of an item.

String Processing
Exponential increase in the textual data due to the various applications over
social media and blogs, string-handling algorithms become a current area of
research . Another reason for blooming strings rather text processing is the
kind of data available and the use of the data. Most of the text data is used to
predict the interest of people involving direct or indirect monetary benefits for
commercial organizations specially e-commerce sectors. One of the most
widely used search engine (Google) is also based on string processing.

Sting matching is one of the string processing problems.

Graph Problems
It is always favourable for researchers to map a computational problem to a
graph problem. Many computational problems can be solved using graph.
Most of the computer network problems can be solved using graph Algorithms
efficiently. Problems like: visiting all the nodes of a graph (broadcasting in
network), routing in networks (finding the minimum cost path, i.e. the shortest
path, path with minimum delay etc. can be solved efficiently with graph
algorithms.

At the same time some of the graph problems are computationally not easy,
like the travelling salesman and the graph-coloring problems. The Travelling
Salesman Problem (TSP) is used to cover n cities by taking the shortest path
and not visiting any of the city more than once. The graph-coloring
problem seeks to color all the vertices of a graph with minimum number colors
such that, no two adjacent vertices having the same color. While solving TSP
cities can be considered as the vertices of the graph. Event scheduling could be

18
one of the problems which can be solved using graph coloring algorithm. Basic of An Algorithm
Considering events to be represented by the vertices, there exists an edge
between two events only if the corresponding events cannot be scheduled at
the same time.

Combinatorial Problems
These types of problems have a combination of solutions i.e. more than one
solution are possible. The aim of the combinatorial problems is to find
permutations, combinations, or subsets, satisfying the given conditions. The
travelling salesman problem, independent set and the graph-coloring problems
can be categorized as examples of combinatorial problems . From both
theoretical as well as practical point of view, the combinatorial problems are
considered to be one of the most difficult problems in computing. Due to the
combinatorial type of solutions, it becomes very difficult to handle the
problems with big size inputs sets. The number of combinatorial objects (the
output solution) grows rapidly with the problem’s size.

Another problem is the lack of the known algorithms to solve this type of
problems within considerable amount of time. Moreover, it is believed by the
most computer scientists that such algorithms do not exist.

Even though there exist solutions to some combinatorial problems, but these
are considered as fortunate exceptions to the rule. The problem of finding
shortest-path in a network is one of such exceptions.

Geometric Problems
Some of the applications of Geometric algorithms are computer graphics,
robotics and tomography. These algorithms are based upon geometric objects
such as points, lines, and polygons. The geometry procedures are developed to
solve various geometric problems, like construction shapes of geometric
objects, triangles, circles, etc., using ruler and compass.

Following are widely known classic problems of computational geometry:

1. The closest-pair problem


2. The convex-hull problem

The closest-pair problem is to find the closest pair out of a given set of points
in the plane.

In the convex-hull problem, the smallest convex polygon is to be constructed


so that it includes all the points of a given set.

Numerical Problems
Problems of numerical computing nature are simultaneous linear equations
(linear algebra), differential equations, definite integration, and statistics.
Most of the numerical problems could be solved approximately.

19
Introduction to Algorithm The biggest drawback of numerical algorithms is the accumulation of errors
over the multiple iterations, due to rounding off the approximated result at
each iteration.

1.7 Problem Solving Techniques

Brute Force and Exhaustive Search Approach

Brute force and exhaustive search algorithms are known as blind algorithms.
These algorithms create and evaluate every possible solution and take
exponential and factorial running time. In the previous section we discussed
three such problems: independent set, bipartite matching and travelling
salesman problem. It can be understood by a simple example of finding the
correct four letters word using Brute Force and Exhaustive Search Algorithms.
When the problem size increases, its possible outcomes increases
exponentially which is practically impossible to find.

Divide and Conquer Approach

This is one of the popular approaches in which a problem is divided into


smaller subproblems. These subproblems are further divided into smaller
subproblems until they can no longer be divided. It is a top down approach in
which the algorithm logically progresses from the initial instance down to the
smallest sub-instances via intermediate sub-instances.

An algorithm, following divide & conquer technique, involves following


steps:

Step 1. Divide the problem (top level) into a set of sub-problems (lower
level).
Step 2. Solve every sub-problem individually by recursive approach.
Step 3. Merge the solution of the sub-problems into a complete solution of
the problem.

Following are the examples of the problems that can efficiently be solved
using divide and conquer approach.

 Binary Search.
 Quick Sort.
 Merge Sort.
 Strassen's Matrix Multiplication.
 Closest Pair of Points.

20
Greedy Technique Basic of An Algorithm

Using Greedy approach, optimization problems are solved efficiently. In an


optimization problem, the given set of input values are either to be maximized
or minimized (called as objective), subject to some constraints or conditions.

 Greedy algorithm always picks the best choice (greedy approach) out of
many at a particular moment to optimize a given objective.
 The greedy method chooses the local optimum at each step and this
decision may result in overall non-optimum or optimum solution.
 The greedy approach doesn't always produce the optimal solution rather
produces very nearby solution to the optimal solution.

Consequently, Greedy algorithms are often very easy to design for the
optimisation problems. Following are some of the examples of the greedy
approach.

 Kruskal’s Minimum Spanning Tree


 Prim's Minimal Spanning Tree
 Dijkstra's shortest path
 Knapsack Problem

Dynamic Programming

Dynamic Programming approach is a bottom-up approach which involves


finding solution of all sub-problems ,saving these partial results, and then
reusing them to solve larger sub-problems until the solution to the original
problem is obtained. Reusing the results of sub-problems (by maintaining a
table of results) is the major advantage of dynamic programming because it
avoids the re- computations (computing results twice or more) of the same
problem. Thus Dynamic programming approach takes much less time than
naïve or straightforward methods.

The working style of dynamic programming is similar to divide and conquer


approach. Both solve a problem by breaking it down into a several sub
problems that can be solved recursively. The drawback of divide and conquer
method is the calling of a recursive function repeatedly which is overcome in
dynamic programming by maintaining a table to store the results. It is
dynamically decided whether to call a function or retrieve values from the
table, that’s why the word dynamic is used in it. The Dynamic programming
approach is faster than the divide and conquer method as the redundancy of
calling functions with same result are omitted in it.

0-1 Knapsack and Subset-sum problem are the examples of dynamic


programming.

21
Introduction to Algorithm Branch and Bound

Branch and bound algorithm efficiently solves the discrete and combinatorial
optimization problems. In branch-and-bound algorithm, a rooted tree is formed
with the full solution set at the root. The algorithm explores the branches of
this tree, representing the subsets of the solution set. A candidate solution of a
root node is considered as a branch only if it is better than the already explored
solution, and is discarded if it cannot produce a better solution than the best
one found so far by the algorithm. Branch and Bound algorithm are methods
for solving global optimization problems. However, it is much slower. Indeed,
it often leads to exponential time complexities in the worst case. On the other
hand, if applied carefully, it can lead to algorithms that run reasonably fast on
average. The general idea of B&B is a BFS-like search for the optimal
solution, but not all nodes get expanded (i.e., their children generated).

Randomized Algorithms

In a randomized algorithm, a random number is selected at any stage of the


solution and is used for computation of the solution, that’s why it is called as
randomized algorithm. In other words it can be said that algorithms that make
random choices for faster solutions are known as randomized algorithms. For
example, in the Quick sort algorithm, a random number can be generated and
considered as a pivot. In other example, a random number can be chosen as
possible divisor to factor a large number.

Backtracking Algorithm

Backtracking algorithm is like creating checkpoints while exploring new


solutions. It works analogues to depth-first search. It searches all the possible
solutions. During the exploration of solutions, if a solution doesn't work, it
backtrack to the previous place and then find the other alternatives to get the
solution. If there are no more choice points the search fails.

1.8 Deterministic and Stochastic Algorithm

Algorithms can be categorized either deterministic or stochastic in nature. An


algorithm is deterministic if the next output can be predicted/ determined from
the input and the state of the program, whereas stochastic algorithms are
random in nature. Problems with unpredictable result cannot be solved using
deterministic approach. For example, the next output of a card shuffling
program of blackjack game should not be predictable by players even if the
source code of the program is visible. Pseudorandom number generator
method can be compromised and is often not sufficient to ensure the true
randomness. The random number generated using Pseudorandom number
generator method might be precisely predicted. To avoid this problem, use of
a cryptographically secure pseudo-random number generator with an
unpredictable random seed to initialize the generator can be better way.
22
A hardware random number generator is the best way for achieving real Basic of An Algorithm
randomness.

Check Your Progress-3

Q1. Map the Set B with corresponding problems listed in Set B.

S.
Set A S.N. Set B
N.

Arranging a list of numbers in


1 Sorting Problem A
ascending order.

Geometric
2 B Finding an item in set items.
Problem

Finding the shortest path between


3 Graph Problem C
two nodes.

Numerical Finding Euler graph for a given


4 D
Problem graph.

Searching Finding the solutions of a given set


5 E
Problem of linear equations.

Finding the pair of points (from a set


6 String Processing F of points) with the smallest distance
between them.

7 G Match a word in a paragraph.

Q2. Differentiate Dynamic programming and backtracking problem solving


approach. What problems can be solved by each technique?

Q3. When should the deterministic approach of problem solving to be


avoided? Explain with an example.

Q4 What problems can be solved through greedy technique?

1.9 Summary

In computation, an algorithm is independent from a programming language.


Algorithm is designed to understand and analyze the solution of a
computational problem. Running time of an algorithm is one of the most
widely used parameter to judge the performance of an algorithm. Running time
is computed as a sum of the frequency of the number of instructions execution.
Algorithm, taking less time to produce the desired output, is desirable. Space
complexity is measurement of memory storage used during the execution. For
a solution to a computational problem, there may exist multiple algorithms .
One has to understand which one is the appropriate one..

23
Introduction to Algorithm Problems generally fall into any one of the commonly known categories,
namely sorting, searching, string processing, graph problems, combinatorial
problems, geometric problems, numerical problems, but may be overlapping
also. Many graph theoretic problems can be also combinatorial problems

Similar type of problems can be solved with similar approach. Some of the
commonly used problems solving techniques are Brute Force and Exhaustive
search approach, Divide and Conquer approach, Greedy technique, Dynamic
Programming, Branch and Bound, Randomized algorithms, and Backtracking
algorithm.

Another widely known categories of Algorithms, based on the type of the


inputs used, are Deterministic and Stochastic Algorithms. If the output of an
algorithm can be predicted by looking at the input, such algorithms are called
as deterministic in nature. While stochastic algorithms are random in nature,
means the output cannot be determined from the input.

1.10 Solution to Check Your Process

Check Your Progress-1

Q1. What is an Algorithm? What are the important characteristics of an


algorithm?

Solution: An Algorithm is a set of steps to solve a problem or a set of


problems. Also, an algorithm is a step by step procedure to solve logical and
mathematical or computational problems. A recipe is a good example of
Algorithm. To cook a dish a recipe says what to be done step by step.

Important characteristics of an algorithm are: input, output, definiteness,


effectiveness and finiteness

Q2.What are the building blocks of an Algorithm?

Solution: Selection, iteration, procedure and recursion

Q3. How to judge an algorithm, whether it is efficient or not?

Solution: An algorithm should be both correct and efficient. The efficiency of


an algorithm is defined in terms of the resource usage to yield a correct answer
which is mainly time complexity in the worst case .Many factors influence the
performance of the algorithm such as data structure and types of algorithms
applied for solving a problem.

Check Your Progress -2

Q1 What are the common running times for the algorithms?


Ans. Constant time, linear time, logarithmic time, polynomial time,
exponential time and factorial time
Q2 Define independent set problem.

24
Ans. Independent set problem can be defined as a set of nodes which are not Basic of An Algorithm

joined by any edge. One way to formulate this problem is that given a constant
k and a graph G having n nodes (vertices) find out an independent set of a size
k.

Check Your Progress-3

Q1. Map the Set B with corresponding problems listed in Set B.

S.
Set A S.N. Set B
N.

Arranging a list of numbers in


1 Sorting Problem A
ascending order.

Geometric
2 B Finding an item in set items.
Problem

Finding the shortest path between


3 Graph Problem C
two nodes.

Numerical Finding Euler graph for a given


4 D
Problem graph.

Searching Finding the solutions of a given set


5 E
Problem of linear equations.

Finding the pair of points (from a set


6 String Processing F of points) with the smallest distance
between them.

7 G Match a word in a paragraph.

Solution: The correct match is as follows:

1 – A, 2 – F, 3 – C, D, 4 – E, 5 – B, 6 – G.

Q2. Differentiate Dynamic programming and backtracking problem solving


approach. What problems can be solved by each technique?

Solution: Dynamic programming is a technique widely used to solve


optimization problem. Optimization problem is used to find the either
minimum or maximum result (a single result) out of all possible outcomes. In
backtracking method, a brute force approach is used, hence it is not used for
optimization problem. Backtracking approach is suitable for solving problems
having multiple results and out of which, all or some of them are acceptable.

Following problems can be solved using backtracking approach:

25
Introduction to Algorithm  Eight queen puzzle
 Map coloring
 Sudoku
Following problems can be solved using dynamic programming approach:

 All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-


Ford
 0/1 knapsack problem
 Chain matrix multiplication
 Traveling salesman problem
Q3. When should the deterministic approach of problem solving to be
avoided? Explain with an example.

Solution: An algorithm is deterministic if the next output can be predicted/


determined from the input and the state of the program. Deterministic approach
of problem solving technique is not suitable for the problems with
unpredictable result. For example, the next output of a card shuffling program
of blackjack game should not be predictable by players even if the source code
of the program is visible.

Q4 What problems can be solved through greedy technique?


Ans

 Fractional knapsack problem


 Minimum cost spanning tree
 Single source shortest path algorithm

1.11 Further Readings

1. Introduction to Algorithms. Thomas H. Cormen, Charles E. Leiserson,


Ronald L. Rivest, and Clifford Stein.
2. Algorithm Design, Jon Kleinberg and Eva Tardos, Pearson

26

You might also like