Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 the position, we can access the key immediately.

2 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 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 4 6.3 Binary Heap - Other Heap Operations Initial heap

5 5 6.3 Binary Heap - Other Heap Operations After PercolateDown (7)

6 6 6.3 Binary Heap - Other Heap Operations After PercolateDown (6)

7 7 6.3 Binary Heap - Other Heap Operations After PercolateDown (5)

8 8 6.3 Binary Heap - Other Heap Operations After PercolateDown (4)

9 9 6.3 Binary Heap - Other Heap Operations After PercolateDown (3)

10 10 6.3 Binary Heap - Other Heap Operations After PercolateDown (2)

11 11 6.3 Binary Heap - Other Heap Operations After PercolateDown (1)

12 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 2 h+1 - 1 - (h+1), which is approximately N.

13 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 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 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 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 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 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 19 6.4 Applications of Priority Queues Skip the rest of Chapter 6.


Download ppt "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."

Similar presentations


Ads by Google