Docsity

Linked Lists Variations - Lecture Slides | CS 261, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS261–DataStructures
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 directlyto the nth element
– But can reduce the time bya 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?
Sorted List: FindMin Interface
The FindMin interface is simple for sorted data:
// Implementation of the FindMin interface.
public Object getFirst() { return list.getFirst(); }
public void removeFirst() { list.removeFirst(); }
Inheritance vs. Composition
Why does SortedList (and SortedVector)use
composition (i.e., it contains a LinkedList (or Vector))
instead of inheriting?
– Both mechanisms create a new abstraction by leveraging an
existing software component
– If the type reduces the numberof operations Æuse composition
– Composition allows objects to be reused whileallowing for
constraints on those aspects (i.e., meth ods and data) you don’t
want accessible by hiding the underlying data structure
– Example: can’t maintain the Sorted property if SortedList
inherits from LinkedList and thus, through inheritance,
implements the Deque interface
pf3
pf4

Partial preview of the text

Download Linked Lists Variations - Lecture Slides | CS 261 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?

Sorted List: FindMin Interface

The FindMin interface is simple for sorted data:

// Implementation of the FindMin interface. public Object getFirst() { return list.getFirst(); } public void removeFirst() { list.removeFirst(); }

Inheritance vs. Composition

Why does SortedList (and SortedVector) use

composition (i.e., it contains a LinkedList (or Vector))

instead of inheriting?

  • Both mechanisms create a new abstraction by leveraging an existing software component
  • If the type reduces the number of operations Æ use composition
  • Composition allows objects to be reused while allowing for constraints on those aspects (i.e., methods and data) you don’t want accessible by hiding the underlying data structure
  • Example: can’t maintain the Sorted property if SortedList inherits from LinkedList and thus, through inheritance, implements the Deque interface

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”

Self-Organizing List public class SelfOrgList extends LinkedList { public boolean containsElement(Object obj) { for (DoubleLink l = head; l != tail; l = l.next) if (obj.equals(l.val)) { l.remove(); // Move to front of list. addFirst(obj); return true; } return false; } }

  • Uses inheritance ( is-a ) instead of the composition ( has-a )

used by SortedList

  • Works well when accesses follow a pattern (e.g., every 7 th access is the same value)

List Variations: Skip List

  • Major problem with sorted list Æ sequential access:
    • Can’t perform a binary search on a linked list
  • What if we have direct access to the middle element:
    • Can reduce the search time by a factor of 2
  • Now add another reference to the mid point of each half:
    • Search time reduced to ¼ the time for the full list

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:

  1. Insert value into bottom list in sorted order
  2. 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)
    • So, why do a skip list?

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