Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
מבני נתונים
אסימפטוטייםסימונים וביצועים
Tzachi (Isaac) Rosen
מטרות הקורס
מבנילעשות היכרות עם קבוצה בסיסית של •
הן יעילותםולנתח את אלגוריתמים-ו נתונים מבחינת זמן והן מבחינת מקום
Tzachi (Isaac) Rosen
פניות ושאלות
122www.cs.bgu.ac.il/~ds:אתר•
את פניותיכם ושאלותיכם אנא שילחו אל האימייל • cs.bgu.ac.il122ds@: של הקורס
:אנא ציינו בכל אימייל את הדברים הבאים•
והמחלקה אליה שימכם המלא ומספר הזהות שלכם– (CS ,SE, ISE or Ahva)אתם שייכים
הנושא של השאלה–
וברוראנא כתבו קצר •
Tzachi (Isaac) Rosen
ספריי לימוד
(קיים גם תרגום לעברית)ספר לימוד עיקרי •
• 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.
Tzachi (Isaac) Rosen
דרישות הקורס
• Midterm Exams - There will be 1 obligatory midterm exam. 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%.
Tzachi (Isaac) Rosen
אלגוריתמים
או קבוצת )ערך כקלטשמקבל , מוגדר היטב, תהליך חישובי
"פונקציה" -כ( או קבוצה של ערכים)ערך כפלטונותן , (ערכים .של הקלט
שעל " פונקציה" -היא ה. בעיהברקע כל אלגוריתם עומדת .האלגוריתם לממש
דוגמה• a1, a2, ..., anמספרים nסדרה של : קלט–
a1, a2, ..., anפרמוטציה מסודרת של : פלט–
.מיון הכנסה: תהליך–
Tzachi (Isaac) Rosen
מיון הכנסה
Tzachi (Isaac) Rosen
insertionSort (A, n) for (i = 2 to n) do key = A[i] j = i – 1 while (j > 0 & A[j] > key) do A[j+1] = A[j] j = j – 1 A[j+1] = key
פסאודו קוד
נכונות
(loop invariant)התכונה המשתמרת •
מכיל את A[1 . . i −1]המערך -בתחילת כל מעבר של הלולאה החיצונית תת–
.הערכים המקוריים שלו מסודרים בסדר נכון
A[i .. n]המערך -שאר האיברים נמצאים בתת–
i = n + 1בסיום •
לכן בסיום המערך ממוין•
בצורה פורמלית ההוכחה מתבצעת באינדוקציה•
Tzachi (Isaac) Rosen
ניתוח אלגוריתמים
:השאלה המעניינת•איזה , כלומר, (complexity) מורכבעד כמה האלגוריתם –
האלגוריתם צורך משאבים זמן ריצה•
גודל זיכרון•
רוחב פס•
ועוד•
(f(n))פונקציה היא ( צריכת המשאבים)המורכבות • של גודל הקלט
במיוןמספר הערכים : במיון–
(בביטים)אורך הערכים : בכפל–
בגרף מספר הקודקודים והקשתות: פעולות על גרפים–
Tzachi (Isaac) Rosen
ניתוח אלגוריתמים
בקלטהמורכבות של אלגוריתם בריצה מסוימת יכולה להיות תלויה • של אותה ריצה המסוים
מקרה טוב• אבל אי אפשר להבטיח חסם למורכבות, נחמד שיש מקרים טובים–
מקרה גרוע• למורכבותחסם עליון נותן –
ברורה ומפורשת הבטחהנותן –
מקרה ממוצע• המורכבות תוחלתנותן את –
שלא תמיד ידועה, תלוי בהתפלגות הקלט– (רנדומליים) אלגוריתמים הסתברותיים•
מניחים שהאיברים של הקלט מתפלגים באופן אחיד–
לפעמים מבצעים הגרלה כדי להבטיח שהקלט יתפלג באופן אחיד–
Tzachi (Isaac) Rosen
ניתוח זמן של מיון הכנסה
Statement Cost insertionSort (A, n) T(n) for (i = 2 to n) do c1n key = A[i] c2(n-1) j = i - 1; c3(n-1) while (j > 0 & A[j] > key) do c4S A[j+1] = A[j] c5(S-(n-1)) j = j - 1 c6(S-(n-1)) A[j+1] = key c7(n-1)
• where, si is number of while expression evaluations
for the i'th loop iteration and • Note that
Tzachi (Isaac) Rosen
n
2ij 1)(s1)(nS
n32 s s s S
ניתוח זמן של מיון הכנסה
T(n) = c1n + c2(n-1) + c3(n-1) + c4S + c5(S - (n-1)) + c6(S - (n-1)) + c7(n-1) = c8S + c9n + c10
• What S can be?
– Best case (inner loop body never executed) • si = 1 • T(n) is a linear function
– Worst case (inner loop body executed for all elements)
• si = i • T(n) is a quadratic function
– Average case • ???
Tzachi (Isaac) Rosen
?איך ננתח
לא נוכל לנתח מורכבות של אלגוריתמים באמצעות •
הרצתם בפועל
, מהדר, זיכרון, מעבד)על איזו חומרה ותוכנה נריץ –
(מערכת הפעלה
לא פשוט יהיה להסיק את המורכבות מהרצות בודדות–
:בעיה נוספת•
בעוד , אלגוריתם אחד יכול להיות יותר יעיל בהתחלה–
שהשני יכול להיות יותר מהיר בהמשך
Tzachi (Isaac) Rosen
קצב הגדילה
את הצורך בהגדרה " מנטרל"קצב הגדילה מושג •
מפורשת של מכונת חישוב מסוימת בשביל
ומכריע גם את , להשוות ביצועים של אלגוריתמים
הבעיה הנוספת
כשהבעיה באיזה קצב גדלה סיבוכיות האלגוריתם –
?גדלה לאינסוף
הגדרות פורמאליות–
•O , ו- גדולים
•o ו- ω קטנים
Tzachi (Isaac) Rosen
(חסם הדוק)גדול
זההמגדיר את קבוצת הפונקציות שקצב הגדילה שלהם הגדול -המושג של ה• .g(n)הגדילה של פונקציה מסוימת לקצב
-המבטאת מורכבות של אלגוריתם נוכל להגיד אם היא ב f(n)בהינתן פונקציה • .או לא g(n)גדול של פונקציה
.g(n) -ל( לא טובה יותר ולא גרועה יותר) שווה f(n) המורכבותהאם , כלומר–
• 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.
Tzachi (Isaac) Rosen
(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 }.
דוגמה
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
Tzachi (Isaac) Rosen 16
מושג קצב הגדילה
למעשה מאפשרת לנו להשאיר רק קצב הגדילהההגדרה של • של נוסחת המורכבות "המרכיבים המשמעותיים"את
לזרוק מרכיבים פחות משמעותיים–
לזרוק קבועים–
של המורכבותסדר הגודל ולבטא באופן פשוט את •
לדוגמה•– 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.
Tzachi (Isaac) Rosen
קצב הגדילהמושג
למעשה מפשטת את קצב הגדילהההגדרה של •
האופן בו אנחנו מסתכלים על אלגוריתמים ותוכניות
, כפל, חיסור, חיבור)כל הפעולות לוקחות אותו זמן –
('קריאה לפונקציה וכו, התניות
כל המשתנים הם תאי זיכרון בגודל זהה–
קבוע RAM -זמן הגישה למשתנים ב–
Tzachi (Isaac) Rosen
Order of Growth
Tzachi (Isaac) Rosen 19
O (חסם עליון)גדול
לא יותר הגדול מגדיר את קבוצת הפונקציות שקצב הגדילה שלהם O -המושג של ה• .g(n)מקצב הגדילה של פונקציה מסוימת גרוע
O -המבטאת מורכבות של אלגוריתם נוכל להגיד אם היא ב f(n)בהינתן פונקציה • .או לא g(n)גדול של פונקציה
.g(n) -מלא גרועה יותר f(n) המורכבותהאם , כלומר–
• If f(n) ∈ O(g(n)), we write f(n) = O(g(n))
• Example: 2n2 = O(n3), with c = 1 and n0 = 2.
Tzachi (Isaac) Rosen
O(g(n)) = { f(n) : there exist positive constants c and natural n0 such that 0 ≤ f (n) ≤ cg(n) for all n ≥ n0 }.
גדול O -דוגמאות ל
Tzachi (Isaac) Rosen 21
פולינומים
• Claim: A polynomial of degree k is O(nk)
• Proof: Suppose f(n) = bkn
k + bk-1nk-1 + … + b1n + b0
Let ai = |bi|
f(n) aknk + ak-1nk-1 + … + a1n + a0 =
k
i
k
k
i
i
k cnann
nan
Tzachi (Isaac) Rosen 22
(חסם תחתון)גדול
לא יותר מגדיר את קבוצת הפונקציות שקצב הגדילה שלהם הגדול -המושג של ה• .g(n)הגדילה של פונקציה מסוימת מקצב טוב
-המבטאת מורכבות של אלגוריתם נוכל להגיד אם היא ב f(n)בהינתן פונקציה • .או לא g(n)גדול של פונקציה
.g(n) -מלא טובה יותר f(n) המורכבותהאם , כלומר–
• If f(n) ∈ (g(n)), we write f(n) = (g(n))
• Example: √n = (lg n), with c = 1 and n0 = 16.
Tzachi (Isaac) Rosen
(g(n)) = { f (n) : there exist positive constants c and natural n0 0 such that 0 ≤ cg(n) ≤ f (n) for all n ≥ n0 0 } .
Big
Tzachi (Isaac) Rosen 24
המורכבות של מיון הכנסה
• 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)
Tzachi (Isaac) Rosen 25
משפט
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)).
Tzachi (Isaac) Rosen 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)
Tzachi (Isaac) Rosen 27
o & ω
o(g(n)) = {f (n) : for all constants c > 0, there exists a constant n0 > 0 such that 0 ≤ f(n) < cg(n) for all n ≥ n0}
ω(g(n)) = {f (n) : for all constants c > 0, there exists a constant n0 > 0 such that 0 ≤ cg(n) < f(n) for all n ≥ n0}
Tzachi (Isaac) Rosen 28
Asymptotic Notations Intuition
• Intuitively: – o is like < ,
– O is like ,
– 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
Tzachi (Isaac) Rosen 29
Relational Properties
• Transitivity: – f(n) = Θ(g(n)) and g(n) = Θ(h(n)) imply f(n) = Θ(h(n)) – Same for O, , o, and ω.
• 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)) – f(n) = o(g(n)) if and only if g(n) = (f(n))
Tzachi (Isaac) Rosen 30
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.
Tzachi (Isaac) Rosen 31
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.
Tzachi (Isaac) Rosen 32
Merge Sort
Tzachi (Isaac) Rosen 33
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)
Merge
Tzachi (Isaac) Rosen
34
merge(A, left, mid, right) n1 ← mid - left + 1 n2 ← right - mid create arrays L[1 ‥ n1 + 1] and R[1 ‥ n2 + 1] for i ← 1 to n1 do L[i] ← A[p + i - 1] for j ← 1 to n2 do R[j] ← A[q + j] L[n1 + 1] ← ∞ R[n2 + 1] ← ∞ i ← 1 j ← 1 for k ← p to r do if L[i] ≤ R[j] then A[k] ← L[i] i ← i + 1 else A[k] ← R[j] j ← j + 1
Complexity of Merge Sort
Tzachi (Isaac) Rosen 35
Complexity of Merge Sort
Statement Cost
mergeSort (A, left, right) T(n)
if (left < right) then (1)
mid = floor((left + right) / 2) (1)
mergeSort(A, left, mid) T(n/2)+(1)
mergeSort(A, mid+1, right) T(n/2)+(1)
merge(A, left, mid, right) (n)+(1)
Tzachi (Isaac) Rosen 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).
Tzachi (Isaac) Rosen 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
Tzachi (Isaac) Rosen 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.
Tzachi (Isaac) Rosen 39
Exponentials
• Useful identities: – a0 = 1, – a1 = a, – a-1 = 1/a, – (am)n = amn, – (am)n = (an)m, – aman = am+n .
• Order of growth of polynomials and exponentials: – For all real constants a and b such that a > 1, – which implies that
Tzachi (Isaac) Rosen 40
Exponentials
• For any integer a ≥ 1 and ε > 0,
– So changing the base of the exponential changes the order of growth.
Tzachi (Isaac) Rosen 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.
Tzachi (Isaac) Rosen 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.
Tzachi (Isaac) Rosen 43
Logarithms
• For all real constants a and b such that a > 1:
Tzachi (Isaac) Rosen 44
Factorials
• Sterling's approximation:
• From Sterling's:
Tzachi (Isaac) Rosen 45