CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Slides:



Advertisements
Similar presentations
The Dictionary ADT: Skip List Implementation
Advertisements

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:
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Skip List & Hashing CSE, POSTECH.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Expected Running Times and Randomized Algorithms Instructor Neelima Gupta
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
© 2004 Goodrich, Tamassia Dictionaries   
Dictionaries and Hash Tables1  
CSC401 – Analysis of Algorithms Lecture Notes 7 Multi-way Search Trees and Skip Lists Objectives: Introduce multi-way search trees especially (2,4) trees,
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
Binary Search Trees Chapter 7 Objectives
Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
CS 221 Analysis of Algorithms Ordered Dictionaries and Search Trees.
CSC 211 Data Structures Lecture 13
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
1 Searching the dictionary ADT binary search binary search trees.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Skip Lists 二○一七年四月二十五日
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
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.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
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.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Searching an Array: Binary Search
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Skip Lists.
Dictionaries < > = Dictionaries Dictionaries
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
Skip Lists S3 + - S2 + - S1 + - S0 + -
Search Sorted Array: Binary Search Linked List: Linear Search
Skip Lists S3 + - S2 + - S1 + - S0 + -
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Parasol Lab, Dept. CSE, Texas A&M University
Binary Search Trees Chapter 7 Objectives
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
Dictionaries < > = Dictionaries Dictionaries
Binary Search Trees < > = Dictionaries
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5, Sec

Dictionaries A dictionary is a collection of elements each of which has a unique search key Uniqueness criteria may be relaxed (multiset) (I.e. do not force uniqueness) Keep track of current members, with periodic insertions and deletions into the set Examples Membership in a club, course records Symbol table (contains duplicates) Language dictionary (WordSmith, Webster, WordNet) Similar to database

Course Records Dictionary Member Record keystudent namehw Stan Smith Sue Margolin Billie King Roy Miller39...

Dictionary ADT simple container methods:size() isEmpty() elements() query methods:findElement(k) findAllElements(k) update methods:insertItem(k, e) removeElement(k) removeAllElements(k) special element NO_SUCH_KEY, returned by an unsuccessful search

How to Implement a Dictionary? Sequences / Arrays ordered unordered Binary Search Trees Skip lists Hashtables

Recall Arrays … Unordered array searching and removing takes O(?) time inserting takes O(?) time applications to log files (frequent insertions, rare searches and removals)

Ordered array searching takes O(log n) time (binary search) inserting and removing takes O(n) time application to look-up tables (frequent searches, rare insertions and removals) Apply binary search More Arrays

narrow down the search range in stages “high-low” game findElement(22) Binary Searches

Implement a dictionary with a BST A binary search tree is a binary tree T such that each internal node stores an item (k, e) of a dictionary. keys stored at nodes in the left subtree of v are less than or equal to k. keys stored at nodes in the right subtree of v are greater than or equal to k. Recall Binary Search Trees…

An Alternative to Arrays Unordered Array: insertion: O(1) search: O(n) Ordered Array insertion: O(n) search: O(log n) Skip Lists: insertion: O(log n) search: O(log n) And avoid the fixed-size drawback of arrays!

Skip Lists good implementation for a dictionary a series of lists {S0, S1, …, Sk} each list Si stores a sorted subset of the dictionary D - - 18 -- -- S0 S1 S2 S3

Skip Lists list S(i+1) contains items picked at random from S(i) each item has probability 50% of being in the upper level list like flipping a coin S0 has n elements S1 has about n/2 elements S2 has about n/4 elements …. S(i) has about ? elements

Traversing Positions in a Skip List Assume a node P in the skip list after(p) before(p) below(p) above(p) Running time of each operation?

Operations in a Skip List Use skip lists to implement dictionaries  Need to deal with Search Insert Remove

Searching Search for key K Start with p = the top-most, left position node in the skip list two steps: 1. if below(p) is null then stop we are at the bottom 2. while key(p) < K move to the right go back to 1

Searching Search for - - 18 -- -- S0 S1 S2 S3

More Searching Search for - - 18 -- -- S0 S1 S2 S3

Pseudocode for Searching Algorithm SkipSearch(k) Input: Search key k Output: Position p in S such that p has the largest key less than or equal to k p = top-most, left node in S while below(p) != null do p  below(p) while(key (after(p))  k do p  after(p) return p

Running Time Analysis log n levels  O(log n) for going down in the skip list at each level, O(1) for moving forward why? works like a binary search in skip lists, the elements in list S(i+1) play the role of search dividers for elements in S(i) (in binary search: mid-list elements to divide the search) total running time: O(log n)

Insertion in Skip Lists First: identify the place to insert new key k  node p in S0 with largest key less or equal than k Insert new item(k,e) after p with probability 50%, the new item is inserted in list S1 with probability 25%, the new item is inserted in list S2 with probability 12.5%, the new item is inserted in list S3 –with probability 6.25%, the new item is inserted in list S4 –….

Insertion in Skip Lists Insert - - 18 -- -- S0 S1 S2 S3 29

Pseudocode for Insertion Algorithm SkipInsert(k,e) Input: Item (k,e) Output: - p  SkipSearch(k) q  insertAfterAbove(p, null, Item (k,e)) while random( )  50% do while(above(p) == null) do p  before(p) p  above(p) q  insertAfterAbove(p, q, Item(k,e))

Running Time for Insertion? Search position for new item(k,e) O(log n) Insertion O(1) Total running time O(log n)

Removal from Skip Lists Easier than insertion Locate item with key k to be removed if no such element, return NO SUCH KEY otherwise, remove Item(k,e) remove all items found with above(Item(k,e))

Removal from Skip Lists Remove 18 Running time? - - 18 -- -- S0 S1 S2 S3

Efficient Implementation of Skip Lists use DoublyLinkedList implementation + two additional pointers above below For a LinkedList  provide pointer to head For a DoublyLinkedList  provide pointers to head and tail For a SkipList  ??