MA 252: Data Structures and Algorithms...Asymptotic Notation • Convenient for describing...

Post on 08-Jul-2020

1 views 0 download

transcript

MA 252: Data Structures and Algorithms Lecture 3

http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html

Partha Sarathi Mandal

Dept. of Mathematics, IIT Guwahati

Asymptotic Notation

• Convenient for describing worst-case running time function T(n) • Big-Oh

– f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)

• big-Omega – f(n) is W(g(n)) if f(n) is asymptotically greater than or equal to g(n)

• big-Theta – f(n) is Q(g(n)) if f(n) is asymptotically equal to g(n)

• little-oh – f(n) is o(g(n)) if f(n) is asymptotically strictly less than g(n)

• little-omega – f(n) is w(g(n)) if is asymptotically strictly greater than g(n)

P. S. Mandal, IITG

big-oh (O)-notation

big-Oh (O)-notation:

f(n) is O(g(n)) if there is a constant c > 0

and an integer constant n0>=1 such

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

• an upper bound that is asymptotically tight.

P. S. Mandal, IITG

big-Omega (W)-notation

big-Omega (W)-notation:

f(n) is W(g(n)) if there is a constant c > 0

and an integer constant n0>=1 such that

f(n) >= c g(n) >=0 for n >= n0

• a lower bound and that is asymptotically

tight. P. S. Mandal, IITG

big-Theta (Q)-notation

P. S. Mandal, IITG

Example Show that:

P. S. Mandal, IITG

Little-oh & Little-omeha • little-oh (o)-notation

– f(n) is o(g(n)) if, for any constant c > 0, there is an integer constant n0 >= 0 such that 0=<f(n) < c g(n) for n >= n0

– an upper bound that is not asymptotically tight.

• little-omega (w)-notation – f(n) is w(g(n)) if, for any constant

c > 0, there is an integer constant n0 >= 0 such that f(n) > c g(n) >=0 for n >= n0

– a lower bound that is not asymptotically tight.

P. S. Mandal, IITG

The family of Bachmann–Landau notations

P. S. Mandal, IITG

Comparison of Functions

• Transitivity:

• Reflexivity:

• Symmetry:

• Transpose Symmetry:

P. S. Mandal, IITG

Exercise1: Give a big-Oh characterization

Algorithm Ex1(A, n)

Input an array X of n integers

Output the sum of the elements in A

s A[0]

for i 0 to n 1 do

s s + A[i]

return s

P. S. Mandal, IITG

Exercise2: Give a big-Oh characterization

Algorithm Ex2(A, n)

Input an array X of n integers

Output the sum of the elements at even cells in A

s A[0]

for i 2 to n 1 by increments of 2 do

s s + A[i]

return s

P. S. Mandal, IITG

Exercise3: Give a big-Oh characterization

Algorithm Ex3(A, n)

Input an array X of n integers

Output the sum of the prefix sums A s 0

for i 0 to n 1 do

s s + A[0]

for j 1 to i do

s s + A[j]

return s

P. S. Mandal, IITG

Merge sort

• divide-and-conquer paradigm:

– Split the list in two parts, sort both, then merge the sorted results to get an overall sorted list.

P. S. Mandal, IITG

Merge sort Algorithm

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Merging two sorted arrays

P. S. Mandal, IITG

Analyzing merge sort

P. S. Mandal, IITG

Recurrence for merge sort

• We shall usually omit stating the base case when T(n) = Θ(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence.

P. S. Mandal, IITG

How to solve Recurrence equation ?

• Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

P. S. Mandal, IITG