Module 1 Introduction Iterative Algorithms & Recurrrence
Module 1 Introduction Iterative Algorithms & Recurrrence
of
Algorithm
s
COURSE CODE: CSC 402
SECOND YEAR SEMESTER IV
COMPUTER ENGINEERING
Marks Distribution
• Termwork: 25 Marks
• Practical: 25 Marks
Syllabus
Introduction
• Performance analysis, space and time complexity, Growth of function, Asymptotic
1 (Big- Oh, Omega, Theta) notation, Mathematical background for algorithm Analysis, 8
Analysis of selection sort, insertion sort.
• Recurrences: The substitution method, Recursion tree method, Master Method
An algorithm is
A sequence of finite instructions
Which converts a given input
Into the desired Output
To solve a well formulated problem
Input Algorith
m Output
Analysis
Which to select?
If you can spend If you don’t want to
You can spend any
some more money spend too much
amount of money for
and want some money but ready to
the comfort
comfort also. lose the comfort
Sort the following 10 numbers
1, 3, 8, 2, 7, 6, 4, 9, 0, 5
Different ways,
Selection Sort
Insertion Sort
Example Quick Sort
Merge Sort
Radix Sort
Shell Sort
…..& so, on
Types of Algorithm
Divide &
Iterative Recursive Greedy
Conquer
Algorithm Algorithm Method
Algorithm
Dynamic
Back Branch & String
Programmin
Tracking Bound Algorithm
g
Iterative Algorithm
An optimization problem is the problem of finding the best solution from all feasible
solutions.
Optimization problems can be divided into two categories, depending on whether the
variables are continuous or discrete:
An optimization problem with discrete variables is known as a discrete optimization, in which
an object such as an integer, permutation or graph must be found from a countable set.
A problem with continuous variables is known as a continuous optimization, in which an
optimal value from a continuous function must be found. They can include constrained
problems and multimodal problems.
Maxima Minima
2 5 30
Profit = 66
3 6 18
Example
B
60
10
20 30
A C E
30 10
D
Dynamic Programming
Branch and bound algorithms are used to find the optimal solution
for combinatory, discrete, and general mathematical optimization
problems. In general, given an NP-Hard problem, a branch and
bound algorithm explores the entire search space of possible
solutions and provides an optimal solution.
String Algorithm
Let f(n) and g(n) be two non-negative Let f(n) and g(n) be two non-negative Let f(n) and g(n) be two non-negative
functions indicating the time functions indicating the time functions indicating the time
complexity of two algorithms. We can complexity of two algorithms. We can complexity of two algorithms. We can
say that g(n) is upper bound of f(n) if say that g(n) is lower bound of f(n) if say that g(n) is tight bound of f(n) if
and 𝑛_0 such that to the right of 𝑛_0 and 𝑛_0 such that to the right of 𝑛_0
there exist some positive constant c there exist some positive constant c there exist some positive constant
and such that to the right of value of
f(n) always lies on or below the value value of f(n) always lies on or above value of f(n) always lies between
𝑓(𝑛)=Ω(𝑔(𝑛)) 𝑓(𝑛)=Θ(𝑔(𝑛))
of c*g(n). the value of c*g(n). It is denoted as, value of inclusively. It is denoted as,
It is denoted as,
Mathematic Background
𝑛
𝑛 ( 𝑛+1 ) 𝑛2 +𝑛
∑ 𝑖=1+2+ 3+ 4+ …+𝑛= ∑ 𝑛=
2
=
2
=O (𝑛2
)
𝑖 =1
𝑛
𝑛𝑘
∑ 𝑖 𝑘
=1+2𝑘
+ 3𝑘
+ 4𝑘
+ …+𝑛𝑘
=
𝑘+1
=O(𝑛𝑘+ 1
)
𝑖 =1
𝑛
𝑘𝑛+1
∑ 𝑘 =𝑘+𝑘 +𝑘 +𝑘 +…+ 𝑘 = 𝑘− 1 =O(𝑘𝑛 )
𝑖 2 3 4 𝑛
𝑖 =1
3 cases of Analysis
It is the case in which the It is the case in which the It is the case in which the
time complexity is minimum. time complexity is maximum. time complexity lies between
This is the most favorable This is the most unwanted the best & worst. This is the
condition or scenario. condition or scenario. case of maximum interest.
Selection Sort
Step Frequency
1 n
2 n-1
3 (n-1)*n
4 (n-1)*(n-1)
5 (n-1)*(n-1)* p(T)
6 (n-1)*(n-1)
7 (n-1)*(n-1)
8 (n-1)*(n-1)
Total
Insertion Sort
¿
Algorithm Cost Frequency
for j 2 to length [A] c1 n
do Key A[j] c2 n-1
I j-1 C3 n-1
while i > 0 and A[i]
> Key
c4
do A[i+1] A[i]
c5
i i -1
c6
Best Case:
In case of best case the while loop will get executed (n-1) times whereas statements within the while will not be
executed. Hence, we can say that
Recursive algorithm that loops through the input to eliminate one item
T(n) = T(n-1) + n
Recursive algorithm that halves the input in one step
T(n) = T(n/2) + c
Recursive algorithm that halves the input but must examine every item in
the input
T(n) = T(n/2) + n O(n)
Recursive algorithm that splits the input into 2 halves and does a constant
amount of other work
T(n) = 2T(n/2) + 1 O(n)
Example Recurrences
Binary Search
Merge Sort
Fibonacci Series
Methods for Solving Recurrences
( )( ) ( )
2 2 2
𝑛 𝑛 𝑛
2
4 16 16
11 1
Recursion Tree Method - Example
2
𝑛
( ) ( ) ( )
2 2 2
𝑛 𝑛 𝑛
4 4 4
( )( ) ( )
2 2 2
𝑛 𝑛 𝑛
2
4 16 16
11 1
Recursion Tree Method - Example
2
𝑛
( ) ( ) ( )
2 2 2
𝑛 𝑛 𝑛
4 4 4
( )( ) ( )
2 2 2
𝑛 𝑛 𝑛
2
4 16 16
11 1
Recursion Tree Method - Example
The Master Method provides a method to solve the recurrence equation of the form
where, a ≥ 1 and b > 1 are constant and f(n) is asymptotically positive function. The
master method requires memorization of the three cases, but the solution of many
recurrence can be determined quite easily.
The above recurrence describes the running time of complexity of an algorithm that
divides a problem of size “n” into “a” subproblems each of size where a & b are
constants. The subproblems are recursively solved in The cost of dividing the problem
& combining the result of the subproblem is described by the function f(n).
can be interpreted as either the floor or celling value.
Master Method Theorem Statement
Let a ≥ 1 and b > 1 be constant, and f(n) be a non-negative function & let T(n) be defined
on non-negative integers by recurrence
Question :
Solve the below recurrence equation using Master Method.
Answer:
Comparing above recurrence equation with we get,
a = 4, b = 2 & f(n) = n.
Question :
Solve the below recurrence equation using Master Method.
Answer:
Comparing above recurrence equation with we get,
a = 2, b = 2 & f(n) = n.
Question :
Solve the below recurrence equation using Master Method.
Answer:
Comparing above recurrence equation with we get,
a = 3, b = 4 & f(n) = .
Click Here