Professional Documents
Culture Documents
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
ca+b*f 1
x sqrt(3) 1
xyz5 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)
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
xx+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
i1
while i<=10 Calculate its execution cost
ii+1 equation T(n)
end
i1
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