c o mp l e x i t y w e i s s c h a p t e r 5
play

C o mp l e x i t y ( W e i s s c h a p t e r 5 - PowerPoint PPT Presentation

C o mp l e x i t y ( W e i s s c h a p t e r 5 ) C o m p l e x i t y Tii s l e c t u r e i s a l l a b o u t h o w t o d e s c r i b e t h e p e r f o r m a n c e o f


  1. C o mp l e x i t y ( W e i s s c h a p t e r 5 )

  2. C o m p l e x i t y Tii s l e c t u r e i s a l l a b o u t h o w t o d e s c r i b e t h e p e r f o r m a n c e o f a n a l g o r i t h m L a s t t i m e w e h a d t h r e e v e r s i o n s o f t h e fj l e - r e a d i n g p r o g r a m . F o r a fj l e o f s i z e n : ● Tie fj r s t o n e n e e d e d t o c o p y n / 2 c h a r a c t e r s 2 ● Tie s e c o n d o n e n e e d e d t o c o p y n / 2 0 0 c h a r a c t e r s 2 ● Tie t h i r d n e e d e d t o c o p y 2 n c h a r a c t e r s W e w o r k e d o u t t h e s e f o r m u l a s , b u t i t w a s a b i t o f w o r k – n o w w e ' l l s e e a n e a s i e r w a y

  3. B i g i d e a : i g n o r e c o n s t a n t f a c t o r s !

  4. W h y d o w e i g n o r e c o n s t a n t f a c t o r s ? W e l l , w h e n n i s 1 0 0 0 0 0 0 . . . ● l o g n i s 2 0 2 ● n i s 1 0 0 0 0 0 0 ● n i s 1 0 0 0 0 0 0 0 0 0 0 0 0 2 ● 2 i s a n u m b e r w i t h 3 0 0 , 0 0 0 d i g i t s . . . n G i v e n t w o a l g o r i t h m s : ● Tie fj r s t t a k e s 1 0 0 0 0 0 0 l o g n s t e p s t o r u n 2 ● Tie s e c o n d t a k e s 0 . 0 0 0 0 0 0 0 1 × 2 n Tie fj r s t i s m i l e s b e t t e r ! C o n s t a n t f a c t o r s n o r m a l l y d o n ' t m a t t e r

  5. B i g O n o t a t i o n I n s t e a d o f s a y i n g . . . ● Tie fj r s t i m p l e m e n t a t i o n c o p i e s n / 2 c h a r a c t e r s 2 ● Tie s e c o n d c o p i e s n / 2 0 0 c h a r a c t e r s 2 ● Tie t h i r d c o p i e s 2 n c h a r a c t e r s W e w i l l j u s t s a y . . . ● Tie fj r s t i m p l e m e n t a t i o n c o p i e s O ( n ) c h a r a c t e r s 2 ● Tie s e c o n d c o p i e s O ( n ) c h a r a c t e r s 2 ● Tie O ( n ) t h i r d c o p i e s c h a r a c t e r s O ( n ) me a n s “ p r o p o r t i o n a l t o n ” 2 2 ( a l mo s t )

  6. T i m e c o m p l e x i t y S u p p o s e a n a l g o r i t h m t a k e s n / 2 s t e p s , 2 a n d e a c h s t e p t a k e s 1 0 0 n s t o r u n ● Tie t o t a l t i m e t a k e n i s 5 0 n n s 2 ● Tii s i s O ( n ) 2 ● Tie n u m b e r o f s t e p s t a k e n i s a l s o O ( n ) 2 I t d o e s n ' t m a t t e r w h e t h e r w e c o u n t s t e p s o r t i m e ! W e s a y t h a t t h e a l g o r i t h m h a s O ( n ) t i m e 2 o r s i m p l y c o m p l e x i t y c o m p l e x i t y

  7. W h y i g n o r e c o n s t a n t f a c t o r s ? B i g O r e a l l y s i m p l i fj e s t h i n g s : ● A s m a l l p h r a s e l i k e O ( n ) t e l l s y o u a l o t 2 ● I t ' s e a s i e r t o c a l c u l a t e t h a n a p r e c i s e f o r m u l a ● W e g e t t h e s a m e a n s w e r w h e t h e r w e c o u n t n u m b e r o f s t a t e m e n t s e x e c u t e d o r t i m e t a k e n ( o r i n t h i s c a s e n u m b e r o f e l e m e n t s c o p i e d ) – s o w e c a n b e a b i t c a r e l e s s w h a t w e c o u n t O n t h e o t h e r h a n d : ● S o m e t i m e s w e d o c a r e a b o u t c o n s t a n t f a c t o r s ! B i g O i s n o r m a l l y a g o o d c o m p r o m i s e

  8. W h a t h a p p e n s w i t h o u t b i g O ? H o w m a n y s t e p s d o e s t h i s f u n c t i o n t a k e o n a n a r r a y o f l e n g t h ( i n t h e w o r s t c a s e ) ? n O b j e c t s e a r c h ( O b j e c t [ ] a , O b j e c t x ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) { i f ( a [ i ] . e q u a l s ( t a r g e t ) ) r e t u r n a [ i ] ; A s s u m e t h a t } l o o p b o d y t a k e s 1 s t e p r e t u r n n u l l ; }

  9. W h a t h a p p e n s w i t h o u t b i g O ? H o w m a n y s t e p s d o e s t h i s f u n c t i o n t a k e o n a n a r r a y o f l e n g t h ( i n t h e w o r s t c a s e ) ? n O b j e c t s e a r c h ( O b j e c t [ ] a , O b j e c t x ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) { A n s w e r : i f ( a [ i ] . e q u a l s ( t a r g e t ) ) n r e t u r n a [ i ] ; } r e t u r n n u l l ; }

  10. W h a t a b o u t t h i s o n e ? b o o l e a n u n i q u e ( O b j e c t [ ] a ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) f o r ( i n t j = 0 ; j < a . l e n g t h ; j + + ) i f ( a [ i ] . e q u a l s ( a [ j ] ) & & i ! = j ) r e t u r n f a l s e ; r e t u r n t r u e ; }

  11. W h a t a b o u t t h i s o n e ? b o o l e a n u n i q u e ( O b j e c t [ ] a ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) f o r ( i n t j = 0 ; j < a . l e n g t h ; j + + ) O u t e r l o o p r u n s n t i m e s i f ( a [ i ] . e q u a l s ( a [ j ] ) & & i ! = j ) E a c h t i m e , i n n e r l o o p r u n s n t i m e s r e t u r n f a l s e ; r e t u r n t r u e ; 2 T o t a l : × = n n n }

  12. W h a t a b o u t t h i s o n e ? b o o l e a n u n i q u e ( O b j e c t [ ] a ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) f o r ( i n t j = 0 ; j < i ; j + + ) i f ( a [ i ] . e q u a l s ( a [ j ] ) ) r e t u r n f a l s e ; L o o p r u n s t o i r e t u r n t r u e ; i n s t e a d o f n }

  13. S o m e h a r d s u m s Wh e n i = 0 , i n n e r l o o p r u n s 0 t i m e s Wh e n i = 1 , i n n e r l o o p r u n s 1 t i m e … Wh e n i = n - 1 , i n n e r l o o p r u n s n - 1 t i m e s T o t a l : n − 1 ∑ i ● = 0 + 1 + 2 + … + n - 1 i = 0 w h i c h i s ( - 1 ) / 2 n n

  14. W h a t a b o u t t h i s o n e ? b o o l e a n u n i q u e ( O b j e c t [ ] a ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) f o r ( i n t j = 0 ; j < i ; j + + ) i f ( a [ i ] . e q u a l s ( a [ j ] ) ) A n s w e r : r e t u r n f a l s e ; ( - 1 ) / 2 n n r e t u r n t r u e ; }

  15. W h a t a b o u t t h i s o n e ? b o o l e a n u n i q u e ( O b j e c t [ ] a ) { f o r ( i n t i = 0 ; i < a . l e n g t h ; i + + ) f o r ( i n t j = 0 ; j < i ; j + + ) f o r ( i n t k = 0 ; k < j ; k + + ) “ s o m e t h i n g t h a t t a k e s 1 s t e p ” }

  16. M o r e h a r d s u m s n − 1 i − 1 i − 1 ∑ ∑ ∑ 1 i = 0 j = 0 k = 0 I n n e r l o o p : k g o e s f r o m 0 t o j - 1 O u t e r l o o p : g o e s f r o m 0 t o i n - 1 Mi d d l e l o o p : g o e s f r o m 0 t o i j - 1 C o u n t s : h o w m a n y v a l u e s , , w h e r e i j k 0 ≤ < , 0 ≤ < , 0 ≤ ≤ i n j i k j

  17. M o r e h a r d s u m s n − 1 i − 1 i − 1 ∑ ∑ ∑ 1 i = 0 j = 0 k = 0 I h a v e n o i d e a h o w t o s o l v e t h i s ! W o l f r a m A l p h a s a y s i t ' s ( - 1 ) ( - 2 ) / 6 n n n C o u n t s : h o w m a n y v a l u e s , , w h e r e i j k 0 ≤ < , 0 ≤ < , 0 ≤ ≤ i n j i k j

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend