You are on page 1of 14

UNIT-I

Introduction: Algorithm Definition, Algorithm Specification, performance Analysis, Performance measurement,


asymptotic notation, Randomized Algorithms

……………………………………………………………………………………………………………………………….

1. Algorithm Definition
An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all
algorithms must satisfy the following criteria:

Input: Zero or more quantities are externally supplied.

Output: At least one quantity is produced.

Definiteness: Each instruction is clear and unambiguous.

Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a
finite number of steps.

Effectiveness: Every instruction must be vey basic so that it can be carried out, in principle, b a person using
only pencil and paper. It is not enough that each operation be definite, it also must be feasible.

Four Distinct areas of study of algorithms:

1. How to devise algorithms: Techniques–Divide & Conquer, Branch and Bound, Dynamic Programming

2. How to validate algorithms:

The purpose of the validation is to assure us that this algorithm will work correctly independently of the issues
concerning the programming language it will eventually be written in.

3. How to analyse algorithms:

analysis algorithms or performance analysis refers to the task of determining how much computing time and
storage time an algorithm required.

4. How to test a program2 phases

• Debugging - Debugging is the process of executing programs on sample data sets to determine whether
faulty results occur and, if so, to correct them.

• Profiling or performance measurement is the process of executing a correct program on data sets and
measuring the time and space it takes to compute the results.

1 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


2. Algorithm Specification
In formal computer science, one distinguishes between an algorithm, and a program. A program does not
necessarily satisfy the fourth condition. One important example of such a program for a computer is its
operating system, which never terminates (except for system crashes) but continues in a wait loop until more
jobs are entered.

We represent algorithm using a pseudo language that is a combination of the constructs of a programming
language together with informal English statements.

Algorithm Specification: Algorithm can be described in three ways.

1. Natural language like English: When this way is choused care should be taken, we should ensure that each &
every statement is definite.

2. Graphic representation called flowchart: This method will work well when the algorithm is small& simple.

3. Pseudo-code Method: In this method, we should typically describe algorithms as program, which resembles
language like Pascal & algol.

1. Pseudo-Code Conventions:

1. Comments begin with // and continue until the end of line.

2. Blocks are indicated with matching braces { and }.

3. An identifier begins with a letter. The data types of variables are not explicitly declared.

4. Compound data types can be formed with records. Here is an example,

Node. Record

data type – 1 data-1;

data type – n data – n;

node * link;

Here link is a pointer to the record type node. Individual data items of a record can be accessed with → and
period.

5. Assignment of values to variables is done using the assignment statement.

<Variable>:= <expression>;

6. There are two Boolean values TRUE and FALSE.

Logical Operators AND, OR, NOT

Relational Operators <, <=,>,>=, =, !=

2 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


7. The following looping statements are employed.

For, while and repeat-until

While Loop:

While < condition > do

<statement-1>

<statement-n>

For Loop:

For variable: = value-1 to value-2 step do

<statement-1>

<statement-n>

repeat-until:

repeat

<statement-1>

<statement-n>

until<condition>

8. A conditional statement has the following forms.

➢ If <condition> then <statement>


➢ If <condition> then <statement-1>
Else <statement-1>

Case statement:

Case

: <condition-1> : <statement-1>
3 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed
.

: <condition-n>: <statement-n>

: else: <statement-n+1>

9. Input and output are done using the instructions read & write.

10. There is only one type of procedure: Algorithm, the heading takes the form,

Algorithm <Name> (<Parameter lists>)

Example:

1. Algorithm selection_sort (a,n)

2. // Sort the array a[1:n] into non-decreasing order.

3.{

4. for I: =1 to n do

5. {

6. j: =I;

7. for k: =i+1 to n do

8. if (a[k]<a[j])

9. t: =a[I];

10. a[I]: =a[j];

11. a[j]:=t;

12. }

13. }

4 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


3. Performance Analysis
The performance of a program is the amount of computer memory and time needed to run a program. We use
two approaches to determine the performance of a program. One is analytical, and the other experimental. In
performance analysis we use analytical methods, while in performance measurement we conduct experiments.

Time Complexity:

The time needed by an algorithm expressed as a function of the size of a problem is called the time complexity
of the algorithm. The time complexity of a program is the amount of computer time it needs to run to
completion.

