+ All Categories

Download - Priority Queue

Transcript
Page 1: Priority Queue

Priority Queue

What is that?Implementation with linked list with O(n) behaviour

The Heap (O(log(n))An implementation using an array

Your mission …

Page 2: Priority Queue

Q.insert(e)e = Q.removeMin()Q.size()Q.isEmpty()Q.min()

Priority queue

• Store a collection of prioritized elements• Elements are comparable

• Allow insertion of an element• Can only remove the element with highest priority

• Comes first in order

We present 3 implementations

Page 3: Priority Queue

Priority queue

Example applications of a priority queue• Dispatching processes in a computer• Hospital waiting lists• Standby passengers for a flight• Queuing at call centres• …

Page 4: Priority Queue

• Store a collection of prioritized elements• Elements are comparable

• Allow insertion of an element• Can only remove the element with highest priority

• Comes first in order

Priority queueHow can we compare objects?

Two examples on comparing things … …

Page 5: Priority Queue

An exampleHow can we compare objects?

This is a Vertex

Page 6: Priority Queue

An exampleHow can we compare objects?

This is a Comparator for vertices

Page 7: Priority Queue

An exampleHow can we compare objects?

Using the comparator to sort an array of Vertex

Page 8: Priority Queue

An exampleHow can we compare objects?

Another example: a Car

Page 9: Priority Queue

An exampleHow can we compare objects?

This is a CarComparator

Page 10: Priority Queue

An exampleHow can we compare objects?

Using the CarComparator

Page 11: Priority Queue

unsorted list

We might use a linked list• To insert we add to the front of the list• To find the minimum we must iterate over entire the list• To remove the minimum we must find the minimum and remove it• Maintain a counter of number of elements in the list

Method Time

size O(1)

isEmpty O(1)

insert O(1)

removeMin O(n)

min O(n)

Implementing a priority queue with an unsorted list

Page 12: Priority Queue

sorted list

We might use a linked list• The list is maintained in non-decreasing order• To insert we scan to find position and splice in (see below)• To find the minimum we deliver the first element in the list• To remove the minimum we return and remove the first element

Implementing a priority queue with an sorted list

Method Time

size O(1)

isEmpty O(1)

insert O(n)

removeMin O(1)

min O(1)

Page 13: Priority Queue

An alternative the heap

Page 14: Priority Queue

heap

• a heap H is a binary tree

Page 15: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree

Page 16: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree

Fill up level d before moving to level d+1• each level but the last must be full• in last level fill from left to right

Page 17: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree

Fill up level d before moving to level d+1• each level but the last must be full• in last level fill from left to right

Page 18: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree

Fill up level d before moving to level d+1• each level but the last must be full• in last level fill from left to right

Page 19: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree

Fill up level d before moving to level d+1• each level but the last must be full• in last level fill from left to right

Not a heap!

Page 20: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree• heap order property is maintained

Page 21: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree• heap order property is maintained

Given a node v (not the root) • the parent of v is less than or equal to v

Page 22: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree• heap order property is maintained

4

5

915

6

207

Given a node v (not the root) • the parent of v is less than or equal to v

Page 23: Priority Queue

heap

• a heap H is a binary tree • H is a complete binary tree• heap order property is maintained

4

5

915

6

207

Given a node v (not the root) • the parent of v is less than or equal to v

A heap H with n nodes has height O(log(n))

Page 24: Priority Queue

Example: adding to a heap

Page 25: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11

20

Page 26: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11

20

Insert 8

Page 27: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

Insert 8

Page 28: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

Insert 8 8 is greater than parent (7) ... done

Page 29: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

Page 30: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

Insert 2

Page 31: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

2

Insert 2

Page 32: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

2

Insert 2 2 is less than parent 20

Page 33: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

20

2

Insert 2 2 is less than parent 20 ... swap!

Page 34: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

2

20

Insert 2

Page 35: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

2

20

Insert 2

Page 36: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

2

20

Insert 2 2 is less than parent 6

Page 37: Priority Queue

heapExample: adding to a heap

4

5 6

15

16 25

9

14 12

7

11 8

2

20

Insert 2 2 is less than parent 6 ... swap!

Page 38: Priority Queue

heapExample: adding to a heap

4

5 2

15

16 25

9

14 12

7

11 8

6

20

Insert 2 2 is less than parent 6 ... swap!

Page 39: Priority Queue

heapExample: adding to a heap

4

5 2

15

16 25

9

14 12

7

11 8

6

20

Insert 2

Page 40: Priority Queue

heapExample: adding to a heap

4

5 2

15

16 25

9

14 12

7

11 8

6

20

Insert 2 2 is less than parent 4

Page 41: Priority Queue

heapExample: adding to a heap

4

5 2

15

16 25

9

14 12

7

11 8

6

20

Insert 2 2 is less than parent 4 ... swap!

Page 42: Priority Queue

heapExample: adding to a heap

2

5 4

15

16 25

9

14 12

7

11 8

6

20

Insert 2 2 is less than parent 4 ... swap!

Page 43: Priority Queue

heapExample: adding to a heap

2

5 4

15

16 25

9

14 12

7

11 8

6

20

Insert 2

Page 44: Priority Queue

heapExample: adding to a heap

2

5 4

15

16 25

9

14 12

7

11 8

6

20

Insert 2 Done!

Page 45: Priority Queue

heapExample: adding to a heap

2

5 4

15

16 25

9

14 12

7

11 8

6

20

Page 46: Priority Queue

Example: removal from a heap

NOTE: it is a heap in a different state

Page 47: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Page 48: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Save off top of heap

Page 49: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Save off top of heap

4

Page 50: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Copy last item in heap to top of heap

4

Page 51: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Copy last item in heap to top of heap

4

Page 52: Priority Queue

heapExample: removal from a heap

4

5 6

15

16 25

9

14 12

7

11 13

20

Copy last item in heap to top of heap

4

Page 53: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11 13

20

Copy last item in heap to top of heap

4

Page 54: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11 13

20

Delete last item in heap

4

Page 55: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11

20

4

Page 56: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11

20

Compare current node with its children

4

Page 57: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11

20

Compare current node with its children

4

Page 58: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Page 59: Priority Queue

heapExample: removal from a heap

13

5 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Page 60: Priority Queue

heapExample: removal from a heap

5

13 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Page 61: Priority Queue

heapExample: removal from a heap

5

13 6

15

16 25

9

14 12

7

11

20

4

Page 62: Priority Queue

heapExample: removal from a heap

5

13 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Compare current node with its children

Page 63: Priority Queue

heapExample: removal from a heap

5

13 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Page 64: Priority Queue

heapExample: removal from a heap

5

13 6

15

16 25

9

14 12

7

11

20

If greater then swap with smallest child

4

Page 65: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

If greater then swap with smallest child

4

Page 66: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

4

Page 67: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

4

Compare current node with its children

Page 68: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

4

If greater then swap with smallest child

Page 69: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

4

If greater then swap with smallest child

Page 70: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

13

14 12

7

11

20

4

If greater then swap with smallest child

Page 71: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

12

14 13

7

11

20

4

If greater then swap with smallest child

Page 72: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

12

14 13

7

11

20

4

Page 73: Priority Queue

4

heapExample: removal from a heap

5

9 6

15

16 25

12

14 13

7

11

20

Return result

Page 74: Priority Queue

heapExample: removal from a heap

5

9 6

15

16 25

12

14 13

7

11

20

Done

Page 75: Priority Queue

upheap bubbling: when we add to the heap

downheap bubbling: when we remove from the heap

What we just saw

Add and remove are O(log(n)) processes

Page 76: Priority Queue

An implementation of a Heap data structure

Page 77: Priority Queue

An implementation of a Heap data structure

Page 78: Priority Queue

An implementation of a Heap data structureNumber the vertices as follows

Page 79: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Page 80: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Note: parent of node i is i/2 ....

Page 81: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Note: parent of node i is i/2 ....

Page 82: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Note: parent of node i is i/2 ....

Page 83: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Note: left child of i is i×2

Page 84: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Number the vertices as follows

Note: right child of i is (i×2) +1

Page 85: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Represent as a one dimensional array

Page 86: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Represent as a one dimensional array S

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15S

Page 87: Priority Queue

An implementation of a Heap data structure

1

2 3

4

8 9

5

10 11

6

12 13

7

14 15

Represent as a one dimensional array S

**

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15S

To simplify implementation we do not use S[0]

Page 88: Priority Queue

An implementation of a Heap data structure

**

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Require two integer variables, last and capacity where last is initially 0In our example capacity is 15

Represent as a one dimensional array S

S

Page 89: Priority Queue

An implementation of a Heap data structure

**

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Represent as a one dimensional array S

S

last: 0capacity: 15

Page 90: Priority Queue

An implementation of a Heap data structure

Consider the following heap H

Page 91: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55

16

Consider the following heap H

S

last: 12capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 92: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

H.add(6)

Page 93: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55 6

16

S

last: 12capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

S[last+1] = 6

Page 94: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55 6

16

S

last: 13capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

last++

Page 95: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55 6

16

S

last: 13capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(13)

Page 96: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55 6

16

S

last: 13capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(13)

Page 97: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

50

55 6

16

S

last: 13capacity: 15

** 4 9 8 17 26 50 16 19 69 32 93 55 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(13)S[6] > S[13] ?

Page 98: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

6

55 50

16

S

last: 13capacity: 15

** 4 9 8 17 26 6 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(13)swap S[6] S[13]

Page 99: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

6

55 50

16

S

last: 13capacity: 15

** 4 9 8 17 26 6 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 100: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

6

55 50

16

S

last: 13capacity: 15

** 4 9 8 17 26 6 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(6)

Page 101: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

6

55 50

16

S

last: 13capacity: 15

** 4 9 8 17 26 6 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(6)

Page 102: Priority Queue

An implementation of a Heap data structure

4

9 8

17

19 69

26

32 93

6

55 50

16

S

last: 13capacity: 15

** 4 9 8 17 26 6 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(6)S[3] > S[6] ?

Page 103: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(6)swap S[3] S[6]

Page 104: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 105: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(3)

Page 106: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(3)

Page 107: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

upheapBubble(3)S[1] > S[3] ?

Page 108: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Done!

Page 109: Priority Queue

Removal from the heap H

Page 110: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

H.remove()

Page 111: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Save S[1]

4

Page 112: Priority Queue

An implementation of a Heap data structure

4

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 4 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

S[1] = S[last]

4

Page 113: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55 50

16

S

last: 13capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55 50

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

S[1] = S[last]

4

Page 114: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

last--

4

Page 115: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)

