Alfred V. Aho, John E. Hopcroft. Jeffrey D. Ullman ,. • Data structures & their
algorithms.Harry R. Lewis, Larry Denenberg ,. • ++Algorithms in C.Robert
Sedgewick ...
מבני נתונים הקדמה ,סימונים ,ביצועים אסימפטוטיים
1
מטרות הקורס • לעשות היכרות עם קבוצה בסיסית של מבני נתונים ו-אלגוריתמים ולנתח את יעילותם הן מבחינת זמן והן מבחינת מקום
2
פניות ושאלות • אתר הקורסwww.cs.bgu.ac.il/~ds132 : • את פניותיכם אנא שילחו אל האימייל של הקורס: ds.bgu.13 AT gmail.com • אנא ציינו בכל אימייל את הדברים הבאים: – שמכם המלא ומספר הזהות שלכם והמחלקה אליה אתם שייכים ()CS ,SE, ISE or Ahva – הנושא של השאלה
• אנא כתבו קצר וברור • בירורים על תרגילי הבית בפורום שבאתר הקורס 3
ספרי לימוד )ספר לימוד עיקרי (קיים גם תרגום לעברית
•
Introduction to Algorithms, Cormen, Leiserson, Rivest and Stein (2nd edition, 2001) ספרים נוספים
• • • • •
•
•
Algorithm design : foundations, analysis, and Internet examples, Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms in Java, Michael T. Goodrich, Roberto Tamassia. Data structures and algorithms, Alfred V. Aho, John E. Hopcroft. Jeffrey D. Ullman. Data structures & their algorithms, Harry R. Lewis, Larry Denenberg. Algorithms in C++, Robert Sedgewick.
4
דרישות הקורס • Midterm Exams - There will be 1 obligatory midterm exam (3/5/13). The weight of the midterm exam is 16%. • Homework Assignments - The weight of the homework assignments is 14%. • Final Exam - In order to pass the course, you must pass the final exam (with grade of at least 56). The weight of the final exam is 70%. 5
אלגוריתמים תהליך חישובי ,מוגדר היטב ,שמקבל כקלט ערך (או קבוצת ערכים) ,ונותן כפלט ערך (או קבוצה של ערכים) כ" -פונקציה" של הקלט. ברקע כל אלגוריתם עומדת בעיה .היא ה" -פונקציה" שעל האלגוריתם לממש.
• דוגמה – קלט :סדרה של nמספרים a , a , ..., a – פלט :פרמוטציה מסודרת של a , a , ..., a – תהליך :מיון הכנסה. 6
n
2
1
n
2
1
מיון הכנסה
פסאודו קוד
7
נכונות • התכונה המשתמרת ()loop invariant – בתחילת כל מעבר של הלולאה החיצונית תת-המערך ] A[1 . . i −1מכיל את הערכים המקוריים שלו מסודרים בסדר נכון. – שאר האיברים נמצאים בתת-המערך ]A[i .. n
• בסיום i = n + 1 • לכן בסיום המערך ממוין • בצורה פורמלית ההוכחה מתבצעת באינדוקציה
8
ניתוח אלגוריתמים • השאלה המעניינת: – עד כמה האלגוריתם מורכב ( ,)complexityכלומר ,איזה משאבים האלגוריתם צורך • • • •
זמן ריצה גודל זיכרון רוחב פס ועוד
• המורכבות (צריכת המשאבים) היא פונקציה ())f(n של גודל הקלט n – במיון :מספר הערכים במיון – בכפל :אורך הערכים (בביטים) – פעולות על גרפים :מספר הקודקודים והקשתות בגרף 9
ניתוח אלגוריתמים • המורכבות Sשל אלגוריתם בריצה מסוימת יכולה להיות תלויה בקלט המסוים של אותה ריצה • מקרה טוב – נחמד שיש מקרים טובים ,אבל אי אפשר להבטיח חסם למורכבות
• מקרה גרוע – נותן חסם עליון למורכבות – נותן הבטחה ברורה ומפורשת
• מקרה ממוצע – נותן את תוחלת המורכבות – תלוי בהתפלגות הקלט ,שלא תמיד ידועה • אלגוריתמים הסתברותיים (רנדומליים) – מניחים שהאיברים של הקלט מתפלגים באופן אחיד – לפעמים מבצעים הגרלה כדי להבטיח שהקלט יתפלג באופן אחיד
10
ניתוח זמן של מיון הכנסה
tj is the number of times the while loop tests in line 5, for each j.
ניתוח זמן של מיון הכנסה • What can S be? – Best case (inner loop body never executed) • tj = 1 for all j • T(n) is a linear function
– Worst case (inner loop body executed for all elements) • tj = j • T(n) is a quadratic function
– Average case • ??? 12
איך ננתח? • לא נוכל לנתח מורכבות של אלגוריתמים באמצעות הרצתם בפועל – על איזו חומרה ותוכנה נריץ (מעבד ,זיכרון ,מהדר, מערכת הפעלה) – לא פשוט יהיה להסיק את המורכבות מהרצות בודדות
• בעיה נוספת: – אלגוריתם אחד יכול להיות יותר יעיל בהתחלה ,בעוד שהשני יכול להיות יותר מהיר בהמשך 13
קצב הגדילה • מושג קצב הגדילה "מנטרל" את הצורך בהגדרה מפורשת של מכונת חישוב מסוימת בשביל להשוות ביצועים של אלגוריתמים ,ומכריע גם את הבעיה הנוספת – באיזה קצב גדלה סיבוכיות האלגוריתם כשגודל הקלט גדל? – הגדרות פורמאליות לקצב גדילה ע"י חסמים • ,Oו -גדולים
14
מושג קצב הגדילה • ההגדרה של קצב הגדילה למעשה מאפשרת לנו להשאיר רק את "המרכיבים המשמעותיים" של נוסחת המורכבות – לזרוק מרכיבים פחות משמעותיים – לזרוק קבועים
• ולבטא באופן פשוט את סדר הגודל של המורכבות • לדוגמה f(n) = an2 + bn + c. Drop lower-order terms ⇒ an2. Ignore constant coefficient ⇒ n2. )Say, It is now grows like n2 (but it doesn’t equal n2 That is, its order of growth is like n2.
15
– – – – –
מושג קצב הגדילה • ההגדרה של קצב הגדילה למעשה מפשטת את האופן בו אנחנו מסתכלים על אלגוריתמים ותוכניות – כל הפעולות לוקחות אותו זמן (חיבור ,חיסור ,כפל, התניות ,קריאה לפונקציה וכו') – כל המשתנים הם תאי זיכרון בגודל זהה – זמן הגישה למשתנים ב RAM -קבוע
16
Order of Growth
17
Oגדול (חסם עליון) •
•
המושג של ה O -הגדול מגדיר את קבוצת הפונקציות שקצב הגדילה שלהם לא יותר גרוע מקצב הגדילה של פונקציה מסוימת ).g(n בהינתן פונקציה ) f(nהמבטאת מורכבות של אלגוריתם נוכל להגיד אם היא בO - גדול של פונקציה ) g(nאו לא. –
כלומר ,האם המורכבות ) f(nלא גרועה יותר מ.g(n) -
O(g(n)) = { f(n) : there exist positive constants c and natural n0 such that 0 ≤ f (n) ≤ cg(n) for all n ≥ n0 }.
18
))If f(n) ∈ O(g(n)), we write f(n) = O(g(n
•
Example: 2n2 = O(n3), with c = 1 and n0 = 2.
•
דוגמאות ל O -גדול
19
פולינומים • Claim: A polynomial of degree k is O(nk)
• Proof: Suppose f(n) = bknk + bk-1nk-1 + … + b1n + b0 Let ai = |bi| f(n) aknk + ak-1nk-1 + … + a1n + a0 = ni n ai k n k
n k ai
cn k
20
גדול (חסם תחתון) •
•
המושג של ה -הגדול מגדיר את קבוצת הפונקציות שקצב הגדילה שלהם לא יותר טוב מקצב הגדילה של פונקציה מסוימת ).g(n בהינתן פונקציה ) f(nהמבטאת מורכבות של אלגוריתם נוכל להגיד אם היא ב - גדול של פונקציה ) g(nאו לא. –
כלומר ,האם המורכבות ) f(nלא טובה יותר מ.g(n) -
(g(n)) = { f (n) : there exist positive constants c and natural n0 such that 0 ≤ cg(n) ≤ f (n) for all n ≥ n0 } . 0
0
21
))If f(n) ∈ (g(n)), we write f(n) = (g(n
•
Example: √n = (lg n), with c = 1 and n0 = 16.
•
Big
22
גדול (חסם הדוק) •
•
המושג של ה -מגדיר את קבוצת הפונקציות שקצב הגדילה שלהם זהה לקצב הגדילה של פונקציה מסוימת ).g(n בהינתן פונקציה ) f(nהמבטאת מורכבות של אלגוריתם נוכל להגיד אם היא ב - גדול של פונקציה ) g(nאו לא. –
כלומר ,האם המורכבות ) f(nשווה (לא טובה יותר ולא גרועה יותר) ל.g(n) -
(g(n)) = { f (n) : there exist positive constants c1, c2, and natural n0 such that 0 ≤ c1g(n) ≤ f (n) ≤ c2g(n) for all n ≥ n0 }.
23
))If f(n) ∈ (g(n)), we write f(n) = (g(n
•
Example: n2/2 − 2n = (n2), with c1 = 1/4, c2 = 1/2, and n0 = 8.
•
דוגמה Claim: Let f(n) = 1/2n2 - 3n. Then, f(n) = Θ(n2).
Proof: Need to show that there are c1, c2 > 0 and n0 ≥ 0 such that c1n2 ≤ 1/2n2 - 3n ≤ c2n2 for all n ≥ n0. Dividing by n2 yields c1 ≤ 1/2 - 3/n ≤ c2.
Holds for c1 = 1/14, c2 = 1/2, and n0 = 7 24
משפט For any two functions f(n) and g(n), we have f(n) = Θ(g(n)) iff f(n) = O(g(n)) and f(n) = Ω(g(n)). 25
המורכבות של מיון הכנסה • Claim: – Insertion Sort is Θ(n2).
• Proof Worst case runtime is an2 + bn + c We saw that a polynomial of degree k is O(nk) It is not hard to see that a polynomial of degree k is also (nk) Hence insertion sort is Θ(n2) 26
Asymptotic Notation in Equations • Stands alone on the right-hand side means set membership – n = O(n2) means n є O(n2)
• Otherwise, it stands for some anonymous function – “2n2 + 3n + 1 = 2n2 + Θ(n)” means that 2n2 + 3n + 1 = 2n2 + f(n) for some functio f(n) in Θ(n).
• On the left-hand side stands for any anonymous function – 2n2 + Θ(n) = Θ(n2) means that for any function g(n) in Θ(n) there is a function f(n) in Θ(n2) such that 2n2 + g(n) = f(n)
27
Asymptotic Notations Intuition • Intuitively:
– O is like , – is like , – is like =.
• But, unlike real numbers, we might not be able to compare functions – n1+sin n and n, since 1 + sin n oscillates between 0 and 2 – n2(n mod 2) - 1 and n, since 2(n mod 2) – 1 jumps between -1 and 1
28
Relational Properties • Transitivity: – f(n) = Θ(g(n)) and g(n) = Θ(h(n)) imply f(n) = Θ(h(n)) – Same for O, .
• Reflexivity: – f(n) = Θ(f(n)) – Same for O,
• Symmetry: – f(n) = Θ(g(n)) if and only if g(n) = Θ(f(n))
• Transpose Symmetry: – f(n) = O(g(n)) if and only if g(n) = Ω(f(n)) 29
The Divide & Conquer Paradigm • Divide: – Divide the problem into a number of sub (smaller) problems. – Solve the sub problems recursively. – If the sub problem sizes are small enough, however, just solve the sub problems in a straightforward manner.
• Conquer: – Combine the solutions of the sub problems into the solution for the original problem. 30
Merge Sort • Divide: – Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. – Sort the two subsequences recursively using merge sort. – If the subsequences sizes are one, do nothing.
• Conquer: – Merge the two sorted subsequences to produce the sorted answer. 31
Merge Sort mergeSort (A, left, right) 1. if (left < right) then 2. mid = ⌊(left + right) / 2⌋ 3. mergeSort (A, left, mid) 4. mergeSort (A, mid+1, right) 5. merge (A, left, mid, right)
32
Merge merge(A, left, mid, right) 1. n1 ← mid - left + 1 2. n2 ← right - mid 3. create arrays L[1 ‥ n1 + 1] and R[1 ‥ n2 + 1] 4. for i ← 1 to n1 do 5. L[i] ← A[p + i - 1] 6. for j ← 1 to n2 do 7. R[j] ← A[q + j] 8. L[n1 + 1] ← ∞ 9. R[n2 + 1] ← ∞ 10. i ← 1 11. j ← 1 12. for k ← p to r do 13. if L[i] ≤ R[j] then 14. A[k] ← L[i] 15. i←i+1 16. else 17. A[k] ← R[j] 18. j←j+1 33
נכונות • באינדוקציה – נשים לב :הפירוק סופו שיביא לבעיית הבסיס (הבעיות הולכות וקטנות) – בסיס :מערך באורך 1ממוין – צעד האינדוקציה :משני החצאים הממוינים האלגוריתם מרכיב שלם ממויין
34
Complexity of Merge Sort
35
Complexity of Merge Sort Statement mergeSort (A, left, right) if (left < right) then mid = ⌊(left + right) / 2⌋ mergeSort(A, left, mid) mergeSort(A, mid+1, right) merge(A, left, mid, right)
Cost T(n) (1) (1) T(n/2)+(1) T(n/2)+(1) (n)+(1)
36
Standard Notations & Common Functions • Monotonicity : – – – –
f(n) is monotonically increasing if m ≤ n ⇒ f(m) ≤ f(n). f(n) is monotonically decreasing if m ≥ n ⇒ f(m) ≥ f(n). f(n) is strictly increasing if m < n ⇒ f(m) < f(n). f(n) is strictly decreasing if m > n ⇒ f(m) > f(n).
37
Floors & Ceilings • For any integer n and integers a ≠ 0 and b ≠ 0, – ⌈⌈n/a⌉/b⌉ = ⌈n/ab⌉ – ⌊⌊n/a⌋/b⌋ = ⌊n/ab⌋ – Also mix of the two
38
Polynomials • A polynomial is asymptotically positive if and only if ad > 0. • For an asymptotically positive polynomial p(n) of degree d, we have p(n) = (nd). • For any integer x ≥ 0 and ε > 0, – So changing the exponent changes the order of growth.
39
Exponentials • Useful identities: – – – – – –
a0 = 1, a1 = a, a-1 = 1/a, (am)n = amn, (am)n = (an)m, an = am+n .
• Order of growth of polynomials and exponentials: – For all real constants a and b such that a > 1, – which implies that nb =O(an) 40
Exponentials • For any integer a ≥ 1 and ε > 0, – So changing the base of the exponential changes the order of growth.
41
Logarithms • Notations: – – – –
lg n = log2 n (binary logarithm) ln n = loge n (natural logarithm) lgk n = (lg n)k (exponentiation) lg lg n = lg(lg n) (composition)
• In the expression logb a: – If b constant, then logb a is strictly increasing as a increases. – If a constant, then logb a is strictly decreasing as b increases.
42
Logarithms • For all real a > 0, b > 0, c > 0, and n, and logarithm bases are not 1:
• Changing the base only changes the value by a constant factor, – So all are of same order of growth. 43
Logarithms • For all real constants a and b such that a > 1:
O
44
Factorials
• Sterling's approximation:
• From Sterling's:
45