DSA3 - Algorithms & Complexity
DSA3 - Algorithms & Complexity
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
What is Algorithm?
1. Top down
2. Bottom up
3. Incremental
4. Divide and conquer
5. Greedy.
6. Dynamic approach
Top-Down Design
• Divide main program into a main module and its
related module.
• Divide each module in sub module according to s/w
engineering and programming style.
• Division of module continues till elementary process
that can’t be divided.
• In this each module has single entry and single exit
point.
• In c language it is done by functions.
Bottom-Up Approach
• Its principle is to start with specific module and
built into more complex structure, ending at top.
• It is widely used for testing because each of
lowest level function is written and tested first.
• This strategy often resembles a "seed" model, by
which the beginnings are small but eventually
grow in complexity and completeness.
Incremental Approach
• This approach start with single module then
adding module to finally build the whole
algorithm.
• It is piecing together of systems to give rise to
more complex systems.
Divide and Conquer
Dynamic Approach
• It is a technique for efficiently computing
recurrences by storing partial results.
• It is a method of solving problems exhibiting
properties of overlapping sub problems.
Algorithm Analysis
• A priori analysis − This is theoretical analysis of an
algorithm.
– Efficiency of algorithm is measured by assuming that all
other factors. Ex. processor speed, are constant and
have no effect on implementation.
• A posterior analysis − The selected algorithm is
implemented using programming language.
– This is then executed on target computer machine. In this
analysis, actual statistics like running time and space
required, are collected.
Algorithm Complexity
• Time Factor − It is measured by counting the no. of
key operations such as comparisons in sorting
algorithm.
• Space Factor − The space is measured by counting
the maximum memory space required by the algo.
• The complexity of an algorithm f(n) gives the
running time and / or storage space required by the
algorithm in terms of n as the size of input data.
Space Complexity
• It is amount of memory space required by the
algorithm in its life cycle.
• Space required by an algo is equal to the sum of the
following two components:-
– A fixed part is a space required to store certain data and
variables, that are independent of the size of the
problem.
– A variable part is a space required by variables; whose
size depends on the size of the problem. Ex: dynamic
memory allocation, recursion stack space etc.
Time Complexity
• It is amount of time required by the algorithm to run
to completion.
• Time requirements can be defined as a numerical
function T(n), which is measured as the number of
steps, provided each step consumes constant time.
• Ex: Addition of two n-bit integers takes n steps.
Thus, the total computational time is T(n) = c*n,
where c is the time taken for addition of two bits.
– Here, we observe that T(n) grows linearly as input size
increases.
Asymptotic Analysis
• Usually, time required by an
algorithm falls under three types −
• Best Case − Minimum time required
for program execution(Ω).
• Average Case − Average time
required for program execution(θ).
• Worst Case − Maximum time
required for program execution(O).
Omega Notation, Ω
• The Ω(n) is the formal way to express the lower
bound of an algorithm's running time.
• It measures best amount of time an algorithm can
possibly take to complete.
• Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
• The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running
time. It is represented as following −
• θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) =
Ω(f(n)) for all n > n0. }
Big Oh Notation, Ο
• The Ο(n) is the formal way to express the upper
bound of an algorithm's running time.
• It measures the worst case or longest amount of
time an algorithm can possibly take to complete.
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0. }
Common Asymptotic Notations
• Constant−Ο(1)
• Logarithmic−Ο(log n) or O(log(log(n))
• Linear−Ο(n) or O(log(n2))
• Quadratic−Ο(n log n) or Ο(n2)
• Cubic−Ο(n3)
• Polynomial−O(nk), where k>0
• Exponential−O(cn), where c>1. Ex: O(2n)
Common Asymptotic Notations
Common Asymptotic Notations
• Constant − Ο(1) -> When algo takes same amount
of time to compute regardless of input size.
• Ex: 1. If a no. is even or odd
• 2. If an item on an array is null
• 3. Print 1st element from list
• 4. Find a value on map
Common Asymptotic Notations
• Logarithmic − Ο(log n) -> Here, it uses divide-&-
conquer strategy to divide the problems into half.
– Ex: 1. Find a word in dictionary
– 2. Find a person on phone book
f(n) g(n)
n2 log n n(log n)10
n.n log n n.log n (log n)9
n (log n)9
Hence, f(n) = Ω(g(n))
Examples
• If f(n) = n2 & g(n) = 2n, then:
Hence, f(n) = O(g(n)) for n>4
f(n) g(n)
0 for n=0 1 for n=0
1 for n=1 2 for n=1
4 for n=2 4 for n=2
9 for n=3 8 for n=3
16 for n=4 16 for n=4
25 for n=5 32 for n=5
36 for n=6 64 for n=6
THANK YOU !!!
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gm
ail.com
9416910303