0% found this document useful (0 votes)
44 views

Lecture 3 - Complexity Analysis Cont

This document discusses complexity analysis and Big O notation. It begins by recapping counting primitives like assignments and comparisons to analyze time complexity. It then covers growth rates like linear, quadratic and exponential functions. It introduces Big O notation as a way to describe an algorithm's worst-case running time by focusing on the most dominant term. Examples are provided to show how to determine the Big O of different functions and algorithms. Exercises are included for students to practice calculating Big O notations.

Uploaded by

jeffreyj.jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lecture 3 - Complexity Analysis Cont

This document discusses complexity analysis and Big O notation. It begins by recapping counting primitives like assignments and comparisons to analyze time complexity. It then covers growth rates like linear, quadratic and exponential functions. It introduces Big O notation as a way to describe an algorithm's worst-case running time by focusing on the most dominant term. Examples are provided to show how to determine the Big O of different functions and algorithms. Exercises are included for students to practice calculating Big O notations.

Uploaded by

jeffreyj.jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

19CSE212: Data Structures & Algorithms

Lecture 3 : Complexity Analysis Cont.

By
Ritwik M

Based on the reference materials by Prof. Goodrich, OCW METU and Dr. Vidhya Balasubramanian

1 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
A Quick Recap

• Need for Data Structures


• Philosophy of Data Structures
• Complexity Analysis
– Counting Method
• Primitive operations are identified and counted to
analyze cost

2 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Counting Primitives to analyze time complexity

• Primitive operations are identified and counted to analyze cost


• Primitive Operations
̶ Assigning a value to a variable What about an IF -Then
̶ Performing an arithmetic operation -Else Statement?

̶ Calling a method
̶ Comparing two numbers
̶ Indexing into an array
̶ Following an object reference
̶ Returning from a method

3 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Growth Rates and Complexity

• Important factor to be considered when estimating complexity


• When experimental setup (hardware/software) changes
̶ Running time/memory is affected by a constant factor
̶ 2n or 3n or 100n is still linear
̶ Growth rate of the running time/memory is not affected
• Growth rates of functions
̶ Linear
̶ Quadratic
̶ Exponential

4 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Comparing Growth Rates

5 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Ideal Logic / From the Growth Rates

• The exponential functions are increasing at an extremely fast


rate when compared to linear functions.
• Data structure operations to run in times proportional to the
constant or logarithm function
• Algorithms to run in linear or n-log-n time

6 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Asymptotic Analysis

• Can be defined as a method of describing limiting behavior


• Used for determining the computational complexity of
algorithms
̶ A way of expressing the main component of the cost of an algorithm
using the most determining factor
̶ e.g if the running time is 5n2+5n+3, the most dominating factor is 5n2
• Capturing this dominating factor is the purpose of asymptotic
notations

7 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Big-Oh Notation

• Given a function f(n) we say, f(n) is O(g(n)) if there are positive


constants c and n0 such that f(n)<= cg (n) when n>= n0
• i.e. c>0 and n0>=1

8 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Big-Oh Significance

• The big-Oh notation gives an upper bound on the growth rate


of a function
• The statement “f(n) is O(g(n))” means that the growth rate of
f(n) is not more than the growth rate of g(n)
̶ We are guaranteeing that f(n) grows at a rate no faster than g(n)
̶ Both can grow at the same rate
̶ Though 1000n is larger than n2, n2 grows at a faster rate
o n2 will be larger function after n = 1000
o Hence 1000n = O(n2)

• The big-Oh notation can be used to rank functions according to


their growth rate

9 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Big-Oh Significance

• Table of max-size of a problem that can be solved in one


second, one minute and one hour for various running time
measures in microseconds [Goodrich]

10 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
• Take away from the table
̶ Algorithms with quadratic or cubic running times are less practical, and
algorithms with exponential running times are infeasible for all but the
smallest sized inputs

11 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Common Rules for Big-Oh

• If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,


̶ Drop lower-order terms
̶ Drop constant factors
• Use the smallest possible class of functions to represent in big
Oh
̶ “2n is O(n)” instead of “2n is O(n2)”
• Use the simplest expression of the class
̶ “3n+ 5 is O(n)” instead of “3n + 5 is O(3n)”

12 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Big-Oh Example
• Show that 8n+5 is O(n)
• By the big-Oh definition, we need to find a real constant c > 0 and an integer
constant n 0 ≥ 1 such that 8n + 5 ≤ cn for every integer n ≥ n0.

• A quick solution strategy is to assume a minimal value for n0 and calculate c.

• 8n + 5<=cn0 for n0=1 where n>=n0

• 8(1)+5<=c(1)

• 8+5<=c

• c>=13

• Hence 8n+5 is O(n) for c>=13 and n0=1 where n>=n0

• This is just one of many choices available because there is a trade-off


between c and n0. c=9 and n0=5 is also a viable option.

13 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Big-Oh Example
• Show 7n-2 is O(n) – [Hint: prove that f(n)<=cg(n)]
̶ need c > 0 and n0 >= 1 such that 7n-2 <= cn for n >= n0
̶ this is true for c =7 and n0 = 1
• Show 2n+100logn is O(n)
̶ need c > 0 and n0 >= 1 such that 2n+100logn <= cn for n >= n0
̶ this is true for c = 102 and n0 = 1
• n2 is not O(n)
̶ Must prove n2 <= cn
̶ n <= c
̶ The above inequality cannot be satisfied since c must be a constant
̶ Hence proof by contradiction
14 Amrita Vishwa Vidhyapeetham Ritwik M
Amrita School of Engineering
Exercises

• Show that 3n+5 is O(n)


• Show that 20n3 +10nlogn+5 is O(n3)
• Show that 3logn+2 is O(logn)
• Show that 5n4+3n3+2n2+4n+1 is O(n4 )
• Show that 5n2+3nlogn+2n+5 is O(n2)

15 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Try This Out

• Consider a set of numbers from 1 to n. All the values except


one value are present
̶ Goal: Find the missing number
̶ Give 3 solutions to find the missing number
̶ What is the time and space complexity in terms of n?

16 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Exercises

• Calculate the value returned by total


def example4(S):
”””Return the sum of the prefix sums of sequence S.”””
n = len(S)
prefix = 0
total = 0
for j in range(n):
prefix += S[j]
total += prefix
return total

17 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Quick Summary

• Asymptotic Analysis
• Big Oh
• Rules for Big Oh
• Verification of Big Oh
• Examples
• Exercises

18 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering
Up Next

Sorting Algorithms

19 Amrita Vishwa Vidhyapeetham Ritwik M


Amrita School of Engineering

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy