<<

Dynamic Sets Dynamic sets are data structures that support some or all of the following Dynamic Sets and Elementary Data Structures operations. Search(S,k) Return x with x.key = k. Insert(S,x) Insert x into S. Dynamic Sets Delete(S,x) Delete x from S. Stacks Minimum(S) Return x minimum x.key. Queues Maximum(S) Return x maximum x.key. Successor(S,x) Return y next highest key. Linked Lists Predecessor(S,x) Return y next lowest key.

S is a . x and y are elements. k is a key. nil is returned if operation can’t be performed.

CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 2 DynamicSets...... 2 SimpleDynamicSetDataStructures ...... 3 Average Case Analysis of List-Search ...... 4 list-search2 ...... 5 Allocating and Freeing Objects ...... 6 BinaryTree...... 7 Unbounded Representation...... 8 Simple Dynamic Set Data Structures Dynamic Set Stack Queue Search List-Search Insert Push Enqueue List-Insert Delete Pop Dequeue List-Delete

All stack and queue operations are Θ(1). List-Insert is Θ(1) for unsorted lists, O(n) for sorted lists. List-Delete is Θ(1) for doubly-linked lists, O(n) for singly-linked lists.

CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 3

1 2 Average Case Analysis of List-Search Allocating and Freeing Objects

List-Search(L,k) Allocate-Object() x ← L.head if free = nil then return nil while x =6 nil and x.key =6 k x ← free do x ← x.next free ← free.next return x return x

List Search is Θ(n) on average. Free-Object(x) Suppose there are n elements. x.next ← free Each element is equally likely to be searched. free ← x Search for the ith element uses i comparisons. CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 6 CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 4

Binary Tree parent NIL Average Case of List-Search, Part 2 root key data The expected value is the following summation: n left right Σ There are n elements to search for. i=1 1/n Prob. for ith element is 1/n. i i comparisons needed to find ith element. NIL n i 1 n n +1 Σ = Σ i = i=1 n n i=1 2

The result is Θ(n). CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 5 NIL NIL NIL NIL NIL NIL

CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 7

3 4 Unbounded Tree Representation parent root NIL key data NIL childsibling

NIL

NIL NIL NIL NIL NIL

CS 5633 Analysis of Algorithms Dynamic Sets and Chapter 10: Slide – 8

5