Randomized Skip List n n Skip List Or

  • Slides: 29
Download presentation
Randomized Skip List n n Skip List Or, In Hebrew: רשימות דילוג 12/4/2020 Randomized

Randomized Skip List n n Skip List Or, In Hebrew: רשימות דילוג 12/4/2020 Randomized Algorithms - Treaps 1

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n Analyzing performance n The role of randomness Randomized Algorithms - Treaps 2

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n Analyzing performance n The role of randomness Randomized Algorithms - Treaps 3

Starting from scratch n Initial goal just search, no updates (insert, delete). n Simplest

Starting from scratch n Initial goal just search, no updates (insert, delete). n Simplest Data Structure? n linked list! n Search? O(n)! Can we do it faster? yes we can! 12/4/2020 Randomized Algorithms – Skip List 4

Developing the skip list n Let’s add an express lane. n Can quickly jump

Developing the skip list n Let’s add an express lane. n Can quickly jump from express stop to next express stop, or from any stop to next normal stop. n To search, first search in the express layer until about to go to far, then go down and search in the local layer. 12/4/2020 Randomized Algorithms – Skip List 5

Search cost n What is the search cost? 12/4/2020 Randomized Algorithms – Skip List

Search cost n What is the search cost? 12/4/2020 Randomized Algorithms – Skip List 6

Search cost – Cont. n This is minimized when: 12/4/2020 Randomized Algorithms – Skip

Search cost – Cont. n This is minimized when: 12/4/2020 Randomized Algorithms – Skip List 7

Discovering skip lists n If we keep adding linked list layers we get: 12/4/2020

Discovering skip lists n If we keep adding linked list layers we get: 12/4/2020 Randomized Algorithms – Skip List 8

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n Analyzing performance n The role of randomness Randomized Algorithms - Treaps 9

Initial definitions n Let S be a totally ordered set of n elements. n

Initial definitions n Let S be a totally ordered set of n elements. n A leveling with r levels of S, is a sequence of nested subsets (called levels) : where n and Given a leveling for S, the level of any element x in s is defined as: 12/4/2020 Randomized Algorithms - Treaps 10

Skip List Description n Given any leveling of the set S, we define the

Skip List Description n Given any leveling of the set S, we define the skip list corresponding to this structure like this: ¨ The level is stored in a sorted link list. ¨ Each node x in this linked list has a pile of nodes above it. ¨ There are horizontal and vertical pointers between nodes. ¨ For convenience, we assume that two special elements and belong to each of the levels. 12/4/2020 Randomized Algorithms - Treaps 11

Skip List Example n For example, for this leveling: n This is the skip

Skip List Example n For example, for this leveling: n This is the skip list corresponding to it: 12/4/2020 Randomized Algorithms - Treaps 12

Skip list as binary tree n An interval level I, is the set of

Skip list as binary tree n An interval level I, is the set of elements of S, spanned by a specific horizontal pointer at level i. n For example, for the previous skip list, this is the interval at level 2: 12/4/2020 Randomized Algorithms - Treaps 13

Skip list as binary tree n The interval partition structure is more conveniently viewed

Skip list as binary tree n The interval partition structure is more conveniently viewed as a tree, where each node corresponds to an interval. n If an interval J at level i+1 contains as a subsets an interval I at level i, then node J is the parent of node I in the tree. n For interval I at level i+1, C(I) denotes the number of children of Interval I at level i. 12/4/2020 Randomized Algorithms - Treaps 14

Searching skip lists n Consider an element y, that is not necessarily an member

Searching skip lists n Consider an element y, that is not necessarily an member of S, and assume we want to search for it in skip list s. n Let be the interval at level j that contains y. 12/4/2020 Randomized Algorithms - Treaps 15

Searching skip lists – Cont. n We can now view the nested sequence of

Searching skip lists – Cont. n We can now view the nested sequence of intervals as a root-leaf path in the tree representation of the skip list. 12/4/2020 Randomized Algorithms - Treaps 16

Random skip list To complete the description of the skip list, we have to

