תרגול 8 skip lists hash tables. skip lists definition: – a skip list is a probabilistic data...

Post on 01-Apr-2015

220 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8תרגול

Skip ListsHash 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.– 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.

Skip Lists• Skip Levels

– Doubly Linked lists S1..S

h, 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 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 Si is (n/2)i-1.

– The expected number of levels required is O(log n).

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

(

Skip Lists

דוגמה:

1שאלה הפונקציה • את לממש ניתן כיצד Select(S,k)הסבירו

ה האיבר את ליסט kהמחזירה בסקיפ nעם Sבגודלושל, ממוצע בזמן לבצע. O(log n)איברים עלינו שינויים אילו

? ליסט בסקיפתשובה:

תא בכל ערך pנשמור ליסט הערכים – dis(p)בסקיפ מספר , ב) התאים מספר אחריו( S1כלומר הבא התא לבין בינו

.Siבשרשרת - , ביותר הגבוהה ברמה ∞ ב נתחיל חיפוש לבצע מנת על

בהתחלה ) המיקום את שנתקדם(. 0ונשמור פעם בכלנוסיף .dis(p)בשרשרת למיקום

עד שנותרו המקומות מספר .k< dis(p)אם ונמשיך, רמה נרד

1שאלה קוד:

Select(S, k) p ← leftmost and upmost node of S pos ← 0 for i ← h (the height of S) downto 1 while (pos + p.dis ≤ k) pos ← pos + p.dis p ← p.next if (pos = k) return p //return the basis of p's tower else p ← below(p)

1שאלה .Search(7,S)נבצע הדגמה:

pos= 01467

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).

Question 2Solution: Build(T, S)

S1inorder(T) int i1 while (i < log n) for j1 to |Si| if (j mod 2 = 0) link = Si+1.add(Si[j]) //method returns a pointer to the link added. link.below = Si[j] ii+1

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.

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 , where m is the size of the hash table.

Hash TableA hash table for a given key type consists of: Hash function h: keys-set →[0,m-1] Array (called table) of size m

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) 

3שאלה עם נתון: • גיבוב גיבוב m=11טבלת ופונקציות

• h1(k)=k mod m

• h2(k)=1+(k mod (m-1))

• ) לימין ) משמאל הסדר לפי הבאים האיברים את הכניסו22, 1, 13, 11, 24, 33, 18, 42, 31

.a , גיבוב פונקציית עם שרשור מבוססת גיבוב לטבלתh(k)=h1(k).

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

///////////

012345678910

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

///////////

012345678910

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

///////////

012345678910

h(22)=0

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

//////////

012345678910

h(22)=0

/22Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

//////////

012345678910

/22Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

//////////

012345678910

h(1)=1

/22Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11/////////

012345678910

h(1)=1

/22

/1Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11/////////

012345678910

/22

/1Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11/////////

012345678910

h(13)=2

/22

/1Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

h(13)=2

/22

/1

/13

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

/22

/1

/13

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

h(11)=0

/22

/1

/13

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

h(11)=0

11

/1

/13

/22Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

h(24)=2

11

/1

24

/22

/13

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////////

012345678910

h(33)=0

33

/1

24 /13

11 /22Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////

///

012345678910

h(18)=7

33

/1

24

11

/13

/22

/18

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////

/

/

012345678910

h(42)=9

33

/1

24

11

/13

/22

/18

/42

Chaining

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11 ////

/

/

012345678910

h(31)=9

33

/1

24

11

/13

/22

/18

31 /42

Chaining

Hash Tables

Open Addressing

Linear Probing:h(k,i) = (h'(k) + i)mod m    0≤ i ≤ m-1h'(k) - common hash functionFirst try h(k,0) = h'(k) , if it is occupied, try h(k,1) etc.. Advantage: simplicityDisadvantage: clusters, uses Θ(m) permutations of index addressing sequences

Double Hashing:h(k,i) = (h1(k) + i·h2(k))mod m    0≤ i ≤ m-1h1 – hash functionh2 – step functionFirst try h(k,0) = h1(k), if it is occupied, try h(k,1) etc. Advantage: less clusters , uses Θ(m*m) permutations of index addressing sequences

3שאלה עם נתון: • גיבוב גיבוב m=11טבלת ופונקציות

• h1(k)=k mod m

• h2(k)=1+(k mod (m-1))

• ) לימין ) משמאל הסדר לפי הבאים האיברים את הכניסו22, 1, 13, 11, 24, 33, 18, 42, 31

