6 views

Uploaded by m1u2n3n4i5

asymptotic notations

- • Fundamental Application for Computers • One of the Most
- Assignment 1
- Class 10 Mental Ability Competitive Exams
- Vazirani
- Heaps
- Prisoners Short
- Introduction.to.Algorithms
- HCL Commonly Asked Questions 2
- Keral SEM 2
- Reference for Discrete Calculus and Functional Equations
- lec7n8
- Sorting
- msort
- CS6212 PDS Lab Manual CSE 2013 Regulations
- Analysis of Algorithms
- Lecture 1
- Algo Notes
- Exam Sol
- Sorting Analysis
- C Balaguru Solution

You are on page 1of 14

Agenda: Worst/Best/Avg case analysis InsertionSort example Loop invariant Asymptotic Notations Textbook pages: 15-27, 41-57

InsertionSort(A) **sort A[1..n] in place for j 2 to n do key A[j ] **insert A[j ] into sorted sublist A[1..j 1] ij1 while (i > 0 and A[i] > key ) do A[i + 1] A[i] ii1 A[i + 1] key

InsertionSort(A) for j 2 to n do key A[j ] ij1 while (i > 0 and A[i] > key ) do A[i + 1] A[i] ii1 A[i + 1] key cost c1 c2 c3 c4

j =2 n

times n n1 n1

n

tj (tj 1)

j =2 n

c5 c6

j =2

(tj 1) n1

c7

n

j =2

tj

Assumptions: (Uniform cost) RAM Key comparison (KC) happens: i > 0 and A[i] > key (minors: loop counter increment operation, copy) RAM running time proportional to the number of KC Best case (BC) What is the best case? already sorted One KC for each j , and so Worst case (WC) What is the worst case? reverse sorted j KC for xed j , and so Average case (AC)

n j j =2 n 1 j =2

=n1

n(n+1) 2

Average case: always ask average over what input distribution? Unless stated otherwise, assume each possible input equiprobable Uniform distribution Here, each of the possible inputs equiprobable (why?)

Key observation: equiprobable inputs imply for each key, rank among keys so far is equiprobable e.g., when j = 4, expected number of KC is

1+2+3+4 4

j

= 2 .5

n

i=1

j +1 2

j+1 1 = 2 2

n+1

=

j =3

1 2

(n + 1)(n + 2) 3 2

n2 + 3n 4 4

j =2

We will ignore the constant factors; we also only care about the dominant term, here it is n2 .

Claim: At the start of each iteration of the for loop, the subarray A[1..j 1] consists of the elements originally in A[1..j 1] and in sorted order. Proof of claim. initialization: j = 2 maintenance: j j + 1 termination: j = n + 1

Common points initialization vs. base step maintenance vs. inductive step Dierence termination vs. innite

Why correctness? Always a good idea to verify correctness Becoming more common in industry This course: a simple introduction to correctness proofs When loop is involved, use loop invariant (and induction) When recursion is involved, use induction Loop invariant (LI) Initialization: Maintenance: next? Termination #1: correctness? Termination #2: Insert sort LI: At start of for loop, keys initially in A[1..j 1] are in A[1..j 1] and sorted. Initialization: A[1..1] is trivially sorted does LI hold 1st time through? if LI holds one time, does LI hold the upon completion, LI implies does loop terminate?

Maintenance: none from A[1..j ] moves beyond j ; sorted Termination #1: A[1..n] is sorted upon completion, j = n + 1 and by LI

Termination #2: for loop counter j increases by 1 at a time, and no change inside the loop 6

Assume LI holds when j = k and so A[1] A[2] . . . A[k 1] The for loop body contains another while loop. Use another LI. LI2: let A [1..n] denote the list at start of while loop. Then each time execution reaches start of while loop: A[1..i + 1] = A [1..i + 1] A[i + 2..j ] = A [i + 1..j 1] Prove LI2 (exercise) Using LI2, prove LI Hint: when LI2 terminates, either i = 0 or A[i] key (the latter implies either i = j 1 or A[i + 1] > key ).

Analysis of algorithms becomes analysis of functions: e.g., f (n) denotes the WC running time of insertion sort g (n) denotes the WC running time of merge sort f (n) = c1 n2 + c2 n + c3 g (n) = c4 n log n Which algorithm is preferred (runs faster)? To simplify algorithm analysis, want function notation which indicates rate of growth (a.k.a., order of complexity)