The limiting behavior of the complexity as size increases is called the asymptotic time complexity. It is the
asymptotic complexity of an algorithm, which ultimately determines the size of problems that can be solved by
the algorithm.

The Running time of a program

When solving a problem, we are faced with a choice among algorithms. The basis for this can be any one of the
following:

i. We would like an algorithm that is easy to understand code and debug.

ii. We would like an algorithm that makes efficient use of the computer’s resources, especially, one that runs as
fast as possible.

Measuring the running time of a program

The running time of a program depends on factors such as:

1. The input to the program.

2. The quality of code generated by the compiler used to create the object program.

3. The nature and speed of the instructions on the machine used to execute the program,

4. The time complexity of the algorithm underlying the program

Space Complexity:

The space complexity of a program is the amount of memory it needs to run to completion. The space need by
a program has the following components:

5 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


Data space: Data space is the space needed to store all constant and variable values. Data space has two
components:

➢ Space needed by constants and simple variables in program.


➢ Space needed by dynamically allocated objects such as arrays and class instances.

Environment stack space: The environment stack is used to save information needed to resume execution of
partially completed functions.

Instruction Space: Instruction space is the space needed to store the compiled version of the program instructions.
The amount of instructions space that is needed depends on factors such as:

➢ The compiler used to complete the program into machine code.


➢ The compiler options in effect at the time of compilation
➢ The target computer.

The space requirement s(p) of any algorithm p may therefore be written as,

S(P) = c+ Sp(Instance characteristics)

Where ‘c’ is a constant.

Example 2:

Algorithm sum(a,n)

s=0.0;

for I=1 to n do

s= s+a[I];

return s;

• The problem instances for this algorithm are characterized by n, the number of elements to be summed.
The space needed by ‘n’ is one word, since it is of type integer.
• The space needed by ‘a’ is the space needed by variables of type array of floating-point numbers. This is
at least ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be summed.
• So, we obtain Sp(sum(n)>=(n+s))[ n for a[ ], one each for n, I a& s]

Complexity of Algorithms

The complexity of an algorithm M is the function f(n) which gives the running time and/or storage space
requirement of the algorithm in terms of the size ‘n’ of the input data. Mostly, the storage space required by
an algorithm is simply a multiple of the data size ‘n’. Complexity shall refer to the running time of the
algorithm.

The function f(n), gives the running time of an algorithm, depends not only on the size ‘n’ of the input data
but also on the particular data. The complexity function f(n) for certain cases is:

1. Best Case: The minimum possible value of f(n) is called the best case.

2. Average Case: The expected value of f(n).

3. Worst Case: The maximum value of f(n) for any key possible input

6 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


4. Performance measurement
If we want to go from city "A" to city "B", there can be many ways of doing this. We can go by flight, by bus,
by train and also by bicycle. Depending on the availability and convenience, we choose the one which suits us.
Similarly, in computer science, there are multiple algorithms to solve a problem. When we have more than one
algorithm to solve a problem, we need to select the best one. Performance analysis helps us to select the best
algorithm from multiple algorithms to solve a problem.

When there are multiple alternative algorithms to solve a problem, we analyze them and pick the one which is
best suitable for our requirements. The formal definition is as follows...

Performance of an algorithm is a process of making evaluative judgement about algorithms.

It can also be defined as follows...

Performance of an algorithm means predicting the resources which are required to an algorithm to perform its
task.

That means when we have multiple algorithms to solve a problem, we need to select a suitable algorithm to
solve that problem.
We compare algorithms with each other which are solving the same problem, to select the best algorithm. To
compare algorithms, we use a set of parameters or set of elements like memory required by that algorithm,
the execution speed of that algorithm, easy to understand, easy to implement, etc.,

Generally, the performance of an algorithm depends on the following elements...

1. Whether that algorithm is providing the exact solution for the problem?

2. Whether it is easy to understand?

3. Whether it is easy to implement?

4. How much space (memory) it requires to solve the problem?

5. How much time it takes to solve the problem? Etc.,

When we want to analyse an algorithm, we consider only the space and time required by that particular
algorithm and we ignore all the remaining elements.
Based on this information, performance analysis of an algorithm can also be defined as follows...

Performance analysis of an algorithm is the process of calculating space and time required by that algorithm.

Performance analysis of an algorithm is performed by using the following measures...

1. Space required to complete the task of that algorithm (Space Complexity). It includes program space
and data space