.a , גיבוב פונקציית עם שרשור מבוססת גיבוב לטבלתh(k)=h1(k).

.b מבוססת גיבוב פונקציית, linear probingלטבלת אותה עםגיבוב.

.c מבוססת גיבוב גיבוב, double hashingלטבלת פונקציית עםצעד h1(k)ראשית .h2(k)ופונקציית

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(22)=0

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(22)=0

Linear Probingפנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(22)=0

22 Linear Probingפנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

22Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(1)=1

22פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(1)=1

221 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

221

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(13)=2

221

פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(13)=2

221

13 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

221

13

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(11)=0

221

13

תפוס

תפוס

תפוס

פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(11)=0

221

1311 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(24)=2

221

1311

תפוס

תפוס

פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(24)=2

221

131124 פנוי

Linear Probing

תפוס

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(33)=0

221

131124

תפוס

תפוס

תפוס

פנוי

תפוס

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(33)=0

221

13112433 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(18)=7

221

13112433

18 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(42)=9

221

13112433

18

42 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(31)=9

221

13112433

18

42פנוי

תפוס

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h(k)=k mod 11

012345678910

h(31)=9

221

13112433

18

4231 פנוי

Linear Probing

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

h1(k)=k mod 11

012345678910

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

h1(22)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

פנוי

h1(22)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

22 פנוי

h1(22)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

22

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

h1(1)=1

22פנוי

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221 פנוי

h1(1)=1

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

h1(13)=2

221

פנוי

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13 פנוי

h1(13)=2

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

תפוס

תפוס

פנויh1(11)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(11)=2

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

11 פנויh1(11)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(11)=2

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

11h1(24)=2

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(24)=5

תפוס

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

11

24

h1(24)=2

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(24)=5

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

11

24

h1(33)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(33)=4

תפוס

תפוס

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

11

2433

h1(33)=0

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(33)=4

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

1118

2433

h1(18)=7

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(18)=9

תפוס

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

1118

243342

h1(42)=9

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)

פנוי

3שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31

012345678910

221

13

111831243342

h1(31)=9

h1(k)=k mod 11

Double Hashing

Step Function h2(k)=1+ (k mod 10)h2(31)=2 תפוס

תפוס

פנוי

תפוס

תפוס

Chaining Linear Probing Double Hashing

0 33 → 11→ 22 22 22

1 1 1 1

2 24 → 13 13 13

3 11

4 24 11

5 33 18

6 31

7 18 18 24

8 33

9 31→ 42 42 42

10 31

3שאלה טבלת

השוואה:

Hash Tables

Load Factor , Hash table with m slots that stores n elements (keys) 

Average (expected) Search Time

Open Addressingunsuccessful search: Osuccessful search: O

Chainingunsuccessful search: Θ (1 + α)successful search: Θ = Θ (1 + α) 

לוח!

Hash Tables : חשובות הערות

נמדדת • גיבוב בטבלאות הממוצע היעילות לא, בזמן. ביותר הגרוע

המפתחות load factorה • מספר את בממוצעמייצג. הגיבוב בפונקציית ערך אותו את שמקבלים

שרשור דוגמא: עם גיבוב טבלת על נסתכלבטבלה load factorה– שרשרת של הממוצע האורך הינובמקרה – נסתכל ביותר אם אותו, הגרוע מקבלים המפתחות כל

, באורך שרשרת נוצרת ולכן תא באותו ומושמים גיבוב . nערך– , בטבלה, מפתח של החיפוש זמן ביותר הגרוע במקרה לכן

הינו ).– , עקב גיבוב בטבלאות משתמשים שאיננו ברור כן על

, גיבוב בפעולות זמן וכשננתח ביותר הגרוע בזמן הביצועים. הממוצע המקרה לפי נחשב

4שאלה

, 3בשאלה • עם גיבוב טבלת עבור השתמשנוm=11 ,ראשית גיבוב h1(k)=k mod 11בפונקציית

צעד .h2(k)= k mod 10 + 1ופונקציית.a בפונקציה להשתמש היה ניתן h1האם

ובפונקציה הצעד כפונקציית h2כפונקצייתהגיבוב?

