+ All Categories
Home > Documents > Heap, Heapsort and Priority Queues - Seattle...

Heap, Heapsort and Priority Queues - Seattle...

Date post: 14-Mar-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
33
Heap, Heapsort and Priority Queues Dr. Yingwu Zhu
Transcript
Page 1: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heap, Heapsort and Priority

QueuesDr. Yingwu Zhu

Page 2: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Overview

What is a heap? Semiheap?

How does heapsort work?

Time efficiency of heapsort

Priority queues

Page 3: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heaps (maxheap)

A heap is a binary tree with properties:

1. It is complete

• Each level of tree completely filled

• Except possibly bottom level (nodes in left

most positions)

2. It satisfies heap-order property

• Data in each node >= data in children

Page 4: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heaps

Which of the following are heaps?

A B C

Page 5: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heaps

Maxheap?– by default

Minheap?

Page 6: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing a Heap

What data structure is good for its

implementation?

Page 7: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing a Heap

Use an array or vector, why?

Number the nodes from top to bottom

◦ Number nodes on each row from left to right

Store data in ith node in ith location of array

(vector)

0

1 2

3 4

Page 8: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing a Heap

Note the placement of the nodes in the array

0 1 2 3 4 5

0 1 2 3 4 5

Page 9: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing a Heap

Using an array myArray[]

◦ Children of ith node are at myArray[2*i+1]

and myArray[2*i+2]

◦ Parent of the ith node is at mayArray[(i-

1)/2]

Page 10: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing Heap

#define CAP 10000

typedef int T;

class Heap {private:T myArray[CAP];

int mySize;

public:Heap():mySize(0) {}~Heap();

void deleteMax(); //remove the max element

void insert(const T& item); //insert an item

};

Page 11: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Basic Heap Operations

Constructor

◦ Set mySize to 0, allocate array

Empty

◦ Check value of mySize

Retrieve the max item

◦ Return root of the binary tree, myArray[0]

How about delete max item?

◦ Think about it?

Page 12: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Delete Max Item?

0 1 2 3 4

Page 13: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Basic Heap Operations

Delete max item

◦ Max item is the root, replace with last node in tree

◦ Then interchange root with larger of two children

◦ Continue this with the resulting sub-tree(s) percolate down

◦ Semiheap: [1] complete tree [2] both subtrees are heaps

Result called a

semiheap

0 1 2 3 4

Page 14: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Implementing Heap

#define CAP 10000

typedef int T;

class Heap {private:

T myArray[CAP];

int mySize;

private:

void percolate_down(int pos); //percolate down

public:Heap():mySize(0) {}~Heap();

void deleteMax(); //remove the max element

void insert(const T& item); //insert an item

};

Page 15: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Percolate Down Algorithm

1. Set c = 2 * r + 1

2. While c < n do following //what does this mean?

a. If c < n-1 and myArray[c] < myArray[c + 1]Increment c by 1

b. If myArray[r] < myArray[c]i. Swap myArray[r] and myArray[c]ii. set r = ciii. Set c = 2 * c + 1

elseTerminate repetition

End while

Page 16: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Percolate down

Recursive

Non-recursive

void delete_max()

Page 17: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Insert an item into the heap

What is the basic idea?

Page 18: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Basic Heap Operations

Insert an item

◦ Amounts to a percolate up routine

◦ Place new item at end of array

◦ Interchange with parent so long as it is greater than its

parent

000 1 2 3 4 5 6

Page 19: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Percolate Up Algorithm

Why percolate up?

◦ When to terminate the up process?

void Heap::percolate_up()

void Heap::insert(const T& item)

Page 20: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

How to do remove?

Remove an item from the heap?

Hint question: A leaf node must be less or

equal than any internal node in a heap?

Page 21: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

HeapSort

What is the basic idea?

Discussions and comments.

Page 22: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Given a list of numbers in an array

◦ Stored in a complete binary tree

Convert to a heap

◦ Begin at last node not a leaf: pos = (size-2)/2?

◦ Apply percolated down to this subtree

◦ Continue

[0] [1] [0] [0][3]

[0] [1] [2] [3] [4] [5]

Page 23: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Algorithm for converting a complete

binary tree to a heap – called "heapify"

For r = (n-1-1)/2 down to 0:

apply percolate_down to the subtree

in myArray[r] , … myArray[n-1]

End for

Puts largest element at root

Do you understand it? Think why?

Page 24: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Why?

◦ Heap is a recursive ADT

◦ Semiheap heap: from bottom to up

Percolate down for this conversion

Page 25: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Now swap element 1 (root of tree) with last

element

◦ This puts largest element in correct location

Use percolate down on remaining sublist

◦ Converts from semi-heap to heap

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

Page 26: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Again swap root with rightmost leaf

Continue this process with shrinking sublist

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

Page 27: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Summary of HeapSort

Step 1: put the data items into an array

Step 2: Heapify this array into a heap

Step 3: Exchange the root node with the last

element and shrink the list by pruning the last

element (imaginarily -).

◦ Now it is a semi-heap

◦ Apply percolate-down algorithm

◦ Go back step 3

Page 28: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort Algorithm

1. Consider x as a complete binary tree, use

heapify to convert this tree to a heap

2. for i = n-1 down to 1:

a. Interchange x[0] and x[i]

(puts largest element at end)b. Apply percolate_down to convert

binary tree corresponding to sublist in x[0] .. x[i-1]

Page 29: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heapsort

Fully understand how heapsort works!

T(n) = O(nlogn)

◦ Why?

Page 30: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Heap Algorithms in STL

Found in the <algorithm> library

◦ make_heap() heapify

◦ push_heap() insert

◦ pop_heap() delete

◦ sort_heap() heapsort

Note program which illustrates these

operations, Fig. 13.1

Page 31: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Priority Queue

A collection of data elements

◦ Items stored in order by priority

◦ Higher priority items removed ahead of lower

Operations

◦ Constructor

◦ Insert

◦ Find, remove smallest/largest (priority) element

◦ Replace

◦ Change priority

◦ Delete an item

◦ Join two priority queues into a larger one

Page 32: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Priority Queue

Implementation possibilities

◦ As a list (array, vector, linked list)

T(n) for search, removeMax, insert operations

◦ As an ordered list

T(n) for search, removeMax, insert operations

◦ Best is to use a heap

T(n) for basic operations

Why?

Page 33: Heap, Heapsort and Priority Queues - Seattle Universityfac-staff.seattleu.edu/zhuy/web/teaching/Fall10/CPSC250/... · 2010-11-09 · Priority Queue A collection of data elements Items

Priority Queue

STL priority queue adapter uses heap

◦ Note operations in table of Fig. 13.2 in text,

page 751


Recommended