priority queue. priority queues queue (fifo). priority queue. deletion from a priority queue is...

60
Priority Queue

Upload: della-hodges

Post on 04-Jan-2016

239 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Priority Queue

Page 2: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Priority Queues

• Queue (FIFO).• Priority queue.

• Deletion from a priority queue is determined by the element priority.

• Two kinds of priority queues:• Min priority queue. (delete min element first)• Max priority queue. (delete max element first)

Page 3: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Max/Min Priority Queue

68

2

41 Max/Min

Priority Queue

empty Size Push Top pop

Page 4: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Min Priority Queue

• Collection of elements.• Each element has a priority or key.• Supports following operations:

empty size insert an element into the priority queue (push) get element with min priority (top) remove element with min priority (pop)

five elements whose keys are 6, 8, 2, 4, 1

Page 5: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Max Priority Queue

• Collection of elements.• Each element has a priority or key.• Supports following operations:

empty size insert an element into the priority queue (push) get element with max priority (top) remove element with max priority (pop)

five elements whose keys are 6, 8, 2, 4, 1

Page 6: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Implementation of Priority Queue

• Array, linked list, heap and complete binary tree.

• Operations:• empty • size• top• insert (push)• remove (pop)

Page 7: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority
Page 8: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

C++ STL priority_queue(max priority queue)

Page 9: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority
Page 10: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Applications

Sorting• use element key as priority• insert elements to be sorted into a priority queue• remove/pop elements in priority order

if a min priority queue is used, elements are extracted in ascending order of priority (or key)

if a max priority queue is used, elements are extracted in descending order of priority (or key)

Page 11: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Sorting Example

Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. Insert the five elements into a max priority queue. Do five remove max operations placing removed

elements into the sorted array from right to left.

Page 12: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After Inserting Into Max Priority Queue

Sorted Array

68

2

41 Max Priority

Queue

Page 13: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After First Remove Max Operation

Sorted Array

6

2

41

8

Max Priority Queue

Page 14: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After Second Remove Max Operation

Sorted Array

2

41

86

Max Priority Queue

Page 15: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After Third Remove Max Operation

Sorted Array

21

864

Max Priority Queue

Page 16: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After Fourth Remove Max Operation

Sorted Array

1

8642

Max Priority Queue

Page 17: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

After Fifth Remove Max Operation

Sorted Array

86421

Max Priority Queue

Page 18: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

18

Implementing a priority queue

•Using a sorted Array•Using a sorted Chain•Using a heap

Page 19: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

19

Heap properties• heap: a tree with the following two properties:

– 1. completenesscomplete tree: every level is full except possibly the lowest level, which must be filled from left to right with no leaves to the right of a missing node (i.e., a node may not have any children until all of its possible siblings exist)

Heap shape:

Page 20: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

20

Heap properties 2– 2. heap ordering

a tree has heap ordering (min heap) if P < X for every element X with parent P• in other words, in heaps, parents' element values are

always smaller than those of their children• implies that minimum element is always the root

Page 21: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

21

Which are min-heaps?

1530

8020

10

996040

8020

10

50 700

85

996040

8020

10

50 700

85 996040

8010

20

50 700

85

6040

8020

10

996040

8020

10

wrong! wrong!

wrong!

wrong!

Page 22: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

22

24

7 3

30

10 40

30

80

2510

48

21

14

10 17

33

91828

11

22

3530

50

30

10 20

wrong!

wrong!

Which are max-heaps?

Page 23: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Heap Operations

• Adding to a heap (Min)• Remove from a heap• Build a heap (turn any input into a heap)

Page 24: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

24

Adding to a heap• when an element is added to a heap, it

should be initially placed as the rightmost leaf (to maintain the completeness property)– heap ordering property becomes broken!

996040

8020

10

50 700

85

65

996040

8020

10

50 700

85

65 15

Page 25: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

25

Adding to a heap, cont'd.• to restore heap ordering property, the newly added

element must be shifted upward ("bubbled up") until it reaches its proper place– bubble up by swapping with parent

996040

8020

10

50 700

85

65 15

992040

8015

10

50 700

85