Page 116: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)findMin(S[2],S[3])

Page 117: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)findMin(S[2],S[3])

Page 118: Priority Queue

An implementation of a Heap data structure

50

9 6

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 50 9 6 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)swap(S[1],S[3])

Page 119: Priority Queue

An implementation of a Heap data structure

6

9 50

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 6 9 50 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)swap(S[1],S[3])

Page 120: Priority Queue

An implementation of a Heap data structure

6

9 50

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 6 9 50 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(1)

Page 121: Priority Queue

An implementation of a Heap data structure

6

9 50

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 6 9 50 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)findMin(S[6],S[7])

Page 122: Priority Queue

An implementation of a Heap data structure

6

9 50

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 6 9 50 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)findMin(S[6],S[7])

Page 123: Priority Queue

An implementation of a Heap data structure

6

9 50

17

19 69

26

32 93

8

55

16

S

last: 12capacity: 15

** 6 9 50 17 26 8 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)swap(S[3],S[6])

Page 124: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)swap(S[3],S[6])

Page 125: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)

Page 126: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)findMin(S[12])

Page 127: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)No swap

Page 128: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

4

downHeapBubble(3)No swap

Page 129: Priority Queue

An implementation of a Heap data structure

6

9 8

17

19 69

26

32 93

50

55

16

S

last: 12capacity: 15

** 6 9 8 17 26 50 16 19 69 32 93 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Return result

4

Page 130: Priority Queue

Method Time

size O(1)

isEmpty O(1)

insert O(log(n))

removeMin O(log(n))

min O(1)

heap

Page 131: Priority Queue

Have a look at priority queue as given in Java distribution

Page 132: Priority Queue
Page 133: Priority Queue

… based on a priority heap

Page 134: Priority Queue

Different method names … add rather than insert

Page 136: Priority Queue

fin


Top Related