Download Dictionaries - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Dictionaries
- Collection of items.
- Each item is a pair. (key, element) Pairs have different keys.
Application
- Collection of student records in this class. (key, element) = (student name, linear list of assignment and exam scores) All keys are distinct.
- Collection of in-use domain names. (godaddy.com, owner information) All keys are distinct.
Dictionary With Duplicates
- Keys are not required to be distinct.
- Word dictionary. Pairs are of the form (word, meaning). May have two or more entries for the same word. - (bolt, a threaded pin) - (bolt, a crash of thunder) - (bolt, to shoot forth suddenly) - (bolt, a gulp) - (bolt, a standard roll of cloth) - etc.
Dictionary Operations
- Static Dictionary. initialize/create get(theKey) (a.k.a. search) CD ROM word dictionary CD ROM geographic database of cities, rivers, roads, auto navigation system, etc.
- Dynamic Dictionary. get(theKey) (a.k.a. search) put(theKey, theElement) (a.k.a. insert) remove(theKey) (a.k.a. delete)
Bin Packing Heuristics
- Best Fit. Items are packed one at a time in given order. To determine the bin for an item, first determine set S of bins into which the item fits. If S is empty, then start a new bin and put item into this new bin. Otherwise, pack into bin of S that has least available capacity.
Best Fit Example
n = 4
weights = [4, 7 , 3 , 6 ]
capacity = 10
Pack red item into first bin.
Best Fit
n = 4
weights = [4, 7 , 3 , 6 ]
capacity = 10
Pack blue item next.
Doesn’t fit, so start a new bin.
Best Fit
n = 4
weights = [4, 7 , 3 , 6 ]
capacity = 10
Best Fit
n = 4 weights = [4, 7 , 3 , 6 ] capacity = 10
Optimal packing.
Implementation Of Best Fit
- Use a dynamic dictionary in which the elements are of the form (available capacity, bin index).
- Pack an item whose requirement is s.
Find a bin with smallest available capacity >= s. Reduce available capacity of this bin by s.
- May be done by removing old pair and inserting new one. If no such bin, start a new bin.
- Insert a new pair into the dictionary.
Best Fit Example
- 12 active bins.
- Pack item whose size is 22.
20
10
6
2 8
15
40
30
25 35
7
18
Complexity Of Best Fit
- Use a balanced binary search tree in which the pairs are (available capacity, bin index).
- O(n) get, put, and remove/put operations, where n is the number of items to be packed.
- O(n log n).
leftSize And Rank
Rank of an element is its position in inorder (inorder = ascending key order).
[2,6,7,8,10,15,18,20,25,30,35,40]
rank(2) = 0 rank(15) = 5
rank(20) = 7
lextSize(x) = rank(x) with respect to elements in subtree rooted at x
leftSize And Rank
20
10
6
2 8
15
40
30
25 35
7
18
0
(^0 )
1
4
0
0
7
(^0 )
1
3
sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
get(index) And remove(index)
(^720)
10
6
2 8
15
40
30
25 35
7
18
0
(^0 )
1
4
0
0
(^0 )
1
3
sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
get(index) And remove(index)
- if index = x.leftSize desired element is x.element
- if index < x.leftSize desired element is index’th element in left subtree of x
- if index > x.leftSize desired element is (index – x.leftSize – 1)’th element in right subtree of x
Experimental Results
- 40,000 of each operation.
- Java code on a 350MHz PC
Performance
Indexed AVL Tree (IAVL)
Operation FastArrayLinearList Chain IAVL get 5.6ms 157sec 63ms best-case puts 31.2ms 304ms 253ms average puts 5.8sec 115sec 392ms worst-case puts 11.8sec 157sec 544ms best-case removes 8.6ms 13.2ms 1.3sec average removes 5.8sec 149sec 1.5sec worst-case removes 11.7sec 157sec 1.6sec
Focus
- Tree structures for static and dynamic dictionaries.