Presented By:
Introduction to Heapsort
• Running time: O(n lg n)
Like merge sort
• Sorts in place: only a constant number of array elements are stored
outside the input array at any time
Like insertion sort
• Heap
A data structure used by Heapsort to manage information during the
execution of the algorithm
• Can be used as an efficient priority queue
Perfect Binary Tree
• For binary tree with height h
All nodes at levels h–1 or less have 2 children (full)
Complete Binary Trees
• For binary tree with height h
All nodes at levels h–2 or less have 2 children (full)
All leaves on level h are as far left as possible
Complete Binary Trees
Heaps
• Two key properties
• Complete binary tree
• Value at node
• Smaller than or equal to values in sub-trees (min-heap)
• Greater than or equal to values in sub-trees (max-heap)
• Example max-heap
Y ≤ X
Z ≤ X
Heap and Non-heap Examples
Binary Heap
• An array object that can be viewed as a nearly complete binary tree
• Each tree node corresponds to an array element that stores the value in the
tree node
• The tree is completely filled on all levels except possibly the lowest, which is
filled from the left up to a point
• A has two attributes
• length[A]: number of elements in the array
• heap-size[A]: number of elements in the heap stored within A
heap-size[A] ≤ length[A]
• max-heap and min-heap
A Max-heap
Length and Heap-size
Basic procedures
• Given the index i of a node, the indices of its parent, left child, and
right child can be computed simply:
• A[1] is the root of the tree
Basic Operations - Continued
• the LEFT procedure can compute 2i in one instruction by simply
shifting the binary representation of i left by one bit position
• The RIGHT procedure can quickly compute 2i + 1 by shifting the
binary representation of i left by one bit position and then adding in a
1 as the low-order bit.
• The PARENT procedure can compute Floor(i/2) by shifting i right one
bit position.
Heap Computation
Heap property
• Heap property
• The property that the values in the node must satisfy
• Max-heap property, for every node i other than the root
• A[PARENT(i)] ≥ A[i]
• The value of a node is at most the value of its parent
• The largest element in a max-heap is stored at the root
• The sub-tree rooted at a node contains values no larger than that contained
at the node itself
Max and min heaps
Heap Height
• The height of a node in a heap
• The number of edges on the longest simple downward path from the node to
a leaf
• The height of a heap is the height of its root
• The height of a heap of n elements is Θ (lg n)
• For example
• Height of node 2 is 2
• Height of the heap is 3
Heap Procedures
• MAX-HEAPIFY
• Maintains the max-heap property
• O(lg n)
• BUILD-MAX-HEAP
• Produces a max-heap from an unordered input array
• O(n)
• HEAPSORT
• Sorts an array in place
• O(n lg n)
Maintaining the Heap Property
• MAX-HEAPIFY is an important subroutine to maintain the max-heap
property
• Inputs: an array A and an index i into the array
• Outputs: the sub-tree rooted at index I becomes a max-heap
• Assume: the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but
A[i] may be smaller than its children
• violate the max-heap property
• Method: MAX-HEAPIFY let the value at A[i] floats down in the max-heap
Example of MAX-HEAPIFY
Example of MAX-HEAPIFY
Example of MAX-HEAPIFY
MAX-HEAPIFY
Extract the indices of LEFT and RIGHT
children of i
Choose the largest of A[i], A[l], A[r]
Float down A[i] recursively
Running Time of MAX-HEAPIFY
• Θ(1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)]
• Plus the time to run MAX-HEAPIFY on a sub-tree rooted at one of the
children of node I
• The children’s sub-trees each have size at most 2n/3 (why?)
• the worst case occurs when the last row of the tree is exactly half full
• T(n) ≤ T(2n/3) + Θ(1)
• By case 2 of the master theorem
• T(n) = O(lg n)
Heapify Example
Building a Max-Heap
• Observation: A[(Floor(n/2)+1)..n] are all leaves of the tree
• Each is a 1-element heap to begin with
• Upper bound on the running time
• O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n)
• Not tight
Example
Example
Example
Correctness
• To show that Build-Heap works, we need to prove its correctness
• Invariant
• At the start of each iteration of the for loop of lines 2–3, each node i + 1, i + 2,
… , n is the root of a max-heap.
• Initialization: Prior to the first iteration of the loop, i = Floor (n/2). Each node Floor(n/2)+1,
Floor(n/2)+2,…,n is a leaf and is thus the root of a trivial max-heap.
• Maintenance: By the loop invariant, therefore, they are both roots of max-heaps. This is precisely
the condition required for the call MAX-HEAPIFY(A, i) to make node i a max-heap root. Moreover,
the MAX-HEAPIFY call preserves the property that nodes i+1, i+2,…,n are all roots of max-heaps.
Decrementing i in the for loop update reestablishes the loop invariant for the next iteration.
• Termination: At termination, i = 0. By the loop invariant, each node 1,2,…,n is the root of a
max-heap. In particular, node 1 is.
Heapsort
• Using BUILD-MAX-HEAP to build a max-heap on the input array
A[1..n], where n=length[A]
• Put the maximum element, A[1], to A[n]
• Then discard node n from the heap by decrementing heap-size(A)
• A[2..n-1] remain max-heaps, but A[1] may violate
• call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1]
• Repeat the above process from n down to 2
• Cost: O(n lg n)
• BUILD-MAX-HEAP: O(n)
• Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
Heapsort Algorithm
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Priority queues
• A priority queue is a data structure for maintaining a set S of elements,
each with an associated value called a key. A max-priority queue supports
the following operations:
• INSERT(S, x): inserts the element x into the set S, which is equivalent to the
operation S = S U {x}
• MAXIMUM(S) returns the element of S with the largest key.
• EXTRACT-MAX(S) removes and returns the element of S with the largest
key.
• INCREASE-KEY(S,x,k) increases the value of element x’s key to the new
value k, which is assumed to be at least as large as x’s current key value.
Finding and Extracting
the maximum element
Θ (1)
O(lg n)
Increasing key value and Insert
O(lg n)
O(lg n)

