Download presentation
Presentation is loading. Please wait.
Published byJaqueline Greener Modified over 10 years ago
1
תרגול 8 Skip Lists Hash Tables
2
Skip Lists Definition: – A skip list is a probabilistic data structure where elements are kept sorted by key. – It allows quick search, insertions and deletions of elements with simple algorithms. – It is basically a linked list with additional pointers such that intermediate nodes can be skipped. – It uses a random number generator to make some decisions.
3
Skip Lists Skip Levels – Doubly Linked lists S 1..S h, each start at - ∞ and end at ∞. – Level S 1 - Doubly linked list containing all the elements in the set S. – Level S i is a subset of level S i-1. – Each element in Level i has the probability 1/2 to be in level i+1, thus if there are n elements in level S 1, the expected number of elements in level S i is (n/2) i-1. – The expected number of levels required is O(log n).
4
Skip Lists
5
דוגמה :
6
שאלה 1 הסבירו כיצד ניתן לממש את הפונקציה Select(S,k) המחזירה את האיבר ה k בגודלו בסקיפ ליסט S עם n איברים, בזמן ממוצע של O(log n). אילו שינויים עלינו לבצע בסקיפ ליסט ? תשובה : נשמור בכל תא p בסקיפ ליסט ערך dis(p) – מספר הערכים ( כלומר, מספר התאים ב S 1 ) בינו לבין התא הבא אחריו בשרשרת S i. על מנת לבצע חיפוש, נתחיל ב -∞ ברמה הגבוהה ביותר ונשמור את המיקום ( בהתחלה 0). בכל פעם שנתקדם בשרשרת נוסיף dis(p) למיקום. אם מספר המקומות שנותרו עד k< dis(p), נרד רמה ונמשיך.
7
שאלה 1 קוד :
8
שאלה 1 הדגמה : נבצע Search(7,S). pos=01467
9
Question 2 Write an algorithm that builds a skip list S from the given BST T with n elements (T can be unbalanced ), such that – the worst query time in S will be O(log n). – The time complexity of the algorithm should be O(n).
10
Question 2 Solution: Time Complexity: The inorder traversal is O(n). The running time of the rest of the algorithm is linear in the number of elements in the skip list, that is O(n). The worst query time in such a skip list is O(log n). This question demonstrates how to construct a deterministic skip-list from an ordered set of n keys in O(n) time.
11
Hash Tables Hash Function A hash function h maps keys of a given type into integers in a fixed interval [0,m-1] Uniform Hash Hash Table A hash table for a given key type consists of: Hash function h: keys-set →[0,m-1] Array (called table) of size m
12
Hash Tables Direct Addressing K is a set whose elements' keys are in the range [0,m-1]. Use a table of size m and store each element x in index x.key. Disadvantage: when |K| << m → waste of space Chaining h(k) = k mod m (This is an example of a common hash function) If h(k) is occupied, add the new element in the head of the chain at index h(k)
13
שאלה 3 נתון : טבלת גיבוב עם m=11 ופונקציות גיבוב h 1 (k)=k mod m h 2 (k)=1+(k mod (m-1)) הכניסו את האיברים הבאים לפי הסדר ( משמאל לימין ) 22, 1, 13, 11, 24, 33, 18, 42, 31 a. לטבלת גיבוב מבוססת שרשור, עם פונקציית גיבוב h(k)=h 1 (k).
14
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 Chaining
15
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 Chaining
16
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(22)=0 Chaining
17
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(22)=0 /22 Chaining
18
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 /22 Chaining
19
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(1)=1 /22 Chaining
20
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(1)=1 /22 /1 Chaining
21
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 /22 /1 Chaining
22
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(13)=2 /22 /1 Chaining
23
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(13)=2 /22 /1 /13 Chaining
24
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 /22 /1 /13 Chaining
25
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(11)=0 /22 /1 /13 Chaining
26
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(11)=0 11 /1 /13 /22 Chaining
27
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(24)=2 11 /1 24 /22 /13 Chaining
28
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(33)=0 33 /1 24 /13 11/22 Chaining
29
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(18)=7 33 /1 24 11 /13 /22 /18 Chaining
30
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(42)=9 33 /1 24 11 /13 /22 /18 /42 Chaining
31
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 / / / / / / 0 1 2 3 4 5 6 7 8 9 10 h(31)=9 33 /1 24 11 /13 /22 /18 31/42 Chaining
32
Hash Tables Open Addressing Linear Probing: h(k,i) = (h'(k) + i)mod m 0≤ i ≤ m-1 h'(k) - common hash function First try h(k,0) = h'(k), if it is occupied, try h(k,1) etc.. Advantage: simplicity Disadvantage: clusters, uses Θ(m) permutations of index addressing sequences Double Hashing: h(k,i) = (h 1 (k) + i·h 2 (k))mod m 0≤ i ≤ m-1 h 1 – hash function h 2 – step function First try h(k,0) = h 1 (k), if it is occupied, try h(k,1) etc. Advantage: less clusters, uses Θ(m*m) permutations of index addressing sequences
33
שאלה 3 נתון : טבלת גיבוב עם m=11 ופונקציות גיבוב h 1 (k)=k mod m h 2 (k)=1+(k mod (m-1)) הכניסו את האיברים הבאים לפי הסדר ( משמאל לימין ) 22, 1, 13, 11, 24, 33, 18, 42, 31 a. לטבלת גיבוב מבוססת שרשור, עם פונקציית גיבוב h(k)=h 1 (k). b. לטבלת גיבוב מבוססת linear probing, עם אותה פונקציית גיבוב. c. לטבלת גיבוב מבוססת double hashing, עם פונקציית גיבוב ראשית h 1 (k) ופונקציית צעד h 2 (k).
34
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 Linear Probing
35
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 Linear Probing
36
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(22)=0 Linear Probing
37
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(22)=0 פנוי Linear Probing
38
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(22)=0 22 פנוי Linear Probing
39
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 Linear Probing
40
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(1)=1 22 פנוי Linear Probing
41
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(1)=1 22 1 פנוי Linear Probing
42
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 1 Linear Probing
43
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(13)=2 22 1 פנוי Linear Probing
44
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(13)=2 22 1 13 פנוי Linear Probing
45
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 1 13 Linear Probing
46
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(11)=0 22 1 13 תפוס פנוי Linear Probing
47
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(11)=0 22 1 13 11 פנוי Linear Probing
48
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(24)=2 22 1 13 11 תפוס פנוי Linear Probing
49
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(24)=2 22 1 13 11 24 פנוי Linear Probing
50
תפוס שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(33)=0 22 1 13 11 24 תפוס פנוי תפוס Linear Probing
51
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(33)=0 22 1 13 11 24 33 פנוי Linear Probing
52
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(18)=7 22 1 13 11 24 33 18 פנוי Linear Probing
53
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(42)=9 22 1 13 11 24 33 18 42 פנוי Linear Probing
54
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(31)=9 22 1 13 11 24 33 18 42 פנוי תפוס Linear Probing
55
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 h(31)=9 22 1 13 11 24 33 18 42 31 פנוי Linear Probing
56
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 h 1 (k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
57
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
58
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 h 1 (22)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
59
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 פנוי h 1 (22)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
60
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 פנוי h 1 (22)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
61
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
62
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 h 1 (1)=1 22 פנוי h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
63
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 פנוי h 1 (1)=1 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
64
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
65
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 h 1 (13)=2 22 1 פנוי h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
66
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 פנוי h 1 (13)=2 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
67
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10)
68
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 תפוס פנוי h 1 (11)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (11)=2
69
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 פנוי h 1 (11)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (11)=2
70
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 h 1 (24)=2 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (24)=5 תפוס פנוי
71
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 h 1 (24)=2 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (24)=5 פנוי
72
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 h 1 (33)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (33)=4 תפוס פנוי
73
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 h 1 (33)=0 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (33)=4 פנוי
74
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 18 24 33 h 1 (18)=7 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (18)=9 תפוס פנוי
75
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 18 24 33 42 h 1 (42)=9 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) פנוי
76
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 18 31 24 33 42 h 1 (31)=9 h 1 (k)=k mod 11 Double Hashing Step Function h 2 (k)=1+ (k mod 10) h 2 (31)=2 תפוס פנוי תפוס
77
שאלה 3 טבלת השוואה :
78
Hash Tables Average (expected) Search Time לוח !
79
Hash Tables
80
שאלה 4 בשאלה 3 השתמשנו, עבור טבלת גיבוב עם m=11, בפונקציית גיבוב ראשית h 1 (k)=k mod 11 ופונקציית צעד h 2 (k)= k mod 10 + 1. a. האם ניתן היה להשתמש בפונקציה h 1 כפונקציית הצעד ובפונקציה h 2 כפונקציית הגיבוב ?
81
שאלה 4 a. האם ניתן היה להשתמש בפונקציה h 1 כפונקציית הצעד ובפונקציה h 2 כפונקציית הגיבוב ? תשובה : לא, כיוון ש h 1 (k) עלול לקבל ערך 0, ואם התא כבר תפוס לא נוכל למקם את הערך החדש. לדוגמא, אם נכניס את הערך 1, ולאחר מכן ננסה להכניס את הערך 11, לא נוכל. בעיה נוספת היא ש h 2 לא מקבלת את הערך 0. h 1 (k)=k mod 11 h 2 (k)= k mod 10 + 1
82
שאלה 4 בשאלה 3 השתמשנו, עבור טבלת גיבוב עם m=11, בפונקציית גיבוב ראשית h 1 (k)=k mod 11 ופונקציית צעד h 2 (k)=1 + k mod 10. a. האם ניתן היה להשתמש בפונקציה h 1 כפונקציית הצעד ובפונקציה h 2 כפונקציית הגיבוב ? b. מדוע חשוב שתוצאת פונקציית הצעד וגודל הטבלה יהיו זרים ? כלומר, אם h step הינה פונקציית הצעד, מדוע חשוב לדרוש gcd(h step (k),m)=1 לכל k?
83
שאלה 4
84
0 1 2 3 4 5 6 7 אפשרי לא אפשרי
85
שאלה 4
86
שאלה 3 הציעו כיצד לממש delete(k) בטבלה עם open addressing פתרון : כל תא בטבלה יכיל סימן מיוחד לציין אם הוא נמחק. בחיפוש, אם נתקל בתא שמסומן " מחוק ", נמשיך בחיפוש.
87
שאלה 3 מה קורה כאשר תנאי זה מקבל ערך TRUE? קוד :
88
שאלה 3 הדגמה :
89
שאלה 5
90
קוד :
91
שאלה 5
92
b. הציעו אלגוריתם בעל זמן ריצה יותר טוב בממוצע, ע " י שימוש בטבלת גיבוב בגודל m. תשובה : ראשית, נכניס את ערכי T לטבלת גיבוב ( מבוססת שרשור ). לאחר מכן, נחפש עבור כל ערך ב S אם הוא כבר נמצא בטבלת הגיבוב. קוד :
93
שאלה 5
95
Hash Tables Bloom Filter
96
שאלה 5
98
שאלה 6 נתון מערך של מספרים ממשיים וערך כלשהו X. עליכם למצוא האם קיימים שני ערכים במערך שסכומם הוא X. a. הראו כיצד ניתן לעשות זאת ב O(nlog n) במקרה הגרוע ביותר פתרון : נמיין את המערך. עבור כל ערך A[i] במערך, נבצע חיפוש בינארי במערך עבור הערך X-A[i]. סה " כ זמן ריצה : O(nlog n)
99
שאלה 6 נתון מערך של מספרים ממשיים וערך כלשהו X. עליכם למצוא האם קיימים שני ערכים במערך שסכומם הוא X. a. הראו כיצד ניתן לעשות זאת ב O(nlog n) במקרה הגרוע ביותר. b. כיצד ניתן לעשות זאת ב O(n) זמן ממוצע ?
100
שאלה 6 b. כיצד ניתן לעשות זאת ב O(n) זמן ממוצע ? פתרון : 1. בוחרים פונקציית גיבוב אוניברסלית h מקבוצת פונקציות גיבוב אוניברסליות H. 2. מכניסים את ערכי המערך לטבלת גיבוב בגודל n מבוססת שירשור ע " י פונקציית הגיבוב h. 3. עבור כל ערך A[i] במערך, נבדוק האם X-A[i] נמצא בטבלת הגיבוב. כפי שהראתם בשיעור, ע " י שימוש בפונקציית גיבוב אוניברסלית, זמן החיפוש של ערך בטבלת הגיבוב הינו O(1), ללא תלות בהתפלגות הערכים במערך. לכן זמן הריצה של האלגוריתם הינו O(n) בממוצע.
101
Question 7 Suppose we have n elements and a very good hash function. We have a hash table with m=n (1.5) double slots, that is, each hash table slot can hold two elements. We perform insert and search in the obvious manner, checking both slots if necessary, but we do not implement any collision resolution as in open addressing. Instead, we have an overflow linked list for those elements that do not fit. – This list accepts all overflow elements, regardless of where they come from. – Clearly, one must search the overflow list after having inspected a full slot. Show that the expected unsuccessful search time is O(1). In other words, show that the expected number of elements in the overflow list is O(1) – (Hint: look at the total number of triples of n elements, and what is the chance that the hash function takes a triple to the same slot).
102
Question 7 Solution: Number of triplets among n indices = Θ(n 3 ) The probability for two keys to fall into the same slot 1/m The probability of a triplet to fall into the same slot 1/m 2 m = n (1.5) so the average number of collisions which will cause a key to go into the overflow list is: Θ(n 3 )/m 2 = Θ(n 3 )/n 3 = O(1)
103
שאלה 8 בטבלת גיבוב מבוססת double hashing, בכל תא i בטבלה הוסיפו מונה c i, המונה את מספר המפתחות k שהוכנסו לטבלה עם h 1 (k)=i. a. כיצד ניתן להשתמש במונים אלו כדי ליעל את החיפוש בטבלה, במקרה של חיפוש לא מוצלח ?
104
שאלה 8 בטבלת גיבוב מבוססת double hashing, בכל תא i בטבלה הוסיפו מונה c i, המונה את מספר המפתחות k שהוכנסו לטבלה עם h 1 (k)=i. a. כיצד ניתן להשתמש במונים אלו כדי ליעל את החיפוש בטבלה, במקרה של חיפוש לא מוצלח ? תשובה : נחפש באותו אופן, אולם נשמור את מספר המפתחות k’ עם h 1 (k’)=h 1 (k) שראינו. אם ראינו כבר c i מפתחות כאלו, נדע ש h 1 (k) לא נמצא.
105
שאלה 8 a. כיצד ניתן להשתמש במונים אלו כדי ליעל את החיפוש בטבלה, במקרה של חיפוש לא מוצלח ? קוד :. האם תיתכן שגיאה אם נשמיט את הבדיקה הזאת ?
106
שאלה 8 בטבלת גיבוב מבוססת double hashing, ללא מחיקות, בכל תא i בטבלה הוסיפו מונה c i, המונה את מספר המפתחות k שהוכנסו לטבלה עם h 1 (k)=i. a. כיצד ניתן להשתמש במונים אלו כדי ליעל את החיפוש בטבלה, במקרה של חיפוש לא מוצלח ? b. הראו דוגמה בה מספר הגישות לטבלה בחיפוש לא מוצלח יורד מ n ל 2 ( ע " י שימוש בסעיף הקודם )
107
שאלה 8 דוגמה : נסתכל על טבלת גיבוב עם – m=7 – h 1 (k)=k mod 7 – h 2 (k)=1+(k mod 6) נכניס את המפתחות הבאים לפי הסדר ( משמאל לימין ) { -3, 3, 1, 8, 9, 12,21} כעת, חיפוש עבור 29 יקח 2 קריאות מהטבלה, במקום 7.
108
שאלה 8 בטבלת גיבוב מבוססת double hashing, בכל תא i בטבלה הוסיפו מונה c i, המונה את מספר המפתחות k שהוכנסו לטבלה עם h 1 (k)=i. a. כיצד ניתן להשתמש במונים אלו כדי ליעל את החיפוש בטבלה, במקרה של חיפוש לא מוצלח ? b. הראו דוגמה בה מספר הגישות לטבלה בחיפוש לא מוצלח יורד מ n ל 2 ( ע " י שימוש בסעיף הקודם ) c. האם ניתן להשתמש באלגוריתם הנ " ל גם בטבלת גיבוב מבוססת linear probing?
109
שאלה 8 c. האם ניתן להשתמש באלגוריתם הנ " ל גם בטבלת גיבוב מבוססת linear probing? תשובה : כן.
110
שאלה 9 In Moshe's grocery store, an automatic ordering system that uses a Queue (FIFO) is installed. Whenever a client places an order, the order's data (product, quantity, client's name) are being inserted to the back of the Queue. Moshe is extracting orders from the front of the queue, handling them one by one. In order to avoid a scenario where the store runs out of some product, every time Moshe extracts an order from the front of the Queue, he would like to know the total quantity of the currently extracted product, summed over all of this product's orders in the Queue. Find a data structure that supports the following operations in the given time:
111
שאלה 9 1.Enqueue(r)-Inserting an order to the back of the queue, r = (product, quantity, client's name). Running time - O(1) on average. 2.Dequeue()-Extracting an order from the front of the queue. Running time - O(1) on average. 3.Query(p)-Returns the total quantity of the product p in the Queue - O(1) on average. It is known that there are n products in the grocery. The Queue may hold at most m orders at any given time. We know that m<n. The data structure may use O(m) memory.
112
שאלה 9 - פתרון Solution: We will use a Queue and a hash table with chaining of size O(m). Each element (in the linked list) contains a key – the product's name and another field – its quantity. Enqueue(r) – Insert the order to the back of the queue. Search for r.product in the hash table. If r.product is in the table, add r.quantity to the quantity field of the appropriate element. If not, insert r.product to the hash table and update its quantity. Dequeue() – Extract r from the front of the queue. Search for r.product in the hash table (it must be in it). Decrement the quantity field of the element by r.quantity. If the quantity is 0, remove the element from the hash table.
113
שאלה 9 - פתרון Solution: Query(p) – Look for p in the hash table. If p is in the table, return p.quantity else return 0. Notice that ; therefore, the running time of the three operations is O(1) in average.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.