The Dictionary ADT: Skip List Implementation

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
© 2004 Goodrich, Tamassia Skip Lists1 S0S0 S1S1 S2S2 S3S
Skip Lists. Outline and Reading What is a skip list (§9.4) – Operations (§9.4.1) – Search – Insertion – Deletion Implementation Analysis (§9.4.2) – Space.
CS 225 Lab #11 – Skip Lists.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
SKIP LISTS Amihood Amir Incorporationg the slides of Goodrich and Tamassia (2004)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Skip Lists Present By PAKDEE PATTANAJEDSADA SITTHICHOK SNANSIENG SIWAKORN THAMMAYTHA PATOMPOL TAESUJI
CSC 172 DATA STRUCTURES. SKIP LISTS Read Weiss
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Expected Running Times and Randomized Algorithms Instructor Neelima Gupta
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
CSC401 – Analysis of Algorithms Lecture Notes 7 Multi-way Search Trees and Skip Lists Objectives: Introduce multi-way search trees especially (2,4) trees,
Skip Lists Michael Oudshoorn. CS351 Software Engineering (AY05)2 Skip Lists Binary Search Trees: O(log n) –If, and only if, the tree is “balanced” Insertion.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Skip Lists Mrutyunjay. Introduction ▪ Linked Lists Benefits & Drawbacks: – Benefits: – Easy Insert and Deletes, implementations. – Drawbacks: – Hard to.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Skip Lists 二○一七年四月二十五日
CMSC 341 Skip Lists. 8/3/2007 UMBC CMSC 341 SkipList 2 Looking Back at Sorted Lists Sorted Linked List What is the worst case performance of find( ),
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
Instructor Neelima Gupta Expected Running Times and Randomized Algorithms Instructor Neelima Gupta
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5, Sec
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Skip Lists – Why? BSTs –Worse case insertion, search O(n) –Best case insertion, search O(log n) –Where your run fits in O(n) – O(log n) depends on the.
Chapter 16: Searching, Sorting, and the vector Type.
Skip Lists. Linked Lists Fast modifications given a pointer Slow traversals to random point.
Skip Lists S3   S2   S1   S0  
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Searching an Array: Binary Search
Skip Lists S3 + - S2 + - S1 + - S0 + -
Skip Lists.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Skip Lists S3 + - S2 + - S1 + - S0 + -
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Parasol Lab, Dept. CSE, Texas A&M University
Skip List: formally A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , … , Sh such that Each list Si contains the special.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CSCI 104 Skip Lists Mark Redekopp.
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
Binary Search Trees < > = Dictionaries
CSE 373: Data Structures and Algorithms
Presentation transcript:

The Dictionary ADT: Skip List Implementation CSCI 2720 Eileen Kraemer Spring 2005

Definition of Dictionary Primary use: to store elements so that they can be located quickly using keys Motivation: each element in a dictionary typically stores additional useful information beside its search key. (eg: bank accounts) Can be implemented using lists, binary search trees, Red/black trees, hash table, AVL tree, Skip lists

Dictionary ADT Size(): Returns the number of items in D IsEmpty(): Tests whether D is empty FindElement(k): If D contains an item with a key equal to k, then it return the element of such an item FindAllElements(k): Returns an enumeration of all the elements in D with key equal k InsertItem(k, e): Inserts an item with element e and key k into D. remove(k): Removes from D the items with keys equal to k, and returns an numeration of their elements

Performance considerations: lists Size(): O(1) if size is stored explicitly, else O(n) IsEmpty(): O(1) FindElement(k): O(n) … and we’ll talk about variations lists that can improve this InsertItem(k, e): O(1) (assumes item already found) Remove(k):

Pros and cons: list and array implementations Lists: Efficient insertions/deletions -- O(1) Inefficient searching – O(n) Optimizations exist, but still …. Arrays: Efficient lookup/searching On index O(1) On key values (using Binary Search) -- O(log n) Inefficient insertions/deletions -- O(n)

Skip List: characteristics of both lists and arrays A list on which we can perform binary search Skip Lists support O(log n) Insertion Deletion Querying A relatively recent data structure “A probabilistic alternative to balanced trees”

(Perfect) Skip Lists A skip list is a collection of lists at different levels The lowest level (0) is a sorted, singly linked list of all nodes The first level (1) links alternate nodes The second level (2) links every fourth node In general, level i links every 2ith node In total, log2 n levels (i.e. O(log2 n) levels). Each level has half the nodes of the one below it

Example of a (perfect)Skip List + - S2 - 31 + S1 - 23 31 44 64 + S0 - 12 23 26 31 34 44 56 64 +

Insertions/Deletions When we add a new node, our beautifully precise structure might become invalid. We may have to change the level of every node One (sneaky) option is to move all the elements around Both options take O(n) time, which is back where we began Is it possible to achieve a net gain?

