Download presentation
Presentation is loading. Please wait.
Published byMadeleine Walters Modified over 7 years ago
1
Dictionaries Collection of items. Each item is a pair. (key, element)
Pairs have different keys. The word item is used as a synonym for element to avoid the confusion that would otherwise result by saying that a dictionary is a collection of elements and each element is a pair (key, element). A linear list is an ordered sequence of elements. A dictionary is just a collection of elements/items. C++ STL: Map and Multimap
2
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)
3
Dictionary With Duplicates
Keys are not required to be distinct. Word dictionary. Items/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. C++ STL Map and Multimap Can covert a dictionary with duplicates into one with no duplicates by associating with each key a list of elements/values.
4
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. put(theKey, theElement) (a.k.a. insert) remove(theKey) (a.k.a. delete)
5
Hash Table Dictionaries
O(1) expected time for get, put, and remove. O(n) worst-case time for get, put, and remove. O(log n) if overflows handled by balanced search trees. Not suitable for nearest match queries. Get element with smallest key >= theKey. Not suitable for range queries. Not suitable for indexed operations. Get element with third smallest key. Remove element with 5th smallest key. Hash table with balanced search trees for overflow lists. Useful to provide better expected performance in applications where you must guarantee log n worst-case performance. Range search.
6
Bin Packing n items to be packed into bins each item has a size
each bin has a capacity of c minimize number of bins Problem first studied in the context of tournament trees. Winner trees used to implement first-fit packing.
7
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.
8
Best Fit Example n = 4 weights = [4, 7, 3, 6] capacity = 10
Pack red item into first bin.
9
Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10
Pack blue item next. Doesn’t fit, so start a new bin.
10
Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10
11
Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10
Pack yellow item into second bin.
12
Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10
Pack green item into first bin.
13
Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Optimal packing.
14
Implementation Of Best Fit
Use a dynamic dictionary (with duplicates) in which the items 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.
15
Best Fit Example 12 active bins. Pack item whose size is 22. 20 10 6 2
8 15 40 30 25 35 7 18 12 active bins. Pack item whose size is 22.
16
Complexity Of Best Fit Use a balanced binary search tree (with duplicates) 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). Could also use balanced binary search tree with no duplicates and pairs of the form (available capacity, list of bin indexes)
17
Indexed Binary Search Tree
Each node has an additional field. leftSize = number of nodes in its left subtree
18
Example Indexed Binary Search Tree
7 20 4 3 10 40 1 1 6 15 30 1 18 25 35 2 8 7 leftSize values are in red
19
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 In inorder we do left subtree, root, right subtree. So, rank = #nodes in left subtree = leftSize.
20
leftSize And Rank sorted list = [2,6,7,8,10,15,18,20,25,30,35,40] 7 4
1 6 15 30 1 18 25 35 2 8 7 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
21
get(index) And remove(index)
7 20 10 6 2 8 15 40 30 25 35 18 1 4 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
22
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
23
Linear List As Indexed Binary Tree
h e b a d f l j i k c g 1 4 7 3 list = [a,b,c,d,e,f,g,h,i,j,k,l] List elements are placed in a binary tree so that an inorder traversal of the binary tree visits the elements in list order from left to right. get() and remove() work as they do in an indexed binary search tree.
24
Indexed AVL Tree (IAVL)
Performance Linear List. get(index) put(index, element) remove(index) Array. O(1), O(n), O(n). Chain. O(n), O(n), O(n). Indexed AVL Tree (IAVL) O(log n), O(log n), O(log n).
25
40,000 of each operation. Java code on a 350MHz PC
Experimental Results 40,000 of each operation. Java code on a 350MHz PC Start with an empty list and either do 40,000 best-case inserts or 40,000 inserts at random positions (average-case measurements). For remove, either do 40,000 worst-case removes starting from a list with 40,000 elements or do remove from randomly generated positions. For search, get each element once. Reported time is total time for 40,000 ops.
26
Performance Time for 40,000 operations Indexed AVL Tree (IAVL)
Operation Array Chain IAVL get ms sec ms average puts sec sec ms worst-case puts sec sec ms average removes sec sec sec worst-case removes sec sec sec Average and worst-case adds/removes are much faster when an IAVL tree, a linked data structure is used. A sequence of 40K inserts, 40K puts, 40K removes (all average case) takes 11.6 sec using arrays, 421 sec using chains and 1.9 sec using IAVL. True worst-case times for a chain could be somewhat higher as in the tests, nodes were not randomized and so 2 adjacent nodes may lie in the same cache line. For IAVL. Two adjacent nodes on a root to leaf path are unlikely to lie in the same cache line. Average-case inserts randomize memory used along the chain while worst-case inserts result in adjacent nodes being adjacent in memory. Hence, average inserts time was not half worst-case time. Worst-case removes experiment started with worst-case inserts chain. So, this test had a better (memory adjacent) chain to work with than the average removes test that started with the average inserts chain. Time for 40,000 operations
27
Focus Tree structures for static and dynamic dictionaries.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.