65 60

Page 26: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

• The new element is added and placed as the rightmost leaf (completeness property)

• The new element is bubbled up by swapping with parent (heap ordering property)

Page 27: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

• Draw the state of the min-heap tree after adding the following elements to it:

6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

Page 28: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

28

Adding to a max-heap

16

5 11

3 18

16

18 11

3 5

18

16 11

3 5

• same operations, but must bubble up larger values to top

Page 29: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

• Draw the state of the max-heap tree after adding the following elements to it:

6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

Page 30: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

30

The top operation• Top on a min-heap is trivial; because of the heap

properties, the minimum element is always the root– Top on a max-heap, but would return you the maximum

element and not the minimum one

996040

8020

10

50 700

85

65

Page 31: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

31

Removing from a min-heap• min-heaps only support remove of the min element (the

root)– must remove the root while maintaining heap completeness and

ordering properties– intuitively, the last leaf must disappear to keep it a heap– initially, just swap root with last leaf (we'll fix it)

996040

8020

10

700 50

85

65

996040

8020

65

700 50

85

65

Page 32: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

32

Removing from heap, cont'd.• must fix heap-ordering property; root is out of order

– shift the root downward ("bubble down") until it's in place– swap it with its smaller child each time

• What happens if we don't always swap with the smaller child?

996040

8020

65

700 50

85 996050

8040

20

700 65

85

Page 33: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

• Swap the last leaf with root and Remove the root (completeness property)

• The new root is bubbled down by swapping with his min/max child (heap ordering property)

Page 34: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

34

Heap practice problem

• Draw the state of the min-heap tree after adding the following elements to it:

6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

• Show the state of the heap after remove() has been executed on it 3 times, and state which elements are returned by the removal.

Page 35: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

35

Turning any input into a heap• we can quickly turn any complete tree of comparable

elements into a heap with a buildHeap algorithm• simply perform a "bubble down" operation on every node

that is not a leaf, starting from the rightmost internal node and working back to the root

66014

1821

45

32 456021

1814

6

32

Page 36: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing/building A Max Heap

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

8

4

7

6 7

8 9

3

710

1

11

5

2

Page 37: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

Start at rightmost array position that has a child.

8

4

7

6 7

8 9

3

710

1

11

5

2

Index is n/2.

Page 38: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

Move to next lower array position.

8

4

7

6 7

8 9

3

710

1

5

11

2

Page 39: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

4

7

6 7

8 9

3

710

1

5

11

2

Page 40: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Page 41: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Page 42: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Page 43: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Page 44: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

Find a home for 2.

Page 45: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

75

1

11

Find a home for 2.

10

Page 46: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

Done, move to next lower array position.

10

5

Page 47: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

10

5

Find home for 1.

Page 48: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

11

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

10

5

Find home for 1.

Page 49: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.

Page 50: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.

Page 51: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initializing A Max Heap

Done.

8

9

7

6 3

8 4

7

72

11

10

5

1

Page 52: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

• Put input array to an complete binary tree• Start at rightmost array position that has a

child. (index = n/2), bubbled down until it reaches the proper position

• Repeat until root

Page 53: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

practice

• Turn the array : (6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2) to a min heap and a max heap

Page 54: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Implement a priority queue using heap

Lab 5

Page 55: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority
Page 56: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

constructor

template<class T>

maxHeap<T>::maxHeap(int initialCapacity)

{// Constructor.

if (initialCapacity < 1)

{ostringstream s;

s << "Initial capacity = " << initialCapacity << " Must be > 0";

throw illegalParameterValue(s.str());

}

arrayLength = initialCapacity + 1;

heap = new T[arrayLength];

heapSize = 0;

}

Page 57: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Initialize(T* theHeap, int theSize)

Page 58: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Push(theElement)

Page 59: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Pop()

Page 60: Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority

Lab 5• Implement

– initialize(); Push(); Pop()

• Test your code– Push the following element to a maxHeap in order: 6,

50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2– Show the state of the maxheap after each push

operation– Show the state of the maxheap after each Pop

operation.– turn the array to a maxheap and show the state of the

maxheap.