MA 252: Data Structures and Algorithms Lecture 12 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati
Inserting Heap Elements Inserting an element into a heap: increment heapsize and add new element to the highest numbered position of array. walk up tree from new leaf to root, swapping values. Insert input key when a parent key larger than the input key is found. Running time of Max-Heap-Insert: O(lg n), time to traverse leaf to root path (height = O(lg n))
Priority Queues Definition: A priority queue is a data structure for maintaining a set S of elements, each with an associated key. A max-priority-queue gives priority to keys with larger values and supports the following operations. 1. insert(s, x) inserts the element x into set S. 2. max(s) returns element of S with largest key. 3. extract-max(s) removes and returns element of S with largest key. 4. increase-key(s,x,k) increases the value of element x's key to new value k (assuming k is at least as large as current key's value).
Priority Queues: Application for Heaps An application of max-priority queues is to schedule jobs on a shared processor. Need to be able to check current job's priority Heap-Maximum(A) remove job from the queue Heap-Extract-Max(A) insert new jobs into queue Max-Heap-Insert(A, key) increase priority of jobs Heap-Increase-Key(A,i,key) Shared processor Running Increase priority Job Q Insert new job Check current job priority Remove job
Process Scheduling Queues
Priority Queues: Application for Heaps An application of max-priority queues is to schedule jobs on a shared processor. Need to be able to check current job's priority Heap-Maximum(A) remove job from the queue Heap-Extract-Max(A) insert new jobs into queue Max-Heap-Insert(A, key) increase priority of jobs Heap-Increase-Key(A,i,key) Initialize PQ by running Build-Max-Heap on an array A. A[1] holds the maximum value after this step. Heap-Maximum(A) - returns value of A[1]. Heap-Extract-Max(A) - Saves A[1] and then, like Heap-Sort, puts item in A[heapsize] at A[1], decrements heapsize, and uses Max- Heapify(A, 1) to restore heap property.
Heap-Increase-Key Heap-Increase-Key(A, i, key) - If key is larger than current key at A[i], floats inserted key up heap until heap property is restored. An application for a min-heap priority queue is an event driven simulator, where the key is an integer representing the number of seconds (or other discrete time unit) from time zero (starting point for simulation).
Sorting in linear time Counting sort: No comparisons between elements. Input: A[1.. n], where A[j] {1, 2,, k}. Output: B[1.. n], sorted. Auxiliary storage: C[1.. k].
Counting sort
Loop 1
Loop 2
Loop 2
Loop 2
Loop 2
Loop 2
Loop 3
Loop 3
Loop 3
Loop 4
Loop 4
Loop 4
Loop 4
Loop 4
Analysis
Running time If k = O(n), then counting sort takes Θ(n) time. But, sorting takes Ω(nlg n) time! Where s the fallacy? Answer: Comparison sorting takes Ω(nlg n) time. Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs!
Stable sorting Counting sort is a stable sort: it preserves the input order among equal elements. Exercise: What other sorts have this property?