ADS & A Unit-1 Study Material
ADS & A Unit-1 Study Material
Unit-1
Concepts: Introduction to Algorithms:
Algorithms, Pseudocode for expressing algorithms, Performance Analysis-Space
complexity, Time complexity, Asymptotic Notation- Big oh, Omega, Theta notation and
Little oh notation, polynomial Vs Exponential Algorithms, Average, Best and Worst
Case Complexities, Analysing Recursive Programs.
Introduction
Algorithm:
Algorithm was first time proposed a purshian mathematician Al-Chwarizmi in 825
AD. According to web star dictionary, algorithm is a special method to represent the
procedure to solve given problem.
OR
An Algorithm is any well-defined computational procedure that takes some value or
set of values as Input and produces a set of values or some value as output. Thus algorithm is
a sequence of computational steps that transforms the input into the output.
Formal Definition:
An Algorithm is a finite set of instructions that, if followed, accomplishes a particular
task. In addition, all algorithms should satisfy the following criteria.
1. Input. Zero or more quantities are externally supplied.
2. Output. At least one quantity is produced.
3. Definiteness. Each instruction is clear and unambiguous.
4. Finiteness. If we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
5. Effectiveness. Every instruction must very basic so that it can be carried out, in principle,
by a person using only pencil & paper.
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English:
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.
Pseudo code does not have a specific syntax unlike a program that is
written using syntaxes of a particular language. Hence, pseudo code
cannot be executed on a computer; rather it eases the work of a
programmer as it can be easily understood.
While writing pseudo code one can use control structures that include
for, while, if-then-else, repeat until. These control structures are
common in almost all programming languages and hence can be used
when required. Once we are done with the pseudo code we can easily
modify it into a program in our preferred language.
Algorithm:
1. Start.
2. Read r: radius value as the input given by the user.
3. Calculate the Area: 3.14 * r * r.
4. Display the Area.
5. End.
Pseudo code:
AreaofCircle()
{
BEGIN
Read: Number radius, Area;
Input r;
Area = 3.14 * r * r;
Output Area;
END
}
While Loop:
While < condition >do {
<Statement-1>
..
<Statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<Statement-1>
.
.
<Statement-n>
}
One step is a key word, other Step is used for increment or decrement.
Repeat-until:
Repeat {
<Statement-1>
.
.
<Statement-n>
} until<condition>
8. A conditional statement has the following forms.
(1) If <condition> then <statement>
(2) If <condition> then <statement-1>
Else <statement-2>
Case statement:
Case
{ :<condition-1>:<statement-1>
.
.
:<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 list>)
As an example, the following algorithm fields & returns the maximum of ‘n’ given
numbers:
Algorithm Max(A,n)
// A is an array of size n
{
Result := A[1];
for I:= 2 to n do
if A[I] > Result then
Result :=A[I];
return Result;
}
In this algorithm (named Max), A & n are procedure parameters. Result & I are
Local variables.
Performance Analysis:
Time complexity:
T(P)=C+TP(I)
It is combination of-Compile time (C) independent of instance characteristics -run
(execution) time TP dependent of instance characteristics
Time complexity is calculated in terms of program step as it is difficult to know the
complexities of individual operations.
Asymptotic notation
1. Big oh notation’s
The function f(n)=O(g(n)) (read as “f of n is big oh of g of n”) iff there exist positive
constants c and n0 such that f(n)≤C*g(n) for all n, n≥0 The value g(n)is the upper bound
value of f(n).
Example:
3n+2=O(n) as
3n+2 ≤4n for all n≥2
2. Omega notation’s
The function f(n)=Ω (g(n)) (read as “f of n is Omega of g of n”) iff there exist positive
constants c and n0 such that f(n)≥C*g(n) for all n, n≥0 The value g(n) is the lower bound
value of f(n).
Example:
3n+2=Ω (n) as
3n+2 ≥3n for all n≥1
3. Theta notation:θ
The function f(n)= θ (g(n)) (read as “f of n is theta of g of n”) iff there exist positive
Constants c1, c2 and n0 such that C1*g(n) ≤f(n)≤C2*g(n) for all n, n≥0
Example:
3n+2=θ (n) as
3n+2 ≥3n for all n≥2
3n+2 ≤3n for all n≥2
Here c1=3 and c2=4 and n0=2
4. Little oh: o
The function f(n)=o(g(n)) (read as “f of n is little oh of g of n”) if Lim f(n)/g(n)=0 for all n,
n≥0
n~
Example:
3n+2=o(n2) as
Lim ((3n+2)/n2)=0
n~
(OR)
Define the term algorithm and state the criteria the algorithm should satisfy?
(OR)