roughly, The set of functions which, as n gets large, grow no faster than a constant times f (n). precisely, (or mathematically) The set of functions {h(n) : N R} such that for each h(n), there are constants c0 R+ and n0 N such that h(n) c0 f (n) for all n > n0 . examples: h(n) = 3n3 + 10n + 1000 log n O(n3 ) h(n) = 3n3 + 10n + 1000 log n O(n4 ) h(n) = 5n , n2 , n 10120 O(n2 ) 120 n > 10 8

Denitions:

O(f (n)) is the set of functions h(n) that roughly, grow no faster than f (n), namely c0 , n0 , such that h(n) c0 f (n) for all n n0 (f (n)) is the set of functions h(n) that roughly, grow at least as fast as f (n), namely c0 , n0 , such that h(n) c0 f (n) for all n n0 (f (n)) is the set of functions h(n) that roughly, grow at the same rate as f (n), namely c0 , c1 , n0 , such that c0 f (n) h(n) c1 f (n) for all n n0 (f (n)) = O(f (n)) (f (n)) o(f (n)) is the set of functions h(n) that roughly, grow slower than f (n), namely

h(n) limn f =0 (n)

(f (n)) is the set of functions h(n) that roughly, grow faster than f (n), namely

h(n) limn f = (n)

Warning:

the textbook overloads = Textbook uses g (n) = O(f (n)) Incorrect !!! Because O(f (n)) is a set of functions. Correct: g (n) O(f (n)) You should use the correct notations.

Examples: which of the following belongs to O(n3 ), (n3 ), (n3 ), o(n3 ), (n3 ) ?

1. f1 (n) = 19n 2. f2 (n) = 77n2 3. f3 (n) = 6n3 + n2 log n 4. f4 (n) = 11n4

10

Answers:

1. 2. 3. 4. f1 (n) = 19n f2 (n) = 77n2 f3 (n) = 6n3 + n2 log n f4 (n) = 11n4

f1 , f2 , f3 O(n3 ) f1 (n) 19n3 , for all n 0 c0 = 19, n0 = 0 f2 (n) 77n3 , for all n 0 c0 = 77, n0 = 0 f3 (n) 6n3 + n2 n, for all n 1, since log n n if f4 (n) c0 n3 , then n f3 , f4 (n3 ) f3 (n) 6n3 , for all n 1, since n2 log n 0 f4 (n) 11n3 , for all n 0 f3 (n3 ) why? f1 , f2 o(n3 )

n f1 (n): limn 19 = limn 19 =0 n3 n2 n f2 (n): limn 77 = limn 77 =0 n3 n

2

c0 11

no such n0 exists

f3 (n): limn 6n

+n2 log n n3

4

= limn 6 +

log n n

=6

f4 (n3 ) 11

logarithm review:

Denition of logb n (b, n > 0): blogb n = n logb n as a function in n: increasing, one-to-one logb 1 = 0 logb xp = p logb x logb (xy ) = logb x + logb y xlogb y = y logb x logb x = (logb c)(logc x)

ln n = loge n (natural logarithm) lg n = log2 n (base 2, binary) (logb n) = (log{whatever

d dx positive} n)

= (log n)

ln x =

1 x

h(n) O(f (n)) if and only if f (n) (h(n))

h(n) = ... limit rules: limn f (n)

. . . , then h (f ), (f ) . . . 0 < k < , then h (f ) . . . 0, then h O(f ), o(f ) LH opitals rules: if limn h(n) = , limn f (n) = , and h (n), f (n) exist, then h (n) h(n) = lim n f (n) n f (n) lim

n 1 e.g., limn ln = limn n =0 n

Still, we have h(n) O(n2 ), h(n) (1), etc. O(), (), (), o(), () JUST useful asymptotic notations

13

n n . e

Example: The following functions are ordered in increasing order of growth (each is in big-Oh of next one). Those in the same group are in big-Theta of each other.

{n1/ log n ,

1} ,

ln ln n},

log n,

