0% found this document useful (0 votes)
124 views13 pages

ADS & A Unit-1 Study Material

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

ADS & A Unit-1 Study Material

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

ADS & A

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.

Areas of study of Algorithm:

1. How to device or design an algorithm– It includes the study of various design


techniques and helps in writing algorithms using the existing design techniques like
divide and conquer.
2. How to validate an algorithm– After the algorithm is written it is necessary to check
the correctness of the algorithm i.e for each input correct output is produced, known
as algorithm validation. The second phase is writing a program known as program
proving or program verification.
3. How to analysis an algorithm–It is known as analysis of algorithms or performance
nalysis, refers to the task of calculating time and space complexity of the algorithm.
4. How to test a program – It consists of two phases . 1. debugging is detection and
correction of errors. 2. Profiling or performance measurement is the actual amount of
time required by the program to compute the result.

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.

What is a pseudo code?


Pseudo code, as the name suggests, is a false code or is a high-level
description of an algorithm. In other words, we can say that pseudo code
is a cooked-up representation of an algorithm. Pseudo code can be easily
understood by someone who has basic knowledge of programming.

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.

Another advantage of pseudo code is that the running time of the


program can be estimated in a general manner using it. A pseudo code
gives us a count of fundamental operations. Also, pseudo code avoids
any ambiguity that might be present in plain text.

Difference between Algorithm and Pseudo code:


An algorithm is a well defined sequence of instructions that provide a
solution to the given problem.
A pseudo code is a method which is used to represent an algorithm.
An algorithm has some specific characteristics that describe the
process.
A pseudo code on the other hand is not restricted to something. It’s only
objective is to represent an algorithm in a realistic manner.

An algorithm is written in plain English or general language.


A pseudo code is written with a hint of programming concepts such as
control structures.
To understand the difference between an algorithm and pseudocode lets
have a look into the following example:

Consider an example of calculating the area of a circle.

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
}

Advantages of Pseudo code:


 Pseudo code improves the readability and is the best way to start implementing an
algorithm.
 A pseudo code works as a rough documentation. It makes the work of a programmer
easy as it helps them understand the code better.
 It acts as a bridge between a program and an algorithm.
 It makes the process of constructing a code easy.

Pseudo-Code for writing/Expressing Algorithms:

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 <, <=,>,>=, =, !=
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 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:

There are many Criteria to judge an algorithm.


– Is it correct?
– Is it readable?
– How it works
Performance evaluation can be divided into two major phases.
1. Performance Analysis (machine independent)
– Space complexity: The space complexity of an algorithm is the amount of memory it needs
to run for completion.
– Time complexity: The time complexity of an algorithm is the amount of computer time it needs to
run to completion.
2 .Performance Measurement (machine dependent).
Space Complexity:
The Space Complexity of any algorithm P is given by S(P)=C+SP(I),C is constant.
1. Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
– It includes instruction space
– Space for simple variables, fixed-size structured variable, constants
2. Variable Space Requirements (SP(I)) Depend on the instance characteristic I
– Number, size, values of inputs and outputs associated with I
– Recursive stack space, formal parameters, local variables, return address
Examples:
Program 1 :Simple arithmetic function
Algorithmabc( a, b, c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
SP(I)=0
HenceS(P)=Constant

Program 2: Iterative function for sum a list of numbers


Algorithm sum( list[ ], n)
{
tempsum = 0;
for i = 0 ton do
tempsum += list [i];
return tempsum;
}
In the above example list[] is dependent on n. Hence SP(I)=n. The remaining variables
are i,n, tempsum each requires one location.
Hence S(P)=3+n

Program 3: Recursive function for sum a list of numbers


Algorithmrsum( list[ ], n)
{
If (n<=0) then
return 0.0
else
return rsum(list, n-1) + list[n];
}
In the above example the recursion stack space includes space for formal parameters
local variables and return address. Each call to rsum requires 3 locations i.e for list[],n
and return address .As the length of recursion is n+1.
S(P)>=3(n+1)

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.

Definition: A program step is a syntactically or semantically meaningful program segment


whose execution time is independent of the instance characteristics.
Program steps are considered for different statements as:
For comment zero steps.
Assignment statement is considered as one step. Iterative statements such as “for, while and
until-repeat” statements, we consider the step counts based on the expression.
Methods to compute the step count:
1) Introduce variable count into programs
2) Tabular method
– Determine the total number of steps contributed by each statement
Step per execution frequency
– add up the contribution of all statements

Program 1.with count statements


