Presentation is loading. Please wait.

Presentation is loading. Please wait.

קורס מחשב לרפואנים 274121 הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל.

Similar presentations


Presentation on theme: "קורס מחשב לרפואנים 274121 הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל."— Presentation transcript:

1 קורס מחשב לרפואנים 274121 הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל

2 משאבי זמן וזיכרון של תוכניות מחשב 2 אלו מתתי התוכניות הבאות צורכת יותר זמן? הנחה: כל פעולה עולה 1 יחידות זמן. עבור n=0 5 פעולות כל איטרציה מוסיפה 5 פעולות Time(n) = 5 + 5*n עבור n=0 4 פעולות כל איטרציה מוסיפה 3 פעולות סך מספר האיטרציות הוא n 2 Time(n) = 4 + 3*n 2 1. y = 0; 2. i = 0 + 0 + 0; 3. while i < n 4. i=i+1; 5. y = y + i; 6. end 1. i = n*n; 2. y = 0; 3. while i >0 4. i = i - 1; 5. end

3 משאבי זמן וזיכרון של תוכניות מחשב 3 >> n=0:10; >> plot(n,5+5*n,n,4+3*n.^2) >> xlabel('n') >> ylabel('time')

4 משאבי זמן וזיכרון של תוכניות מחשב 4 אמנם עבור n-ים קטנים הפרבולה מתחת לישר אבל עבור n-ים גדולים הפרבולה גבוהה יותר. מה שמעניין אותנו הם סדרי הגודל: התוכנית הראשונה צורכת זמן בסדר גודל לינארי התוכנית השנייה צורכת זמן בסדר גודל ריבועי שאלה: נניח שעבור n מסויים התוכנית הראשונה תצרוך זמן ריצה של דקה, מה יקרה אם נכפיל את גודלו של nב 1000 ? שאלה: נניח שעבור n מסויים התוכנית השנייה תצרוך זמן ריצה של דקה, מה יקרה אם נכפיל את גודלו של nב 1000 ? לינארי ריבועי

