基本數(shù)據(jù)結(jié)構(gòu)PriorityQueuesHeapsDictionariesHashTables課件_第1頁
基本數(shù)據(jù)結(jié)構(gòu)PriorityQueuesHeapsDictionariesHashTables課件_第2頁
基本數(shù)據(jù)結(jié)構(gòu)PriorityQueuesHeapsDictionariesHashTables課件_第3頁
基本數(shù)據(jù)結(jié)構(gòu)PriorityQueuesHeapsDictionariesHashTables課件_第4頁
基本數(shù)據(jù)結(jié)構(gòu)PriorityQueuesHeapsDictionariesHashTables課件_第5頁
已閱讀5頁,還剩39頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、8/20/2022 5:26 PMPriority Queues1Priority Queue ADTA priority queue stores a collection of itemsAn item is a pair(key, element)Main methods of the Priority Queue ADTinsertItem(k, o)inserts an item with key k and element oremoveMin()removes the item with smallest key and returns its elementAdditional

2、 methodsminKey()returns, but does not remove, the smallest key of an itemminElement()returns, but does not remove, the element of an item with smallest keysize(), isEmpty()Applications:Standby flyersAuctionsStock market8/20/2022 5:26 PMPriority Queues2Total Order RelationKeys in a priority queue can

3、 be arbitrary objects on which an order is definedTwo distinct items in a priority queue can have the same keyMathematical concept of total order relation Reflexive property:x xAntisymmetric property:x y y x x = yTransitive property: x y y z x z8/20/2022 5:26 PMPriority Queues3Comparator ADTA compar

4、ator encapsulates the action of comparing two objects according to a given total order relationA generic priority queue uses an auxiliary comparatorThe comparator is external to the keys being comparedWhen the priority queue needs to compare two keys, it uses its comparatorMethods of the Comparator

5、ADT, all with Boolean return typeisLessThan(x, y)isLessThanOrEqualTo(x,y)isEqualTo(x,y)isGreaterThan(x, y)isGreaterThanOrEqualTo(x,y)isComparable(x)8/20/2022 5:26 PMPriority Queues4Sorting with a Priority QueueWe can use a priority queue to sort a set of comparable elementsInsert the elements one by

6、 one with a series of insertItem(e, e) operationsRemove the elements in sorted order with a series of removeMin() operationsThe running time of this sorting method depends on the priority queue implementationAlgorithm PQ-Sort(S, C)Input sequence S, comparator C for the elements of SOutput sequence S

7、 sorted in increasing order according to CP priority queue with comparator Cwhile S.isEmpty ()e S.remove (S. first ()P.insertItem(e, e)while P.isEmpty()e P.removeMin()S.insertLast(e)8/20/2022 5:26 PMPriority Queues5Sequence-based Priority QueueImplementation with an unsorted sequenceStore the items

8、of the priority queue in a list-based sequence, in arbitrary orderPerformance:insertItem takes O(1) time since we can insert the item at the beginning or end of the sequenceremoveMin, minKey and minElement take O(n) time since we have to traverse the entire sequence to find the smallest key Implemen

9、tation with a sorted sequenceStore the items of the priority queue in a sequence, sorted by keyPerformance:insertItem takes O(n) time since we have to find the place where to insert the itemremoveMin, minKey and minElement take O(1) time since the smallest key is at the beginning of the sequence8/20

10、/2022 5:26 PMPriority Queues6Selection-SortSelection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequenceRunning time of Selection-sort:Inserting the elements into the priority queue with n insertItem operations takes O(n) timeRemoving the elements in so

11、rted order from the priority queue with n removeMin operations takes time proportional to 1 + 2 + + nSelection-sort runs in O(n2) time 8/20/2022 5:26 PMPriority Queues7Insertion-SortInsertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequenceRunning time

12、of Insertion-sort:Inserting the elements into the priority queue with n insertItem operations takes time proportional to 1 + 2 + + nRemoving the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) timeInsertion-sort runs in O(n2) time 8/20/2022 5:26 PM