Random skip list To complete the description of the skip list, we have to specify the choice of the leveling that underlies it. n The basic idea is to chose a random leveling, thereby defining a random skip list. n A random leveling is defined as follows: given the choice of level the level is defined by independently choosing to retain each element with probability ½. n 12/4/2020 Randomized Algorithms - Treaps 17

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n Analyzing performance n The role of randomness Randomized Algorithms - Treaps 18

Search cost n What is the expected time to find an element in a

Search cost n What is the expected time to find an element in a random skip list? n We will show it is O(logn) with high probability. n What is the expected space of a random skip list? n O(n). Why? 12/4/2020 Randomized Algorithms - Treaps 19

Random procedure n An alternative view of the random construction is as follows: ¨

Random procedure n An alternative view of the random construction is as follows: ¨ Let l(x) for every be independent random variable, with geometric distribution. ¨ Let r be one more than the maximum of these random variables. ¨ Place x in each of the levels, n . …. . . As with random priorities in treaps, a random level is chosen once for every element in it’s insertion. 12/4/2020 Randomized Algorithms - Treaps 20

The expected number of levels n Lemma: the number of levels r in a

The expected number of levels n Lemma: the number of levels r in a random leveling of a set S of size n is O(logn) with high probability. n Proof: look at the board! n Does it mean that the E[r]=O(logn)? Why? Is it enough? No! 12/4/2020 Randomized Algorithms - Treaps 21

The search path length n The last result implies that the tree representing the

The search path length n The last result implies that the tree representing the skip list has height o(logn) with high probability. n Unfortunately, since the tree need not be binary, it does not immediately follows that the search time is similarly bounded. n The implementation of Find(x, s) corresponds to walking down the path 12/4/2020 Randomized Algorithms - Treaps 22

The search path length – Cont. n Walking down the path is as follows:

The search path length – Cont. n Walking down the path is as follows: ¨ At level j, starting at the node , use a vertical pointer to descend to the leftmost child of the current interval; ¨ n then using the horizontal pointers, move rightward till the node The cost of FIND(x, s) proportional to the number of levels as well as the number of intervals visited at each level. 12/4/2020 Randomized Algorithms - Treaps 23

The search path length is O(logn) n Lemma 2: Let y be any element

The search path length is O(logn) n Lemma 2: Let y be any element and consider the search path followed by FIND(y, S) in a random skip list for the set S of size n, then: ¨ A. is the length of the search path. ¨ B. 12/4/2020 Randomized Algorithms - Treaps 24

Proof of Lemma 2 n Proof of A: the number of nodes visited at

Proof of Lemma 2 n Proof of A: the number of nodes visited at level j does not exceed the number of children of the interval therefore in each level, you walk through elements and in total . Proof of B: look at the board! n This result shows that the expected time of search is o(logn). n 12/4/2020 Randomized Algorithms - Treaps 25

Insert and delete in skip list Insert and delete operation can be done both

Insert and delete in skip list Insert and delete operation can be done both in o(logn) expected time also. n To insert an element y: n ¨ a random level l(y) (may exceed r) should be chosen for y as described earlier. ¨ Then a search operation should find the search path of y ¨ Then update the correct intervals, and add pointers. n 12/4/2020 Delete operation is just the converse of insert. Randomized Algorithms - Treaps 26

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n

Outline 12/4/2020 n Motivation for the skipping n Skip List definitions and description n Analyzing performance n The role of randomness Randomized Algorithms - Treaps 27

The role of randomness n What is the role of the randomness in skip

The role of randomness n What is the role of the randomness in skip lists? n The random leveling of S enables us to avoid complicated “balancing” operations. n This simplifies our algorithm and decreases the overhead. 12/4/2020 Randomized Algorithms - Treaps 28

The role of randomness - cont. n It also saves us the need to

The role of randomness - cont. n It also saves us the need to “remember” the state of the node or the system. n Unlike binary search trees, skip lists behavior is indifferent to the input distribution. 12/4/2020 Randomized Algorithms - Treaps 29