4שאלה

.a בפונקציה להשתמש היה ניתן h1האםובפונקציה הצעד כפונקציית h2כפונקציית

הגיבוב?תשובה:ש, כיוון ערך h1(k)לא לקבל התא, 0עלול ואם

. החדש הערך את למקם נוכל לא תפוס כברהערך, את נכניס אם מכן, 1לדוגמא ולאחר

הערך את להכניס .11ננסה נוכל, לאש היא נוספת הערך h2בעיה את מקבלת .0לא

h1(k)=k mod 11 h2(k)= k mod 10 + 1

4שאלה

, 3בשאלה • עם גיבוב טבלת עבור השתמשנוm=11 ,ראשית גיבוב h1(k)=k mod 11בפונקציית

צעד .h2(k)=1 + k mod 10ופונקציית.a בפונקציה להשתמש היה ניתן כפונקציית h1האם

ובפונקציה ?h2הצעד הגיבוב כפונקציית

.b וגודל הצעד פונקציית שתוצאת חשוב מדוע , ? אם כלומר זרים יהיו הינה hstepהטבלה

, לדרוש חשוב מדוע הצעד פונקצייתgcd(hstep(k),m)=1 לכלk?

4שאלה

.b וגודל הצעד פונקציית שתוצאת חשוב מדוע , ? אם כלומר זרים יהיו הינה hstepהטבלה

, לדרוש חשוב מדוע הצעד פונקצייתgcd(hstep(k),m)=1 לכלk?

של gcd(hstep(k),m)=d>1אם תשובה: ההשמה אזk ב רק , אפשרית נוכל. שלא ייתכן לכן מהתאים

עבור פנוי תא אינה, kלמצוא הטבלה אם אפילומלאה.

4שאלה

ש הדגמה: אז. m=8, h1(k)=1, hstep(k)=2נניחgcd(hstep(k),m)=2 ,את להכניס ניתן ב kלכן רק. אפשריים תאים

01234567

אפשרי

אפשרי

אפשרי

אפשרי

לא אפשרי

לא אפשרי

לא אפשרי

לא אפשרי

4שאלה

להבטיח 2• :gcd(hstep(k),m)=1דרכים מפתח לכל.1 , ו ראשוני מספר הוא הטבלה לכל hstep(k)<mגודל