13、Priority Queues8In-place Insertion-sortInstead of using an external data structure, we can implement selection-sort and insertion-sort in-placeA portion of the input sequence itself serves as the priority queueFor in-place insertion-sortWe keep sorted the initial portion of the sequenceWe can use sw

14、apElements instead of modifying the sequence542315423145231245312345112345123458/20/2022 5:26 PMPriority Queues9What is a heap (2.4.3)A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:Heap-Order: for every internal node v other than the root,key(v) ke

15、y(parent(v)Complete Binary Tree: let h be the height of the heapfor i = 0, , h - 1, there are 2i nodes of depth iat depth h - 1, the internal nodes are to the left of the external nodes26579The last node of a heap is the rightmost internal node of depth h - 1last node8/20/2022 5:26 PMPriority Queues

16、10Height of a Heap (2.4.3)Theorem: A heap storing n keys has height O(log n)Proof: (we apply the complete binary tree property)Let h be the height of a heap storing n keysSince there are 2i keys at depth i = 0, , h - 2 and at least one key at depth h - 1, we have n 1 + 2 + 4 + + 2h-2 + 1 Thus, n 2h-

17、1 , i.e., h log n + 1122h-21keys01h-2h-1depth8/20/2022 5:26 PMPriority Queues11Heaps and Priority QueuesWe can use a heap to implement a priority queueWe store a (key, element) item at each internal nodeWe keep track of the position of the last nodeFor simplicity, we show only the keys in the pictur

18、es(2, Sue)(6, Mark)(5, Pat)(9, Jeff)(7, Anna)8/20/2022 5:26 PMPriority Queues12Insertion into a Heap (2.4.3)Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heapThe insertion algorithm consists of three stepsFind the insertion node z (the new last node)Store

19、 k at z and expand z into an internal nodeRestore the heap-order property (discussed next)26579insertion node265791zz8/20/2022 5:26 PMPriority Queues13UpheapAfter the insertion of a new key k, the heap-order property may be violatedAlgorithm upheap restores the heap-order property by swapping k alon

20、g an upward path from the insertion nodeUpheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time215796z125796z8/20/2022 5:26 PMPriority Queues14Removal from a Heap (2.4.3)Method remove

21、Min of the priority queue ADT corresponds to the removal of the root key from the heapThe removal algorithm consists of three stepsReplace the root key with the key of the last node wCompress w and its children into a leafRestore the heap-order property (discussed next)26579last nodew7659w8/20/2022

22、5:26 PMPriority Queues15DownheapAfter replacing the root key with the key k of the last node, the heap-order property may be violatedAlgorithm downheap restores the heap-order property by swapping key k along a downward path from the rootUpheap terminates when key k reaches a leaf or a node whose ch

23、ildren have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time7659w5679w8/20/2022 5:26 PMPriority Queues16Updating the Last NodeThe insertion node can be found by traversing a path of O(log n) nodesWhile the current node is a right child, go to the paren

24、t nodeIf the current node is a left child, go to the right childWhile the current node is internal, go to the left childSimilar algorithm for updating the last node after a removal8/20/2022 5:26 PMPriority Queues17Heap-Sort (2.4.4)Consider a priority queue with n items implemented by means of a heap

25、the space used is O(n)methods insertItem and removeMin take O(log n) timemethods size, isEmpty, minKey, and minElement take time O(1) timeUsing a heap-based priority queue, we can sort a sequence of n elements in O(n log n) timeThe resulting algorithm is called heap-sortHeap-sort is much faster than

26、 quadratic sorting algorithms, such as insertion-sort and selection-sort8/20/2022 5:26 PMPriority Queues18Vector-based Heap Implementation (2.4.3)We can represent a heap with n keys by means of a vector of length n + 1For the node at rank ithe left child is at rank 2ithe right child is at rank 2i +

27、1Links between nodes are not explicitly storedThe leaves are not representedThe cell at rank 0 is not usedOperation insertItem corresponds to inserting at rank n + 1Operation removeMin corresponds to removing at rank nYields in-place heap-sort26579256971234508/20/2022 5:26 PMPriority Queues19Merging

28、 Two HeapsWe are given two two heaps and a key kWe create a new heap with the root node storing k and with the two heaps as subtreesWe perform downheap to restore the heap-order property 735826435826423584678/20/2022 5:26 PMPriority Queues20We can construct a heap storing n given keys in using a bot

29、tom-up construction with log n phasesIn phase i, pairs of heaps with 2i -1 keys are merged into heaps with 2i+1-1 keysBottom-up Heap Construction2i -12i -12i+1-18/20/2022 5:26 PMPriority Queues21Example1516124962023251516512411962720238/20/2022 5:26 PMPriority Queues22Example (contd.)251516512411962

30、72023152516412569112320278/20/2022 5:26 PMPriority Queues23Example (contd.)715251641258691123202741525165127689112320278/20/2022 5:26 PMPriority Queues24Example (end)4152516512710689112320275152516712104689112320278/20/2022 5:26 PMPriority Queues25AnalysisWe visualize the worst-case time of a downhe

31、ap with a proxy path that goes first right and then repeatedly goes left until the bottom of the heap (this path may differ from the actual downheap path)Since each node is traversed by at most two proxy paths, the total number of nodes of the proxy paths is O(n) Thus, bottom-up heap construction ru

32、ns in O(n) time Bottom-up heap construction is faster than n successive insertions and speeds up the first phase of heap-sort8/20/2022 5:26 PMPriority Queues26Dictionaries and Hash Tables01234451-229-0004981-101-0002025-612-00018/20/2022 5:26 PMPriority Queues27Dictionary ADT (2.5.1)The dictionary A

33、DT models a searchable collection of key-element itemsThe main operations of a dictionary are searching, inserting, and deleting itemsMultiple items with the same key are allowedApplications:address bookcredit card authorizationmapping host names (e.g., ) to internet addresses (e.g., 128.148.34.101)

34、Dictionary ADT methods:findElement(k): if the dictionary has an item with key k, returns its element, else, returns the special element NO_SUCH_KEY insertItem(k, o): inserts item (k, o) into the dictionaryremoveElement(k): if the dictionary has an item with key k, removes it from the dictionary and

35、returns its element, else returns the special element NO_SUCH_KEY size(), isEmpty()keys(), elements()8/20/2022 5:26 PMPriority Queues28Log File (2.5.1)A log file is a dictionary implemented by means of an unsorted sequenceWe store the items of the dictionary in a sequence (based on a doubly-linked l

36、ists or a circular array), in arbitrary orderPerformance:insertItem takes O(1) time since we can insert the new item at the beginning or at the end of the sequencefindElement and removeElement take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for

37、an item with the given keyThe log file is effective only for dictionaries of small size or for dictionaries on which insertions are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation)8/20/2022 5:26 PMPriority Queues29Hash