Algorithm sum( list[ ], n)
{
tempsum := 0; count++; /* for assignment */
for i := 1 to n do {
count++; /*for the for loop */
tempsum := tempsum + list[i]; count++; /* for assignment */
}
count++; /* last execution of for */
return tempsum;
count++; /* for return */
Hence T(n)=2n+3

Program 2:Recursive sum


Algorithmrsum( list[ ], n)
{
count++; /*for if conditional */
if (n<=0) {
count++; /* for return */
return 0.0 }
else
returnrsum(list, n-1) + list[n];
count++;/*for return and rsum invocation*/
}
T(n)=2n+2

Program for matrix addition

Algorithm add( a[ ][MAX_SIZE], b[ ][MAX_SIZE],


c[ ][MAX_SIZE], rows, cols )
{
for i := 1 to rows do {
count++; /* for i for loop */
for j := 1 to cols do {
count++; /* for j for loop */
c[i][j] := a[i][j] + b[i][j];
count++; /* for assignment statement */
}
count++; /* last time of j for loop */
}
count++; /* last time of i for loop */
}
T(n)=2rows*cols+2*rows+1
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 are:
1. Best Case: The minimum possible value of f(n) is called the best case.
2. Average Case: The average value off(n).
3. Worst Case: The maximum value of f(n) for any key possible input.
The field of computer science, which studies efficiency of algorithms, is known as analysis of
algorithms.
Algorithms can be evaluated by a variety of criteria. Most often we shall be interested
in the rate of growth of the time or space required to solve larger and larger instances of a
problem. We will associate with the problem an integer, called the size of the problem, which
is a measure of the quantity of input data. Rate of Growth:
The following notations are commonly use notations in performance analysis and used to
Characterize the complexity of an algorithm:

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~

Polynomial vs. Exponential Algorithms:


The time complexity (generally referred as running time) of an algorithm is
expressed as the amount of time taken by an algorithm for some size of the input to the
problem. Big O notation is commonly used to express the time complexity of any algorithm
as this suppresses the lower order terms and is described asymptotically. Time complexity is
estimated by counting the operations (provided as instructions in a program) performed in an
algorithm. Here each operation takes a fixed amount of time in execution. Generally time
complexities are classified as constant, linear, logarithmic, polynomial, exponential etc.
Among these the polynomial and exponential are the most prominently considered and
defines the complexity of an algorithm. These two parameters for any algorithm are always
influenced by size of input.
Polynomial Running Time
An algorithm is said to be solvable in polynomial time if the number of steps required
to complete the algorithm for a given input is O(n k) for some non-negative integer k, where n
is the complexity of the input. Polynomial-time algorithms are said to be "fast." Most familiar
mathematical operations such as addition, subtraction, multiplication, and division, as well as
computing square roots, powers, and logarithms, can be performed in polynomial time.
Computing the digits of most interesting mathematical constants, including pi and e, can also
be done in polynomial time.
All basic arithmetic operations ((i.e.) Addition, subtraction, multiplication, division),
comparison operations, sort operations are considered as polynomial time algorithms.
Exponential Running Time
The set of problems which can be solved by an exponential time algorithms, but for
which no polynomial time algorithms is known. An algorithm is said to be exponential time,
if T(n) is upper bounded by 2poly(n), where poly(n) is some polynomial in n. More formally, an
algorithm is exponential time if T(n) is bounded by O(2nk) for some constant k.
Algorithms which have exponential time complexity grow much faster than
polynomial algorithms. The difference you are probably looking for happens to be where
the variable is in the equation that expresses the run time. Equations that show a polynomial
time complexity have variables in the bases of their terms. Examples: n 3 + 2n2 + 1. Notice n
is in the base, NOT the exponent. In exponential equations, the variable is in the exponent.
Examples: 2n. As said before, exponential time grows much faster. If n is equal to 1000 (a
reasonable input for an algorithm), then notice 1000 3 is 1 billion, and 21000 is simply huge! For
a reference, there are about 280 hydrogen atoms in the sun, this is much more than 1 billion.
UNIT-1 10-Marks Questions
1. What is an algorithm? What are the specifications of an algorithm?

(OR)

Define the term algorithm and state the criteria the algorithm should satisfy?

2. What is Pseudocode? how to write Pseudocode for expressing an algorithm?


(OR)

Give brief description about the Pseudocode conventions.

3. What is Performance Analysis and complexity of an algorithm? Explain with


example?
(OR)

Discuss in detail about the performance evaluation of an algorithm.

4. Explain the following Asymptotic Notation : O, Ω, ᴓ with suitable examples?

(OR)

Explain in detail about the various Asymptotic Notations.


5. Difference between Polynomial Vs Exponential Algorithms?
6. Write about the complexity of an algorithm? Explain about the Average, Best and
Worst Case Complexities?
7. What is Recursive Programing? Explain with example?
8. Differentiate between recursive and iterative algorithms?

UNIT-1 2MARKS QUESTIONS


1. What is an algorithm?
Ans. An algorithm is a sequence of unambiguous instructions for solving a problem.
i.e., for obtaining a required output for any legitimate input in a finite amount of time
2. What are the characteristics of an algorithm?
Ans. Every algorithm should have the following five characteristics
(i) Input
(ii) Output
(iii) Definiteness
(iv) Effectiveness
(v) Termination
Therefore, an algorithm can be defined as a sequence of definite and effective
instructions, which terminates with the production of correct output from the given
input.
In other words, viewed little more formally, an algorithm is a step by step
formalization
of a mapping function to map input set onto an output set.
3. What are algorithm design techniques?
Ans. Algorithm design techniques ( or strategies or paradigms) are general
approaches
to solving problems algorithmatically, applicable to a variety of problems from
different
areas of computing. General design techniques are:
(i) Brute force (ii) divide and conquer
(iii) decrease and conquer (iv) transform and concquer
(v) greedy technique (vi) dynamic programming
(vii) backtracking (viii) branch and bound
4. Define the terms: pseudocode, flow chart
Ans. A pseudocode is a mixture of a natural language and programming language like
constructs. A pseudocode is usually more precise than natural language. A flowchart
is a method of expressing an algorithm by a collection of connected geometric shapes
containing descriptions of the algorithm’s steps.
5. How is an algorithm’s time efficiency measured?
Ans. Time efficiency indicates how fast the algorithm runs. An algorithm’s time
efficiency is measured as a function of its input size by counting the number of times
its basic operation (running time) is executed. Basic operation is the most time
consuming operation in the algorithm’s innermost loop.
6. What do you mean by time complexity and space complexity of an algorithm?
Ans. Time complexity indicates how fast the algorithm runs. Space complexity deals
with
extra memory it require. Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size. Basic operation: the
operation that contributes most towards the running time of the algorithm The running
time of an algorithm is the function defined by the number of steps (or amount of
memory) required to solve input instances of size n.
7. What is Big ‘Oh’ notation?
Ans. A function t(n) is said to be in O(g(n)), denoted t(n) O(g(n)) , if t(n) is bounded
above
by some constant multiple of g(n) for all large n, i.e., if there exist some positive
constant c and some nonnegative integers n0 such that
t(n) ≤ cg(n) for all n≥ n0
8. Define Big Omega Notations
Ans. A function t(n) is said to be in Ω(g(n)) , denoted t(n) Є Ω((g(n)) , if t(n) is
bounded below by some positive constant multiple of g(n) for all large n, i.e., if there
exist some positive constant c and some nonnegative integer n0 such that
t(n) ≥cg(n) for all for all n ≥ n0
9. Define Big Theta Notations
Ans. A function t(n) is said to be in Θ(g(n)) , denoted t(n) Є Θ (g(n)) , if t(n) is
bounded both above and below by some positive constant multiple of g(n) for all large
n, i.e., if there exist some positive constants c1 and c2 and some nonnegative integer
n0 such that
c1 g(n) ≤t(n) ≤ c2g(n) for all n ≥ n0
10. How is the efficiency of the algorithm defined?
Ans. The efficiency of an algorithm is defined with the components.
(i) Time efficiency -indicates how fast the algorithm runs
(ii) Space efficiency -indicates how much extra memory the algorithm needs
11. What are the different criteria used to improve the effectiveness of algorithm?
Ans. (i) The effectiveness of algorithm is improved, when the design, satisfies the
following
constraints to be minimum.
Time efficiency - how fast an algorithm in question runs.
Space efficiency – an extra space the algorithm requires
(ii) The algorithm has to provide result for all valid inputs.
12. What is performance measurement?
Ans. Performance measurement is concerned with obtaining the space and the time
requirements of a particular algorithm.
13. What is space complexity and time complexity ?
Ans. The space complexity of an algorithm is the amount of memory it needs to run
to
completion. The time complexity of an algorithm is the amount of computer time it
needs to run to completion.
14. Define best-case step count.
Ans. The best-case step count is the minimum number of steps that can be executed
for
the given parameters.
15. Define worst-case step count.
Ans. The worst-case step count is the maximum number of steps that can be executed
for the given parameters.
16. Define average step count.
Ans. The average step count is the average number of steps executed an instances
with
the given parameters.
17. Define Little “oh”.
Ans. The function f(n) = 0(g(n)) iff
Lim f(n) =0
n →∞ g(n)
18. Write an algorithm using Recursive function to fine sum of n numbers,
Ans. Algorithm Rsum (a,n)
{
If(n≤0) then
Return 0.0;
Else Return Rsum(a, n- 1) + a(n);

You might also like