k.של 2. חזקה הוא הטבלה ופונקציית )(, 2גודל

. הצעד - זוגיות אי תוצאות רק מחזירה

5שאלה שלמים • מספרים של קבוצות שתי ,S={s1,s2נתונות

…,sm} וT={t1,t2,…,tn} ,כאשרm≤n..a בעל האם לבדיקה דטרמיניסטי אלגוריתם הציעו

. ביותר הגרוע במקרה יעיל ריצה זמן

Subset(T,S,n,m) T:=sort(T) for each sj S found := BinarySearch(T, sj) if (!found) return "S is not a subset of T" return "S is a subset of T"

5שאלה שלמים • מספרים של קבוצות שתי ,S={s1,s2נתונות

…,sm} וT={t1,t2,…,tn} ,כאשרm≤n..a בעל האם לבדיקה דטרמיניסטי אלגוריתם הציעו

. ביותר הגרוע במקרה יעיל ריצה זמןתשובה:

את, נמיין .Tראשיתעל נעבור מכן לאחר

ב ונחפש Sהאיבריםב באמצעות Tאותם

. בינארי חיפוש: ריצה O(nlog n+mlog n)=O(nlog n) זמן

קוד:

5שאלה שלמים • מספרים של קבוצות שתי S={s1,s2,…,sm}נתונות

.m≤nכאשר, T={t1,t2,…,tn}ו .a זמן בעל האם לבדיקה דטרמיניסטי אלגוריתם הציעו

. ביותר הגרוע במקרה יעיל ריצה.b , בממוצע טוב יותר ריצה זמן בעל אלגוריתם הציעו

בגודל" גיבוב בטבלת שימוש י .mע

5שאלה .b " , י ע בממוצע טוב יותר ריצה זמן בעל אלגוריתם הציעו

בגודל גיבוב בטבלת .mשימוש תשובה:

ערכי, את נכניס (.Tראשית שרשור ) מבוססת גיבוב לטבלת , ב ערך כל עבור נחפש מכן נמצא Sלאחר כבר הוא אם

. הגיבוב בטבלת

SubsetWithHashTable(T,S,n,m) for each ti T insert(HT, ti) for each sj S found = search(HT, sj) if (!found) return "S is not a subset of T" return "S is a subset of T"

קוד:

5שאלה .b טוב יותר ריצה זמן בעל אלגוריתם הציעו

" בגודל, גיבוב בטבלת שימוש י ע .mבממוצעהריצה זמן :הממוצעניתוח

של הערכים הגיבוב - Tהכנסת לטבלתישנם – זה במצב . mאם לכן מוצלחים חיפושים

הוא הריצה זמןישנם – ) ביותר הגרוע במקרה חיפושים( m-1אם

. לכן מוצלח אינו האחרון והחיפוש מוצלחים , הוא הריצה זמן דומה באופן

5שאלה שלמים • מספרים של קבוצות שתי S={s1,s2,…,sm}נתונות

.m≤nכאשר, T={t1,t2,…,tn}ו .a זמן בעל האם לבדיקה דטרמיניסטי אלגוריתם הציעו

. ביותר הגרוע במקרה יעיל ריצה.b , בממוצע טוב יותר ריצה זמן בעל אלגוריתם הציעו

בגודל" גיבוב בטבלת שימוש י .mע.c בגודל בלום פילטר גיבוב kו rבהינתן ,h1,h2פונקציות

…,hk:U→[r] בזמן אם לבדוק ניתן כיצד O(n)הראו . לקבלת ההסתברות מהי ביותר הגרוע falseבמקרה

positive? זה במקרה

Hash Tables

Bloom Filter

A Bloom filter model consists of a bit-Array of size m (the bloom filter) and k hash functions h1, h2, ..., hk. It supports insertion and search queries only: Insert at O(1) Search(x) in O(1), with probability of getting a

false positive 

5שאלה .c בגודל פילטר בלום גיבוב kו rבהינתן פונקציות

h1,h2,…,hk:U→[r] אם לבדוק ניתן כיצד הראו. O(n)בזמן מהי ביותר הגרוע במקרה

לקבלת ?false positiveההסתברות זה במקרהפתרון:

ב, האיברים כל את נכניס .Tראשית פילטר לבלום , ב איבר כל עבור מכן הוא Sלאחר אם נבדוק

. פילטר בבלום נמצאריצה (:worst case)זמן

n ו , mהכנסות לכן פילטר בבלום חיפושים

5שאלה ל סיכוי :false positiveניתוח

אם , . רק שגויה תשובה לקבל נוכל כל עבורכאשר, גם ש יזהה פילטר שהבלום ייתכן כלומר

. , בהסתברות המצב לא זהבכל טעה אם רק שגיאה מחזיר האלגוריתם

ב , S\Tהאיברים נסמן. אם אז|, x=|S\Tכלומרבהסתברות . שגיאה מחזיר האלגוריתם

נסמן, אם האלגוריתם|, t=|S∩Tלחילופין אזבהסתברות . טועה

6שאלה

כלשהו • וערך ממשיים מספרים של מערך . Xנתוןבמערך ערכים שני קיימים האם למצוא עליכם

הוא .Xשסכומם.a ב זאת לעשות ניתן כיצד O(nlog n)הראו

ביותר הגרוע במקרהפתרון:

. ערך כל עבור המערך את , A[i]נמיין נבצע במערךהערך עבור במערך בינארי .X-A[i]חיפוש

: ריצה" זמן כ O(nlog n)סה

6שאלה

כלשהו • וערך ממשיים מספרים של מערך נתוןX .ערכים שני קיימים האם למצוא עליכם

הוא שסכומם .Xבמערך.a ב זאת לעשות ניתן כיצד O(nlog n)הראו

. ביותר הגרוע במקרה.b ב זאת לעשות ניתן ?O(n)כיצד ממוצע זמן

6שאלה

.b ב זאת לעשות ניתן ?O(n)כיצד ממוצע זמןפתרון:

אוניברסלית 1. גיבוב פונקציית מקבוצת hבוחריםאוניברסליות גיבוב .Hפונקציות

בגודל 2. גיבוב לטבלת המערך ערכי את nמכניסים " הגיבוב פונקציית י ע שירשור .hמבוססת

ערך 3. כל , A[i]עבור האם נבדוק נמצא X-A[i]במערך. הגיבוב בטבלת

" , גיבוב בפונקציית שימוש י ע בשיעור שהראתם כפיהינו, הגיבוב בטבלת ערך של החיפוש זמן אוניברסלית

O(1) . זמן, לכן במערך הערכים בהתפלגות תלות ללאהינו האלגוריתם של .O(n)הריצה בממוצע

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) .

