Fundamental Algorithms Chapter 3: Search Structures

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

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 eS with minimal key(e) so that key(e)≥k Chapter 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Splay Operation • Weight of node x: w(x) • Tree weight of tree Twith root x: tw(x)= yTw(y) • Rank of node x: r(x) = log(tw(x)) • Potentialof tree T: (T) = xTr(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

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

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

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

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

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

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

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