1 / 15

Chapter 11: Tables and Priority Queues

Chapter 11: Tables and Priority Queues. Table ADT. Table Implementations. Priority Queue ADT. Heaps. Heap Applications. CS 240. 222. Language. Developer. Runtime. Codesize. Memory. Codetime. C. AT&T. 3. 239. 18. 8. C++. AT&T. 53. 233. 7. 12. Java. Sun. 44. 235. 45.

kiaria
Download Presentation

Chapter 11: Tables and Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11: Tables and Priority Queues Table ADT Table Implementations Priority Queue ADT Heaps Heap Applications CS 240 222

  2. Language Developer Runtime Codesize Memory Codetime C AT&T 3 239 18 8 C++ AT&T 53 233 7 12 Java Sun 44 235 45 10 Perl open 16 76 18 3 Python CWI 25 80 16 3 Rexx IBM 623 111 15 6 Tcl Sun 40 98 28 4 Table Abstract Data Type The concept of a table (or dictionary) abstract data type is intended to facilitate looking up complex records by using one of the data fields as a special search key. For example, the table of programming languages above could be searched by the name of the language, the amount of memory required for a certain test program, the name of the developer, or any of its other fields. CS 240 223

  3. Language: C Developer: AT&T Runtime: 3 Codesize: 239 Memory: 1 Codetime: 8 Language: C++ Developer : AT&T Runtime: 53 Codesize : 233 Memory: 7 Codetime : 12 Language: Java Developer : Sun Runtime: 44 Codesize : 235 Memory: 45 Codetime : 10 Language: Perl Developer : open Runtime: 16 Codesize : 76 Memory: 18 Codetime : 3 Language: Python Developer : CWI Runtime: 25 Codesize : 80 Memory: 16 Codetime : 3 Language: Rexx Developer : IBM Runtime: 623 Codesize : 111 Memory: 15 Codetime : 6 Language: Tcl Developer : Sun Runtime: 40 Codesize : 98 Memory: 28 Codetime : 4 Table: Array Implementation Implementing the table as an array has several advantages: • Insertion is efficient if the list is unsorted – just insert at the end of the list. • If the list is kept sorted by the search key, then a binary search is made possible. An array implementation also has several disadvantages: • Memory must be allocated in advance, so some might be wasted or an adequate amount might not be reserved. • Deletion from the list requires inefficient gap-filling steps. CS 240 224

  4. Lang: C Dev: AT&T RnTm: 3 CdSz: 239 Mem: 1 CdTm: 8 Lang: C++ Dev: AT&T RnTm: 53 CdSz: 233 Mem: 7 CdTm : 12 Lang: Java Dev: Sun RnTm: 44 CdSz: 235 Mem: 45 CdTm: 10 Lang: Perl Dev: open RnTm: 16 CdSz: 76 Mem: 18 CdTm: 3 Lang: Python Dev: CWI RnTm: 25 CdSz: 80 Mem: 16 CdTm: 3 Lang: Rexx Dev: IBM RnTm: 623 CdSz: 111 Mem: 15 CdTm: 6 Lang: Tcl Dev: Sun RnTm: 40 CdSz: 98 Mem: 28 CdTm: 4 Table: Linked List Implementation Implementing the table as an linked list has several advantages: A linked list implementation of the table also has several disadvantages: • The dynamic allocation of memory ensures that the nodes being used are necessary and sufficient. • Sorting the list and retrieving from the sorted list are difficult tasks (unless we use a skip list!). • Insertion is efficient if the list is unsorted – just insert at the head of the list. • There is extra overhead in maintaining the pointer fields of each node. • Deletion is efficient – just relink around the node being deleted. CS 240 225

  5. Language: Perl Developer: open Runtime: 16 Codesize: 76 Memory: 18 Codetime: 3 Language: C++ Developer: AT&T Runtime: 53 Codesize: 233 Memory: 7 Codetime: 12 Language: Rexx Developer: IBM Runtime: 623 Codesize: 111 Memory: 15 Codetime: 6 Language: C Developer: AT&T Runtime: 3 Codesize: 239 Memory: 1 Codetime: 8 Language: Java Developer: Sun Runtime: 44 Codesize: 235 Memory: 45 Codetime: 10 Language: Python Developer: CWI Runtime: 25 Codesize: 80 Memory: 16 Codetime: 3 Language: Tcl Developer: Sun Runtime: 40 Codesize: 98 Memory: 28 Codetime: 4 Table: Binary Search Tree Implementation Implementing the table as a binary search tree has several advantages: • The dynamic allocation of memory ensures that the nodes being used are necessary and sufficient. • If the search key of the table needs to be sorted, then searching, removing, and inserting can all have O(logn) time complexity (if the tree is kept balanced!). A binary search tree implementation of the table also has several disadvantages: • Ensuring that the binary tree is balanced is difficult (unless we use an AVL, 2-3, or red-black tree – see Carrano’s Chapter12). • There is extra overhead in maintaining the pointer fields of each node. CS 240 226

  6. Priority Queue Abstract Data Type Often, a FIFO queue structure has a need for a prioritization capability, so elements with greater priority are removed before lower-priority elements that were actually inserted first. Examples: • Short print jobs may be prioritized over longer print jobs on a printer queue. • Real-time network applications (e.g., video, audio) may be prioritized over e-mail and simple file transfers on a network switch’s forwarding queue. • System maintenance tasks (e.g., memory defragmentation, mouse interrupts) may be prioritized over application software tasks on an operating system’s job queue. CS 240 227

  7. 10 17 13 25 31 15 30 45 61 58 47 23 10 17 13 25 31 15 30 45 61 58 47 23 Heap Implementation of Priority Queue A min-heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values. Note: One convenient aspect of the heap is that it can be stored in an array. • Offspring of node i: nodes 2i+1 and 2i+2 • Parent of node i: node (i-1)/2 CS 240 228

  8. 10 10 17 13 17 13 Insert 12 15 30 25 31 15 30 25 31 47 23 12 45 61 58 47 23 45 61 58 10 10 17 13 17 12 Percolate Up Percolate Up 12 30 13 30 25 31 25 31 47 23 15 47 23 15 45 61 58 45 61 58 Inserting Into A Heap When inserting a new element into a heap, start at the new leaf node that would maintain the binary tree’s completeness, and “percolate” the new element up to its appropriate position to maintain the heap order property. CS 240 229

  9. 29 43 51 Delete Min 63 77 43 51 65 58 91 73 63 77 75 87 80 65 58 91 73 75 87 80 43 43 58 51 Percolate Down Percolate Down 63 77 65 73 51 91 75 87 80 63 77 65 58 91 73 75 87 80 Removing From A Heap When deleting the minimum element from a heap, create a “hole” node at the root (where the minimum element was), and slide the smaller of the hole’s offspring up until an appropriate slot is found for the last heap element. CS 240 230

  10. PC2A PC2B ATM SWITCH #2 PC1A PC1B PC2C TOKEN RING #2 PC1C PC2D PC1G PC2G TOKEN RING #1 FIBER BACKBONE PC1F ATM SWITCH #1 PC2E PC2F PC1D PC1E Heap Application: Discrete Event Simulation Rather than implementing a complicated (expensive) system, it is sometimes possible to simulate the system using a statistical model, and to work out the obvious bugs prior to actual implementation. A heap conveniently structures such simulations. The nodes represent the discrete “events” of the system, ordered according to the time at which they “occur”. Network Simulation Example CS 240 231

  11. Delete Minimum 045: PC1B xmits on TR1 053: ATMS1 xmits on FB 049: ATMS2 recvs on TR2 Process Event 072: PC1B xmits on TR1 068: PC2F xmits on TR2 080: PC1D xmits on TR1 059: PC2B recvs on TR2 049: ATMS2 recvs on TR2 049: ATMS2 recvs on TR2 Delete Minimum 053: ATMS1 xmits on FB 053: ATMS1 xmits on FB 059: PC2B recvs on TR2 059: PC2B recvs on TR2 072: PC1B xmits on TR1 072: PC1B xmits on TR1 068: PC2F xmits on TR2 068: PC2F xmits on TR2 080: PC1D xmits on TR1 080: PC1D xmits on TR1 049: ATMS2 recvs on TR2 047: ATMS1 recvs on TR1 053: ATMS1 xmits on FB 053: ATMS1 xmits on FB 049: ATMS2 recvs on TR2 050: ATMS1 xmits on FB Process Event 072: PC1B xmits on TR1 072: PC1B xmits on TR1 068: PC2F xmits on TR2 068: PC2F xmits on TR2 080: PC1D xmits on TR1 080: PC1D xmits on TR1 059: PC2B recvs on TR2 059: PC2B recvs on TR2 Delete Minimum... Network Simulation Example (continued) CS 240 232

  12. Application: Heap Sort // Sort the size-n list (indexed // 1 through n) by converting it // into a max-heap swapping the // largest elements to put them // in their proper positions, and // then percolating as needed. template <classEtype> void heapSort(Etype A[], intn) { Etypetemp; // Set list up as a max-heap for (int j = n/2; j > 0; j--) percDown(A, j, n); for (int i = n; i >= 2; i--) { // Swap the first element // with the i-th element and // percolate! temp = A[1]; A[1] = A[i]; A[i] = temp; percDown(A, 1, i-1); } } Take advantage of the max-heap property to sort an unordered list of values! // Percolate node i’s contents down // the size-n max-heap as needed. template <classEtype> voidpercDown(Etype A[], inti, intn) { intchildIndex; Etype temp = A[i]; for ( ; 2*i <= n; i = childIndex) { childIndex = ((2*i != n) && (A[2*i+1] > A[2*i])) ? (2*i+1) : (2*i); if (temp < A[childIndex]) A[i] = A[childIndex]; else break; } A[i] = temp; } CS 240 233

  13. Heap Sort Example Original List: Frodo Samwise Gandalf Sauron Merry Pippin Aragorn Legolas Gollum GimliBoromirSarumanArwen After Converting Into Max-Heap: SauronSamwiseSarumanLegolas Merry Pippin Aragorn Frodo Gollum GimliBoromir Gandalf Arwen Rest Of Heap Sort: SarumanSamwise Pippin Legolas Merry Gandalf Aragorn Frodo Gollum GimliBoromirArwenSauron Samwise Merry Pippin LegolasGimli Gandalf Aragorn Frodo Gollum ArwenBoromirSarumanSauron Pippin Merry Gandalf LegolasGimliBoromir Aragorn Frodo Gollum ArwenSamwiseSarumanSauron Merry Legolas Gandalf Gollum GimliBoromir Aragorn Frodo ArwenPippin SamwiseSarumanSauron Legolas Gollum Gandalf Frodo GimliBoromir Aragorn ArwenMerry Pippin SamwiseSarumanSauron Gollum Gimli Gandalf Frodo ArwenBoromir Aragorn Legolas Merry Pippin SamwiseSarumanSauron Gimli Frodo Gandalf Aragorn ArwenBoromirGollum Legolas Merry Pippin SamwiseSarumanSauron Gandalf Frodo Boromir Aragorn ArwenGimli Gollum Legolas Merry Pippin SamwiseSarumanSauron Frodo ArwenBoromir Aragorn Gandalf Gimli Gollum Legolas Merry Pippin SamwiseSarumanSauron BoromirArwen Aragorn Frodo Gandalf Gimli Gollum Legolas Merry Pippin SamwiseSarumanSauron Arwen Aragorn Boromir Frodo Gandalf Gimli Gollum Legolas Merry Pippin SamwiseSarumanSauron Aragorn ArwenBoromir Frodo Gandalf Gimli Gollum Legolas Merry Pippin SamwiseSarumanSauron CS 240 234

  14. Heap Sort Time Complexity template <class Etype> void percDown(Etype A[], inti, intn) { intchildIndex; Etype temp = A[i]; for ( ; 2*i <= n; i = childIndex) { childIndex = ((2*i != n) && (A[2*i+1] > A[2*i])) ? (2*i+1) : (2*i); if (temp < A[childIndex]) A[i] = A[childIndex]; else break; } A[i] = temp; } At most log(n-i) iterations; 3 time units each: 1 assignment, 1 multiplication, 1 comparison 1 array access, 1 assignment: 4 time units 2 array accesses (3 time units each), 5 multiplications, 2 additions, 2 comparisons, 1 booleanoperation, 1 assignment: At most 17 time units 3 array accesses, 1 comparison: At most 10 time units 1 array access (including the assignment): 3 time units Total time units for percDown: At most 7 + 30log(n-i) CS 240 235

  15. Heap Sort Time Complexity (continued) (n / 2) iterations; 2 time units each: 1 division or subtraction, 1 comparison (+ 1 assignment in the first iteration) template <classEtype> void heapSort(Etype A[], intn) { Etype temp; for (int j = n/2; j > 0; j--) percDown(A, j, n); for (int i = n; i >= 2; i--) { temp = A[1]; A[1] = A[i]; A[i] = temp; percDown(A, 1, i-1); } } (n - 1) iterations; 2 time units each: 1 comparison, 1 subtraction or assignment At most 7 + 30log(n-j) time units 13 time units: 4array accesses, 1 additional assignment At most 7 + 30log(i-2) time units Total time units for heapSort: At most 1+j=1,n/2(2+7+30log(n-j))+i=2,n(2+13+7+30log(i-2)) = 1 + j=1,n/2(9) + 30j=1,n/2log(n-j)) + i=2,n(22) + 30i=2,nlog(i-2)) = 1 + 9(n/2) + 30j=1,n/2log(n-j)) + 22(n-1) + 30i=2,nlog(i-2))  1 + 4.5n + 30(n/2)logn + 22n -22 +30nlogn = 15nlogn + 26.5n – 21, which is O(nlogn). CS 240 236

More Related