0% found this document useful (0 votes)
13 views31 pages

Daa Unit-I Introduction

Uploaded by

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

Daa Unit-I Introduction

Uploaded by

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

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU

Department of Computer Science and Engineering


DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

UNIT - I

Introduction: Algorithm Definition, Algorithm Specification, performance Analysis,


Performance measurement, Asymptotic notation, Randomized Algorithms.

ALGORITHM

An Algorithm is a finite sequence of instructions, each of which has a clear meaning and
can be performed with a finite amount of effort in a finite length of time.
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, an algorithm terminates
after executing a finite number of instructions.
In addition every algorithm must satisfy the following criteria:
Input: zero or more quantities externally supplied.
Output: At least one quantity is produced.
Definiteness: Each instruction must be clear and unambiguous;
Finiteness: (algorithm terminates after a finite number of steps)
If we trace out the instructions of an algorithm, then for all cases the algorithm will
terminate after a finite number of steps.

Effectiveness: (every instruction must be basic i.e. simple instruction)


Every instruction must be sufficiently basic that it can in principle be carried out by a
person using only pencil and paper. It is not enough that each operation be definite, but it must
also befeasible.

Here we have to define clear and unambiguous statements. Not allow any unnecessary
statements.

UNIT – 1 Page - 1 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Difference between Algorithm and Program:-

Algorithm Program
It refers to a set of instructions for a computer to
It is a well-defined, step-by-step, logical
follow. A program can be an implementation of
procedure for solving a given problem.
many algorithms.
Implementation phase: After getting design we
Design Phase: An algorithm is a design before
need to implement codes to build the machine in
construct a machine in software engineering.
software engineering.
To write an algorithm person has respective
To write programs we need programmers.
Domain Knowledge.

An algorithm provides abstract steps for


The constituents of a program may not be
processing one sequence of related information
conceptually related.
into a different sequence of derived information.

Algorithm are fully independent on Hardware and Programs are fully dependent on Hardware and
operating system. operating system.

It is written in any language like Hindi, French, It could be written in any programming language
Chinese, plain English language and can be such as Python, Java, C++, JavaScript, or any
understood by those from a non- programming other language, depending on the particular task
background. the program is being designed for.

We write computer programs in a computer


It can be expressed in natural language, flow
language. Then a compiler or interpreter
charts, pseudo code, and in a variety of
translates it into a language that is
programming languages..
understandable by any computer system.
We use the analyze techniques to check the logics
We use the testing techniques to check the logics
(errors and bugs) in any algorithm through time
(errors and bugs) in any programming language.
and space complexities.

An algorithm can be executed by a person. A program is always executed by a computer.

An algorithm is done at design time. A program is done at implementation time.

UNIT – 1 Page - 2 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

How to study of an Algorithm:

The study of algorithms includes many important and active areas of research.
 How to devise algorithms?
Creating an algorithm is an art which may never be fully automated.
Example Sorting Approaches
Insertion sort, bubble sort, quick sort, heap sort,
block sort, merge sort, radix sort, tree sort, smooth sort, etc…
Example Searching Approaches
Binary search, linear search, interpolation search, exponential search, etc….

 How to validate algorithms?


 Once an algorithm is devised, it is necessary to show that it computes the correct
answer for all possible legal inputs.
 A program can be written and then be verified.
 How to test a program?
 Debugging is the process of executing programs on sample data sets to determine
whether the faulty results occur and, if so, to correct them.

 Profiling is the process of executing a correct program on data sets and measuring the
time and space it takes to compute the results.
 How to analyze algorithms:
 As an algorithm is executed, it uses the computer's central processing unit (CPU) to
perform operations and its memory to hold the program and data.
 Analysis of algorithms or performance analysis refers to the task of determining how
much computing time and storage algorithms replace.
 We analyze the algorithm based on Time complexity and Space complexity.
 The amount of time need to run the algorithm is called Time complexity.
 The amount of memory need to run the algorithm is called Space complexity.

UNIT – 1 Page - 3 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

1. Theoretical analysis:
 All possible inputs.
 Independent of hardware / software implementation
2. Experimental Study:
 Some typical inputs.
 Depends on hardware / software implementation.

 How to test a program:


