



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
CS231 Algorithms Handout # 32 Prof. Lyn Turbak November 21, 2001 Wellesley College
Reading: CLRS Chapter 6/CLR Chapter 7
Priority Queue Contract
Given a set of elements with a priority ordering (which we will denote by >), A mutable priority queue is a collection of such elements supporting the following key operations:
PQ-Empty() Returns an empty priority queue.
PQ-Insert(P, elt) Modifies P by inserting elt into priority queue P.
PQ-Delete-Max(P) Deletes from P and returns the largest element (by the priority ordering) of priority queue P.
Array-To-PQ(A) Constructs and returns a priority queue containing the n elements of array A. May mutate A.
Priority Queue Implementations
What are the running times of the priority queue operations for an n-element priority queue P using the following representations?
PQ-Insert(P,elt) PQ-Delete-Max(P) Array-To-PQ(A) unsorted list list sorted high to low binary search tree heap (this lecture)
Heap Sort
Given heap operations with the above running times, itโs easy to construct a guaranteed O(n(lg(n))) sorting algorithm:
HeapSort(A) H โ Array-To-PQ(A) for i โ length[A] downto 1 do A[i] โ PQ-Delete-Max(H)
We will see below that when priority queues are represented as heaps, the priority queue used by HeapSort can be stored within the argument array A, so that HeapSort can be an in-place sorting algorithm.
Complete Binary Trees
The binary address of a node in a binary tree specifies the order in which it would be visited in a breadth first traversal. For example:
Operations on binary addresses:
An n-element binary tree is complete if the set of binary addresses of its nodes is { 1 , 2 ,... , n}. For example:
An n-element binary tree is full if it a complete tree of height h with 2h^ โ 1 nodes.
Important implementation detail: The elements of a complete binary tree can be represented as an array. In this representation, tree navigation is performed by pointer arithmetic.
Heaps
A heap is a complete binary tree satisfying the following heap condition:
At every node in a heap, the node value is greater than or equal to all the values in both of its subtrees.
Example:
Heap Deletion
PQ-Delete-Max(H) if size[H] < 1 then error "heap underflow" A โ elts[H] max โ A[1] A[1] โ A[size[H]] size[H] โ size[H] - 1 BubbleDown(A, 1) return max
Bubble-Down(A, address) B This function is called Heapify in CLR if Left(address) โค heap_size[A] and lt(A[address], A[Left(address)]) then largest โ Left(address) else largest โ address if Right(address) โค heap_size[A] and lt(A[largest], A[Right(address)]) then largest โ Right(address) if largest 6 = address then swap(A, address, largest) Bubble-Down(A, largest)
Analysis:
Constructing a Heap
Naive version of Array-To-PQ:
Array-To-PQ(A) H โ PQ-Empty for i โ 1 to length[A] do B Uses array slots for heap storage! PQ-Insert(H, A[i]) return H
Analysis:
Clever version of Array-To-PQ:
Array-To-PQ(A) H โ PQ-Empty size[H] โ length[A] for i โ (length[A] div 2) downto 1 do Bubble-Down(A, i) return H
Analysis: