0% found this document useful (0 votes)
63 views19 pages

Asymptotic Notations and Complexity Analysis

This document provides an overview of asymptotic notations and complexity analysis of algorithms. It defines key terminology like algorithms, time and space complexity measures, and asymptotic notations like Big-O, Big-Omega, Big-Theta. Examples are given of analyzing the insertion sort algorithm and classifying algorithms based on their growth rates as logarithmic, linear, quadratic, polynomial and exponential. Common asymptotic notations and their properties and uses are explained.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views19 pages

Asymptotic Notations and Complexity Analysis

This document provides an overview of asymptotic notations and complexity analysis of algorithms. It defines key terminology like algorithms, time and space complexity measures, and asymptotic notations like Big-O, Big-Omega, Big-Theta. Examples are given of analyzing the insertion sort algorithm and classifying algorithms based on their growth rates as logarithmic, linear, quadratic, polynomial and exponential. Common asymptotic notations and their properties and uses are explained.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lovely Professional University, Punjab

Data Structures

Lecture: Asymptotic Notations & Complexity Analysis


Contents
• Basic Terminology
• Complexity of Algorithm
• Asymptotic Notations
• Review Questions
Basic Terminology
• Algorithm: is a finite step by step list of well-defined
instructions for solving a particular problem.

• Complexity of Algorithm: is a function which gives running


time and/or space requirement in terms of the input size.

• Time and Space are two major measures of efficiency of an


algorithm.
Algorithm

Specification of Output
Specification of Input
(e.g. any sequence of natural Algorithm as a function of Input
numbers) (e.g. sequence of sorted
natural numbers)
Characteristics of Good Algorithm
• Efficient
• Running Time
• Space used

• Efficiency as a function of input size


• Size of Input
• Number of Data elements
Time-Space Tradeoff
• By increasing the amount of space for storing the
data, one may be able to reduce the time needed for
processing the data, or vice versa.
Complexity of Algorithm
• Time and Space used by the algorithm are two main
measures for efficiency of any algorithm M.

• Time is measured by counting the number of key


operations.

• Space is measured by counting the maximum of


memory needed by the algorithm.
• Complexity of Algorithm is a function f(n) which gives
running time and/or space requirement of algorithm M in terms
of the size n of the input data.

• Worst Case: The maximum value of f(n) for any possible input.

• Average Case: The expected or average value of f(n).

• Best Case: Minimum possible value of f(n).


Analysis of Insertion Sort Algorithm
cost times
for j←2 to n do c1 n
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-- c6
A[i+1] ← key c7 n-1
Total Time = n(c1 + c2 + c3 + c7) +
- (c2 + c3 + c5 + c6 + c7)
Analysis of Insertion Sort
Total Time = n(c1 + c2 + c3 + c7) +
- (c2 + c3 + c5 + c6 + c7)
• Best Case: Elements are already sorted, tj=1
running time = f(n)
• Worst Case: Elements are sorted in reverse order,
tj=j
running time = f(n2)
• Average Case: tj= j/2
running time = f(n2)
Rate of Growth
• The rate of growth of some standard functions
g(n) is:

log2n < n < nlog2n < n2 < n3 < 2n


Asymptotic Notations
• Goal: to simplify analysis of running time .

• Useful to identify how the running time of an


algorithm increases with the size of the input in
the limit.

• Asymptotic is a line that approaches a curve but


never touches.
Asymptotic Notations
Special Classes of Algorithms
• Logarithmic: O(log n)
• Linear: O(n)
• Quadratic: O(n2)
• Polynomial: O(nk), k >= 1
• Exponential: O(an), a > 1
Big-Oh (O) Notation
• Asymptotic upper bound

• f(n) = O (g(n)), if there exists


constants c and n0 such that,

• f(n) <= c g(n) for n >= n0

• f(n) and g(n) are functions over non-


negative integers.
• Used for Worst-case analysis.
Big-Oh (O) Notation
• Simple Rule:
Drop lower order terms and constant factors.

Example:
• 50n log n is O(n log n)
• 8n2 log n + 5 n2 + n is O(n2 log n)
Big-Omega (Ω) Notation
• Asymptotic lower bound

• f(n) = Ω (g(n)), if there


exists constants c and n0
such that,
c g(n) <= f(n) for n >= n0

• Used to describe Best-case


running time.
Big-Theta (Ө)Notation
• Asymptotic tight bound

• f(n) = Ө (g(n)), if there exists


constants c1, c2 and n0 such that,

• c1 g(n) <= f(n) <= c2 g(n) for n


>= n0

• f(n) = Ө (g(n)), iff f(n) = O(g(n)) and


f(n) = Ω (g(n))
Little-Oh (o) Notation
• Non-tight analogue of Big-Oh.

• f(n) = o (g(n)), if for every c, there exists n0


such that,
f(n) < c g(n) for n >= n0

• Used for comparisons of running times.


Questions

You might also like