Testing a program consists of two phases
1. Debugging
2. Profiling
1. Debugging: It is the process of executing programs on sample data sets to determine
whether faulty results occur and, if so to correct them. However, as E. Dijkstra has
pointed out, “debugging can only point to the presence of errors, but not to the absence".
2. Profiling: 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.

Algorithm Specification:
Algorithm can be described in three ways.

1. Natural language like English:


When this way is choose 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.

UNIT – 1 Page - 4 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

3. Pseudo-code Method:
This method describes algorithms as program, which resembles language like Pascal & algol.

Pseudo-Code Conventions for 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.

UNIT – 1 Page - 5 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

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>
}

UNIT – 1 Page - 6 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

For Loop:
For variable: = value-1 to value-2 step 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>
.
.
:<condition-n> :<statement-n>
:else :<statement-n+1>
}

UNIT – 1 Page - 7 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

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)
Examples:
Algorithm for find max of two 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;
}
Algorithm for Selection Sort:
Algorithm selection sort (a,n)
// Sort the array a[1:n] into non-decreasing order
{
for i:=1 to n do
{
j:=i;
for k:=i+1 to n do
if (a[k]<a[j]) then j:=k;
t:=a[i];
a[i]:=a[j];
a[j]:=t;
}
}

UNIT – 1 Page - 8 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Recursive Algorithms:
A Recursive function is a function that is defined in terms of itself.
 Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the
body. An algorithm that calls itself is Direct Recursive.
 Algorithm „A‟ is said to be Indirect Recursive if it calls another algorithm which in
turns calls „A‟.
 The Recursive mechanism, are externally powerful, but even more importantly, many
times they can express an otherwise complex process very clearly. Or these reasons we
introduce recursion here.
 The following 2 examples show how to develop recursive algorithms.
In the first, we consider the Towers of Hanoi problem, and in the second, we generate
all possible permutations of a list of characters.

1. Towers of Hanoi:

Towers of Hanoi is a problem in which there will be 3 Platinum Towers and some
Golden disks which of decreasing sizes from top to bottom. Besides this there are two other
towers (I and D) in which one tower will be act as destination tower and other act as intermediate
tower (Auxiliary memory). In this problem we have to move the disks from source tower to the
destination tower.

UNIT – 1 Page - 9 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

The conditions included during this problem are:


1) At a time only one disk should be moved.
2) No larger disks should be kept on the smaller disks.
3) Move only upper disk from the towers from one to another. Don’t move middle disk in the tower.

Solution for Towers of Hanoi:


There are three towers S, I, D and there will be three disks present in tower S.

Consider D as destination tower and I as intermediate tower.


The steps involved during moving the disks from source tower A to Destination Tower C using
intermediate tower (Auxiliary Memory) B are as follows.

UNIT – 1 Page - 10 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

The steps involved during moving the disks from source tower A to Destination Tower C using
intermediate tower (Auxiliary Memory) B are as follows.
Step 1: Move the smaller disk which is present at the top of the tower A to C.
Step 2: Then move the next smallest disk present at the top of the tower A to B.
Step 3: Now move the smallest disk present at tower C to tower B
Step 4: Now move the largest disk present at tower A to tower C

Step 5: Move the disk smallest disk present at the top of the tower B to tower A.
Step 6: Move the disk present at tower B to tower C.
Step 7: Move the smallest disk present at tower A to tower C

In this way disks are moved from source tower to destination tower.

ALGORITHM FOR TOWERS OF HANOI:


Algorithm Towers of hanoi (n, S, I, D)
{
if (n==1) then
move “ s->d
else
{
Towers of hanoi (n-1, S, D, I);
Write (“move top disk from tower “,S,“ to top of tower”, D);

Towers of hanoi (n-1, S, I, D);


}
}

UNIT – 1 Page - 11 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

TIME COMPLEXITY OF TOWERS OF HANOI:


The recursive relation is:

Time complexity of towers of Hanoi is O(2^n)

UNIT – 1 Page - 12 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

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.
1. Space Complexity:
The space complexity of an algorithm is the amount of memory it needs to run to
compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs to run to
compilation.

Space Complexity:
Space complexity is the amount of memory used by the algorithm (including the input
values to the algorithm) to execute and produce the result.
Auxiliary Space is the extra space or the temporary space used by the algorithm during
it's execution.

Space Complexity = Auxiliary Space + Input space.


The Space needed by each of these algorithms is seen to be the sum of the following
component.
A fixed part that is independent of the characteristics (eg: number, size)of the inputs and
outputs.
The part typically includes the instruction space (i e. Space for the code), space
for simple variable and fixed-size component variables (also called aggregate) space for
constants, and so on.
The space requirement s(p) of any algorithm p may therefore be written as,

UNIT – 1 Page - 13 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

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


Where “c” is a constant
C - Fixed Part / Input memory / Independent

Sp - Variable part / Auxiliary memory / Dependent

Example 1:

Algorithm abc (a,b,c)


{
return a+b*c+(a+b-c)/(a+b) +4.0;
}
In this algorithm sp=0; let assume each variable occupies one word.
Then the space occupied by above algorithm is >=3.
S(P)>=3

Example 2:
Algorithm sum(a,n)
{

s:=0.0;
for I=1 to n do

s:=s+a[I];

returns;
}
In the above algoritm n, s and occupies one word each and

array „a‟ occupies n number of words

so S(P)>=n+3

UNIT – 1 Page - 14 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Example 3:
ALGORITHM FOR SUM OF NUMBERS USING RECURSION:
Algorithm RSum (a, n)
{
if(n<=0) then
return 0.0;

else
return RSum(a,n-1)+a[n];
}
Space complexity for above algorithm is:
In the above recursion algorithm the space need for the values of n, return address and
pointer to array. The above recursive algorithm depth is (n+1).
To each recursive call we require space for values of n, return address and pointer to
array. Sothe total space occupied by the above algorithm is S(P) >= 3(n+1)

Time Complexity:
The time T(p) taken by a program P is the sum of the compile time and the run time
(execution time)
The compile time does not depend on the instance characteristics. Also we may assume
that a compiled program will be run several times without recompilation. This run time is
denoted by Tp (instance characteristics).
The number of steps any problem statement is assigned depends on the kind of
statement.

For example
Comments, brases // 0 steps
Assignment statements : = 1 step
Expression statements : = 1 step

UNIT – 1 Page - 15 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Interactive statement such as for, while & repeat-untilControl part of the statement.
We can determine the number of steps needed by a program to solve a particular problem
instance in two ways.
We introduce a variable, count into the program statement to increment count with initial
value 0. Statement to increment count by the appropriate amount are introduced into the
program.
This is done so that each time a statement in the original program is executes count is
incremented by the step count of that statement.

Example:-1 Calculate Time and Space complexity

UNIT – 1 Page - 16 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Example:-2 Calculate Time and Space complexity

UNIT – 1 Page - 17 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Time complexity calculating methods.


1. Count Method
2. Frequency count method

1. Count method:-

Example 1:

Algorithm sum(a,n) Algorithm sum(a,n) Algorithm sum(a,n)


{ { {
Countcount+2; // 2units
s:=0.0; s:=0.0;
For I=1to n do count=count+1;
for I=1 to n do
{
for I=1 to n do {
S:=s+a[I]; Countcount+2; //2n units
{
} }
count=count+1;
returns; s:=s+a[I]; }
} count= count+1;
}
count=count+1;
returns;
count=count+1;
}

Time Complexity T(p) = 2n+2

Eliminating constant values, so F(n) = O(n)

UNIT – 1 Page - 18 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Example 2:

Algorithm add(a,b) Algorithm sum(a,n) Algorithm sum(a,n)