ln n, {n2 , en ,

n

log n

( 2)log n , {(log n)

{n log n, }, 3 2

log(n!)},

n

(log n)!,

log n

log log n

n.2n ,

(n!)2 ,

14

- • Fundamental Application for Computers • One of the MostUploaded byMichael Salazar
- Assignment 1Uploaded byjulyhoping
- Class 10 Mental Ability Competitive ExamsUploaded bysachin pant
- VaziraniUploaded byvij_ks
- HeapsUploaded byMonica Baxter
- Prisoners ShortUploaded byRuth
- Introduction.to.AlgorithmsUploaded bydiego
- HCL Commonly Asked Questions 2Uploaded byShabin Crzyracer
- Keral SEM 2Uploaded bySuresh Kumar
- lec7n8Uploaded byhardnut
- Reference for Discrete Calculus and Functional EquationsUploaded bycolstelhml
- SortingUploaded byPriyanka Sharma
- msortUploaded byariyan_eghbal
- CS6212 PDS Lab Manual CSE 2013 RegulationsUploaded byheyramzz
- Analysis of AlgorithmsUploaded byAsha Rose Thomas
- Lecture 1Uploaded byBenjamin Howard
- Algo NotesUploaded byAhitsham Latif
- Exam SolUploaded bygsandynitw
- Sorting AnalysisUploaded byJiswanath Mondal
- C Balaguru SolutionUploaded byanon_231909170
- Asintótica fuerte de polinomios de SobolevUploaded byVilma Diaz Cabrera
- ROLAPUploaded bywasoft
- ma691_ch1.pdfUploaded byham.karim
- cs8451 qb.pdfUploaded byDileep Yenuganti
- CH3Uploaded byMaruf_007
- 5 - 1 - Sorting Introduction (14-43)Uploaded byAadheeshwar Vijayakumar
- DAA NEW1Uploaded byManukumar
- Bubble SortUploaded byPraveen Pal
- cucs-473-89.pdfUploaded byVinayak Agrawal
- Integral Es in Media t AsUploaded bydavid

- Life After Loss Grieving With Hope(1)Uploaded bym1u2n3n4i5
- optimality of naive bayes.pdfUploaded bym1u2n3n4i5
- ShinyUploaded bym1u2n3n4i5
- unit-IV.pptUploaded bym1u2n3n4i5
- 02DataUploaded bym1u2n3n4i5
- DWDMUploaded bym1u2n3n4i5
- New Text Document (2)Uploaded bym1u2n3n4i5
- AcdUploaded bym1u2n3n4i5
- SongUploaded bym1u2n3n4i5
- Ecm-oops Through JavaUploaded bym1u2n3n4i5
- 10.1.1.94.3837[1]Uploaded bym1u2n3n4i5

- YVCUploaded bynetterinder
- BoeingUploaded bysuriakumaran
- SynopsisUploaded byrm_yegu7645
- motorxoneUploaded byDario Alberto Rodriguez Morocoima
- importance of building economicsUploaded byEmime Lee
- 2. Greetings - OkUploaded bychristie
- required_info_map.txtUploaded byjohn
- atr4517r0-pdf.pdfUploaded byMostifa Mastafa
- Brain VisaUploaded byKumar Nikhil
- cortisol on p.gingivalisUploaded bykarthik
- new energy lpUploaded byapi-270351267
- Non Disclosure Agreement.docxUploaded byAnonymous 0MBgAvXgZ4
- Avaya Octel Voice Mail System_042110Uploaded bykkayaturk
- weqweqweUploaded byheherson juan
- Commision on Macroeconomic and Health Bg P2 Oral and Dental DiseasesUploaded byAnonymous 9aIw2a
- CQIUploaded bygolden_hunter_1980
- IFE Matrix.docxUploaded byShu Xin Chua
- SI520_week_06 Lab_Helvetica Documentary_changlaw.pdfUploaded byLawrenceC
- 23243334 Kfc Project on Market Research (1)Uploaded byPrashant Chaubey
- R12-ITURKA.BAND-C-0001!!MSW-E (1).docUploaded byhoma
- Bruno Breitmeyer-Blindspots_ The Many Ways We Cannot See-Oxford University Press, USA (2010).pdfUploaded byFrancisco Villar
- Blackhat Arsenal 2Uploaded byakasame
- Deadworld - Aces High (World War I)Uploaded byThomas
- Omicron Cmc 256plus Brochure EnuUploaded byDuško Tovilović
- Maseei 5(Module 2)Uploaded byShiva Goyal
- assessment 2 portfolio 3Uploaded byapi-382084216
- LATIN-081206-1-0-A (Revised)Uploaded byOscar Escobar Figueroa
- How to ... Realize an ABC AnalysisUploaded byyshrini
- What is RealityUploaded byGreenchartreuse
- Thermal Management 2014Uploaded byEarl James