Lecture 07 - HeapSort.pptx

  • 1.
  • 3.
    Introduction to Heapsort •Running time: O(n lg n) Like merge sort • Sorts in place: only a constant number of array elements are stored outside the input array at any time Like insertion sort • Heap A data structure used by Heapsort to manage information during the execution of the algorithm • Can be used as an efficient priority queue
  • 4.
    Perfect Binary Tree •For binary tree with height h All nodes at levels h–1 or less have 2 children (full)
  • 5.
    Complete Binary Trees •For binary tree with height h All nodes at levels h–2 or less have 2 children (full) All leaves on level h are as far left as possible
  • 6.
  • 7.
    Heaps • Two keyproperties • Complete binary tree • Value at node • Smaller than or equal to values in sub-trees (min-heap) • Greater than or equal to values in sub-trees (max-heap) • Example max-heap Y ≤ X Z ≤ X
  • 8.
  • 9.
    Binary Heap • Anarray object that can be viewed as a nearly complete binary tree • Each tree node corresponds to an array element that stores the value in the tree node • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point • A has two attributes • length[A]: number of elements in the array • heap-size[A]: number of elements in the heap stored within A heap-size[A] ≤ length[A] • max-heap and min-heap
  • 10.
  • 11.
  • 12.
    Basic procedures • Giventhe index i of a node, the indices of its parent, left child, and right child can be computed simply: • A[1] is the root of the tree
  • 13.
    Basic Operations -Continued • the LEFT procedure can compute 2i in one instruction by simply shifting the binary representation of i left by one bit position • The RIGHT procedure can quickly compute 2i + 1 by shifting the binary representation of i left by one bit position and then adding in a 1 as the low-order bit. • The PARENT procedure can compute Floor(i/2) by shifting i right one bit position.
  • 14.
  • 15.
    Heap property • Heapproperty • The property that the values in the node must satisfy • Max-heap property, for every node i other than the root • A[PARENT(i)] ≥ A[i] • The value of a node is at most the value of its parent • The largest element in a max-heap is stored at the root • The sub-tree rooted at a node contains values no larger than that contained at the node itself
  • 16.
  • 17.
    Heap Height • Theheight of a node in a heap • The number of edges on the longest simple downward path from the node to a leaf • The height of a heap is the height of its root • The height of a heap of n elements is Θ (lg n) • For example • Height of node 2 is 2 • Height of the heap is 3
  • 18.
    Heap Procedures • MAX-HEAPIFY •Maintains the max-heap property • O(lg n) • BUILD-MAX-HEAP • Produces a max-heap from an unordered input array • O(n) • HEAPSORT • Sorts an array in place • O(n lg n)
  • 19.
    Maintaining the HeapProperty • MAX-HEAPIFY is an important subroutine to maintain the max-heap property • Inputs: an array A and an index i into the array • Outputs: the sub-tree rooted at index I becomes a max-heap • Assume: the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but A[i] may be smaller than its children • violate the max-heap property • Method: MAX-HEAPIFY let the value at A[i] floats down in the max-heap
  • 20.
  • 21.
  • 22.
  • 23.
    MAX-HEAPIFY Extract the indicesof LEFT and RIGHT children of i Choose the largest of A[i], A[l], A[r] Float down A[i] recursively
  • 24.
    Running Time ofMAX-HEAPIFY • Θ(1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)] • Plus the time to run MAX-HEAPIFY on a sub-tree rooted at one of the children of node I • The children’s sub-trees each have size at most 2n/3 (why?) • the worst case occurs when the last row of the tree is exactly half full • T(n) ≤ T(2n/3) + Θ(1) • By case 2 of the master theorem • T(n) = O(lg n)
  • 25.
  • 26.
    Building a Max-Heap •Observation: A[(Floor(n/2)+1)..n] are all leaves of the tree • Each is a 1-element heap to begin with • Upper bound on the running time • O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n) • Not tight
  • 27.
  • 28.
  • 29.
  • 30.
    Correctness • To showthat Build-Heap works, we need to prove its correctness • Invariant • At the start of each iteration of the for loop of lines 2–3, each node i + 1, i + 2, … , n is the root of a max-heap. • Initialization: Prior to the first iteration of the loop, i = Floor (n/2). Each node Floor(n/2)+1, Floor(n/2)+2,…,n is a leaf and is thus the root of a trivial max-heap. • Maintenance: By the loop invariant, therefore, they are both roots of max-heaps. This is precisely the condition required for the call MAX-HEAPIFY(A, i) to make node i a max-heap root. Moreover, the MAX-HEAPIFY call preserves the property that nodes i+1, i+2,…,n are all roots of max-heaps. Decrementing i in the for loop update reestablishes the loop invariant for the next iteration. • Termination: At termination, i = 0. By the loop invariant, each node 1,2,…,n is the root of a max-heap. In particular, node 1 is.
  • 31.
    Heapsort • Using BUILD-MAX-HEAPto build a max-heap on the input array A[1..n], where n=length[A] • Put the maximum element, A[1], to A[n] • Then discard node n from the heap by decrementing heap-size(A) • A[2..n-1] remain max-heaps, but A[1] may violate • call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1] • Repeat the above process from n down to 2 • Cost: O(n lg n) • BUILD-MAX-HEAP: O(n) • Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
    Priority queues • Apriority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. A max-priority queue supports the following operations: • INSERT(S, x): inserts the element x into the set S, which is equivalent to the operation S = S U {x} • MAXIMUM(S) returns the element of S with the largest key. • EXTRACT-MAX(S) removes and returns the element of S with the largest key. • INCREASE-KEY(S,x,k) increases the value of element x’s key to the new value k, which is assumed to be at least as large as x’s current key value.
  • 42.
    Finding and Extracting themaximum element Θ (1) O(lg n)
  • 43.
    Increasing key valueand Insert O(lg n) O(lg n)