1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a...

19
1 6.3 Binary Heap - Other Heap Operations • There is no way to find any particular key without a linear scan through the entire heap. • However, if we know the position, we can access the key immediately.

Transcript of 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a...

Page 1: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

1

6.3 Binary Heap - Other Heap Operations

• There is no way to find any particular key without a linear scan through the entire heap.

• However, if we know the position, we can access the key immediately.

Page 2: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

2

6.3 Binary Heap - Other Heap Operations

DecreaseKey (P, , H)

• Lower the key value at position P by .

• Fix the heap order by percolating up.

• Advance the priority of a job.

Delete (P, H)

• Remove the node at position P.

• DecreaseKey (P, , H) and DeleteMin (H)

Page 3: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

3

6.3 Binary Heap - Other Heap Operations

BuildHeap (H)

• N successive appends at the end of the array, each takes O (1). The tree is unordered.

• PercolateDown (i), for i = N/2 to 1.

Page 4: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

4

6.3 Binary Heap - Other Heap Operations

• Initial heap

150

80 40

30 10 70 110

100 20 90 60 50 120 140 130

Page 5: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

5

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (7)

150

80 40

30 10 70 110

100 20 90 60 50 120 140 130

Page 6: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

6

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (6)

150

80 40

30 10 50 110

100 20 90 60 70 120 140 130

Page 7: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

7

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (5)

150

80 40

30 10 50 110

100 20 90 60 70 120 140 130

Page 8: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

8

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (4)

150

80 40

20 10 50 110

100 30 90 60 70 120 140 130

Page 9: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

9

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (3)

150

80 40

20 10 50 110

100 30 90 60 70 120 140 130

Page 10: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

10

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (2)

150

10 40

20 60 50 110

100 30 90 80 70 120 140 130

Page 11: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

11

6.3 Binary Heap - Other Heap Operations

• After PercolateDown (1)

10

20 40

30 60 50 110

100 150 90 80 70 120 140 130

Page 12: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

12

6.3 Binary Heap - Other Heap Operations

• Bound of the running time is the sum of the heights of all the nodes in the heap.

• For a perfect binary tree of height h, the sum of the heights of the nodes is 2h+1 - 1 - (h+1), which is approximately N.

Page 13: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

13

6.4 Applications of Priority Queues

Find the kth smallest elements

• It requires k DeleteMin operations.

• O (log N) to create the heap.

• O (log N) for each DeleteMin.

• Total running time is O (N + k log N).

• If k = O (N/log N), running time is O (N).

• For large value of k, running time is O (k log N).

Page 14: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

14

6.4 Applications of Priority Queues

Discrete Event Simulation

• Bank waiting line

• Given – customers interarrival distribution

– number of tellers (server)

– one common waiting line

– customers are served on FIFO basis

– service time (transaction time) distribution

Page 15: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

15

6.4 Applications of Priority Queues

• Statistics required– average waiting time

– average banking time (waiting time + service time)

– maximum waiting time

– maximum banking time

– maximum queue length

• Generate service time of each customer

Page 16: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

16

6.4 Applications of Priority Queues• Generate arrival time of each customer

(arrival time of previous customer + time interval for the next customer to come)

• One customer queue for each teller

• Event queue with 2 types of events (in event occurrence sequence)– customer arrival

– complete of service of one customer

Page 17: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

17

6.4 Applications of Priority Queues• At customer arrival event

– generate service time for this customer– insert the customer into the end of the shortest

teller queue – generate interarrival time and then compute

arrival time of the next customer– using the arrival time of the next customer,

generate an arrival event and insert it into the event queue (not necessarily the last in the queue)

Page 18: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

18

6.4 Applications of Priority Queues• At service completion event

– remove the customer from the teller queue

– compute relevant statistics for this customer

– if this teller queue is not empty, serve the next customer in the queue

– compute the service completion time (current time + service time)

– generate service completion event, and insert it into the event queue

Page 19: 1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.

19

6.4 Applications of Priority Queues

• Skip the rest of Chapter 6.