{ { {
for i=1to n do for i=1 to n do for i=1 to n do
{ { {
Countcount+2; //2n units
for j=1to n do count=count+1;
{ for j=1to n do for j=1to n do
{
c[i,j] := a[i,j] + b[i,j]; {
Countcount+2; //2n2 units
} count= count+1; }
} c[i,j] := a[i,j] + b[i,j]; Countcount+1; // 1unit
} count= count+1; }
}
count=count+1;
}
count=count+1;
}

Time Complexity T(p) = 2n2+2n+2

Eliminating constant values, so F(n) = O(n2)

UNIT – 1 Page - 19 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

2. Frequency Count method :-

In this mechanism how many no.of statements executed and for each statement how many
no.of times it is executed.

S/e  statement of execution


Frequency  how many no.of times a particular step is executed
Total  S/e * Frequency

Example:- 1

Time Complexity T(p) = 2n+3

Eliminating constant values, so F(n) = O(n)

UNIT – 1 Page - 20 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Example:- 2

Time Complexity T(p) = 2mn+2m+1

Eliminating constant values, so F(n) = O(mn)

Asymptotic Notations:

The best algorithm can be measured by the efficiency of that algorithm. The efficiency of
an algorithm is measured by computing time complexity. The asymptotic notations are used to
find the time complexity of an algorithm.
Asymptotic notations gives fastest possible, slowest possible time and average time of the
algorithm.
The basic asymptotic notations are
1. Big Oh (O)
2. Big Omega (Ω)
3. Theta (Ɵ) Notation
4. Small oh (o)
5. Small Omega (ω)

UNIT – 1 Page - 21 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

1. BIG-OH (O) NOTATION: (Upper Bound) ( worst case)

(i) It is denoted by 'O'.

(ii) It is used to find the upper bound time of an algorithm that means the maximum time taken
by the algorithm.
Definition :
Let f(n), g(n) are two non-negative functions.
If there exists two positive constants c, n0.
such that c>0 and ∀ n>=n0

if f(n)<=c*g(n) , c>0 and ∀ n>=n0

then we say that f(n)=O(g(n))

UNIT – 1 Page - 22 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Example:-

Here f(n), g(n) are two +ve functions, c is a constants and n 0, n are +ve integers
Then prove that f(n)= O(g(n)) for these functions f(n)=2n+2, g(n)= n2.

f(n)<=c*g(n) , c=1 and ∀ n>=3 proved,

so, f(n)= O(g(n)) ∀ n>=3

UNIT – 1 Page - 23 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

2. OMEGA (Ω) NOTATION: (Lower Bound) (Best case)

(i) It is denoted by ' Ω'.

(ii) It is used to find the lower bound time of an algorithm, that means the minimum time taken
by an algorithm.
Definition: Let f(n),g(n) are two non-negative functions. If there exists two positive constants c,
n0. such that c>0and for all n>=n0.
if f(n)>=c*g(n) then we say that f(n)=Ω(g(n))

Example 1: consider f(n)=2n+5, g(n)=2n


Sol : Let us assume as c=1
If n=1:
2n+5 >= 2n => 2(1) + 5 >=2(1) => 7 > = 2 (true)
If n=2:

2n+3>=2n=> 2(2)+5>=2(2)=> 9>=4 (true)

UNIT – 1 Page - 24 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

If n=3:
2n+3>=2n=> 2(3)+5>=2(3)=> 11>=6 (true)
f(n)>=c*g(n), c=1 and ∀ n>=3 proved n>=2 proved

so, f(n)=Ω(g(n)) ∀ n>=3

3. THETA (Θ) NOTATION: (Avg case)


(i) It is denoted by the symbol called as (Θ).

(ii) It is used to find the time in-between lower bound time and upper bound time of
an algorithm.
Definition :
Let f(n),g(n) are two non-negative functions. If there exists positive constants
c1,c2,n0.such that c1>0,c2>0 and for all n>=n0.

UNIT – 1 Page - 25 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

if c1*g(n)<=f(n)<=c2*g(n) then

we say that f(n)=Θ(g(n))

Example :

UNIT – 1 Page - 26 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

4. LITTLE-OH (O)NOTATION:

Definition: Let f(n),g(n) are two non-negative functions


if lim [f(n) / g(n)] = 0 then we say that f(n)=o(g(n))
n->∞

Example:

consider f(n)=2n+3, g(n)=n2

sol : let us

lim f(n)/g(n) = 0
n->∞
= lim (2n+3) / (n2) = lim n(2+(3/n)) / (n2)
n->∞ n->∞
= lim (2+(3/n)) /n
n->∞
=2/∞ = 0

So, f(n)=o(n2)

5. LITTLE OMEGA NOTATION:


Definition: Let f(n) and g(n) are two non-negative functions.
if lim g(n)/f(n) = 0 then we say that f(n)= Ω(g(n))
n->∞

Example : consider f(n)=n^2, g(n)=2n+5

sol :

let us lim g(n)/f(n) = 0


(n->∞)

UNIT – 1 Page - 27 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

= lim (2n+5) /(n^2)


(n->∞)
= lim n(2+(5/n)) / (n^2)
(n->∞)
= lim (2+(5/n)) / n=2/ =0
(n->∞)
So, f(n)= Ω (n).

RANDOMIZED ALGORITHMS:

Randomized algorithms are non-deterministic

 Makes some random choices in some steps of the algorithms


 Output and/or running time of the algorithm may depend on the random choices made
 If you run the algorithm more than once on the same input data, the results may differ
depending on the random choice.

UNIT – 1 Page - 28 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

A randomized algorithm is an algorithm that employs a degree of randomness as part of


its logic. The algorithm typically uses uniformly random bits as an auxiliary input to guide its
behavior, in the hope of achieving good performance in the "average case" over all possible
choices of random bits.

The output or the running time are functions of the input and random bits
choosen.

Basics of Probability Theory:

Probability theory has the goal of characterizing the outcomes of natural or Conceptual
"experiments.” Example of such experiment is include tossing a Coin ten times, rolling a die
three times, playing a lottery, gambling, picking a ball from an urn containing white and red

Each possible outcome of an experiment is called a sample point and the set of all
possible outcomes is known as the sample space S. In this text e assume that S is finite (such a
sample space is called a discrete sample space).An event E is a subset of the sample space S. If
the sample space consists of n sample points, then there are 2n possible events.
Example : [Tossing three coins]

When a coin is tossed, there are two possible outcomes: heads (H) and tails (T).Consider
the experiment of throwing three coins. There are eight possible outcomes: HHH,HHT, HTH,
HTT, THH, THT, TTH, and TTT. Each such outcome is a sample point. The
sets{HHT,HTT,TTT},{HHH,TTT},and { } are three possible events. The third event has no
sample points and is the empty set.
For this experiment there are 28 possible events.

UNIT – 1 Page - 29 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

Types of Randomized Algorithms:

1. Randomized Las Vegas Algorithms:


2. Randomized Monte Carlo Algorithms:

Randomized Las Vegas Algorithms:

 These algorithms always produce correct or optimum result.


 Running time is a random variable
Example: Randomized Quick Sort

In a Las Vegas randomized algorithm, the answer is guaranteed to be correct no matter


which random choices the algorithm makes. But the running time is a random variable, making
the expected running time the most important parameter. Our first algorithm today, Quicksort, is
a Las Vegas algorithm.

Quick sort is one of the basic sorting algorithms normally encountered in a data
structures course.The idea is simple:
• Choose a pivot element p
• Divide the elements into those less than p, those equal to p, and those greater than p
• Recursively sort the “less than” and “greater than” groups
• Assemble the results into a single list

The algorithm as written here is underspecified because we haven‟t said how to choose
the pivot. The choice turns out to be important! Any pivot will have some number k of elements
less than it, and thus at most n − k – 1 elements greater than it. The non-recursive parts of the
algorithm pretty clearly take O(n) time. We thus get a recurrence:

UNIT – 1 Page - 30 G. Monika Devi, Assistant Professor, CSE Department


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
Department of Computer Science and Engineering
DESIGN AND ANALYSIS OF ALGORITHMS (R2031052)
III / IV - CSE – I SEM, 2022-2023

T(n) ≤ T(k) + T(n − k − 1) + O(n)

Randomized Monte Carlo Algorithms:


 Randomized algorithms that always has the same time complexity, but may sometimes
produce incorrect outputs depending on random choices made
 Time complexity is deterministic, correctness is probabilistic Produce correct or optimum
result with some probability.
Running time is deterministic.

Example:

Randomized algorithm for approximate median


Let p = probability that it will produce incorrect output
Design the algorithm such that p ≤ ½
 Run the algorithm k times on the same input

 Probability that it will produce incorrect output on all k trials ≤ 1/ 2𝑘


 Can make the probability of error arbitrarily small by making k sufficiently large

UNIT – 1 Page - 31 G. Monika Devi, Assistant Professor, CSE Department

You might also like