unit03_complexityanalysis1

12
7/21/2019 Unit03_ComplexityAnalysis1 http://slidepdf.com/reader/full/unit03complexityanalysis1 1/12 Complexity Analysis (Part I) Motivations for Complexity Analysis. Example of Basic Operations  Average, Best, and orst Cases. !imple Complexity Analysis Examples.

Upload: rutvij111

Post on 04-Mar-2016

218 views

Category:

Documents


0 download

DESCRIPTION

Data

TRANSCRIPT

Page 1: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 1/12

Complexity Analysis (Part I)

• Motivations for Complexity Analysis.

• Example of Basic Operations

•  Average, Best, and orst Cases.

• !imple Complexity Analysis Examples.

Page 2: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 2/12

Motivations for Complexity Analysis

• "#ere are often many different algorithms $#ic# can %e &sed to

solve t#e same pro%lem. "#&s, it ma'es sense to develop

tec#ni&es t#at allo$ &s to

o compare different algorit#ms $it# respect to t#eir *efficiency+

o c#oose t#e most efficient algorit#m for t#e pro%lem

• "#e efficiency  of any algorit#mic sol&tion to a pro%lem is a meas&re

of t#e

o Time efficiency t#e time it ta'es to exec&te.

o Space efficiency t#e space (primary or secondary memory) it &ses.

• e $ill foc&s on an algorit#ms efficiency $it# respect to time.

Page 3: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 3/12

Machine independence• "#e eval&ation of efficiency s#o&ld %e as mac#ine

independent as possi%le.• -t is not &sef&l to meas&re #o$ fast t#e algorit#m r&ns as

t#is depends on $#ic# partic&lar comp&ter, O!,programming lang&age, compiler, and 'ind of inp&ts are&sed in testing

• -nstead,o $e co&nt t#e n&m%er of basic operations t#e algorit#m performs.

o $e calc&late #o$ t#is n&m%er depends on t#e sie of t#e inp&t.

•  A %asic operation is an operation $#ic# ta'es a constant

amo&nt of time to exec&te.• /ence, t#e efficiency of an algorit#m is t#e n&m%er of

%asic operations it performs. "#is n&m%er is a f&nction oft#e inp&t sie n.

Page 4: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 4/12

Example of Basic Operations

•  Arit#metic operations 0, 1, 2, 3, 4•  Assignment statements of simple data types.

• 5eading of primitive types

• $riting of a primitive types

• !imple conditional tests if (x 6 78) ...

• met#od call (9ote t#e exec&tion time of t#e met#od itself may

depend on t#e val&e of parameter and it may not %e constant)

• a met#od:s ret&rn statement

• Memory Access

• e consider an operation s&c# as 33 , 3; , and 0; as consisting oft$o %asic operations.

• Note "o simplify complexity analysis $e $ill not consider memory

access (fetc# or store) operations.

Page 5: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 5/12

Best, Average, and Worst case complexities

• e are &s&ally interested in t#e worst case complexity $#at aret#e most operations t#at mig#t %e performed for a given pro%lem

sie. e $ill not disc&ss t#e ot#er cases 44 best and average case.

• Best case depends on t#e inp&t

•  Average case is diffic&lt to comp&te

• !o $e &s&ally foc&s on $orst case analysis < Easier to comp&te

 < =s&ally close to t#e act&al r&nning time

 < Cr&cial to real4time systems (e.g. air4traffic control)

Page 6: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 6/12

Best, Average, and Worst case complexities

• Example >inear !earc# Complexity

• Best Case -tem fo&nd at t#e %eginning One comparison• orst Case -tem fo&nd at t#e end n comparisons

•  Average Case -tem may %e fo&nd at index ?, or 7, or 8, . . . or n 4 7

 <  Average n&m%er of comparisons is (7 3 8 3 . . . 3 n) 1 n ; (n!" # $ 

•  orst and Average complexities of common sorting algorit#ms

Met#od orst Case Average Case

!election sort n8 n8

-nserstion sort n8 n8

Merge sort n log n n log n

@&ic' sort n8 n log n

Page 7: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 7/12

!imple Complexity Analysis >oops

• e start %y considering #o$ to co&nt operations

in for 4loops. < e &se integer division t#ro&g#o&t.

• irst of all, $e s#o&ld 'no$ t#e n&m%er ofiterations of t#e loop say it is x. < "#en t#e loop condition is exec&ted x ! times.

 < Eac# of t#e statements in t#e loop %ody is exec&ted x times.

 < "#e loop4index &pdate statement is exec&ted x times.

Page 8: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 8/12

!imple Complexity Analysis >oops ($it# 6)

•-n t#e follo$ing for4loop

  "#e n&m%er of iterations is (n % & " # m

• "#e initialiation statement, i ' &, is exec&ted one time.

• "#e condition, i n, is exec&ted (n % & " # m

  ! times.

• "#e &pdate statement, i ' i m, is exec&ted (n % & " # m

 times.

• Eac# of statement! and statement$ is exec&ted (n % & " # m

 times.

for (int i = k; i < n; i = i + m){

statement1;

statement2;

 }

Page 9: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 9/12

!imple Complexity Analysis >oops ($it# 6;)

•-n t#e follo$ing for4loop 

• "#e n&m%er of iterations is (n % &" # m !

• "#e initialiation statement, i ' &, is exec&ted one time.

• "#e condition, i ' n, is exec&ted (n % &" # m !

  ! times.

• "#e &pdate statement, i ' i m, is exec&ted (n % &" # m ! times.

• Eac# of statement! and statement$ is exec&ted (n % &" # m ! times.

for (int i = k; i <= n; i = i + m){

statement1;

statement2;

 }

Page 10: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 10/12

!imple Complexity Analysis >oop Example

• ind t#e exact n&m%er of %asic operations in t#e follo$ing program fragment

• "#ere are 8 assignments o&tside t#e loop ; 8 operations.• "#e for  loop act&ally comprises• an assignment (i ; ?) ; 7 operation

• a test (i 6 n) ; n 3 7 operations• an increment (i33) ; 8 n operations• t#e loop %ody t#at #as t#ree assignments, t$o multiplications, and an

addition ; D n operations"#&s t#e total n&m%er of %asic operations is D 0 n 3 8 0 n 3 (n 3 7) 3

; )n *

double x, y;

x = 2.5 ; y = .!;

for(int i = !; i < n; i++){

a"i# = x $ y;

x = 2.5 $ x;

y = y + a"i#;}

Page 11: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 11/12

!imple Complexity Analysis Examples

• !&ppose n is a m&ltiple of 8. Fetermine t#e n&m%er of %asic operationsperformed %y of t#e met#od myMet#od()

• !ol&tion "#e n&m%er of iterations of t#e loop

for(int i ; 7 i n i ; i 0 8)

  s&m ; s&m 3 i 3 helper(i)

is log$n (A Proof $ill %e given later)/ence t#e n&m%er of %asic operations is

! ! (! log$n" log$n[2 + 4 + 1 + 1 + (n + 1) + n[2 + 2] + 1]   !

' + log$n log$n!- .n/ !

' . n log$n !! log$n *

static int myMet#od(int n)G

  int s&m ; ?

  for(int i ; 7 i 6 n i ; i 0 8)

  s&m ; s&m 3 i 3 helper(i)

  ret&rn s&m

 H

static int #elper(int n)G

  int s&m ; ?

  for(int i ; 7 i 6; n i3

3)

  s&m ; s&m 3 i

  ret&rn s&mH

Page 12: Unit03_ComplexityAnalysis1

7/21/2019 Unit03_ComplexityAnalysis1

http://slidepdf.com/reader/full/unit03complexityanalysis1 12/12

!imple Complexity Analysis

>oops it# >ogarit#mic -terations

• -n t#e follo$ing for4loop ($it# 6)

 

 < "#e n&m%er of iterations is (0ogm (n # &" "

• -n t#e follo$ing for4loop ($it# 6;)

 < "#e n&m%er of iterations is (0ogm (n # &" !"

for (int i = k; i < n; i = i $ m){

statement1;

statement2;

 }

for (int i = k; i <= n; i = i $ m){statement1;

statement2;

 }