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

Module 1 Chapter 1

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

Module 1 Chapter 1

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

EID305 : DESIGN AND

ANALYSIS OF ALGORITHMS
Algorithm Analysis and Design

Chapter 1 : Introduction to
Algorithms
Definition of Algorithm : An algorithm is a finite sequence of precise
instructions for performing a computation or for solving a problem.

OR
An algorithm is a step by step procedure for solving the given problem.

An algorithm is independent of any programming language and machine.


Algorithm Analysis and Design

Algorithms criteria’s

In addition, all algorithms must satisfy the


following criteria:
1.Input
2.Output
3.Definiteness
4.Finiteness
5.Effectiveness
Algorithm Analysis and Design

Algorithm Specification

1. Pseudocode Conventions : It is used to represent


algorithm using programming construct.
Algorithm Analysis and Design

Algorithm Specification
The following are the conventions used for
writing an algorithm using Pseudocode:
• Procedure
• Comments
• Blocks
• Identifier
• Data types
• Assignment
• Boolean
• Array
• Control statements
Conventions - Procedure

⚫ Procedure – Heading and Body

Syntax: Algorithm name_of_Procedure


(parameter1, parameter2,…. Parameter n)
Algorithm Analysis and Design

Conventions - Blocks
• Indicated with matching braces:
{}.
• Statement delimiter ; •
Examples
– A compound statement
Algorithm Analysis and Design

– The body of a procedure


Conventions - Comments
• Begin with //

• Continue until the end of line


Algorithm Analysis and Design

Conventions - Identifier
• Begins with a letter.
• Identifier can be alphanumeric string
Algorithm Analysis and Design

Conventions- Data Types


• Simple
– Integer, float, char, Boolean, and so on.
• Compound
– Formed with records

node = record
{ Datatype_1 data_1;
--------------
---------------
Datatype_n data_n;
Node *link; }
Algorithm Analysis and Design

Conventions - Assignment
• Assignment of values to variables
• Assignment statement format

<variable> := <expression>
Conventions - Boolean
• Boolean operators
Algorithm Analysis and Design

– True , False.
• Logical operators
- and, or , not
• Relational operators
– <, <= , = , # , >= , >

Conventions - Array
• Array indices start at zero.
• Elements represented using [ ]
Algorithm Analysis and Design

• Example
– (i,j) th element in A denoted as A[i,j].

Conventions - Condition
Conditional statement in two forms:
• Form1
If <condition> then <statement>
• Form2

If <condition> then <statement 1> else <statement 2>


Algorithm Analysis and Design

Conventions - Loops
• Looping statements
– For
– While
– Repeat-until
Algorithm Analysis and Design

Conventions – Loop Contd..

• While Loop
while<condition> While (variable <=n) do
{ {
<statement 1> <statement 1>
………….
--------------<statement
n> …………
} <statement n>
variable :=variable +
Algorithm Analysis and Design

incr;
}

Conventions - Loop Contd..


• For Loop
For variable:=value 1 to value
n step do
{
<statement 1>
:
:
Algorithm Analysis and Design

<statement n>
}

Conventions - Loop Contd..

Repeat-until loop

Repeat
<statement 1>
--------- --
<statement n>
Algorithm Analysis and Design

Until <condition>

Conventions - Input / Output

• Input
– read
• Output
– write
• No format is used to specify the size of input
or output quantities.
Algorithm Analysis and Design

Example Algorithm
• An algorithm to find the maximum in array of n
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;
}s
Algorithm Analysis and Design

2.Recursion Algorithm
• An algorithm to find factorial of n numbers
n=3

3*fact(2)
3*2*fact(1)
3*2*1
6
Algorithm fact(n)
// n is an number
{
if (n = = 1) then return 1; else return n * fact(n-
1);
}
Tower of hanoi

Algorithm
Algorithm MoveTower(disk, source, dest, spare)
{
if disk == 0 then
write(“move disk from source to dest”)
else
{
MoveTower(disk - 1, source, spare, dest)
write(“move disk from source to dest”)
MoveTower(disk - 1, spare, dest, source)
}
}
Algorithm Analysis and Design

Performance Analysis
• The efficiency of an algorithm can be
decided by measuring the performance of an
algorithm

• The performance of an algorithm can be


measured by computing amount of time and
storage requirement
Algorithm Analysis and Design

Space Complexity
• Amount of memory the algorithm needs to
run to completion
• Requirements
– Fixed part
– Variable part
• S(P)= c+Sp
Algorithm Analysis and Design

Example Algorithm
Algorithm abc(a,b,c)
{
return a+b+b*c+(a-c)/(a+b) +2.0; }

S(P) = c + Sp
Sp = 0
Algorithm Analysis and Design

Example Algorithm
Algorithm sum(a,n)
{ s = 0.0; for i:=1 to n do s:=s+a[i];
return s;
}
one word for each integer n,i,s,a[]
S(P) >= n+3
Example Algorithm
algorithm rsum(a,n)
Algorithm Analysis and Design

{
if (n<=0) then return 0
else return rsum(a,n-1)+a[n];
}

•3 unit=local variables, formal parameters, return


address
• S(P)>=3*(n+1)
Algorithm Analysis and Design

Time Complexity
• Amount of computer time needed for the
algorithm to run to completion
• T(P) = C(P) + R(P)
• C(P) is independent of problem instance and
is always not required (recompilation)
• So usually only R(P) is considered
Time Complexity
2 types
⚫ Step Count or Steps per execution(s/e)
⚫ Frequency Count
Computing Instruction Count

• Initialization instructions
• Loops.
– Number of passes of the loops
Algorithm Analysis and Design

• Count the basic operations/steps


Counting Steps
Implementation
• Comments – 0 steps
• Assignment – count 1
• For, while, repeat loops
– Using a variable count
– Control part or terminating part
Algorithm Analysis and Design
Algorithm Analysis and Design

Al
{

}
Algorithm Analysis and
Design

Example 2
Algorithm Analysis and
Design

algorithm rsum(a,n)
{
if (n<=0) then // count:=count+1;
{
return 0.0; // count:=count+1;
}
else
{
return rsum(a,n-1)+a[n]; //count:=count+1;
}
}
Count = ?
Algorithm Analysis and
Design

Computing Complexity
X = trsum(n-1)

trsum(n)= { 2 2+ trsum(n-1) if if n=0 n>0

Known as a recurrence relation.


Solving the Recurrence
trsum(n) = 2+ trsum(n-1)
= 2+2+ trsum(n-2)
Algorithm Analysis and
Design

= 2*2+ trsum(n-2)
= 2*3+ trsum(n-3)
----------
= 2*n + trsum(n-n) trsum(n)
= 2n+2 , n>=0
Algorithm Analysis and Design

Example 1
s/e freq total
Algorithm sum(a,n) 0 0 0
{ 0 0 0 s=0.0; 1 1 1
for i: =1 to n do 1 n+1 n+1
{ 0 0 0 s:=s+a[i]; 1 n n
} 0 0 0 return s; 1 1 1
} 0 0 0
-------
2n+3
Algorithm Analysis and Design

Example 2
S/e freq Total
algorithm add(a,b,c,m,n) 00 0
{ 0 0 0
for i:=1 to m do for 1 m+1 m+1
j:=1 to n do 1 m(n+1) m(n+1)
c[i,j]:=a[i,j]+b[i,j]; 1 mn mn
} 00 0
----------
2mn+2m+1
Algorithm Analysis and Design

Example 3 s/e freq freq total total


n=0 n>0 n=0 n>0
algorithm rsum(a,n) 0 0 0 0 0
{ if (n<=0) 0 0 0 0 0
then 1 1 1 1 1
{ 0 0 0 0 0
return 0.0; 1 1 0 1 0
0 0 0 0 0
}
0 0 0 0 0
else { return rsum(a,n-
0 0 0 0 0
1)+a[n]; 1+x 0 1 0 1+x
} 0 0 0 0 0
} 0 0 0 0 0
-----------------
2 2+x
Algorithm Analysis and Design

Asymptotic Notations
• Describing complexity using order of
growth of an algorithm
• Various notation such as O, Ω, θ
Order of Growth
⚫ 1- Constant
⚫ logN
⚫ N
⚫ NlogN
⚫ N2 ⚫ N3
⚫ 2N
⚫ N!
1<logN<n<nlogn<n2<n3<2n<n!
Algorithm Analysis and Design
O-Notation (Upper Bound)
• f(n) = O(g(n)) if there are positive constants
n0 and c such that to the right of n0, the
value of f(n) always lies on or below c*g(n)
or f(n)<=c*g(n) for all n > n0.
O-Notation
1. Let f(n) =100n + 5. Express f(n) using Big-Oh
f(n)=100n+5 (Replace 5 with n)
C*g(n) = 100n+n C*g(n)
= 101n
f(n) <= C*g(n) n >= n0
100n+5 <= 101n n >= 5 f(n)
€ O(g(n))
2. Let f(n) =5n3 + 6. Express f(n) using Big-Oh
f(n)=5n3+6 (Replace 6 with n3 )
C*g(n) = 5n3+n3 C*g(n)
= 6n3
f(n) <= C*g(n) n >= n 0
5n3+6 <= 6n3 n >= 2
f(n) O(g(n))
Algorithm Analysis and Design

Ω-Notation (Lower Bound)


• f(n) = Ω(g(n)) if there are positive constants
n0 and c such that to the right of n0, the value
of f(n) always lies on or above cg(n). or
f(n)>=c*g(n) for all n > n .
0

Ω-Notation (Lower Bound)


1. Let f(n) =100n +5. Express f(n) using Big-Omega
f(n)=100n+5 (Replace 5 with n)
Algorithm Analysis and Design

C*g(n) = 100n+n C*g(n)


= 101n
f(n) >= C*g(n) n >= n0
100n+5 >= 101n n >= 0 f(n)
€ Ω (g(n))

θ -Notation
• f(n) = θ(g(n)) if there exist positive constants
n0, c1 and c2 such that to the right of n0 the
value of f(n) always lies between c1g(n) and
c2g(n) inclusive. or
c1*g(n)<=f(n)<=c2*g(n)
Algorithm Analysis and Design

θ -Notation
1. Let f(n) =100n +5. Express f(n) using Big-theta
c1*g(n)<=f(n)<=c2*g(n)
100n <= 100n+5 <= 101n
c1=100, c2=105, n0 =1, g(n) = n
f(n) € θ(g(n)) f(n) € θ(n)
Examples
• n2+10n = O(n2)
• 3logn+8 = Ω (logn)
• n2+10n = θ(n3) • 2n+1=Ω (n)
10n2 +12n+5=O(n2)
5n5 +100n2+1000

You might also like