Download SortedList and SkipList: Implementations and Complexity and more Exams Data Structures and Algorithms in PDF only on Docsity!
CS 261 – Data Structures
Linked Lists Variations
List Variations: SortedList
- Sorting doesn’t change the asymptotic time to find an
element since a list only allows sequential access (can’t
jump directly to the n th^ element
- But can reduce the time by a factor of 2
- Sorting does help to produce a faster merge sort since
elements can be added in constant time (requires no
temporary data buffer
- The sorted list class is called SortedList?
Sorted List public class SortedList implements Sorted, Bag, FindMin { private LinkedList list = new LinkedList(); protected Comparator test; public SortedList(Comparator t) { test = t; } public SortedList() { test = new DefaultComparator; } ... }
- Collection interface handled by the underlying
LinkedList data member
- Much of the Bag interface is also handled by LinkedList
Sorted List: Bag Interface // Implementation of the Bag interface. public void addElement(Object obj) { DoubleLink link = list.head; while (link != list.tail) { if (test.compare(obj, link.val) < 0) { // Skip smaller elements. link.insert(obj); return; } link = link.next } link.insert(obj); }
What’s different?
- LinkedList addElement is O(1)
- What is it for SortedList?
Merge Sort
To merge a SortedList A with a sorted collection B
(does not have to be a sorted list), walk down both lists and
insert elements from B into A at the correct position:
B: 1 - 1 - 3 - 7 - 9 - 13 - 14
- Since inserting a link is O(1), the entire merge is O( n )
- The merge sort for SortedVector was also O( n ) … so which one is better?
2
A 4 5 8 10 sentinel Call insert at …
Merge Sort
public void merge(Sorted data) { DoubleLink link = list.head; Enumeration e = data.elements(); while (e.hasMoreElements()) { Object obj = e.nextElement(); while (link != list.tail && test.compare(link.val, obj) < 0) link = link.next; link.insert(obj); } }
- No temporary data buffer required
- Thus, no copying of data back and forth
List Variations (cont.)
- What is the algorithmic complexity of finding an
particular element in an linked list (sorted or unsorted)?
- How can we speed this up?
- Sorting the list still requires a linear time traversal to get to the n th^ element
- Hint: How does a cache work (e.g., in a CPU or the WWW)?
- Can reduce the average search time if the most often
accessed elements are near the front!!
Æ Self-Organizing List!
List Variations: Self-Organizing List
- Takes advantage of non-uniform accesses to data
- Assume that when an element is accessed there is a high
probability it will be accessed again in the near future
- Upon a successful search, move element so that it is
found faster in a subsequent search:
- Up one spot
- To front of list
- Up m spots
- Halfway between front and previous position
- Series of requests need to “fit a very particular pattern”
List Variations: Skip List
- What about imposing a tree structure on top of the list:
- Search time reduced to log n
- Can do it … but expensive to keep up-to-date
- Rather, maintain an approximate tree structure Æ Skip List
- Store elements in sorted order
- Keep hierarchy of ordered lists with downward pointers
- Each level has approximately half the elements as level below it
Skip List
70
2 54 70 87
2 14 54 70 83 87
2 14 18 42 54 70 78 83 83 87 94
1 2 6 10 14 18 34 41 42 52 54 70 71 78 83 83 84 87 94 90
top
tail
Skip List public class SkipList implements Sorted, Bag { private SkipLink top; private LinkedList list = new LinkedList(); protected Comparator test; public SkipList(Comparator t) { test = t; top = new SkipLink(null, list.tail); } // Implementation of the Collection interface. public boolean isEmpty() { return list.isEmpty(); } public int size() { return list.size(); } public Enumeration elements() { return list.elements(); } ... }
list list.tail
top
Skip List: Add Element
1. Starting at top:
(a) Slide left until element value is greater than previous list element (b) If not at bottom list, move down one level and repeat 1(a) using recursive call
2. Insert element into bottom list (LinkedList)
3. On the way back up (from recursion):
(a) Flip coin to determine if new link added at this level (b) If yes, insert new link and repeat (by returning the new link to the next higher level) (c) If no, stop (by returning null to next higher level)
Skip List: Example
Insert the following into a skip list: 32, 5, 17, 8, 23, 44, 12
Flip results: T, T, H, T, H, T, H, H, H, T, T (insert if heads)
Simplified algorithm for inserting values into skip list:
- Insert value into bottom list in sorted order
- While heads and new top list not created, insert value into upper list and move up 23 top
8 17 23 44
5 8 12 17 23 32 44 tail
Skip List: Algorithmic Complexity – Insert
- Best case:
- When every other link in each list is mirrored by a node in the next level
- Results in every level being half the size of the level below
- Total of log n levels Æ O(log n ) for insertion
- Average case: average path length of O(log n )
- Improvement over the SortedVector and SortedList insertion time of O( n )
- Insertion time for a LinkedList is O(1)
Skip List: Bag Interface – Search & Remove public boolean containsElement(Object obj) { Doublelink p = top; while (p != null) { p = slideLeft(p, obj); if (p.val != null && obj.equals(p.val)) return true; if (p instanceof SkipLink) p = ((SkipLink)p).down; else p = null; } return false; }
- The findElement method is similar except it returns the
element if found and an exception if not
- The removeElement method is also similar except that it
invokes remove in the SkipLink inner class
- Computational complexity?
Complexity Comparison
- All three operations are O(log n ) for a SkipList
- Searching and removing is faster than a LinkedList
- Inserting and removing is faster than a SortedVector
SkipList O(log n ) O(log n ) O(log n )
SortedList O( n ) O( n ) O( n )
LinkedList O(1) O( n ) O( n )
SortedVector O( n ) O(log n ) O( n )
Container Insert Search Remove