cs302 topic algorithm analysis
play

CS302 Topic: Algorithm Analysis Thursday, Sept. 22, 2005 - PowerPoint PPT Presentation

CS302 Topic: Algorithm Analysis Thursday, Sept. 22, 2005 Announcements Lab 3 (Stock Charts with graphical objects) is due this Friday, Sept. 23!! Lab 4 now available (Stock Reports); due Friday, Oct. 7 (2 weeks) Dont


  1. CS302 Topic: Algorithm Analysis Thursday, Sept. 22, 2005

  2. Announcements � Lab 3 (Stock Charts with graphical objects) is due this Friday, Sept. 23!! � Lab 4 now available (Stock Reports); due Friday, Oct. 7 (2 weeks) � Don’t procrastinate!! Procrastination is like a credit card: Procrastination is like a credit card: it’s a lot of fun until you get the bill. it’s a lot of fun until you get the bill. -- Christopher Parker Christopher Parker --

  3. Check your knowledge of C+ + & libs: Assume: a. Stack() b. Stack(int size); c. Stack(const Stack &); d. Stack &operator= (const Stack &) Which type of constructor gets called? Answer: b Stack *stack1 = new Stack(10); #8

  4. Check your knowledge of C+ + & libs: Assume: a. Stack() b. Stack(int size); c. Stack(const Stack &); d. Stack &operator= (const Stack &) Which type of constructor gets called? Answer: a Stack stack2; #9

  5. Check your knowledge of C+ + & libs: Assume: a. Stack() b. Stack(int size); c. Stack(const Stack &); d. Stack &operator= (const Stack &) Which type of constructor gets called? Answer: c Stack stack3 = stack2; #10

  6. Check your knowledge of C+ + & libs: Assume: a. Stack() b. Stack(int size); c. Stack(const Stack &); d. Stack &operator= (const Stack &) Which type of constructor gets called? Answer: c Stack stack4(stack2); #11

  7. Check your knowledge of C+ + & libs: Assume: a. Stack() b. Stack(int size); c. Stack(const Stack &); d. Stack &operator = (const Stack &) Which type of constructor gets called? Answer: d stack4 = stack2; #12

  8. Analysis of Algorithms � The theoretical study of computer program performance and resource usage � What’s also important (besides performance/ resource usage)? � Modularity � Correctness � Maintainability � Functionality � Robustness � User-friendliness � Programmer time � Simplicity � Reliability

  9. Why study algorithms and performance? � Analysis helps us understand scalability � Performance often draws the line between what is feasible and what is impossible � Algorithmic mathematics provides a language for talking about program behavior � The lessons of program performance generalize to other computing resources � Speed is fun!

  10. The problem of sorting < > � Input: a a , ,..., a sequence of numbers 1 2 n ′ ′ ′ < > � Output: a a , ,..., a permutation such that 1 2 n ′ ′ ′ ≤ ≤ ≤ a a ... a 1 2 n � Example: � Input: 8 2 4 9 3 6 � Output: 2 3 4 6 8 9

  11. Insertion sort I NSERTION -S ORT ( A , n ) A [ 1 .. n ] for j ← 2 to n do key ← A [ j ] i ← j – 1 “pseudocode” while i > 0 and A [ i ] > key do A [ i + 1] ← A [ i ] i ← i – 1 A [ i + 1] = key 1 i j n A: key sorted

  12. Example of insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6 2 3 4 6 8 9 done

  13. Running time � Running time depends on the input; an already-sorted sequence is easier to sort � Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones � Generally, we seek an upper bounds on the running time, since everybody likes a guarantee

  14. Kinds of analyses � Worst-case: (usually) � T ( n ) = maximum time of algorithm on any input of size n � Average case: (sometimes) � T ( n ) = expected time of algorithm over all inputs of size n � Need assumption of statistical distribution of inputs � Best case: (bogus) � Cheat with a slow algorithm that works fast on some input

  15. Machine-independent time � What is insertion sort’s worst-case time? � It depends on the speed of our computer � Relative speed (on the same machine) � Absolute speed (on different machines) � B � B I G I G I I DEA : DEA : � Ignore machine-dependent constants Look at growth of T ( n ) as n → ∞ � “Asymptotic Analysis” “Asymptotic Analysis”

  16. Θ -notation (tight upper bound) � Θ ( n 2 ): Pronounce it: “Theta of n 2 ” � Math: Θ ( g ( n )) = { f ( n ): there exist positive constants c 1 , c 2 , and n 0 , such that 0 ≤ c 1 g ( n ) ≤ f ( n ) ≤ c 2 g ( n ) for all n ≥ n 0 } � Engineering: � Drop low-order terms; ignore leading constants � Example: 3 n 3 + 90 n 2 – 4 n + 6046 = Θ ( n 3 )

  17. Asymptotic performance � When n gets large enough, a Θ ( n 2 ) algorithm always beats a Θ ( n 3 ) algorithm � We shouldn’t ignore asymptotically slower algorithms, however. � real-world design situations often call for a careful balancing of engineering objectives � Asymptotic analysis is a useful tool to help structure our thinking

  18. Closer look at mathematical definition of Θ ( n ) Θ ( g ( n )) = { f ( n ): there exist positive constants c 1 , c 2 , and n 0 , such that 0 ≤ c 1 g ( n ) ≤ f ( n ) ≤ c 2 g ( n ) for all n ≥ n 0 } c 2 g ( n ) f ( n ) c 1 g ( n ) n n 0 f ( n ) = Θ ( g ( n ))

  19. O -notation (upper bound – may not be tight upper bound) � O( n 2 ): Pronounce it: “Big-Oh of n 2 ” or “Order n 2” � Math: O( g ( n )) = { f ( n ): there exist positive constants c and n 0 , such that 0 ≤ f ( n ) ≤ cg ( n ) for all n ≥ n 0 }

  20. Closer look at mathematical definition of O( n ) O( g ( n )) = { f ( n ): there exist positive constants c and n 0 , such that 0 ≤ f ( n ) ≤ cg ( n ) for all n ≥ n 0 } cg ( n ) f ( n ) n n 0 f ( n ) = O( g ( n ))

  21. Insertion sort analysis � Worst case: Input reverse sorted n ∑ = Θ = Θ 2 T n ( ) ( ) j ( n ) = j 2 n ∑ = + = Θ 2 1 k n n ( 1) ( n ) [ Recall arithmetic series: ] 2 = k 1 � Average case: All permutations equally likely n ∑ = Θ = Θ 2 T n ( ) ( / 2) j ( n ) = j 2 � Is insertion sort a fast sorting algorithm? � Moderately so, for small n � Not at all, for large n

  22. Merge sort � M ERGE -S ORT A [ 1.. n ] 1. If n = 1, we’re done. 2. Recursively sort A [ 1.. ⎡ n / 2 ⎤ ] and A [ ⎡ n / 2 ⎤ + 1 .. n ] 3. “Merge” the 2 sorted arrays � Subroutine M ERGE � From the head of each array, output the smallest value and increment the pointer to that array; continue until reach end of both arrays

  23. Merging two sorted arrays 20 12 13 11 7 9 2 1 1

  24. Merging two sorted arrays 20 12 20 12 13 11 13 11 7 9 7 9 2 1 2 1 2

  25. Merging two sorted arrays 20 12 20 12 20 12 13 11 13 11 13 11 7 9 7 9 7 9 2 1 2 7 1 2

  26. Merging two sorted arrays 20 12 20 12 20 12 20 12 13 11 13 11 13 11 13 11 7 9 9 7 9 7 9 2 1 2 9 7 1 2

  27. Merging two sorted arrays 20 12 20 12 20 12 20 12 20 12 13 11 13 11 13 11 13 11 13 11 7 9 9 7 9 7 9 2 1 2 Etc. 11 9 7 1 2 Time = Θ (n) to merge a total of n elements (i.e., linear time)

  28. Analyzing merge sort T ( n ) M ERGE -S ORT A [ 1.. n ] Θ ( 1 ) 1.If n = 1, we’re done. 2.Recursively sort A [ 1.. ⎡ n / 2 ⎤ ] and 2 T ( n / 2) A [ ⎡ n / 2 ⎤ + 1 .. n ] Θ ( n ) 3.“Merge” the 2 sorted arrays Should be T [ ⎡ n / 2 ⎤ ] + T [ ⎣ n / 2 ⎦ ] , but it Sloppiness: turns out not to matter asymptotically Means constant time

  29. Recurrence for merge sort � Recurrence: an equation defined in terms of itself � For merge sort: Θ = ⎧ (1) if n 1 = ⎨ T n ( ) + Θ > ⎩ 2 ( / 2) T n ( ) n if n 1 � We 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

  30. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 T ( n )

  31. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn T ( n / 2) T ( n / 2)

  32. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn / 2 cn / 2 T ( n / 4) T ( n / 4) T ( n / 4) T ( n / 4 )

  33. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn / 2 cn / 2 cn / 4 cn / 4 cn / 4 cn / 4 … Θ (1)

  34. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn / 2 cn / 2 h = lg n cn / 4 cn / 4 cn / 4 cn / 4 … Θ (1)

  35. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn cn / 2 cn / 2 h = lg n cn / 4 cn / 4 cn / 4 cn / 4 … Θ (1)

  36. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn cn cn / 2 cn / 2 h = lg n cn / 4 cn / 4 cn / 4 cn / 4 … Θ (1)

  37. Solving recurrences: The recursion tree � Solve T ( n ) = 2 T ( n / 2) + cn , for constant c > 0 cn cn cn cn / 2 cn / 2 h = lg n cn cn / 4 cn / 4 cn / 4 cn / 4 … … Θ (1)

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