informatik ii
play

Informatik II Ubung 4 FS 2020 1 Program Today Survey - PowerPoint PPT Presentation

Informatik II Ubung 4 FS 2020 1 Program Today Survey Productive Failure Case Study 1 1 Feedback of last exercise 2 Repetition Theory 3 Analysis of programs Analysis of recurrences Divide & Conquer Sorting Algorithms 4 2 3.


  1. Informatik II ¨ Ubung 4 FS 2020 1

  2. Program Today Survey Productive Failure Case Study 1 1 Feedback of last exercise 2 Repetition Theory 3 Analysis of programs Analysis of recurrences Divide & Conquer Sorting Algorithms 4 2

  3. 3. Repetition Theory 3

  4. Analysis How many calls to f() ? for(unsigned i = 1; i <= n/3; i += 3) for(unsigned j = 1; j <= i; ++j) f(); 4

  5. Analysis How many calls to f() ? for(unsigned i = 1; i <= n/3; i += 3) for(unsigned j = 1; j <= i; ++j) f(); The code fragment implies Θ( n 2 ) calls to f() : the outer loop is executed n/ 9 times and the inner loop contains i calls to f() . 4

  6. Analysis How many calls to f() ? for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j ∗ j >= 1; −− j) f(); for(unsigned k = 1; k <= n; k ∗ = 2) f(); } 5

  7. Analysis How many calls to f() ? for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j ∗ j >= 1; −− j) f(); for(unsigned k = 1; k <= n; k ∗ = 2) f(); } We can ignore the first inner loop because it contains only a constant number of calls to f() 5

  8. Analysis How many calls to f() ? for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j ∗ j >= 1; −− j) f(); for(unsigned k = 1; k <= n; k ∗ = 2) f(); } We can ignore the first inner loop because it contains only a constant number of calls to f() The second inner loop contains ⌊ log 2 ( n ) ⌋ + 1 calls to f() . Summing up yields Θ( n log( n )) calls. 5

  9. Analysis How many calls to f() in g(n) , depending on n > 0 ? void g(int n){ if (n>1){ g(n/2); } else{ f(); } } 6

  10. Analysis How many calls to f() in g(n) , depending on n > 0 ? void g(int n){ if (n>1){ g(n/2); } else{ f(); } } There is only a single call to f() at the end of the recursion. 6

  11. Analysis How many calls to f() in g(n) , depending on n > 0 ? void g(int n){ if (n>1){ g(n − 1); } f(); } 7

  12. Analysis How many calls to f() in g(n) , depending on n > 0 ? void g(int n){ if (n>1){ g(n − 1); } f(); } Recurrence � T ( n − 1) + 1 n > 1 T ( n ) = 1 n = 1 7

  13. Analysis How many calls to f() in g(n) , depending on n > 0 ? void g(int n){ if (n>1){ g(n − 1); } f(); } Recurrence � T ( n − 1) + 1 n > 1 T ( n ) = ∈ Θ( n ) 1 n = 1 7

  14. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } } 8

  15. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } } Recurrence � 2 T ( n/ 2) n > 1 T ( n ) = 1 n = 1 8

  16. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } } Recurrence � 2 T ( n/ 2) n > 1 T ( n ) = ∈ Θ( n ) 1 n = 1 8

  17. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } } 9

  18. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } } Recurrence � 2 T ( n/ 2) + n n > 1 T ( n ) = 1 n = 1 9

  19. Analysis How many calls to f() in g(n) , depending on n = 2 k ? void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } } Recurrence � 2 T ( n/ 2) + n n > 1 T ( n ) = ∈ Θ( n log n ) 1 n = 1 9

  20. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  21. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  22. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  23. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  24. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  25. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  26. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  27. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  28. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  29. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  30. Merge 7 1 4 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16 10

  31. Algorithm Merge ( A, l, m, r ) Input : Array A with length n , indexes 1 ≤ l ≤ m ≤ r ≤ n . A [ l, . . . , m ] , A [ m + 1 , . . . , r ] sorted Output : A [ l, . . . , r ] sorted 1 B ← new Array ( r − l + 1) 2 i ← l ; j ← m + 1 ; k ← 1 3 while i ≤ m and j ≤ r do if A [ i ] ≤ A [ j ] then B [ k ] ← A [ i ] ; i ← i + 1 4 else B [ k ] ← A [ j ] ; j ← j + 1 5 k ← k + 1 ; 6 7 while i ≤ m do B [ k ] ← A [ i ] ; i ← i + 1 ; k ← k + 1 8 while j ≤ r do B [ k ] ← A [ j ] ; j ← j + 1 ; k ← k + 1 9 for k ← l to r do A [ k ] ← B [ k − l + 1] 11

  32. Algorithm (recursive 2-way) Mergesort ( A, l, r ) Input : Array A with length n . 1 ≤ l ≤ r ≤ n Output : A [ l, . . . , r ] sorted. if l < r then m ← ⌊ ( l + r ) / 2 ⌋ // middle position Mergesort ( A, l, m ) // sort lower half Mergesort ( A, m + 1 , r ) // sort higher half Merge ( A, l, m, r ) // Merge subsequences 12

  33. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1

  34. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1

  35. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1 5 6 8 3 7 9 2 4 1

  36. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1 5 6 8 3 7 9 2 4 1

  37. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1 5 6 8 3 7 9 2 4 1 2 3 4 5 6 7 8 9 1

  38. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1 5 6 8 3 7 9 2 4 1 2 3 4 5 6 7 8 9 1

  39. Natural 2-way mergesort 5 6 2 4 8 3 9 7 1 5 6 8 3 7 9 2 4 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 13

  40. Algorithm NaturalMergesort ( A ) Input : Array A with length n > 0 Output : Array A sorted repeat r ← 0 while r < n do l ← r + 1 m ← l ; while m < n and A [ m + 1] ≥ A [ m ] do m ← m + 1 if m < n then r ← m + 1 ; while r < n and A [ r + 1] ≥ A [ r ] do r ← r + 1 Merge( A, l , m , r ); else r ← n until l = 1 14

  41. Algorithm Partition ( A, l, r, p ) Input: Array A , that contains the pivot p in A [ l, . . . , r ] at least once. Output: Array A partitioned in [ l, . . . , r ] around p . Returns position of p . while l ≤ r do while A [ l ] < p do l ← l + 1 while A [ r ] > p do r ← r − 1 swap( A [ l ] , A [ r ] ) if A [ l ] = A [ r ] then l ← l + 1 return l-1 15

  42. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 l r 3 1 2 8 4 6 5 3 1 2 4 8 6 5 16

  43. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 l r 3 1 2 4 8 6 5 16

  44. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 l r 3 1 2 4 8 6 5 16

  45. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5 l r 16

  46. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5 l r 16

  47. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5 l r 16

  48. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5 l r 16

  49. Illustration Partition Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5 r l 16

  50. Algorithm Quicksort ( A, l, r ) Input : Array A with length n . 1 ≤ l ≤ r ≤ n . Output : Array A , sorted in A [ l, . . . , r ] . if l < r then Choose pivot p ∈ A [ l, . . . , r ] k ← Partition ( A, l, r, p ) Quicksort ( A, l, k − 1 ) Quicksort ( A, k + 1 , r ) 17

  51. Stable and in-situ sorting algorithms Stabe sorting algorithms don’t change the relative position of two elements. 1 5 2 6 6 8 4 not stable 5 6 6 8 2 4 1 Every sorting algorithm can be made stable by storing the array position together with the element. 18

  52. Stable and in-situ sorting algorithms Stabe sorting algorithms don’t change the relative position of two elements. 1 5 2 6 6 8 4 not stable 5 6 6 8 2 4 5 2 6 6 8 4 stable 5 6 6 8 2 4 1 Every sorting algorithm can be made stable by storing the array position together with the element. 18

  53. Stable and in-situ sorting algorithms Stabe sorting algorithms don’t change the relative position of two elements. 1 5 2 6 6 8 4 not stable 5 6 6 8 2 4 5 2 6 6 8 4 stable 5 6 6 8 2 4 In-situ algorithms require only a constant amount of additional memory. (Mergesort is not in-situ) 1 Every sorting algorithm can be made stable by storing the array position together with the element. 18

  54. Quiz: Sorting and Running Times Algorithm Comparisons Swaps average worst average worst Bubble Sort 19

  55. Quiz: Sorting and Running Times Algorithm Comparisons Swaps average worst average worst Θ( n 2 ) Θ( n 2 ) Bubble Sort 19

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