CS225: Data Structures and Software Principles Section 12 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CS225: Data Structures and Software Principles Section 12

Description:

Here in our discussion were are going to assume that our lists are sorted ... Arrays have better performance for a binary search unlike lists ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 21
Provided by: Qix
Category:

less

Transcript and Presenter's Notes

Title: CS225: Data Structures and Software Principles Section 12


1
CS225 Data Structures and Software
PrinciplesSection 12
  • Skip Lists

2
Agenda
  • Skip Lists
  • Motivation (Why Skip Lists?)
  • Definition/Structure (What is it?)
  • Implementation (How do you make one?)
  • Performance (Is it any good?)
  • Sample Code for Skip Lists
  • Node implementation
  • Find()
  • Insert()
  • More example review questions

3
Motivation
  • Here in our discussion were are going to assume
    that our lists are sorted
  • Average runtime for insert, find, remove in
    ordinary binary trees is O(log(n)), but O(n)
    worst case.
  • Arrays v/s Lists
  • Lists have better performance for Insert/Remove
  • Arrays have faster random access
  • Arrays have better performance for a binary
    search unlike lists
  • This last drawback is overcome using Skip Lists
  • Skip Lists let you do a good binary search!!

4
Skip Lists
  • Skip Lists come in two flavors
  • Perfect Skip Lists
  • Randomized Skip Lists
  • Perfect Skip Lists
  • In Lists we keep a pointer in each node so that
    we can move from one node to next (one traversal
    path)
  • In Skip Lists we maintain multiple traversal paths

5
Skip Lists general structure
Level 2 list
Level 4 list
  • We have additional means of traversing every
    second node in the list, every fourth node, every
    eight node and so on
  • We maintain an array of pointers each of which
    points to the starting node in these different
    traversals ? an array of lists!
  • New terminology used in Skip Lists
  • Level (by itself refers to the level of a
    traversal of the list). All nodes have a level 0
    pointer.
  • Level-i node (node with highest pointer at level
    i)

Level i list
6
Perfect Skip List structure
  • Level-i node A node which has a maximum level of
    i
  • In a Perfect Skip List with n nodes
  • Exactly ?n/2? nodes are level-0 nodes
  • Exactly ?n/4? nodes are level-1 nodes
  • Exactly ?n/8? nodes are level-2 nodes
  • .. and so on ( stop when you have determined the
    level of every node on the list )

7
Find() idea
  • When doing a search for an element in the list we
    first try a traversal at the highest level and
    drop a level when needed
  • Given n nodes we will have ?log2 n? levels or in
    other words we have O(log2 n) levels
  • As we drop levels, each successive level has
    approximately twice the number of nodes as the
    previous one

8
Find() idea
  • At a given level, we will never need to look at
    more than two nodes !! ( at most two comparisons
    )
  • So at any level, we either move forward one node
    or we do not move at all instead drop a level
  • This implies that at each level we spend only a
    constant time O(1) time
  • Given O(log2 n) levels, our search algorithm runs
    in O(1) O(log2 n) O(log2 n) time !!!
    (matching our performance for binary search in
    arrays)

9
Perfect Skip List problems
  • Insert/Remove may not be too elegant and may
    require too much work
  • Current approach When inserting/removing nodes,
    rearrange values in nodes by shifting values
    rather than rearranging nodes themselves
  • But this way Insert/Remove run in O(n) time!
  • Question Can we do better than that?
  • Answer Yes we can ? use Randomized Skip Lists

10
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 have
    exact traversals of successive mid-points but
    its going to be approximately so
  • We choose the height of a new node randomly when
    we Insert instead of shifting values.
  • Want proportion of level-0 nodes, level-1 nodes,
    etc. to be similar to the Perfect skip list,
    mixed uniformly
  • Works well when we have many nodes
  • The randomizing algorithm better do a good job!

11
Randomized Skip Lists
  • Consider this code-snippet (assume n is the of
    nodes)
  • SkipNode(int nodeMaxLevel)
  • Assert(nodeMaxLevel gt 0, "Cannot create
    a node with negative level!")
  • nodeTopLevel 0
  • while ((rand() 2 0)
    (nodeTopLevel lt nodeMaxLevel))
  • nodeTopLevel
  • ptrArray.setBounds(0, nodeTopLevel)
  • ptrArray.initialize(NULL)
  • So approximately, 1/2 of the time it is going to
    create a level-0 node, 1/4 of the time a level-1
    node, 1/8 of the time a level-2 node and so on
  • This generates a good mix if rand( ) does a good
    job
  • Ratio doesnt have to be ½. Could use smaller
    one to cut down on pointers

12
Randomized Skip List Performance
  • Expected performance of Randomized Skip Lists
  • Here we assume that randomized level number
    generation works as expected
  • So this means that we have the distribution as
    expected
  • Then the operations Find, Insert and Remove, all
    three run in O(log2 n) time which is an
    improvement over Perfect Skip Lists

13
Randomized Skip List Problem
  • We may have an unexpected worst case performance
    of O(n) for all three operations
    (Find/Insert/Remove)! (worse than Perfect Skip
    Lists)
  • This happens when the distribution of node-level
    numbers is not as expected and is way off

14
Skip List Implementation Data
  • private
  • class SkipNode
  • public
  • SkipNode(int nodeMaxLevel) // randomly sets
    node level
  • SkipNode(int level, int nodeMaxLevel) //
    user-specified level
  • Etype element // value stored in
    this node
  • ArrayltSkipNodegt ptrArray // array of "next
    pointers" for all
  • // levels this node is a part of
  • int nodeTopLevel // top level of this node
  • ArrayltSkipNodegt head // array of pointers
    to first element in each // level of
    skiplist
  • SkipNode currentPtr // pointer to current
    element of skiplist
  • int maxLevel // the maximum allowable level
    of a skiplist node
  • int size // the number of nodes in the
    skiplist

15
Sample Code
  • We are going to see some sample code for Find
    Insert on a Randomized Skip List
  • Complete code available at
  • cs225/src/library/05-skiplist/

16
Skip List Practice Problem
  • Given the following sequence of randomly
    generated 0s and 1s, decide the randomized skip
    list that would be constructed if 8 nodes are
    inserted into the list.
  • 100011001000101101

17
Practice Problems
  • Suppose you want to know how many entering edges
    there are for a particular vertex in a graph of V
    vertices and E edges. Assume that this graph is
    implemented using an adjacency matrix and that
    you know the index of the vertex for which you
    want this knowledge of entering edges.
  • Complete the function below to return the
    complement of a graph represented by an
    adjancency matrix
  • void Complement(int graph, int n)

18
Practice Problems
  • 3) Consider the following adjacency list
    implementation for a graph class
  • class EdgeNode
  • public
  • int target
  • EdgeNode next
  • Complete the following function that takes a
    graph and a vertex index as arguments and checks
    whether that vertex is the first target for some
    other vertex in the graph
  • bool isFirst(ArrayltEdgeNodegt graph, int vert)

19
Practice Problems
  • During the union by height algorithm, when can
    the height of an up-tree actually change?

20
Practice Problems
  • 4). Why in the heap implementation for Dijkstras
    algorithm there is no use for a marked field?
    Recall that the use of a known field in the table
    implementation is to indicate whether we have
    previously found the best path to this vertex
    already.
Write a Comment
User Comments (0)
About PowerShow.com