You are on page 1of 24

Design & Analysis of Algorithms

Lecture#02
Designing Algorithms,
Calculating Execution Cost
Today’s Lecture Contents
Complexity of algorithms
 Developing Algorithms (Few Examples)
 Algorithm Execution (Best / Average / Worst Cases)
 Algorithmic Complexity
 Single Statement
 If – else statement
 Loop Structures
 Linear Search
 Binary Search
 Dry run an algorithm (Bubble Sort)
 Analyze Bubble Sort, Insertion Sort, Selection Sort
Developing Algorithms (one input)
Write down an algorithm that inputs radius of a circle (radius) & prints its
circumference:
Formula:
C=2πr
Developing Algorithms
Write down an algorithm that inputs base & altitude of a triangle; it
prints its area:
Developing Algorithms (No Input)
Write down an algorithm that outputs the largest number between 1-100
that is neither divisible by 5 nor by 7.
Developing Algorithms
Write down an algorithm that receives an arbitrary array of integers as
argument and prints sum of all integers.

An Arbitrary Array
33 10 20 12 8
Developing Algorithms
Write down an algorithm that receives an arbitrary array of integers as
argument and prints the largest of all values.

An Arbitrary Array
33 10 20 12 8
Developing Algorithms
Write down an algorithm that receives an arbitrary array of integers as
argument and prints the pair of values with maximum difference among
them.

An Arbitrary Array
33 10 20 12 8
Developing Algorithms
Write down an algorithm that receives an arbitrary array of integers as
argument and prints the pair of neighboring values with maximum
difference among them.

An Arbitrary Array
33 10 20 12 8
Developing Algorithms
Write down an algorithm prints the values from an array within given range.
Arguments: array, startRange, endRange

An Arbitrary Array
33 10 20 12 8
Execution Cost(time)
Time taken to execute one statement is called its execution cost in terms of
time only.
Execution cost is evaluated using RAM model
This concept can be extended to illustrate the execution cost for an
algorithm
For sake of algorithm analysis, we will always consider individual
statements’ execution cost to be same (taken to be exactly 1)
Statement may be a simple assignment statement or a very complex
expression evaluation
Even a control structure statement has same cost
Execution Cost(time) Examples
Sample Statement Execution Cost
x 1 1
x  (-b + SQRT(b*b-4*a*c))/(2*a) 1
if a < b then 1
while a < 10 1
ca+b*f 1
x  sqrt(3) 1
xyz5 1
Algorithm’s Execution Cost
An algorithm’s execution cost is equal to the sum of execution costs of its
statements. Below is an algorithm that that prints the sum, difference,
product and division of two integers (its parameters)

Proc showOutput(x, y as integers) Cost


Sum  x + y 1
Diff  x – y 1
Prod  x * y 1
Div  x / y 1
Print Sum 1
Print Diff 1
Print Prod 1
Print Div 1
End showOutput

Program’s Cost: T(n) = 1+1+1+1+1+1+1+1 = 8 i.e. CONSTANT


Algorithm’s Execution Cost … Rules
If an algorithm’s execution cost is a fixed value i.e. some numeric value
independent of its parameter values, its cost is considered to be a CONSTANT
If an algorithm or a section of algorithm iterates (loop structure) N times where N
is some structure (typically input) size, execution cost will be the sum of execution
costs (loop statements) of all iterations
For use of if-else structure in an algorithm cost of one path is added into
algorithm’s cost (typically the part with higher cost) as only one part is bound to
be executed (branching)
Even in case of nested loops, execution cost is equal to the sum of costs of all
statements during nested loops execution no matter how much levels of nesting is
used
Algorithm’s Execution Cost … Rules
Execution cost is referred as T(n) where n is typically the size of
input
Execution cost is evaluated as some constant value or some
equation in terms of n (as above)
When execution cost equation has multiple terms, cost is taken to
be equal to the term with highest value
Execution Time Functions (Running Time)
Following is the list of execution cost functions in ascending order:
Functions

Constant
log n
n
n log n
n2
n3
2n
cn
nn
Execution Cost … if-else statement
Cost is equal to the maximum number of statements being executed
(whether it is in if-body or in else-body
Sample Algorithm Execution Cost
if age < 10 then 1
ticket_price  5 1
Else
ticket_price  10 1
Overall Cost:
T(n) = 1 (if statement evaluation) + 1 (if-body or else body) = 2
= Constant time = O (c)
Execution Cost … if-else statement … Example
Sample Statement Execution Cost
if age < 10 then 1
ticket_price = 5 2
with_parent = true
else
ticket_price = 10 1

Execution Cost:
T(n) = 1 (if statement evaluation) + 2 (body with more statements)
= 3 = Constant time = O (c)
Execution Cost … Loop Structures
Sample Statement Execution Cost
tag = 5 1
for counter = 1 to n n+1
Output counter * tag n
end for

Overall Cost:
T(n) = 1 + (n+1)+n = 2n + 2 = O(n)
Execution Cost … Loop Structures
Sample Statement Execution Cost
sum = 0 1
x=1 1
while x < n n
sum  sum + x n-1
xx+1 n-1
End
Output sum 1

Overall Cost:
T(n) = 1 + 1+ n+(n-1) + 1 = 2n + 2 = O(n)
Execution Cost … Loop Structures …. Example
Write down an algorithm generating n terms as below :
0, 1, 1, 2, 3, 5, 8, 13, 21, …….
Calculate its execution cost
Execution Cost … Loop Structures …. Exercise
i1
while i<=10 Calculate its execution cost
ii+1 equation T(n)
end
i1
while i<=n
a 2+g
i  i+1
if i <= n then a  2
else a  10
Execution Cost … Linear Search Example
Proc linearSearch (arr as array, key)
index 1
while arr[index] != key and index <= |arr|
index  index + 1
next
if index <= |arr| then
return index
else
return -1 Calculate the execution cost
end proc
Execution Cost … Binary Search Example
Proc binarySearch (arr as array, key)
mid  (low + high)/2
while arr[mid]!=key and low<=high
if key > arr[mid] then
low  mid +1
else
high mid – 1
end if
mid  (low + high)/2
next
if low <= high then
return mid
else
return -1 Calculate the execution cost
end-if
end proc

You might also like