1 heaps and priority queues v2 starring: min heap co-starring: max heap

84
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

Upload: moris-randall

Post on 13-Dec-2015

254 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

1

Heaps and Priority Queues v2

Starring: Min Heap

Co-Starring: Max Heap

Page 2: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

2

Purpose:

In this lecture we will discuss the Priority Queue ADT , the Java PriorityQueue Class and a Heap implementation of a priority Queue

Page 3: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

3

Resources:

Java Methods AB Data Structures Chapter 7 p.175

Java Essentials Study Guide Chapter 17 .7/8 p.330 & Chapter 20.1 p.398 & Chapter 20A.4/5 p.418

Barrons Chapter 9 p.305 & Chapter 12 p.414

AP Java Text Chapter 19.7 p.843 & Chapter 21.4 p.950

Page 4: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

4

Handouts:

1. Priority Queue Class

CODE:

PQ.java

PriorityString.java

PriorityQueueExample.java

Jobs.java

Page 5: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

5

Intro:

So far we have seen data structures that maintain and process data in the following ways:

Store data in order based on a KeyStore data in order based on the “value” of

the objectStore data in order of arrivalStore data in reverse order of its arrivalStore data in random order

Page 6: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

6

There are times when we need to maintain and process data based on their relative importance or Priority

Page 7: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

7

For example, people waiting to but concert tickets. The priority of the order they are allowed to purchase tickets is based on the number on a wrist band that are distributed. The number on the wristband determines when they get served

Page 8: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

8

If you go to an amusement park, you can select a predetermined time to get on an attraction. When the time comes, you get into a Priority Queue which gets processed first

Page 9: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

9

In a hospital emergency room, a triage system identifies the urgent patients all of whom are seem before the normal patients regardless

of their order of arrival

Page 10: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

10

In operating systems, jobs are processed based on a specific priority so that a recently submitted job with a high priority will process before lower priority jobs even though these jobs were in the queue longer

Page 11: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

11

What we Will Cover in This Lecture:

Priority Queue ADT

Priority Queue Java Class Priority Queue Implementations

Heap Implementation

The AP AB Requirements

Page 12: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

12

Priority Queue ADT

PQ’s Support functionality that maintains a large set of elements that are ranked in some way and to have quick access to the HIGHEST ranked element

Page 13: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

13

Priority Queue ADT

PQ is a collection of the same type object values

These object values contain data and a priority

These objects are ordered in a way that allows the HIGHEST priority item to be removed first

Page 14: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

14

Example of a priority queue where items entered have a priority of 1,2 or 3:EMPTY PQ(ADD)A (2)B (2) A (2)C (2) B (2) A (2)(REMOVE MIN)

C (2) B(2)(ADD)D (3) C (2) B(2)(REMOVE MIN)D (3) C (2) (ADD)E (1) D (3) C (2) F (1) E (1) D (3) C (2) (REMOVE MIN)

F (1) D (3) C (2) D (3) C (2)

D (3)EMPTY PQ

Page 15: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

15

If several items maintain the same priority then the first one in the queue is processed first

Page 16: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

16

Priority Queues Must Provide for the adding and removing of elements in a way that ensures the HIGHEST Priority Item is removed First

Here is the Priority Queue Java Class:

Page 17: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

17

public Class PriorityQueue { boolean isEmpty()

boolean add(E x)

E remove ()

E peek ()

Iterator E iterator()}

Page 18: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

18

import java.util.PriorityQueue;SPVMPriorityQueue<String> P = new PriorityQueue<String>(10); P.add("Sally"); P.add("Betty"); P.add("Xavier"); P.add("Teresa"); P.add("David");

Page 19: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

19

if(P.isEmpty()) { System.out.println("QUEUE EMPTY"); } else { System.out.println(P.size()); } P.remove(); System.out.println(P.peek());

Page 20: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

20

Iterator i = P.iterator();i = P.iterator();

while(i.hasNext()) { System.out.println(i.next()); } System.out.println(); for(String x:P) { System.out.println(x.toString()); }

Page 21: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

21

The priority of each item in the queue is established using the Comparable interface

Every time the remove or peek method is executed the SMALLEST element with respect to the compareTo method is returned

Page 22: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

22

Other Priority Queue Implementations

You are required to know Java’s PQ implementation as well as to understand their general principles that require any Data Structure to allow for:

Quick insertion of PQ elements

Quick retrieval of an element with the top priority

Potential PQ implementations include:

Page 23: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

23

Linked List where new nodes are inserted at the front of the list O(1) and the removal searches the list for the highest priority node O(n)

Page 24: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

24

Linked List where nodes are inserted in priority order with the smallest elements in the front nodes O(n) and removal simply unlinks the front node O(1)

Page 25: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

25

Array with nodes inserted at the end of the List O(1) and removals search for the highest priority element O(n)

Page 26: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

26

A sorted Array where the highest priority elements (smallest values) are added at the end of the List O(n) and removal takes off he last element in the array O(1)

Page 27: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

27

TreeSet where adding an element is O(Log N) and Removal is O(Log N)

Page 28: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

28

Heap Implementation

The most common implementation of a PQ is a Binary Heap

With a PQ we use a Minimum Heap

With these type of heaps the VALUE of every node is LESS THAN or EQUAL to that of its children

Page 29: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

29

A HEAP is viewed as a Complete Binary Tree where there are no GAPS at any level.

The Last level may have some leaves missing on the right, but every node except the last must have a node after it.

The nodes are in the leftmost positions

Page 30: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

30

Each addition to the heap MUST maintain the property where the parent node is at least as small as its children

Page 31: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

31

Example:

1

6 4

8 7 9 10

12 20

Page 32: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

32

The lower the number, the higher the priority

The element with the HIGHEST priority is kept at the root so removing an element requires removal of the root and then restructuring of the heap

Page 33: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

33

ReHeaping is a O(LogN) operation

Inserting into the heap also requires Reheaping and is O(LogN)

ALL elements added to a PQ must implement the Comparable interface and be of the same class

Page 34: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

34

Inserting and Removing elements to the Heap

Page 35: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

35

create a heap: 25 57 48 37 12 92 86 33

25

Page 36: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

36

create a heap: 25 57 48 37 12 92 86 33

25

57

Page 37: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

37

create a heap: 25 57 48 37 12 92 86 33

25

57 48

Page 38: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

38

Create a heap: 25 57 48 37 12 92 86 33

25

37 48

57

Page 39: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

39

Create a heap: 25 57 48 37 12 92 86 33

12

25 48

57 37

Page 40: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

40

create heap: 25 57 48 37 12 92 86 33

12

25

57 37

48

92

Page 41: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

41

create heap: 25 57 48 37 12 92 86 33

12

25

57 37

48

92 86

Page 42: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

42

create heap: 25 57 48 37 12 92 86 33

12

25

33 37

48

92 86

57

Page 43: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

43

Given the following Tree:

1

6 4

8 7 9 10

12 20

Page 44: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

44

Match this heap with an indices of an Array:

Heap Priority Array Element (IGNORE ZERO)

01 16 24 38 47 59 610 712 820 9

Page 45: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

45

Array Index:

1

2 3

4 5 6 7

8 9

Page 46: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

46

Lets Insert an element with a priority of 5

We insert the new element in the next open slot ( index 10 or left child of element with Priority of 7)

Page 47: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

47

We then Compare This element against its Parent

1

6 4

8 7 9 10

12 20 5

Page 48: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

48

If the Parent is Greater, Then SWAP the elements

1

6 4

8 5 9 10

12 20 7

Page 49: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

49

We AGAIN Compare This element against its Parent

1

6 4

8 5 9 10

12 20 7

Page 50: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

50

If the Parent is Greater, Then SWAP the elements

1

5 4

8 6 9 10

12 20 7

Page 51: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

51

Again We Compare This Element against its New Parent

But since the Parent is SMALLER than the this element, we are done RE-HEAPING

The order of the Heap is maintained as all children are LARGER than their parent

Page 52: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

52

The Heap has been updated and its order of priority maintained

1

5 4

8 6 9 10

12 20 7

Page 53: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

53

We can implement a Heap as a Binary Tree but it is more efficient to implement a Binary Heap as an Array

Using your own array or the ArrayList

Page 54: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

54

Non-Linked Representation of Binary Trees is a more efficient way of implementing heaps.