38、Functions and Hash Tables (2.5.2) A hash function h maps keys of a given type to integers in a fixed interval 0, N - 1Example:h(x) = x mod Nis a hash function for integer keysThe integer h(x) is called the hash value of key xA hash table for a given key type consists ofHash function hArray (called t

39、able) of size NWhen implementing a dictionary with a hash table, the goal is to store item (k, o) at index i = h(k)8/20/2022 5:26 PMPriority Queues30ExampleWe design a hash table for a dictionary storing items (SSN, Name), where SSN (social security number) is a nine-digit positive integerOur hash t

40、able uses an array of size N = 10,000 and the hash functionh(x) = last four digits of x01234999799989999451-229-0004981-101-0002200-751-9998025-612-00018/20/2022 5:26 PMPriority Queues31Hash Functions ( 2.5.3)A hash function is usually specified as the composition of two functions:Hash code map: h1:

41、 keys integersCompression map: h2: integers 0, N - 1The hash code map is applied first, and the compression map is applied next on the result, i.e., h(x) = h2(h1(x)The goal of the hash function is to “disperse” the keys in an apparently random way8/20/2022 5:26 PMPriority Queues32Hash Code Maps (2.5

42、.3)Memory address:We reinterpret the memory address of the key object as an integer (default hash code of all Java objects)Good in general, except for numeric and string keysInteger cast:We reinterpret the bits of the key as an integerSuitable for keys of length less than or equal to the number of b

43、its of the integer type (e.g., byte, short, int and float in Java)Component sum:We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows)Suitable for numeric keys of fixed length greater than or equal to the number of bits o

