Skip List: Implementation

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
Hashing. 2 Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Hashing. Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
Hashing Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hashing. 2 Preview A hash function is a function that: When applied to an Object, returns a number When applied to equal Objects, returns the same number.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hashing Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Lecture No.01 Data Structures Dr. Sohail Aslam
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
ADSA: Hashing/ Advanced Data Structures and Algorithms Objectives – –introduce hashing, hash functions, hash tables, collisions, linear probing,
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
H ASH TABLES. H ASHING Key indexed arrays had perfect search performance O(1) But required a dense range of index values Otherwise memory is wasted Hashing.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
CSCS-200 Data Structure and Algorithms Lecture
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
Hashing. Searching Consider the problem of searching an array for a given value If the array is not sorted, the search requires O(n) time If the value.
Hashing. 2 Preview A hash function is a function that: When applied to an Object, returns a number When applied to equal Objects, returns the same number.
Sets and Maps Chapter 9.
Skip Lists S3   S2   S1   S0  
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Hashing.
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.
Tables and Dictionaries
Hashing.
COMP 53 – Week Eleven Hashtables.
Lecture No.06 Data Structures Dr. Sohail Aslam
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Searching an Array: Binary Search
Hashing.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Searching.
Hash Tables banana apple banana apple cantaloupe kiwi
CS223 Advanced Data Structures and Algorithms
CMSC 341 Hashing Prof. Neary
Hashing.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS202 - Fundamental Structures of Computer Science II
Hashing.
Sets and Maps Chapter 9.
Hashing.
Algorithms: Design and Analysis
CS223 Advanced Data Structures and Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Hashing.
Hashing.
Hash Maps Introduction
Hashing.
Data Structures and Algorithm Analysis Hashing
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Hash Tables banana apple banana apple cantaloupe kiwi
Lecture-Hashing.
Lecture No.42 Data Structures Dr. Sohail Aslam.
Presentation transcript:

Skip List: Implementation - + S2 - 34 + S1 - 23 34 + Start of 41. S0 - 12 23 34 45 +

Lecture No.41 Data Structure Dr. Sohail Aslam

Implementation: TowerNode 40 50 60 head tail 20 30 26 57 Tower Node TowerNode will have array of next pointers. Actual number of next pointers will be decided by the random procedure. Define MAXLEVEL as an upper limit on number of levels in a node.

Implementation: QuadNode A quad-node stores: item link to the node before link to the node after link to the node below link to the node above This will require copying the key (item) at different levels quad-node x Start lecture 41

Skip Lists with Quad Nodes - + S2 - 31 + S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 +

Performance of Skip Lists In a skip list with n items The expected space used is proportional to n. The expected search, insertion and deletion time is proportional to log n. Skip lists are fast and simple to implement in practice

Implementation 5: AVL tree An AVL tree, ordered by key insert: a standard insert; (log n) find: a standard find (without removing, of course); (log n) remove: a standard remove; (log n) key entry key entry key entry key entry and so on

Anything better? So far we have find, remove and insert where time varies between constant logn. It would be nice to have all three as constant time operations!

Implementation 6: Hashing An array in which TableNodes are not stored consecutively Their place of storage is calculated using the key and a hash function Keys and entries are scattered throughout the array. key entry 4 10 hash function array index Key 123

Hashing insert: calculate place of storage, insert TableNode; (1) find: calculate place of storage, retrieve entry; (1) remove: calculate place of storage, set it to null; (1) key entry 4 10 123 All are constant time (1) !

Hashing We use an array of some fixed size T to hold the data. T is typically prime. Each key is mapped into some number in the range 0 to T-1 using a hash function, which ideally should be efficient to compute.

Example: fruits Suppose our hash function gave us the following values: hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("cantaloupe") = 7 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 kiwi banana watermelon apple mango cantaloupe grapes strawberry 1 2 3 4 5 6 7 8 9

Example Store data in a table array: kiwi banana watermelon apple table[5] = "apple" table[3] = "watermelon" table[8] = "grapes" table[7] = "cantaloupe" table[0] = "kiwi" table[9] = "strawberry" table[6] = "mango" table[2] = "banana" kiwi banana watermelon apple mango cantaloupe grapes strawberry 1 2 3 4 5 6 7 8 9

Example Associative array: kiwi table["apple"] table["watermelon"] table["grapes"] table["cantaloupe"] table["kiwi"] table["strawberry"] table["mango"] table["banana"] kiwi banana watermelon apple mango cantaloupe grapes strawberry 1 2 3 4 5 6 7 8 9

Example Hash Functions If the keys are strings the hash function is some function of the characters in the strings. One possibility is to simply add the ASCII values of the characters: æ length - 1 ö å h ( str ) = ç str [ i ] ÷ % TableSize ç ÷ è ø i = Example : h ( ABC ) = ( 65 + 66 + 67 )% TableSize

Finding the hash function int hashCode( char* s ) { int i, sum; sum = 0; for(i=0; i < strlen(s); i++ ) sum = sum + s[i]; // ascii value return sum % TABLESIZE; }

Example Hash Functions Another possibility is to convert the string into some number in some arbitrary base b (b also might be a prime number): æ length - 1 ö å h ( str ) = ç str [ i ] ´ b i ÷ % T ç ÷ è ø i = Example : h ( ABC ) = ( 65 b + 66 b 1 + 67 b 2 )% T

Example Hash Functions If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features. For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys. In general, to avoid situations like this, T should be a prime number. End of lecture 41. Start of lecture 42.