Here all nodes are stored in an array in a certain order so that for each node it is easy to find its children and its parent

Page 55: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

55

EXAMPLE:

Given the previous PQ, the following is the construction of the array at each stage of insertion:

8 9 4 6 2 3 10

Page 56: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

56

8

8 9

4 9 8

4 6 8 9

2 4 8 9 6

2 4 3 9 6 8

2 4 3 9 6 8 10

Page 57: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

57

If you match the element number, starting at element 1, with the Binary Heap, you will see an exact correlation

Page 58: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

58

Tree element #s

2 1

/ \ / \

4 3 2 3

/ \ / \ / \ / \

9 6 8 10 4 5 6 7

Page 59: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

59

Each level contains twice as many nodes as the preceding level.

Page 60: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

60

The left and right children of the i-th node, if they are present, have the numbers 2i and 2i+1 and its parent has the number i/2 (truncated to an integer)

Page 61: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

61

Using this algorithm, we can manipulate an array as a Binary HEAP

Page 62: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

62

REMEMBER THAT A COMPLETE TREE is a tree where there are no GAPS at any level.

The Last level may have some leaves missing on the right, but every node except the last must have a node after it.

The nodes are in the leftmost positions

Page 63: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

63

A Complete Tree

Page 64: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

64

Binary Trees: a non-linked representation:

1

2 3

4 5 6 7

8

Page 65: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

65

This property allows us to store a complete tree in an array where the element x[n] corresponds to node number n

Count the elements of the array starting from element 1 (leave element 0 unused)

Page 66: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

66

Heap remove deletes an element from the root of the heap

Then the heap needs to be adjusted to preserve its ordering property and to keep it a complete tree.

Remove the root (first element in the heap)

Page 67: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

67

Place the last element of the heap into its root (first element). This is the RIGHTMOST node in the lowest tree level

Then move down from level to level, swapping it with its smaller child until it falls into place

Page 68: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

68

This process is called the REHEAP DOWN procedure

Page 69: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

69

Using our previous example, execute a remove

1

5 4

8 6 9 10

12 20 7

Page 70: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

70

We take the last element in the heap (10th index / value #7) and move it to the top of the heap

Then swap this element with its smaller child Swap #7 with #4

Continue this until this node is smaller than its children

Page 71: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

71

The heap now looks like this:

4

5 7

8 6 9 10

12 20

Page 72: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

72

We can also have a Maximum Heap where all elements are Stored with the HIGHEST value at the top of the heap

Here, all parents have a HIGHER value than their children

Page 73: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

73

Here element x[1] of the array corresponds to the root of the heap.

Example:

55 21 34 3 8 13 5 1 2 1

queued items

Page 74: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

74

TREEVIEW:

55

2134

3 8 5

1

13

1

23

45 6

7

8 2

9

1 10

Page 75: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

75

ARRAYVIEW: Element # Value

0unused

1 55

2 21

3 34

4 3

5 8

6 13

7 5

8 1

9 2

10 1

Page 76: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

76

Priority Queues maintain data as well as a priority

You can create a class that serves as the “element” that is stored in the Heap

Page 77: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

77

In the PriorityString class there is a data part and an Integer that represents the objects priority

The SMALLER the number the HIGHER the priority

Page 78: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

78

Lets look at the PriorityString.java class in the handout

Page 79: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

79

The ArrayPriorityQueue.java class implements the Priority Queue as an ArrayList

Page 80: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

80

Lets look at the ArrayPriorityQueue.java class in the handout

Page 81: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

81

Finally, to put it all together we use our Array Priority Queue in a Driver Program

Lets look at PriorityQueueExample.java in our handout

Page 82: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

82

RUN the Priority QueuesAndExamples.java

RUN and Review the program JOBS.JAVA

Page 83: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

83

AP AB Subset Requirements

Students are expected to:Understand the PQ ADT & Use Java’s PQ

How the PriorityQueue can be implemented (insert, remove, peek, reheap) in other ways than the Java Class

Create a Class that is used as the element of a PQ

Utilize a PQ implementation in a Driver program

Page 84: 1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap

84

LABS: Write a Heap Class

Social Security Administration

Multiple Choice from Barrons: Chapter 9 p.317 #s 14 to 18