ma 252: data structures and algorithms...asymptotic notation • convenient for describing...

26
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

Upload: others

Post on 08-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MA 252: Data Structures and Algorithms...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

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

Page 2: MA 252: Data Structures and Algorithms...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

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

Page 3: MA 252: Data Structures and Algorithms...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

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

Page 4: MA 252: Data Structures and Algorithms...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

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

Page 5: MA 252: Data Structures and Algorithms...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

big-Theta (Q)-notation

P. S. Mandal, IITG

Page 6: MA 252: Data Structures and Algorithms...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

Example Show that:

P. S. Mandal, IITG

Page 7: MA 252: Data Structures and Algorithms...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

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

Page 8: MA 252: Data Structures and Algorithms...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

The family of Bachmann–Landau notations

P. S. Mandal, IITG

Page 9: MA 252: Data Structures and Algorithms...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

Comparison of Functions

• Transitivity:

• Reflexivity:

• Symmetry:

• Transpose Symmetry:

P. S. Mandal, IITG

Page 10: MA 252: Data Structures and Algorithms...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

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

Page 11: MA 252: Data Structures and Algorithms...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

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

Page 12: MA 252: Data Structures and Algorithms...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

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

Page 13: MA 252: Data Structures and Algorithms...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

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

Page 14: MA 252: Data Structures and Algorithms...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

Merge sort Algorithm

P. S. Mandal, IITG

Page 15: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 16: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 17: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 18: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 19: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 20: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 21: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 22: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 23: MA 252: Data Structures and Algorithms...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

Merging two sorted arrays

P. S. Mandal, IITG

Page 24: MA 252: Data Structures and Algorithms...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

Analyzing merge sort

P. S. Mandal, IITG

Page 25: MA 252: Data Structures and Algorithms...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

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

Page 26: MA 252: Data Structures and Algorithms...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

How to solve Recurrence equation ?

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

P. S. Mandal, IITG