chapter 11: tables and priority queues

15
Chapter 11: Tables and Priority Queues TABLE ADT TABLE IMPLEMENTATIONS CS 240 1 PRIORITY QUEUE ADT HEAPS HEAP APPLICATIONS

Upload: kiaria

Post on 23-Mar-2016

52 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 11: Tables and Priority Queues

Chapter 11: Tables and Priority Queues

TABLE ADT

TABLE IMPLEMENTATIONS

CS 240 1

PRIORITY QUEUE ADT

HEAPS

HEAP APPLICATIONS

Page 2: Chapter 11: Tables and Priority Queues

CS 240 2

Table Abstract Data Type

CC++JavaPerl

PythonRexxTcl

Language35344162562340

Runtime239233235768011198

Codesize1874518161528

Memory812103364

CodetimeAT&TAT&TSunopenCWIIBMSun

Developer

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.

Page 3: Chapter 11: Tables and Priority Queues

CS 240 3

Table: Array ImplementationImplementing the table as an array has several advantages:

Language: C Developer: AT&TRuntime: 3 Codesize: 239Memory: 1 Codetime: 8Language: C++ Developer : AT&TRuntime: 53 Codesize : 233Memory: 7 Codetime : 12Language: Java Developer : SunRuntime: 44 Codesize : 235Memory: 45 Codetime : 10Language: Perl Developer : openRuntime: 16 Codesize : 76Memory: 18 Codetime : 3Language: Python Developer : CWIRuntime: 25 Codesize : 80Memory: 16 Codetime : 3Language: Rexx Developer : IBMRuntime: 623 Codesize : 111Memory: 15 Codetime : 6Language: Tcl Developer : SunRuntime: 40 Codesize : 98Memory: 28 Codetime : 4

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.

Page 4: Chapter 11: Tables and Priority Queues

CS 240 4

Table: Linked List Implementation

Implementing the table as an linked list has several advantages:

Insertion is efficient if the list is unsorted – just insert at the head of the list.

Deletion is efficient – just relink around the node being deleted.

A linked list implementation of the table also has several disadvantages:× Sorting the list and

retrieving from the sorted list are difficult tasks (unless we use a skip list!).× There is extra overhead in maintaining the pointer fields of each node.

Lang: CDev: AT&T

RnTm: 3CdSz: 239

Mem: 1CdTm: 8

Lang: C++

Dev: AT&T

RnTm: 53CdSz: 233

Mem: 7 CdTm :

12

Lang: JavaDev: SunRnTm: 44CdSz: 235Mem: 45CdTm: 10

Lang: PerlDev: openRnTm: 16CdSz: 76Mem: 18CdTm: 3

Lang: Python

Dev: CWIRnTm: 25CdSz: 80Mem: 16CdTm: 3

Lang: Rexx

Dev: IBMRnTm:

623CdSz: 111Mem: 15CdTm: 6

Lang: TclDev: SunRnTm:

40CdSz: 98Mem: 28CdTm: 4

The dynamic allocation of memory ensures that the nodes being used are necessary and sufficient.

Page 5: Chapter 11: Tables and Priority Queues

CS 240 5

Table: Binary Search Tree Implementation

Language: CDeveloper:

AT&TRuntime: 3

Codesize: 239Memory: 1

Codetime: 8

Language: C++Developer:

AT&TRuntime: 53

Codesize: 233Memory: 7

Codetime: 12

Language: JavaDeveloper: Sun

Runtime: 44Codesize: 235Memory: 45

Codetime: 10

Language: PerlDeveloper:

openRuntime: 16Codesize: 76Memory: 18Codetime: 3

Language: Python

Developer: CWIRuntime: 25Codesize: 80Memory: 16Codetime: 3

Language: Rexx

Developer: IBMRuntime: 623Codesize: 111Memory: 15Codetime: 6

Language: TclDeveloper:

SunRuntime: 40Codesize: 98Memory: 28Codetime: 4

Implementing the table as a binary search tree has several advantages:

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.

The dynamic allocation of memory ensures that the nodes being used are necessary and sufficient.

Page 6: Chapter 11: Tables and Priority Queues

CS 240 6

Priority Queue Abstract Data TypeOften, 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.

Page 7: Chapter 11: Tables and Priority Queues

CS 240 7

Heap Implementation of Priority QueueA min-heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values.

10

17

25 31

45 61 58 47

13

15 30

23

Note: One convenient aspect of the heap is that it can be stored in an array. 1

017

13

25

31

15

30

45

61

58

47

23

• Offspring of node i: nodes 2i+1 and 2i+2

• Parent of node i: node (i-1)/2

Page 8: Chapter 11: Tables and Priority Queues

CS 240 8

Inserting Into A HeapWhen 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. 10

17

25 31

45 61 58 47

13

15 30

23

10

17

25 31

45 61 58 47

13

15 30

23 12

Insert 12

10

17

25 31

45 61 58 47

13

12 30

23 15

Percolate Up

10

17

25 31

45 61 58 47

12

13 30

23 15

Percolate Up

Page 9: Chapter 11: Tables and Priority Queues

CS 240 9

Removing From A HeapWhen 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.

29

43

65 58

75 87 80 91

51

63 77

73

Delete Min

Percolate Down

43

65 58

75 87 80 91

51

63 77