5 משאבי זמן וזיכרון של תוכניות מחשב (סיבוכיות) 5 דוגמאות לסדרי גודל קבוע: (מסומן ב-(O(1): 1, 10, 123.235, 1000 1000 לינארי (מסומן ב-(O(n) : 5 + 5*n, 5n+1999, 0.001n+100000, n+n 0.7 ריבועי (מסומן ב-(O(n 2 ) : 5 + 5*n 2, 5n+1999+n 2, 0.001n 2 +100000, 1+2+…+n פולינומי: (מסומן ב poly(n) או (n O(1): 5 + 5*n 11, 5n+1999+n 12, 0.001n 22 +100000, לוגריתמי (מסומן ב-((O(log(n) : 5 + 5*log(n), log(1+2+3+…+n), log 22 (n) ובלתי נסבל: אקספוננציאלי:, 1.1 3n+2,2 n שאלה: נניח שעבור n מסויים התוכנית האקספוננציאלית תצרוך זמן ריצה של שניה אחת, מה יקרה אם נגדיל את גודלו של nב 100 ? תשובה: 2 100 שניות > 10 29 שניות > 10 26 שעות > 10 22 שנים

6 6 דוגמאות לסדרי גודל קבוע: (מסומן ב-(O(1): 1, 10, 123.235, 1000 1000 לינארי (מסומן ב-(O(n) : 5 + 5*n, 5n+1999, 0.001n+100000, n+n 0.7 ריבועי (מסומן ב-(O(n 2 ) : 5 + 5*n 2, 5n+1999+n 2, 0.001n 2 +100000, 1+2+…+n פולינומי: (מסומן ב poly(n) או (n O(1): 5 + 5*n 11, 5n+1999+n 12, 0.001n 22 +100000, לוגריתמי (מסומן ב-((O(log(n) : 5 + 5*log(n), log(1+2+3+…+n), log 22 (n) ובלתי נסבל: אקספוננציאלי:, 1.1 3n+2,2 n שאלה: נניח שעבור n מסויים התוכנית הלוגריתמית תצרוך זמן ריצה של דקה אחת, מה יקרה אם נגדיל את גודלו של nפי 10 1000000 ? תשובה: הזמן הקודם t=log(n) הזמן החדש 10 7 +t >log(n) +log(10 1000000 ) =log(10 1000000 n) שזה טיפ טיפה יותר מדקה. כלומר זניח. משאבי זמן וזיכרון של תוכניות מחשב (סיבוכיות)

7 סיבוכיות זמן ומקום כתוב פונקציה שמחשבת סכום מערך חד מימדי (ללא שימוש ב sum ) 7 1.function y = my_sum(x) 2. y = 0; n = length(x); 3. for i = 1:n 4. y = y + x(i); 5. end סיבוכיות זמן: כל איטרציה דורשת זמן קבוע, לכן סיבוכיות הזמן היא לינארית, כלומר.o(n) סיבוכיות זכרון נוסף: המערך x, לא משוכפל בגלל שהפונקציה לא משנה את ערכו. מלבד המערך x, יש לנו צורך במערך 1:n, ולכן הזכרון הנוסף הוא לינארי. אפשר להגיע לזכרון קבוע ע"י ריצה ישירה על איברי המערך x 1.function y = my_sum(x) 2. y = 0; 3. for next = x 4. y = y + next; 5. end

8 מה סיבוכיות זמן/זכרון של תתי התוכניות הבאות: 8 משאבי זמן וזיכרון של תוכניות מחשב (סיבוכיות) a = 1:n; max(a); קריאה לפונקציה min(a(2:n)) קריאה לפונקציה sum(n:n^2)

9 Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 5 12 35 42 77101 1 2 3 4 5 6

10 מיונים שנלמד מיון בזמן ריבועי – היום: מיון ע"י מקסימום מיון בועות – Bubble Sort מיון בזמן לינארי (של ציונים) – היום מיון דליים – Bucket sort מיונים יעילים (בזמן O(nlogn)) – בעוד כשבועיים Quick Sort Merge Sort 10

11 Sort by max: time complexity O(n 2 ) 1.function a = sort_by_max( a ) 2.% sort members of array in non decreasing order 3. for top = length(a):-1:1 % at this point all members above index “top” are in their proper location 4. i = index_of_max(a,top); 5. 6. %swap a(i) with a(top) 7. temp = a(i); a(i) = a(top); a(top) = temp; 8. end 9.end 10. 11.function i_max = index_of_max( a, n ) 12.% find the index of the maximum member among a(1:n) 13. i_max = 1; 14. for i = 2:n % invariant: at this point a(i_max) = MAXIMUM{a(1), a(2)…a(i-1)} 15. if a(i) > a(i_max) 16. i_max = i; 17. end 18. end 19.end 11

12 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 3542 77 101 1 2 3 4 5 6

13 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 3542 77 101 Swap 4277 1 2 3 4 5 6

14 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 3577 42 101 Swap 3577 1 2 3 4 5 6

15 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 7735 42 101 Swap 1277 1 2 3 4 5 6

16 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 77 1235 42 101 No need to swap 1 2 3 4 5 6

17 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 5 77 1235 42 101 Swap 5101 1 2 3 4 5 6

18 "Bubbling Up" the Largest Element Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to the end using pair-wise comparisons and swapping 77 1235 42 5 101 Largest value correctly placed 1 2 3 4 5 6

19 The “Bubble Up” Algorithm 1.for index = 1:top-1 2.%Invariant: a(index) is maximum among a(1:index) 3. if a(index)>a(index+1) 4. a([index,index+1]) = a([index+1,index]); 5. end 6.end * 77 1235 42 5 101 1 2 3 4 top=5 6

20 Items of Interest Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process 77 1235 42 5 101 Largest value correctly placed 1 2 3 4 top=5 6

21 Repeat “Bubble Up” How Many Times? If we have N elements… And if each time we bubble an element, we place it in its correct location… Then we repeat the “bubble up” process N – 1 times. This guarantees we’ll correctly place all N elements.

22 “Bubbling” All the Elements 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35 5 12 77 1 2 3 4 5 6 10142 35 12 5 77 1 2 3 4 5 6 101 N - 1

23 Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 577 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35 5 12 77 1 2 3 4 5 6 101

24 On the N th “bubble up”, we only need to do MAX-N comparisons. For example: –This is the 4 th “bubble up” –MAX-N is 6 –Thus we have 2 comparisons to do 42 5 35 12 77 1 2 3 4 5 6 101 Reducing the Number of Comparisons

25 Already Sorted Collections? What if the collection was already sorted? What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? We want to be able to detect this and “stop early”! 42 35 12 5 77 1 2 3 4 5 6 101

26 Using a Boolean “Flag” We can use a boolean variable to determine if any swapping occurred during the “bubble up.” If no swapping occurred, then we know that the collection is already sorted! This boolean “flag” needs to be reset after each “bubble up.”

27 Putting It All Together

28 1.function a = bubble_sort( a ) 2. n = length(a); 3. for top = n-1:-1:1 4. % all above top+1 are placed in their final position 5. did_swap = false; 6. for index = 1:top 7. %disp(a) 8. if a(index)>a(index+1) 9. a([index,index+1]) = a([index+1,index]); 10. did_swap = true; 11. end 12. end 13. if ~did_swap 14. return 15. end 16. end 17.end Inner loop Bubble up Outer loop Bubble_sort.m

29 An Animated Example 674523146339842 to_do index 7 N 8 did_swap true 1 2 3 4 5 6 7 8

30 An Animated Example 674523146339842 to_do index 7 1 N 8 did_swap false 1 2 3 4 5 6 7 8

31 An Animated Example 674523146339842 to_do index 7 1 N 8 Swap did_swap false 1 2 3 4 5 6 7 8

32 An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true

33 An Animated Example 674598146332342 to_do index 7 2 N 8 did_swap true 1 2 3 4 5 6 7 8

34 An Animated Example 674598146332342 to_do index 7 2 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

35 An Animated Example 679845146332342 to_do index 7 2 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

36 An Animated Example 679845146332342 to_do index 7 3 N 8 did_swap true 1 2 3 4 5 6 7 8

37 An Animated Example 679845146332342 to_do index 7 3 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

38 An Animated Example 671445986332342 to_do index 7 3 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

39 An Animated Example 671445986332342 to_do index 7 4 N 8 did_swap true 1 2 3 4 5 6 7 8

40 An Animated Example 671445986332342 to_do index 7 4 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

41 An Animated Example 671445698332342 to_do index 7 4 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

42 An Animated Example 671445698332342 to_do index 7 5 N 8 did_swap true 1 2 3 4 5 6 7 8

43 An Animated Example 671445698332342 to_do index 7 5 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

44 An Animated Example 981445667332342 to_do index 7 5 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

45 An Animated Example 981445667332342 to_do index 7 6 N 8 did_swap true 1 2 3 4 5 6 7 8

46 An Animated Example 981445667332342 to_do index 7 6 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

47 An Animated Example 331445667982342 to_do index 7 6 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

48 An Animated Example 331445667982342 to_do index 7 7 N 8 did_swap true 1 2 3 4 5 6 7 8

49 An Animated Example 331445667982342 to_do index 7 7 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

50 An Animated Example 331445667422398 to_do index 7 7 N 8 Swap did_swap true 1 2 3 4 5 6 7 8

51 After First Pass of Outer Loop 331445667422398 to_do index 7 8 N 8 Finished first “Bubble Up” did_swap true 1 2 3 4 5 6 7 8

52 The Second “Bubble Up” 331445667422398 to_do index 6 1 N 8 did_swap false 1 2 3 4 5 6 7 8

53 The Second “Bubble Up” 331445667422398 to_do index 6 1 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8

54 The Second “Bubble Up” 331445667422398 to_do index 6 2 N 8 did_swap false 1 2 3 4 5 6 7 8

55 The Second “Bubble Up” 331445667422398 to_do index 6 2 N 8 did_swap false Swap 1 2 3 4 5 6 7 8

56 The Second “Bubble Up” 334514667422398 to_do index 6 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

57 The Second “Bubble Up” 334514667422398 to_do index 6 3 N 8 did_swap true 1 2 3 4 5 6 7 8

58 The Second “Bubble Up” 334514667422398 to_do index 6 3 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

59 The Second “Bubble Up” 336144567422398 to_do index 6 3 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

60 The Second “Bubble Up” 336144567422398 to_do index 6 4 N 8 did_swap true 1 2 3 4 5 6 7 8

61 The Second “Bubble Up” 336144567422398 to_do index 6 4 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8

62 The Second “Bubble Up” 336144567422398 to_do index 6 5 N 8 did_swap true 1 2 3 4 5 6 7 8

63 The Second “Bubble Up” 336144567422398 to_do index 6 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

64 The Second “Bubble Up” 676144533422398 to_do index 6 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

65 The Second “Bubble Up” 676144533422398 to_do index 6 6 N 8 did_swap true 1 2 3 4 5 6 7 8

66 The Second “Bubble Up” 676144533422398 to_do index 6 6 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

67 The Second “Bubble Up” 426144533672398 to_do index 6 6 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

68 After Second Pass of Outer Loop 426144533672398 to_do index 6 7 N 8 did_swap true Finished second “Bubble Up” 1 2 3 4 5 6 7 8

69 The Third “Bubble Up” 426144533672398 to_do index 5 1 N 8 did_swap false 1 2 3 4 5 6 7 8

70 The Third “Bubble Up” 426144533672398 to_do index 5 1 N 8 did_swap false Swap 1 2 3 4 5 6 7 8

71 The Third “Bubble Up” 426234533671498 to_do index 5 1 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

72 The Third “Bubble Up” 426234533671498 to_do index 5 2 N 8 did_swap true 1 2 3 4 5 6 7 8

73 The Third “Bubble Up” 426234533671498 to_do index 5 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

74 The Third “Bubble Up” 422364533671498 to_do index 5 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

75 The Third “Bubble Up” 422364533671498 to_do index 5 3 N 8 did_swap true 1 2 3 4 5 6 7 8

76 The Third “Bubble Up” 422364533671498 to_do index 5 3 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8

77 The Third “Bubble Up” 422364533671498 to_do index 5 4 N 8 did_swap true 1 2 3 4 5 6 7 8

78 The Third “Bubble Up” 422364533671498 to_do index 5 4 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

79 The Third “Bubble Up” 422363345671498 to_do index 5 4 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

80 The Third “Bubble Up” 422363345671498 to_do index 5 5 N 8 did_swap true 1 2 3 4 5 6 7 8

81 The Third “Bubble Up” 422363345671498 to_do index 5 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

82 The Third “Bubble Up” 452363342671498 to_do index 5 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

83 After Third Pass of Outer Loop 452363342671498 to_do index 5 6 N 8 did_swap true Finished third “Bubble Up” 1 2 3 4 5 6 7 8

84 The Fourth “Bubble Up” 452363342671498 to_do index 4 1 N 8 did_swap false 1 2 3 4 5 6 7 8

85 The Fourth “Bubble Up” 452363342671498 to_do index 4 1 N 8 did_swap false Swap 1 2 3 4 5 6 7 8

86 The Fourth “Bubble Up” 452314334267698 to_do index 4 1 N 8 did_swap true Swap 1 2 3 4 5 6 7 8

87 The Fourth “Bubble Up” 452314334267698 to_do index 4 2 N 8 did_swap true 1 2 3 4 5 6 7 8

88 The Fourth “Bubble Up” 452314334267698 to_do index 4 2 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8

89 The Fourth “Bubble Up” 452314334267698 to_do index 4 3 N 8 did_swap true 1 2 3 4 5 6 7 8

90 The Fourth “Bubble Up” 452314334267698 to_do index 4 3 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8

91 The Fourth “Bubble Up” 452314334267698 to_do index 4 4 N 8 did_swap true 1 2 3 4 5 6 7 8

92 The Fourth “Bubble Up” 452314334267698 to_do index 4 4 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8

93 After Fourth Pass of Outer Loop 452314334267698 to_do index 4 5 N 8 did_swap true Finished fourth “Bubble Up” 1 2 3 4 5 6 7 8

94 The Fifth “Bubble Up” 452314334267698 to_do index 3 1 N 8 did_swap false 1 2 3 4 5 6 7 8

95 The Fifth “Bubble Up” 452314334267698 to_do index 3 1 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8

96 The Fifth “Bubble Up” 452314334267698 to_do index 3 2 N 8 did_swap false 1 2 3 4 5 6 7 8

97 The Fifth “Bubble Up” 452314334267698 to_do index 3 2 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8

98 The Fifth “Bubble Up” 452314334267698 to_do index 3 3 N 8 did_swap false 1 2 3 4 5 6 7 8

99 The Fifth “Bubble Up” 452314334267698 to_do index 3 3 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8

100 After Fifth Pass of Outer Loop 452314334267698 to_do index 3 4 N 8 did_swap false Finished fifth “Bubble Up” 1 2 3 4 5 6 7 8

101 Finished “Early” 452314334267698 to_do index 3 4 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 1 2 3 4 5 6 7 8

102 Summary “Bubble Up” algorithm will move largest value to its correct location (to the right) Repeat “Bubble Up” until all elements are correctly placed: –Maximum of N-1 times –Can finish early if no swapping occurs We reduce the number of elements we compare each time one is correctly placed

103 Truth in CS Act NOBODY EVER USES BUBBLE SORT NOBODY NOT EVER BECAUSE IT IS EXTREMELY INEFFICIENT

104 בדיקת זמנים... >> a = rand(1,5000); >> b = a; >> tic >> b = bubble_sort(a); >> toc Elapsed time is 12.014497 seconds. >> tic >> b = sort(a); >> toc Elapsed time is 0.002093 seconds. >> 104

105 קישורים - Bubble_sortBubble_sort http://en.wikipedia.org/wiki/Bubble_sort ויקיפדיהhttp://en.wikipedia.org/wiki/Bubble_sort http://www.youtube.com/watch?v=lyZQPjUT5B4 ריקוד אנימציה ביו-טיובhttp://www.youtube.com/watch?v=lyZQPjUT5B4 www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture16wdl.ppt מצגתwww.cc.gatech.edu/~bleahy/cs1311/cs1311lecture16wdl.ppt 105

106 מיון דליים: תכנית שממיינת מערך ציונים בזמן לינארי 1.function y = bucket_sort( x ) 2.h = zeros(1,101); % h(i) is the number of students with grade of i-1 3.for i = x+1 4. h(i) = h(i)+1; 5.end 6.k = 0; y = zeros(1,length(x)); 7.for i = 1:101 8. for j = 1:h(i) 9. k = k + 1; 10. y(k) = i - 1; 11. end 12.end 106


Download ppt "קורס מחשב לרפואנים 274121 הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל."

Similar presentations


Ads by Google