1 / 110

תרגול 8

תרגול 8. Skip Lists Hash Tables. 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.

kent
Download Presentation

תרגול 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 S1..Sh, each start at -∞ and end at ∞. • Level S1 - Doubly linked list containing all the elements in the set S. • Level Si is a subset of level Si-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 S1, the expected number of elements in level Si is (n/2)i-1. • The expected number of levels required is O(log n).

  4. Skip Lists • Time Complexity • Search: O(log n) expected • Insert: search and then insert in O(log n) time – O(log n) expected • Delete: search and then delete in O(log n) time – O(log n) expected • Memory Complexity • O(n) expected (

  5. Skip Lists דוגמה:

  6. שאלה 1 • הסבירו כיצד ניתן לממש את הפונקציה Select(S,k) המחזירה את האיבר הk בגודלו בסקיפ ליסט S עם n איברים, בזמן ממוצע של O(log n). אילו שינויים עלינו לבצע בסקיפ ליסט? תשובה: נשמור בכל תא pבסקיפ ליסט ערך dis(p) – מספר הערכים (כלומר, מספר התאים בS1) בינו לבין התא הבא אחריו בשרשרת Si. על מנת לבצע חיפוש, נתחיל ב-∞ ברמה הגבוהה ביותר ונשמור את המיקום (בהתחלה 0). בכל פעם שנתקדם בשרשרת נוסיף dis(p) למיקום. אם מספר המקומות שנותרו עד k< dis(p), נרד רמה ונמשיך.

  7. שאלה 1 קוד:

  8. שאלה 1 הדגמה: נבצע Search(7,S). pos= 0 1 4 6 7

  9. Question 2 • Write an algorithm that builds a skip list S from the given BST T with nelements (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

  12. Hash Tables

  13. שאלה 3 • נתון: טבלת גיבוב עם m=11 ופונקציות גיבוב • h1(k)=k mod m • h2(k)=1+(k mod (m-1)) • הכניסו את האיברים הבאים לפי הסדר (משמאל לימין) 22, 1, 13, 11, 24, 33, 18, 42, 31 • לטבלת גיבוב מבוססת שרשור, עם פונקציית גיבוב h(k)=h1(k).

  14. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 / 1 / 2 / 3 / h(k)=k mod 11 4 / 5 / 6 / 7 / 8 / 9 / 10 /

  15. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 / 1 / 2 / 3 / h(k)=k mod 11 4 / 5 / 6 / 7 / 8 / 9 / 10 /

  16. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 / 1 / 2 / 3 / h(k)=k mod 11 4 / h(22)=0 5 / 6 / 7 / 8 / 9 / 10 /

  17. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 / 2 / 3 / h(k)=k mod 11 4 / h(22)=0 5 / 6 / 7 / 8 / 9 / 10 /

  18. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 / 2 / 3 / h(k)=k mod 11 4 / 5 / 6 / 7 / 8 / 9 / 10 /

  19. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 / 2 / 3 / h(k)=k mod 11 4 / h(1)=1 5 / 6 / 7 / 8 / 9 / 10 /

  20. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 / 3 / h(k)=k mod 11 4 / h(1)=1 5 / 6 / 7 / 8 / 9 / 10 /

  21. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 / 3 / h(k)=k mod 11 4 / 5 / 6 / 7 / 8 / 9 / 10 /

  22. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 / 3 / h(k)=k mod 11 4 / h(13)=2 5 / 6 / 7 / 8 / 9 / 10 /

  23. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 13 / 3 / h(k)=k mod 11 4 / h(13)=2 5 / 6 / 7 / 8 / 9 / 10 /

  24. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 13 / 3 / h(k)=k mod 11 4 / 5 / 6 / 7 / 8 / 9 / 10 /

  25. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 22 / 1 1 / 2 13 / 3 / h(k)=k mod 11 4 / h(11)=0 5 / 6 / 7 / 8 / 9 / 10 /

  26. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 11 22 / 1 1 / 2 13 / 3 / h(k)=k mod 11 4 / h(11)=0 5 / 6 / 7 / 8 / 9 / 10 /

  27. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 11 22 / 1 1 / 2 13 / 24 3 / h(k)=k mod 11 4 / h(24)=2 5 / 6 / 7 / 8 / 9 / 10 /

  28. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 33 11 22 / 1 1 / 2 13 / 24 3 / h(k)=k mod 11 4 / h(33)=0 5 / 6 / 7 / 8 / 9 / 10 /

  29. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 33 11 22 / 1 1 / 2 13 / 24 3 / h(k)=k mod 11 4 / h(18)=7 5 / 6 / 7 18 / 8 / 9 / 10 /

  30. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 33 11 22 / 1 1 / 2 13 / 24 3 / h(k)=k mod 11 4 / h(42)=9 5 / 6 / 7 18 / 8 / 9 42 / 10 /

  31. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining 0 33 11 22 / 1 1 / 2 13 / 24 3 / h(k)=k mod 11 4 / h(31)=9 5 / 6 / 7 18 / 8 / 9 31 42 / 10 /

  32. Hash Tables

  33. שאלה 3 • נתון: טבלת גיבוב עם m=11 ופונקציות גיבוב • h1(k)=k mod m • h2(k)=1+(k mod (m-1)) • הכניסו את האיברים הבאים לפי הסדר (משמאל לימין) 22, 1, 13, 11, 24, 33, 18, 42, 31 • לטבלת גיבוב מבוססת שרשור, עם פונקציית גיבוב h(k)=h1(k). • לטבלת גיבוב מבוססת linear probing, עם אותה פונקציית גיבוב. • לטבלת גיבוב מבוססת double hashing, עם פונקציית גיבוב ראשית h1(k) ופונקציית צעד h2(k).

  34. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 1 2 3 h(k)=k mod 11 4 5 6 7 8 9 10

  35. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 1 2 3 h(k)=k mod 11 4 5 6 7 8 9 10

  36. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 1 2 3 h(k)=k mod 11 4 h(22)=0 5 6 7 8 9 10

  37. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 1 2 3 h(k)=k mod 11 פנוי 4 h(22)=0 5 6 7 8 9 10

  38. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 2 3 h(k)=k mod 11 פנוי 4 h(22)=0 5 6 7 8 9 10

  39. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 2 3 h(k)=k mod 11 4 5 6 7 8 9 10

  40. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 2 3 h(k)=k mod 11 פנוי 4 h(1)=1 5 6 7 8 9 10

  41. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 3 h(k)=k mod 11 פנוי 4 h(1)=1 5 6 7 8 9 10

  42. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 3 h(k)=k mod 11 4 5 6 7 8 9 10

  43. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 3 h(k)=k mod 11 פנוי 4 h(13)=2 5 6 7 8 9 10

  44. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 h(k)=k mod 11 פנוי 4 h(13)=2 5 6 7 8 9 10

  45. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 h(k)=k mod 11 4 5 6 7 8 9 10

  46. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 h(k)=k mod 11 תפוס תפוס תפוס פנוי 4 h(11)=0 5 6 7 8 9 10

  47. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 11 h(k)=k mod 11 פנוי 4 h(11)=0 5 6 7 8 9 10

  48. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 11 h(k)=k mod 11 תפוס פנוי תפוס 4 h(24)=2 5 6 7 8 9 10

  49. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 11 h(k)=k mod 11 פנוי 4 24 h(24)=2 5 6 7 8 9 10

  50. שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing 0 22 1 1 2 13 3 11 h(k)=k mod 11 תפוס תפוס תפוס תפוס פנוי תפוס 4 24 h(33)=0 5 6 7 8 9 10

More Related