73

43

65 58

75 87 80 91

51

63 77

73

43

65 73

75 87 80 91

51

63 77

58Percolate

Down

Page 10: Chapter 11: Tables and Priority Queues

CS 240 10

Heap Application: Discrete Event SimulationRather 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.

TOKENRING#1

TOKENRING#2

ATM SWITCH

#1

ATM SWITCH

#2

FIBERBACKBONE

PC1A PC1B

PC1C

PC1D

PC1E

PC1F

PC1G

PC2A PC2B

PC2C

PC2D

PC2EPC2F

PC2G

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 Simulatio

n Example

Page 11: Chapter 11: Tables and Priority Queues

CS 240 11

Network Simulation Example (continued) 045: PC1B xmits on

TR1053: ATMS1 xmits on

FB072: PC1B xmits on

TR1068: PC2F xmits on TR2

049: ATMS2 recvs on TR2

080: PC1D xmits on TR1

059: PC2B recvs on TR2

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

059: PC2B recvs on TR2

080: PC1D xmits on TR1

Delete Minimum

Process Event

047: ATMS1 recvs on TR1

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

049: ATMS2 recvs on TR2

080: PC1D xmits on TR1

059: PC2B recvs on TR2

Delete Minimum

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

059: PC2B recvs on TR2

080: PC1D xmits on TR1

Process Event

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

050: ATMS1 xmits on FB

080: PC1D xmits on TR1

059: PC2B recvs on TR2

Delete Minimum...

Page 12: Chapter 11: Tables and Priority Queues

CS 240 12

Application: Heap Sort

// Percolate node i’s contents down// the size-n max-heap as needed.template <class Etype>void percDown(Etype A[], int i, int n){ int childIndex; 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;}

Take advantage of the max-heap property to sort an unordered list of values!

// 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 <class Etype> void heapSort(Etype A[], int n){ Etype temp; // 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); }}

Page 13: Chapter 11: Tables and Priority Queues

CS 240 13

Heap Sort ExampleOriginal List:

Frodo Samwise Gandalf Sauron Merry Pippin Aragorn Legolas Gollum Gimli Boromir Saruman ArwenAfter Converting Into Max-Heap:

Rest Of Heap Sort:Saruman Samwise Pippin Legolas Merry Gandalf Aragorn Frodo Gollum Gimli Boromir Arwen Sauron

Sauron Samwise Saruman Legolas Merry Pippin Aragorn Frodo Gollum Gimli Boromir Gandalf Arwen

Samwise Merry Pippin Legolas Gimli Gandalf Aragorn Frodo Gollum Arwen Boromir Saruman SauronPippin Merry Gandalf Legolas Gimli Boromir Aragorn Frodo Gollum Arwen Samwise Saruman SauronMerry Legolas Gandalf Gollum Gimli Boromir Aragorn Frodo Arwen Pippin Samwise Saruman SauronLegolas Gollum Gandalf Frodo Gimli Boromir Aragorn Arwen Merry Pippin Samwise Saruman Sauron Gollum Gimli Gandalf Frodo Arwen Boromir Aragorn Legolas Merry Pippin Samwise Saruman SauronGimli Frodo Gandalf Aragorn Arwen Boromir Gollum Legolas Merry Pippin Samwise Saruman SauronGandalf Frodo Boromir Aragorn Arwen Gimli Gollum Legolas Merry Pippin Samwise Saruman SauronFrodo Arwen Boromir Aragorn Gandalf Gimli Gollum Legolas Merry Pippin Samwise Saruman SauronBoromir Arwen Aragorn Frodo Gandalf Gimli Gollum Legolas Merry Pippin Samwise Saruman SauronArwen Aragorn Boromir Frodo Gandalf Gimli Gollum Legolas Merry Pippin Samwise Saruman Sauron Aragorn Arwen Boromir Frodo Gandalf Gimli Gollum Legolas Merry Pippin Samwise Saruman Sauron

Page 14: Chapter 11: Tables and Priority Queues

CS 240 14

Heap Sort Time Complexitytemplate <class Etype>void percDown(Etype A[], int i, int n){ int childIndex; 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;}

1 array access, 1 assignment:4 time units

At most log(n-i) iterations;

3 time units each: 1 assignment,

1 multiplication,1 comparison

2 array accesses (3 time units each),

5 multiplications,2 additions,

2 comparisons,1 boolean operation,

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)

Page 15: Chapter 11: Tables and Priority Queues

CS 240 15

Heap Sort Time Complexity (continued)template <class Etype> void heapSort(Etype A[], int n){ 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 / 2) iterations;2 time units each:

1 division or subtraction,1 comparison

(+ 1 assignment in the first iteration)

At most 7 + 30log(n-j) time units

13 time units:4 array accesses,

1 additional assignment

At most 7 + 30log(i-2) time units

(n - 1) iterations;

2 time units each:

1 comparison,1 subtraction

or assignment

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) + 30j=1,n/2log(n-j)) + i=2,n(22) + 30i=2,nlog(i-2)) =1 + 9(n/2) + 30j=1,n/2log(n-j)) + 22(n-1) + 30i=2,nlog(i-2))

1 + 4.5n + 30(n/2)logn + 22n -22 +30nlogn = 15nlogn + 26.5n – 21, which is O(nlogn).