2. Time required to complete the task of that algorithm (Time Complexity)

7 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


6. Asymptotic notations
What is Asymptotic Notation?

Whenever we want to perform analysis of an algorithm, we need to calculate the complexity of that
algorithm. But when we calculate the complexity of an algorithm it does not provide the exact amount of
resource required. So instead of taking the exact amount of resource, we represent that complexity in a
general form (Notation) which produces the basic nature of that algorithm. We use that general form
(Notation) for analysis process.

Asymptotic notation of an algorithm is a mathematical representation of its complexity.

Note - In asymptotic notation, when we want to represent the complexity of an algorithm, we use only the
most significant terms in the complexity of that algorithm and ignore least significant terms in the complexity
of that algorithm (Here complexity can be Space Complexity or Time Complexity).

For example, consider the following time complexities of two algorithms...

• Algorithm 1: 5n2 + 2n + 1

• Algorithm 2: 10n2 + 8n + 3

Generally, when we analyze an algorithm, we consider the time complexity for larger values of input data (i.e.
'n' value). In above two-time complexities, for larger value of 'n' the term '2n + 1' in algorithm 1 has least
significance than the term '5n2', and the term '8n + 3' in algorithm 2 has least significance than the term '10n2'.

Here, for larger value of 'n' the value of most significant terms (5n2 and 10n2 ) is very larger than the value of
least significant terms ( 2n + 1 and 8n + 3 ). So, for larger value of 'n' we ignore the least significant terms to
represent overall time required by an algorithm. In asymptotic notation, we use only the most significant
terms to represent the time complexity of an algorithm.

Majorly, we use THREE types of Asymptotic Notations and those are as follows...

1. Big - Oh (O)

2. Big - Omega (Ω)

3. Big - Theta (Θ)

Big - Oh Notation (O)

Big - Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity.
That means Big - Oh notation always indicates the maximum time required by an algorithm for all input
values. That means Big - Oh notation describes the worst case of an algorithm time complexity.
Big - Oh Notation can be defined as follows...

Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If f(n) <= C g(n) for all n
>= n0, C > 0 and n0 >= 1. Then we can represent f(n) as O(g(n)).

f(n) = O(g(n))

Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time
required is on Y-Axis

8 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


In above graph after a particular input value n0, always C g(n) is greater than f(n) which indicates the
algorithm's upper bound.

Example

Consider the following f(n) and g(n)...


f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C g(n) for all values of C > 0 and n0>= 1
f(n) <= C g(n)
⇒3n + 2 <= C n
Above condition is always TRUE for all values of C = 4 and n >= 2.
By using Big - Oh notation we can represent the time complexity as follows...
3n + 2 = O(n)

2. Big - Omege Notation (Ω)

Big - Omega notation is used to define the lower bound of an algorithm in terms of Time Complexity.
That means Big-Omega notation always indicates the minimum time required by an algorithm for all input
values. That means Big-Omega notation describes the best case of an algorithm time complexity.
Big - Omega Notation can be defined as follows...

Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If f(n) >= C
g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as Ω(g(n)).

f(n) = Ω(g(n))

Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time
required is on Y-Axis

9 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


In above graph after a particular input value n0, always C g(n) is less than f(n) which indicates the algorithm's
lower bound.

Example

Consider the following f(n) and g(n)...


f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C > 0 and n0>= 1
f(n) >= C g(n)
⇒3n + 2 >= C n
Above condition is always TRUE for all values of C = 1 and n >= 1.
By using Big - Omega notation we can represent the time complexity as follows...
3n + 2 = Ω(n)

3. Big - Theta Notation (Θ)

Big - Theta notation is used to define the average bound of an algorithm in terms of Time Complexity.
That means Big - Theta notation always indicates the average time required by an algorithm for all input
values. That means Big - Theta notation describes the average case of an algorithm time complexity.
Big - Theta Notation can be defined as follows...

Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If C1 g(n) <=
f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and n0 >= 1. Then we can represent f(n) as Θ(g(n)).

f(n) = Θ(g(n))

Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time
required is on Y-Axis

10 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


In above graph after a particular input value n0, always C1 g(n) is less than f(n) and C2 g(n) is greater than f(n)
which indicates the algorithm's average bound.

Example

Consider the following f(n) and g(n)...