Question 7Solution:• Number of triplets among n indices = Θ(n3)• 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/m2

• m = n(1.5) so the average number of collisions which will cause a key to go into the overflow list is:

• Θ(n3)/m2 = Θ(n3)/n3 = O(1)

8שאלה

מבוססת • גיבוב בכל, double hashingבטבלתמונה iתא הוסיפו מספר, ciבטבלה את המונה

עם kהמפתחות לטבלה .h1(k)=iשהוכנסו.a את ליעל כדי אלו במונים להשתמש ניתן כיצד

, לא חיפוש של במקרה בטבלה החיפושמוצלח?

8שאלה

מבוססת • גיבוב תא, double hashingבטבלת בכלi מונה הוסיפו מספר, ciבטבלה את המונה

עם kהמפתחות לטבלה .h1(k)=iשהוכנסו.a את ליעל כדי אלו במונים להשתמש ניתן כיצד

, לא חיפוש של במקרה בטבלה החיפושמוצלח?

תשובה: , מספר את נשמור אולם אופן באותו נחפש

. h1(k’)=h1(k)עם’ kהמפתחות ראינו אם שראינו, ciכבר ש נדע כאלו . h1(k)מפתחות נמצא לא

8שאלה

.a את ליעל כדי אלו במונים להשתמש ניתן כיצד , לא חיפוש של במקרה בטבלה החיפוש

מוצלח?.קוד:

Search(HT,m,k,h) index=h(k,0) c=cindex

i=0 do if c = 0 return -1 if HT[h(k,i)] is empty return -1 if HT[h(k,i)] = k return h(k,i) else k'=HT[h(k,i)] if (h(k',0) = index) c = c-1 i = i+1 while (i < m)

תיתכן האםנשמיט אם שגיאה

הבדיקה אתהזאת?

8שאלה

מבוססת • גיבוב ללא, double hashingבטבלתתא, בכל מונה iמחיקות הוסיפו , ciבטבלה

המפתחות מספר את שהוכנסו kהמונהעם .h1(k)=iלטבלה

.a את ליעל כדי אלו במונים להשתמש ניתן כיצד , לא חיפוש של במקרה בטבלה החיפוש

מוצלח?.b לטבלה הגישות מספר בה דוגמה הראו

מ יורד מוצלח לא " 2ל nבחיפוש שימוש) י ע) הקודם בסעיף

8שאלה

דוגמה:עם • גיבוב טבלת על נסתכל

–m=7–h1(k)=k mod 7

–h2(k)=1+(k mod 6)

הבאים • המפתחות את נכניס ) לימין ) משמאל הסדר לפי

- { 3 ,3 ,1 ,8 ,9 ,12,21}עבור, • חיפוש 2יקח 29כעת

, במקום מהטבלה .7קריאות

0 8 c0=1 1 1 c1=2 2 9 c2=1 3 3 c3=1 4 -3 c4=1 5 12 c5=1 6 21 c6=0

8שאלה

מבוססת • גיבוב תא, double hashingבטבלת iבכלמונה הוסיפו מספר, ciבטבלה את המונה

עם kהמפתחות לטבלה .h1(k)=iשהוכנסו.a את ליעל כדי אלו במונים להשתמש ניתן כיצד

? , מוצלח לא חיפוש של במקרה בטבלה החיפוש.b בחיפוש לטבלה הגישות מספר בה דוגמה הראו

מ יורד מוצלח ( 2ל nלא " הקודם) בסעיף שימוש י ע.c " בטבלת גם ל הנ באלגוריתם להשתמש ניתן האם

מבוססת ?linear probingגיבוב

8שאלה

.c " גם ל הנ באלגוריתם להשתמש ניתן האםמבוססת גיבוב ?linear probingבטבלת

תשובה:כן.

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:

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.

פתרון - 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.

פתרון - 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.

Pr(1)

( )

numberOfDifferent oductsInQueue mO

SizeOfHashTable m

top related