44、f the integer type (e.g., long and double in Java)8/20/2022 5:26 PMPriority Queues33Hash Code Maps (cont.)Polynomial accumulation:We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) a0 a1 an-1We evaluate the polynomialp(z) = a0 + a1 z + a2 z2 + + a

45、n-1zn-1at a fixed value z, ignoring overflowsEspecially suitable for strings (e.g., the choice z = 33 gives at most 6 collisions on a set of 50,000 English words)Polynomial p(z) can be evaluated in O(n) time using Horners rule:The following polynomials are successively computed, each from the previo

46、us one in O(1) timep0(z) = an-1pi (z) = an-i-1 + zpi-1(z) (i = 1, 2, , n -1)We have p(z) = pn-1(z) 8/20/2022 5:26 PMPriority Queues34Compression Maps (2.5.4)Division:h2 (y) = y mod NThe size N of the hash table is usually chosen to be a prime The reason has to do with number theory and is beyond the

47、 scope of this courseMultiply, Add and Divide (MAD):h2 (y) = (ay + b) mod Na and b are nonnegative integers such that a mod N 0Otherwise, every integer would map to the same value b 8/20/2022 5:26 PMPriority Queues35Collision Handling ( 2.5.5)Collisions occur when different elements are mapped to th

48、e same cellChaining: let each cell in the table point to a linked list of elements that map thereChaining is simple, but requires additional memory outside the table01234451-229-0004981-101-0004025-612-00018/20/2022 5:26 PMPriority Queues36Linear Probing (2.5.5)Open addressing: the colliding item is

49、 placed in a different cell of the tableLinear probing handles collisions by placing the colliding item in the next (circularly) available table cellEach table cell inspected is referred to as a “probe”Colliding items lump together, causing future collisions to cause a longer sequence of probesExamp

50、le:h(x) = x mod 13Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 0123456789101112 41 18445932223173 01234567891011128/20/2022 5:26 PMPriority Queues37Search with Linear ProbingConsider a hash table A that uses linear probingfindElement(k)We start at cell h(k) We probe consecutive location

51、s until one of the following occursAn item with key k is found, orAn empty cell is found, orN cells have been unsuccessfully probed Algorithm findElement(k)i h(k)p 0repeatc Aiif c = return NO_SUCH_KEY else if c.key () = kreturn c.element()elsei (i + 1) mod Np p + 1until p = Nreturn NO_SUCH_KEY8/20/2

52、022 5:26 PMPriority Queues38Updates with Linear ProbingTo handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elementsremoveElement(k)We search for an item with key k If such an item (k, o) is found, we replace it with the special item AVAILABLE a

53、nd we return element oElse, we return NO_SUCH_KEYinsert Item(k, o)We throw an exception if the table is fullWe start at cell h(k) We probe consecutive cells until one of the following occursA cell i is found that is either empty or stores AVAILABLE, orN cells have been unsuccessfully probedWe store

54、item (k, o) in cell i8/20/2022 5:26 PMPriority Queues39Double HashingDouble hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series(i + jd(k) mod N for j = 0, 1, , N - 1The secondary hash function d(k) cannot have zero valuesThe

55、 table size N must be a prime to allow probing of all the cellsCommon choice of compression map for the secondary hash function: d2(k) = q - k mod qwhereq Nq is a primeThe possible values for d2(k) are 1, 2, , q8/20/2022 5:26 PMPriority Queues40Consider a hash table storing integer keys that handles collision with double hashingN = 13 h(k) = k mod 13 d(k) = 7 - k mod 7 Insert keys 18, 41

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論