f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <= C2 g(n) for all values of C1 > 0,
C2 > 0 and n0>= 1
C1 g(n) <= f(n) <= C2 g(n)
⇒C1 n <= 3n + 2 <= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 2.
By using Big - Theta notation we can represent the time compexity as follows...
3n + 2 = Θ(n)

7. Randomized Algorithms

An algorithm that uses random numbers to decide what to do next anywhere in its logic is called Randomized
Algorithm. For example, in Randomized Quick Sort, we use a random number to pick the next pivot (or we
randomly shuffle the array). Typically, this randomness is used to reduce time complexity or space complexity
in other standard algorithms.

There are two classes of randomized algorithms.

1. Las Vegas

2. Monte Carlo

Las Vegas algorithms, always output the correct answer, but their running time may vary. The most prime
examples of such algorithms are quicksort and quick select.

11 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


Monte Carlo algorithms have a constant run time but they might output an incorrect answer, typically with a
probability less than O(1/n).

One subclass is random testing. Such algorithms are the Miller-Rabin primality test, Freivalds' algorithm and
checking if two polynomials are equal. These algorithms base themselves on the fact that there are many
witnesses (at least half) if the answer is no, so after performing the test a few times, we can be fairly sure that
we have the correct answer.

The other subclass is optimization Las Vegas algorithms. Such examples are Karger’s min-cut algorithm and the
linear MST algorithm.

Heuristic algorithms. The most known is probably Simulated Annealing. The second most well-known is the
family of Genetic Algorithms. There is extensive bibliography for heuristic algorithms such as Iterated Local
Search (ILS), GRASP, Ant Colony Optimization (ACO), Cuckoo Search, Harmony Search, Artificial Bee Colony
(ABC), Firefly Search and the list never ends.

Another subclass of randomized algorithms is randomized rounding of linear programming relaxation. Many
problems (Min cut, Set Cover, Vertex Cover, TSP) can be expressed as an Integer Programming problem. In
order to get a solution, we formulate the relaxed Linear Programming problem, solve it and perform
randomized rounding on the solution to obtain the final solution.

UNIT-I Questions

1. Explain the various algorithm design methodologies problem.


2. Write about asymptotic notations and give its properties
3. Give the Big–O notation definition and briefly discuss with suitable example
4. Explain the method of determining the complexity of procedure by the step count approach. Illustrate
with an example.
5. Solve the following recurrence relation

6. Define Time and Space Complexity, and calculate the time space complexity for multiplication of two
matrices.
7. Write different pseudo code conventions used to represent an algorithm
8. What do you mean by performance analysis? Give the algorithm for matrix multiplication and find the
time complexity using step–count method.
9. What are the different mathematical notations used for algorithm analysis? Explain them. (Ans:
asymptotic notations)
10. Give the algorithm for transpose of a matrix of size mxn and determine the time complexity of the
algorithm by frequency – count method.

12 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


Topics beyond the syllabus

Step Count Method in Algorithm

The step count method is one of the method to analyze the algorithm. In this method, we count number of
times one instruction is executing. From that we will try to find the complexity of the algorithm.

Time and space complexity are calculated using step count method. Some basic assumptions are;
– There is no count for { and } .
– Each basic statement like ’assignment’ and ’return’ have a count of 1.
– If a basic statement is iterated, then multiply by the number of times the loop is run.
– The loop statement is iterated n times, it has a count of (n + 1). Here the loop runs n times for the true case
and a check is performed for the loop exit (the false condition), hence the additional 1 in the count.

13 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed


Frequency count method in Algorithms

Basically, frequency count is very simple to understand. It means, the number of times the statement is
executed in the program.

Let’s consider some of the program segments

Program Segment A:

x = x+2

Program Segment B:

for k =1 to n do

x = x+2

end

Program Segment C:

for j= 1 to n do

for x = 1 to n do

x = x+2

end

end

Now, let us estimate the frequency count of the statement x = x+2 occurring in the following three program
statements.(A, B, C)

The frequency count of the statement in the program segment A is 1.(It gets executed once)

In the program segment B, the frequency count of the statement is n, since the for loop in which the statement
is embedded executes n(n>=1) times.

In the program segment C, the statement is executed n^2(n>=1) times, since the statement is embedded in a
nested for loop, executing n times each.

14 © www.tutorialtpoint.net Pepared by D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualififed

You might also like