Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
Advertisements

© 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.
The Dictionary ADT: Skip List Implementation
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
SKIP LISTS Amihood Amir Incorporationg the slides of Goodrich and Tamassia (2004)
Skip Lists Present By PAKDEE PATTANAJEDSADA SITTHICHOK SNANSIENG SIWAKORN THAMMAYTHA PATOMPOL TAESUJI
Alon Efrat Computer Science Department University of Arizona SkipList.
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:
© 2004 Goodrich, Tamassia Hash Tables1  
October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.1 Introduction to Algorithms 6.046J/18.401J LECTURE12 Skip Lists Data.
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
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
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 5 Heaps and Hash Tables Objectives: Introduce Heaps, Heap-sorting, and Heap- construction Analyze the performance.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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,
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.
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
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.
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
CSC Analysis of Algorithms 3-1 CSC401 – Analysis of Algorithms Chapter 3 Search Trees and Skip Lists Objectives: Review binary search trees and present.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
Sorting Fun1 Chapter 4: Sorting     29  9.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
CSC 211 Data Structures Lecture 13
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
1 Searching the dictionary ADT binary search binary search trees.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
Skip Lists 二○一七年四月二十五日
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Instructor Neelima Gupta Expected Running Times and Randomized Algorithms Instructor Neelima Gupta
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
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
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)
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.
Lecture No.43 Data Structures Dr. Sohail Aslam.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Bit Vector Linked List (Sorted and Unsorted) Hash Table Search Trees
Skip Lists.
Skip Lists S3 + - S2 + - S1 + - S0 + -
SKIP LIST & SKIP GRAPH James Aspnes Gauri Shah
Skip Lists S3 + - S2 + - S1 + - S0 + -
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Parasol Lab, Dept. CSE, Texas A&M University
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
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
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
CSE 373: Data Structures and Algorithms
Presentation transcript:

Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005

2 Topics Radix Sort Skip Lists Random Variables

3 Radix Sort Limit input to fixed-length numbers or words. Represent symbols in some base b. Each input has exactly d “digits”. Sort numbers d times, using 1 digit as key. Must sort from least-significant to most-significant digit. Must use any “stable” sort, keeping equal-keyed items in same order.

4 Radix Sort Example ababaccaaacbbabccabbaaac Input data:

5 Radix Sort Example ababaccaaacbbabccabbaaac abc Place into appropriate pile. Pass 1: Looking at rightmost position.

6 Radix Sort Example ababaccaaacbbabccabbaaac abc Join piles. Pass 1: Looking at rightmost position.

7 Radix Sort Example ababaccaaacbbabccabbaaac abc Pass 2: Looking at next position. Place into appropriate pile.

8 Radix Sort Example ababaccaaacbbabccabbaaac abc Join piles. Pass 2: Looking at next position.

9 Radix Sort Example abc baccaababaacababbaacbcca Pass 3: Looking at last position. Place into appropriate pile.

10 Radix Sort Example abc baccaababaacababbaacbcca Pass 3: Looking at last position. Join piles.

11 Radix Sort Example baccaababaacababbaacbcca Result is sorted.

12 Radix Sort Algorithm rsort(A,n): For d = 0 to n-1 /* Stable sort A, using digit position d as the key. */ For i = 1 to |A| Add A[i] to end of list ((A[i]>>d) mod b) A = Join lists 0…b-1  (d  n) time, where d is taken to be a constant.

13 Skip List   S0S0 S1S1 S2S2 S3S3     2315 Below is an implementation of Skip List in which the topmost level is left empty. There is also an implementation in which the topmost level is never left empty. ( As in the lecture notes )

14 Skip List The definition of a dictionary Definition of skip lists Searching in skip lists Insertion in skip lists Deletion in skip lists Probability and time analysis

15 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) Red/black tree, hash table, AVL tree, Skip lists

16 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

17 Definition of Skip List A skip list for a set S of distinct (key, element) items is a series of lists S 0, S 1, …, S h such that –Each list S i contains the special keys  and  –List S 0 contains the keys of S in nondecreasing order –Each list is a subsequence of the previous one, i.e., S 0  S 1  …  S h –List S h contains only the two special keys

18 Example of a Skip List We show how to use a skip list to implement the dictionary ADT      31  64  3134  23 S0S0 S1S1 S2S2 S3S3

19 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

20 Searching in Skip List - general description 1) If S.below(p).the position below p in the same tower is null. We are at the bottom and have located the largest item in S with keys less than or equal to the search key k. Otherwise, we drop down to the next lower level in the present tower to setting p  S.below(p). 2) Starting at position p, we move p forward until it is at the right-most position on the present level such that key(p) <= k. We call this scan forward step.

21 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

22 Searching in Skip List Example   S1S1 S2S2 S3S3  31  64  3134    S0S0 1)P is  at S 1,  is bigger than 78, we drop down At S 0, 78 = 78, we reach our solution

23 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.

24 Randomized Algorithms 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”) 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

25 Insertion in Skip List Example    23   S0S0 S1S1 S2S2   S0S0 S1S1 S2S2 S3S3     2315 p0p0 p1p1 p2p2 1)Suppose we want to insert 15 2)Do a search, and find the spot between 10 and 23 3)Suppose the coin come up “head” three times

26 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.

27 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   4512   23   S0S0 S1S1 S2S2   S0S0 S1S1 S2S2 S3S3       p0p0 p1p1 p2p2

28 Probability Analysis Insertion, whether or not to increase h Worst case for find, insert, delete: O (n + h) Due to low probability events when every item belongs to every level in S Very low probability that it will happen Not a fair assessment

29 Performance of a Dictionary by a Skip List Operation Time Size, isEmpty O(1) findElement O(log n) (expected) insertItem O(log n) (expected) Remove O(log n) (expected) FindAllElements O(log n + s) (expected) removeAll O(log n + s) (expected) - S being the extra matching keys we have to go through