CS 261 - Fall 2009 Skip Lists. Skip Lists Advantages Ordinary linked lists and arrays have fast (O(1)) addition, but slow search Sorted Arrays have fast.

Slides:



Advertisements
Similar presentations
CMSC420: Skip Lists Kinga Dobolyi Based off notes by Dave Mount.
Advertisements

CS 225 Lab #11 – Skip Lists.
The Dictionary ADT: Skip List Implementation
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
CSC 172 DATA STRUCTURES. SKIP LISTS Read Weiss
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
Simple Sorting Algorithms
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
CSE 326 Randomized Data Structures David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
Elementary Data Structures and Algorithms
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Data Structures Binary Trees Phil Tayco Slide version 1.0 Mar. 22, 2015.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
CS Winter 2010 Tree Sort. Useful Properties of Sorted Data Structures Skip Lists (as well as AVL Trees, and various other data structures we will.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Searching Lesson Plan - 6. Contents  Evocation  Objective  Introduction  Sequential Search  Algorithm  Variations on sequential search  Mind map.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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( ),
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
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.
Sorted Dynamic Array Bag and Set
School of Computing Clemson University Fall, 2012
Linked List Introduction
UMBC CMSC 104 – Section 01, Fall 2016
Problems with Linked List (as we’ve seen so far…)
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
COMP 103 Binary Search Trees.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Skip Lists S3 + - S2 + - S1 + - S0 + -
CMSC 341 Skip Lists 1.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
CMSC 341 Skip Lists.
Data Structures Sorted Arrays
Data Structures and Algorithms
Building Java Programs
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CSCI 104 Skip Lists Mark Redekopp.
Data Structures and Algorithms
Presentation transcript:

CS Fall 2009 Skip Lists

Skip Lists Advantages Ordinary linked lists and arrays have fast (O(1)) addition, but slow search Sorted Arrays have fast search (O(log n)) but slow insertion Skip Lists have it all - fast (O(log n)) addition, search and removal times! Cost - Slightly more complicated

Start with an ordered list Lets begin with a simple ordered list, sentinel on the left, single links.

Start of the struct definition struct skiplist { struct skiplink * sentinel; int size; };

Operations Add (Object newValue) - find correct location, add the value Contains (Object testValue) - find correct location, see if its what we want Remove (Object testValue) - find correct location, possibly remove it See anything in common?

Slide Right struct skiplink * slideRight (struct skiplink * current, EleType testValue) { while ((current->next != 0) && LT(current->next->value, testValue)) current = current->next; return current; } Finds the link RIGHT BEFORE the place the value we want should be, if it is there

Add (EleType newValue) void add (struct skiplist* lst, EleType newValue) { struct skiplink * current = slideRight(lst->sentinel, newValue); struct link * newLink = (struct link *) malloc(sizeof(struct link)); assert (newLink != 0); newLink->value = newValue; newLink->next = current->next; current->next = newLink; lst->size++; }

Contains (EleType testValue) int contains (struct skip *lst, EleType testValue) { struct skiplink * current = slideRight (lst->leftSentinel, testValue); if ((current->next != 0) && (EQ(testValue, current->next->value))) return true; return false; } /* reminder, this is one level list, not real skiplist */

Remove (Object testValue) void remove (struct skiplist *lst, EleType testValue) { struct skiplink * current = slideRight (lst->leftSentinel, testValue); if ((current->next != 0) && (EQ(testValue, current->next->value))) { current->next = current->next->next; free(current->next); lst->size--; }

Problem with Sorted List What’s the use? Add is still O(n), so is contains, so is remove. No better than an ordinary list. Major problem with list - sequential access

Why no binary search? What if we kept a link to middle of list?

But need to keep going

And going and going

In theory it would work….. In theory this would work Would give us nice O(log n) search But would be really hard to maintain as values were inserted and removed What about if we relax the rules, and don’t try to be so precise…..

The power of probability Instead of being precise, keep a hierarchy of ordered lists with downward pointers Each list has approximately half the elements of the one below So, if the bottom list has n elements, how many levels are there??

Picture of Skip List

Slightly bigger list

Contains (Object testValue) Starting at topmost sentinel, slide right If next value is it, return true If next value is not what we are looking for, move down and slide right If we get to bottom without finding it, return false

Notes about contains See how it makes a zig-zag from top to bottom On average, slide only moves one or two positions So basically proportional to height of structure Which is????? O( ?? )

Remove Algorithm Start at topmost sentinal Loop as follows –Slide right. If next element is the one we want, remove it –If no down element, reduce size –Move down

Notice about Remove Only want to decrement size counter if at bottom level Again, makes zig-zag motion to bottom, is proportional to height So it is again O(log n)

Add (Object newElement) Now we come to addition - the most complex operation. Intuitive idea - add to bottom row (remember to increment count) Move upwards, flipping a coin As long as heads, add to next higher row At top, again flip, if heads make new row

Add Example Insert the following: Coin toss: T H T H H T H T (insert if heads)

The Power of Chance At each step we flip a coin So the exact structure is not predictable, and will be different even if you add the same elements in same order over again But the gross structure is predictable, because if you flip a coin N times as N gets large you expect N/2 to be heads.

Notes on Add Again proportional to height of structure, not number of nodes in list So also O(log n) So all three operations are O(log n) ! By far the fastest overall Bag structure we have seen! (Downside - does use a lot of memory as values are repeated. But who cares about memory? )

int skipListContains(struct skipList* slst, EleType d) { struct skipLink *tmp = slst->topSentinel; while (tmp != 0) { tmp = slideRight(tmp, d); if ((tmp->next != 0) && EQ(d, tmp->next- >value)) return 1; tmp = tmp->down; } return 0; }

void skipListAdd(struct skipList * slst, EleType d) { struct skipLink *downLink, *newLink; downLink = skipLinkAdd(slideRight(slst- >topSentinel,d),d); if (downLink != 0 && skipFlip()) { newLink = newSkipLink(d, 0, downLink); slst->topSentinel = newSkipLink(0, newLink, slst->topSentinel); } slst->size++; }

struct skipLink* skipLinkAdd(struct skipLink * current, EleType d) { struct skipLink *newLink, *downLink; newLink = 0; if (current->down == 0) { newLink = newSkipLink(d, current->next, 0); current->next = newLink; } else { downLink = skipLinkAdd(slideRight(current->down, d), d); if (downLink != 0 && skipFlip()) { newLink = newSkipLink(d, current->next, downLink); current->next = newLink; } return newLink; }

struct skipLink* newSkipLink(EleType d, struct skipLink * nlnk, struct skipLink* dlnk) { struct skipLink * tmp = (struct skipLink *) malloc(sizeof(struct skipLink)); assert(tmp != 0); tmp->value = d; tmp->next = nlnk; tmp->down = dlnk; return tmp; }

int skipFlip() { return rand() % 2; }

void skipListRemove(struct skipList* slst, EleType d) { struct skipLink *current, *tmp; current = slst->topSentinel; while (current != 0) { current = slideRight(current, d); if (current->next != 0 && EQ(d, current->next->value)) { tmp = current->next; current->next = current->next->next; free(tmp); if (current->down != 0) slst->size--; } current = current->down; }