1 6.3 binary heap - other heap operations there is no way to find any particular key without a...
TRANSCRIPT
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.
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)
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.
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
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
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
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
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
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
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
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
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.
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).
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
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
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
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)
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
19
6.4 Applications of Priority Queues
• Skip the rest of Chapter 6.