Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Priority Queues - Fundamental Algorithms | CS 231, Study notes of Algorithms and Programming

Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-6ta
koofers-user-6ta ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS231 Algorithms Handout # 32
Prof. Lyn Turbak November 21, 2001
Wellesley College
Priority Queues
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 mutabl e
priority queue is a collection of such elements supporting the following key operations:
PQ-Empty()
Returns an empty priority queue.
PQ-Insert(P, e lt )
Modifies Pby inserting elt into priority queue P.
PQ-Delete-Max(P)
Deletes from Pand returns the largest element (by the priority ordering) of priority queue
P.
Array-To-PQ(A)
Constructs and returns a priority queue containing the nelements 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 1do
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,sothatHeapSort can be an in-place sorting
algorithm.
1
pf3
pf4
pf5

Partial preview of the text

Download Priority Queues - Fundamental Algorithms | CS 231 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout # 32 Prof. Lyn Turbak November 21, 2001 Wellesley College

Priority Queues

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:

  • Left(address) = 2 * address
  • Right(address) = (2 * address) + 1
  • Parent(address) = address div 2

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: