1 / 177

# Fundamental Algorithms Chapter 3: Search Structures

Fundamental Algorithms Chapter 3: Search Structures. Christian Scheideler WS 2011. Search Structure. 8. 4. 11. 20. 18. 3. Search Structure. insert(15). 8. 4. 11. 20. 18. 15. 3. Search Structure. delete(20). 8. 4. 11. 20. 18. 15. 3. Search Structure.

Download Presentation

## Fundamental Algorithms Chapter 3: Search Structures

An Image/Link below is provided (as is) to download presentation Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

### Presentation Transcript

1. Fundamental AlgorithmsChapter 3: Search Structures Christian Scheideler WS 2011

2. Search Structure 8 4 11 20 18 3 Chapter 3

3. Search Structure insert(15) 8 4 11 20 18 15 3 Chapter 3

4. Search Structure delete(20) 8 4 11 20 18 15 3 Chapter 3

5. Search Structure search(7) gives 8 (closest successor) 8 4 11 18 15 3 Chapter 3

6. Search Structure S:set of elements Every element eidentified by key(e). Operations: • S.insert(e: Element): S:=S∪{e} • S.delete(k: Key): S:=S\{e}, where eis the element with key(e)=k • S.search(k: Key): outputs eS with minimal key(e) so that key(e)≥k Chapter 3

7. Static Search Structure 1. Store elements in sorted array. search: via binary search (in O(log n)time) search(12) 1 3 5 10 14 19 28 31 58 60 82 85 89 94 98 Chapter 3

8. Binary Search Input: number xand sorted array A[1],…,A[n] Algorithm BinarySearch: l:=1; r:=n while l < r do m:=(r+l) div 2 if A[m] = x then return m if A[m] < x then l:=m+1 else r:=m return l Chapter 3

9. 19 28 31 58 60 82 85 Dynamic Search Structure insert und deleteOperations: Sorted array difficult to update! Worst case:(n)time 15 1 3 5 10 14 Chapter 3

10. 3 Search Structure 2. Sorted List (with an ∞-Element) Problem:insert, delete and searchtake (n)time in the worst case Observation: If searchcould be implemented efficiently, then also all other operations ∞ 19 1 … Chapter 3

11. 3 Search Structure Idee:add navigation structure that allows searchto run efficiently navigation structure ∞ 19 1 … Chapter 3

12. Binary Search Tree (ideal) search(12) 10 3 19 1 5 14 28 1 3 5 10 14 19 28 ∞ Chapter 3

13. Binary Search Tree We assume: every key only once in search tree Invariant: With this rule, a searchoperation is easy to implement. For all keys k´ in T1and k´´ in T2: k´ < k <k´´ k T1 T2 Chapter 3

14. search(k) Operation k Search strategy: • Start at the root, v, of the search tree • While vis a treenode: • If key(v) ≥ k then let v be the left child of v, otherwise let v be the right child of v • Output (list node) v For all keys k´ in T1and k´´ in T2: k´ < k <k´´ T1 T2 Chapter 3

15. search(k) Operation k Correctness of search strategy: • For every left subtree T of a nodev, the rightmost list element e underT satisfies key(v)=key(e). • So whenever search(k) enters T, there is an element e in the list below T with key(e)≥k. For all keys k´ in T1and k´´ in T2: k´ < k <k´´ T1 T2 v T … 1 e Chapter 3

16. Binary Search Tree Formally: for every tree node vlet • key(v) be the key stored at v • d(v) the number of children of v • Search tree invariant: (as before) • Degree invariant:All tree nodes have exactly two children (as long as the number of elements is >1) • Key invariant:For every element e in the list there is exactly one tree node vwith key(v)=key(e). Chapter 3

17. Search(9) 10 3 19 1 5 14 28 1 3 5 10 14 19 28 ∞ Chapter 3

18. Insert and Delete Operations Strategy: • insert(e):First, execute search(key(e)) to obtain a list element e´. If key(e)=key(e´), replace e´ by e, otherwise insert e between e´ and its predecessor in the list and add a new search tree leaf for eand e´with key key(e). • delete(k):First, execute search(k) to obtain a list element e. If key(e)=k, then delete efrom the list and the parent v of efrom the search tree, and set in the tree node wwith key(w)=k: key(w):=key(v). Chapter 3

19. Insert(5) 10 1 14 28 1 10 14 28 ∞ Chapter 3

20. Insert(5) 10 1 14 5 28 1 5 10 14 28 ∞ Chapter 3

21. Insert(12) 10 1 14 5 28 1 5 10 14 28 ∞ Chapter 3

22. Insert(12) 10 1 14 5 12 28 1 5 10 12 14 28 ∞ Chapter 3

23. Delete(1) 10 1 14 5 12 28 1 5 10 12 14 28 ∞ Chapter 3

24. Delete(1) 10 14 5 12 28 5 10 12 14 28 ∞ Chapter 3

25. Delete(14) 10 14 5 12 28 5 10 12 14 28 ∞ Chapter 3

26. Delete(14) 10 12 5 28 5 10 12 28 ∞ Chapter 3

27. 1 3 5 10 14 19 28 1 3 5 10 14 19 28 Binary Search Tree Problem:binary tree can degenerate! Example: numbers are inserted in sorted order Search requires (n)time in the worst case ∞ Chapter 3

28. Search Trees Problem:binary tree can degenerate! Solutions: • Splay tree(very effective heuristic) • Treaps(well balanced with high probability) • (a,b)-tree(guaranteed well balanced) • red-black tree(worst case constant effort for reorganization) • weight-balanced tree(compact embedding into array) Chapter 3

29. Splay Tree Usually: Implementation as internal search tree (i.e., elements directly integrated into tree and not in an extra list) Here: Implementation as external search tree (like for the binary search tree above) Chapter 3

30. Splay Tree search(19) Idea: add shortcutpointerto list element⇒accelerates search 10 3 19 1 5 14 28 1 3 5 10 14 19 28 ∞ Chapter 3

31. Splay Tree Ideas: • Add shortcut pointers in tree to list elements • For every search operation for some key k, move k to the root (if it exists) Movement: via Splay operation Chapter 3

32. Splay Operation Movement of key xto the root: We distinguish between three cases. 1a. xis a child of the root: x y zig y x A C B C A B Chapter 3

33. y x x y A C B C A B Splay Operation Movement of key x to the root: We distinguish between three cases. 1b. x is a child of the root: zig Chapter 3

34. Splay Operation We distinguish between three cases. 2a. x has father and grand father to the right x z y y D A x z zig-zig C B A B C D Chapter 3

35. z x y y D A z x C B A B C D Splay Operation We distinguish between three cases. 2b. x has father and grand father to the left zig-zig Chapter 3

36. Splay Operation We distinguish between three cases. 3a. x: father left, grand father right z x y zig-zag x D y z A A B C D B C Chapter 3

37. Splay Operation We distinguish between three cases. 3b. x: father right, grand father left z x y A x zig-zag z y D A B B C C D Chapter 3

38. Splay Operation Example: zig-zagoperation (3a) 10 3 19 x 1 5 14 28 1 3 5 10 14 19 28 ∞ Chapter 3

39. Splay Operation 5 10 3 19 1 14 28 1 3 5 10 14 19 28 ∞ Chapter 3

40. Splay Operation Examples: x x zig-zig, zig-zag, zig-zig, zig zig-zig, zig-zag, zig-zag, zig Chapter 3

41. 1 3 5 10 14 19 28 1 3 5 10 14 19 28 Splay Operation Observation: In the worst case, tree can still be highly imbalanced! But amortized costs are very low. ∞ Chapter 3

42. Splay Operation search(k)-operation: • Move downwards from the root till kfound in the search tree (which allows shortcut to the list) or the list is reached • k in tree: call splay(k) Amortized Analysis:mSplay operations on arbitrary binary search tree with n elements (m>n) Chapter 3

43. Splay Operation • Weight of node x: w(x) • Tree weight of tree Twith root x: tw(x)= yTw(y) • Rank of node x: r(x) = log(tw(x)) • Potentialof tree T: (T) = xTr(x) Lemma 3.1:Let Tbe a Splay tree with root uand xbe a node in T. The amortized cost for splay(x,T)is at most 1+3(r(u)-r(x)). Chapter 3

44. x y zig y x A C B C A B Splay Operation Proof of Lemma 3.1: Induction over the sequence of rotations. • rand tw: rank and weight before the rotation • r’and tw’: rank and weight after the rotation Case 1: Amortized cost: ≤1+r’(x)+r’(y)-r(x)-r(y) ≤ 1+r’(x)-r(x) since r’(y)≤r(y) ≤1+3(r’(x)-r(x))since r’(x)≥r(x) Runtime(# rotations) Change in  Chapter 3

45. x z y y D A x z zig-zig C B A B C D Splay Operation Case 2: Amortized cost: ≤2+r’(x)+r’(y)+r’(z)-r(x)-r(y)-r(z) = 2+r’(y)+r’(z)-r(x)-r(y) since r’(x)=r(z) ≤2+r’(x)+r’(z)-2r(x) since r’(x)≥r’(y)and r(y)≥r(x) Chapter 3

46. x z y y D A x z zig-zig C B A B C D Splay Operation r’(x) Case 2: Claim: It holds that 2+r’(x)+r’(z)-2r(x) ≤3(r’(x)-r(x)) i.e. r(x)+r’(z) ≤2(r’(x)-1) r(x) r’(z) Chapter 3

47. x z y y D A x z zig-zig C B A B C D Splay Operation r’(x) Case 2: Claim: It holds that r(x)+r’(z) ≤2(r’(x)-1) Use the substitutions r(x)log x, r’(z)log y, r’(x)log 1 and consider the function f(x,y)=log x + log y. To show: f(x,y)≤-2for all x,y>0 with x+y≤1. r(x) r’(z) Chapter 3

48. Splay Operation Lemma 3.2:In the area x,y>0mitx+y≤1, the function f(x,y)=log x + log yhas its maximum at (½,½). Proof: • Since log xmonotonically increasing, the maximum can only lie on the line segment with x+y=1, x,y>0. • Consider determining the maximum forg(x) = log x + log (1-x) • The only root of g’(x) = 1/x - 1/(1-x)is at x=1/2. • For g’’(x)= -(1/x2 + 1/(1-x)2))it holds that g’’(1/2)<0. • Hence, fhas its maximum at (½,½). Chapter 3

49. x z y y D A x z zig-zig C B A B C D Splay Operation r’(x) Case 2: • Hence, it holds thatf(x,y)≤-2 for all x,y>0 with x+y≤1, which implies the claim that r(x)+r’(z) ≤ 2(r’(x)-1) r(x) r’(z) Chapter 3

50. z x y zig-zag x D y z A A B C D B C Splay Operation Case 3: Amortized cost: ≤2+r’(x)+r’(y)+r’(z)-r(x)-r(y)-r(z) ≤2+r’(y)+r’(z)-2r(x)since r’(x)=r(z) and r(x)≤r(y) ≤2(r’(x)-r(x))because… Chapter 3

More Related