DOC PREVIEW
MIT 6 006 - Lecture 9: Sorting II: Heaps

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 9 Sorting II: Heaps 6.006 Fall 2009Lecture 9: Sorting II: HeapsLecture Overview• Priority Queues• Heaps• HeapsortReadingsCLRS 2.1, 2.2, 2.3, 6.1, 6.2, 6.3 and 6.4Priority QueuesThis is an abstract datatype implementing a set S of elements, each associated with a key.Supports the following operations:insert(S, x) : insert element x into set Smax(S): return element of S with largest keyextract max(S): return element of S with largest key and remove it from Sincrease key(S, x, k): increases the value of element x’s key to new value k(assumed to be as large as current value)HeapsAn implementation of a priority queue. It is an array object, visualized as a nearly completebinary tree.Heap Property: The key of a node is ≥ than the keys of its children; e.g., Figure 1.Lecture 9 Sorting II: Heaps 6.006 Fall 2009Lecture 9: Sorting II: HeapsLecture Overview• Priority Queues• Heaps• HeapsortReadingsCLRS 2.1, 2.2, 2.3, 6.1, 6.2, 6.3 and 6.4Priority QueuesThis is an abstract datatype implementing a set S of elements, each associated with a key.Supports the following operations:insert(S, x) : insert element x into set Smax(S): return element of S with largest keyextract max(S): return element of S with largest key and remove it from Sincrease key(S, x, k): increases the value of element x’s key to new value k(assumed to be as large as current value)HeapsAn implementation of a priority queue. It is an array object, visualized as a nearly completebinary tree.Heap Property: The key of a node is ≤ than the key of its parent node; e.g., Figure 1.16148 793 241101 2 3 4 5 6 7 8 9 101016148712534937624981Figure 1: Binary HeapNOTE: For convenience, the first index in the array is 1.1Lecture 9 Sorting II: Heaps 6.006 Fall 2009Lecture 9: Sorting II: HeapsLecture Overview• Priority Queues• Heaps• HeapsortReadingsCLRS 2.1, 2.2, 2.3, 6.1, 6.2, 6.3 and 6.4Priority QueuesThis is an abstract datatype implementing a set S of elements, each associated with a key.Supports the following operations:insert(S, x) : insert element x into set Smax(S): return element of S with largest keyextract max(S): return element of S with largest key and remove it from Sincrease key(S, x, k): increases the value of element x’s key to new value k(assumed to be as large as current value)HeapsAn implementation of a priority queue. It is an array object, visualized as a nearly completebinary tree.Heap Property: The key of a node is ≤ than the key of its parent node; e.g., Figure 1.16148 793 241101 2 3 4 5 6 7 8 9 101016148712534937624981Figure 1: Binary HeapNOTE: For convenience, the first index in the array is 1.1Figure 1: Binary HeapNOTE: For convenience, the first index in the array is 1.1Lecture 9 Sorting II: Heaps 6.006 Fall 2009Visualizing an Array as a Treeroot of tree: first element in the array—corresponding index = 1,node with index i:parent(i) = bi2c; returns index of node’s parent, e.g. parent(5)=2left(i) = 2i; returns index of node’s left child, e.g. left(4)=8right(i) = 2i + 1; returns index of node’s right child, e.g. right(4)=9Note: no pointers required! Height of a binary heap O(log2n).Heap-Size VariableFor flexibility we may only need to consider the first few elements of an array as part of theheap. The variable heap-size denotes the number of items of the array that are part of theheap: A[1], . . . , A[heap-size];Max-Heaps vs Min-HeapsMax Heaps satisfy the Max-Heap Property : for all i, A[i] ≥ max{A[left(i)], A[right(i)]}. Ifleft(i) or right(i) is undefined, replace A[left(i)], respectively A[right(i)], by −∞. In partic-ular, if node i has no children, the property is trivially satisfied.Everything we describe applies to the construction and operation of Min Heaps, satisfyingthe Min-Heap Property : for all i, A[i] ≤ min{A[left(i)], A[right(i)]}. If left(i) or right(i) isundefined, replace A[left(i)], respectively A[right(i)], by +∞. In particular, if node i hasno children, the property is trivially satisfied.Operations with Heapsbuild max heap: produce a max-heap from unordered input array in O(n);max heapify: correct a single violation of the heap property at the root of a subtree inO(log n);heapsort: sort an array of size n in O(n log n) via the use of heaps;insert, extract max: O(lg n)Max Heapify(A,i)Assume that the trees rooted at left(i) and right(i) are max-heaps. If element A[i] violatesthe max-heap property, correct violation by trickling element A[i] down the tree, makingthe subtree rooted at index i a max-heap. See Figure 2; then read the pseudocode below.l ← left(i)r ← right(i)2Lecture 9 Sorting II: Heaps 6.006 Fall 2009if l ≤ heap-size(A) and A[l] > A[i]then largest ← lelse largest ← iif r ≤ heap-size(A) and A[r] > A[largest]then largest ← rif largest 6= ithen exchange A[i] and A[largest]MAX HEAPIFY(A, largest)ExampleBuild Max Heap(A)A[1 ···n] converted to a max heap Observation: Elements A[bn/2c + 1 ···n] are all leavesof the tree (why? 2i > n, for i > bn/2c + 1).Build Max Heap(A):heap size(A) = length(A)O(n) times for i ← b length[A]/2c downto 1O(log n) time do Max Heapify(A, i)O(n log n) overallSee Figure 3 for an example.NOTE: The trivial analysis of the algorithm noted above, shows that the running time isO(n log n). Observe, however, that Max Heapify only takes O(1) time for the nodes thatare one level above the leaves, and in general O(`) for the nodes that are ` levels above theleaves. Using this observation, it can be shown that the overall time for Build Max Heap(A)is O(n).3Lecture 9 Sorting II: Heaps 6.006 Fall 2009101641471253493762898110161447125349376298110161487125349376249811081010MAX_HEAPIFY (A,2)heap_size[A] = 10Exchange A[2] with A[4]Call MAX_HEAPIFY(A,4) because max_heap property is violatedExchange A[4] with A[9]No more callsFigure 2: MAX HEAPIFY Example4Lecture 9 Sorting II: Heaps 6.006 Fall 2009MAX-HEAPIFY (A,5)no changeMAX-HEAPIFY (A,4)Swap A[4] and A[8]161093414718212345671098 4 1 2 16910 1487 3AMAX-HEAPIFY (A,3)Swap A[3] and A[7]161093414718212345671098MAX-HEAPIFY (A,2)Swap A[2] and A[5]Swap A[5] and A[10]161093414718212345671098MAX-HEAPIFY (A,1)Swap A[1] with A[2]Swap A[2] with A[4]Swap A[4] with A[9]161093414718212345671098Figure 3: Example: Building Heaps5Lecture 9 Sorting II: Heaps 6.006 Fall 2009Sorting Strategy• Build max heap from unordered array• Find maximum element (A[1])• Swap elements A[n] and A[1]; now max element is at the right position;• Discard node n from heap (decrement


View Full Document

MIT 6 006 - Lecture 9: Sorting II: Heaps

Documents in this Course
Quiz 1

Quiz 1

7 pages

Quiz 2

Quiz 2

12 pages

Quiz 2

Quiz 2

9 pages

Quiz 1

Quiz 1

10 pages

Quiz 2

Quiz 2

11 pages

Quiz 1

Quiz 1

12 pages

Graphs

Graphs

27 pages

Load more
Download Lecture 9: Sorting II: Heaps
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 9: Sorting II: Heaps and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9: Sorting II: Heaps 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?