Example of a randomized Skip List + - S2 - 31 + S1 - 23 31 34 64 + S0 56 64 78 + 31 34 44 - 12 23 26

Definition of Skip List A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , … , Sh such that: Each list Si contains the special keys + and - List S0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S0  S1  …  Sh List Sh contains only the two special keys

Initialization A new list is initialized as follows: 1) A node NIL (+ ) is created and its key is set to a value greater than the greatest key that could possibly used in the list 2) Another node NIL (-) is created, value set to lowest key that could be used 3) The level (high) of a new list is 1 4) All forward pointers of the header point to NIL

Searching in Skip List We search for a key x in a skip list as follows: We start at the first position of the top list At the current position p, we compare x with y  key(after(p)) x = y: we return element(after(p)) x > y: we “scan forward” x < y: we “drop down” If we try to drop down past the bottom list, we return NO_SUCH_KEY Example: search for 78

Searching in Skip List Example + - S3 S2 - 31 + S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 + P is 64, at S1,+ is bigger than 78, we drop down At S0, 78 = 78, we reach our solution

Insertion The insertion algorithm for skip lists uses randomization to decide how many references to the new item (k,e) should be added to the skip list We then insert (k,e) in this bottom-level list immediately after position p. After inserting the new item at this level we “flip a coin”. If the flip comes up tails, then we stop right there. If the flip comes up heads, we move to next higher level and insert (k,e) in this level at the appropriate position.

Randomized Algorithms A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b  random() if b = 0 do A … else { b = 1} do B … Its running time depends on the outcomes of the coin tosses We analyze the expected running time of a randomized algorithm under the following assumptions the coins are unbiased, and the coin tosses are independent The worst-case running time of a randomized algorithm is large but has very low probability (e.g., it occurs when all the coin tosses give “heads”)

Insertion in Skip List Example Suppose we want to insert 15 Do a search, and find the spot between 10 and 23 Suppose the coin come up “head” three times S3 + - p2 S2 S2 - + + - 15 p1 S1 S1 - 23 + + - 23 15 p0 S0 S0 - 10 23 36 + + - 10 36 23 15

Deletion We begin by performing a search for the given key k. If a position p with key k is not found, then we return the NO SUCH KEY element. Otherwise, if a position p with key k is found (it would be found on the bottom level), then we remove all the position above p If more than one upper level is empty, remove it.

Deletion in Skip List Example 1) Suppose we want to delete 34 2) Do a search, find the spot between 23 and 45 3) Remove all the position above p S3 - + p2 S2 S2 - 34 + - + p1 S1 S1 - 23 34 + - 23 + p0 S0 S0 - 12 23 34 45 + - 12 23 45 +

Randomized Skip Lists We sacrifice “perfection” in order to improve our performance of Insert/Remove. So our array of lists is no longer going to be that of exact traversals of successive mid-points but it’s going to be approximately so. We choose the height of a new node randomly. We want the proportion of number of level-0 nodes, level-1 nodes, etc. to be similar to the Perfect list.

Randomized Skip Lists In Randomized Skip Lists, when we Insert a new node we no longer shift values but instead we insert the node and simply generate a “random” level for it. This works well when we have a large number of nodes which gives a fairly uniform mix of node levels. Not perfect but we can still skip over large groups of nodes

Generating Random Heights Consider this pseudo-code snippet (assume n is the no. of nodes) NodeLevel = 0; x = rand( ); while( x < 0.5 && NodeLevel < log2 n ) { NodeLevel++; } This generates a good mix if rand( ) works correctly

Analysis (Randomized) Expected height of a node: E[h] = 0.5 * 0 + 0.5 * (1 + E[h]) Very high probability that maximum height is O(log n) Probability h > 2 log n = 1/n Probability h > c log n = 1/nc-1 Expected number of nodes on level i is n/2i Expected time for Find( ), Insert( ), Remove( ) is O(log n) Analyzing Find( ) is the important case. Here’s why. Think about what an Insert( ) entails – first we need to figure out where to insert the new node, then we need to link it into the list at each level. This is basically a Find( ) followed by O(log n) pointer operations. Now consider Remove( ). First we need to find the node we’re removing. Then, we need to remove it from the list at each level. This is basically just a Find( ) followed by O(log n) worth of pointer operations again. It turns out that Find( ) takes O(log n) expected time, so all three do.

Analysis of Find( ) Consider traversing the list backwards Assume the list has up and left pointers Algorithm p = CurrentNode level 0 pointer while (p != Head) if up(p) exists p = up(p) else p = left(p) Obviously, this is backwards from the real Find( ) algorithm, but it’s easier to analyze this way. We want to go up whenever possible, because we have to reach the top-left eventually, and the farther up we go, the fewer nodes are in the list at that level.

Analysis of Find( ) Probability that up(p) exists is 0.5 Therefore, we expect to take each branch of the loop an equal number of times So the total number of moves is 2 * the number of upward moves But the height is only O(log n) So, the total number of moves is O(log n)