Priority queue heap
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Priority Queue (Heap) PowerPoint PPT Presentation


  • 58 Views
  • Uploaded on
  • Presentation posted in: General

Priority Queue (Heap). A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority) Example Applications: printer -> print (dequeue) the shortest document first

Download Presentation

Priority Queue (Heap)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Priority queue heap

Priority Queue (Heap)

  • A kind of queue

  • Dequeue gets element with the highest priority

  • Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority)

  • Example Applications:

    • printer -> print (dequeue) the shortest document first

    • operating system -> run (dequeue) the shortest job first

    • normal queue -> dequeue the first enqueued element first

Source: Muangsin / Weiss


Priority queue heap operations

Priority Queue (Heap) Operations

Priority Queue

  • insert (enqueue)

  • deleteMin (dequeue)

    • smaller value higher priority

    • Find / save the minimum element, delete it from structure and return it

deleteMin

insert

Source: Muangsin / Weiss


Implementation using linked list

Implementation using Linked List

  • Unsorted linked list

    • insert takes O(1) time

    • deleteMin takes O(N) time

  • Sorted linked list

    • insert takes O(N) time

    • deleteMin takes O(1) time

Source: Muangsin / Weiss


Implementation using binary search tree

Implementation using Binary Search Tree

  • insert takes O(log N) time

  • deleteMin takes O(log N) time

  • support other operations that are not required by priority queue (for example, findMax)

  • deleteMin operations make the tree unbalanced

Source: Muangsin / Weiss


Terminology full tree

C

G

F

B

E

D

A

J

H

I

Terminology: full tree

  • completely filled (bottom level is filled from left to right

  • size between 2h(bottom level has only one node) and 2h+1-1

Source: Muangsin / Weiss


Heap order property for minimum heap

16

68

19

21

31

24

13

32

65

26

Heap: Order Property (for Minimum Heap)

  • Any node is smaller than (or equal to) all of its children (any subtree is a heap)

  • Smallest element is at the root (findMin take O(1) time)

Source: Muangsin / Weiss


Insert

24

19

68

16

68

19

21

31

16

21

24

32

26

65

13

13

65

26

32

31

Insert

  • Create a hole in the next available location

  • Move the hole up (swap with its parent) until data can be placed in the hole without violating the heap order property (called percolate up)

Source: Muangsin / Weiss


Insert1

24

16

68

19

21

31

21

24

19

68

16

26

13

13

32

26

65

32

65

31

Insert

insert14

Percolate Up -> move the place to put 14 up

(move its parent down) until its parent <= 14

Source: Muangsin / Weiss


Insert2

14

68

19

21

31

24

16

21

19

68

16

24

31

65

32

26

26

65

32

13

13

Insert

Source: Muangsin / Weiss


Deletemin

19

14

19

16

31

21

14

68

16

68

19

21

19

65

26

26

13

32

32

65

31

deleteMin

  • Create a hole at the root

  • Move the hole down (swap with the smaller one of its children) until the last element of the heap can be placed in the hole without violating the heap order property (called percolate down)

Source: Muangsin / Weiss


Deletemin1

14

68

14

19

21

19

16

21

19

68

16

19

31

26

13

32

65

32

65

26

31

deleteMin

Percolate Down -> move the place to put 31 down (move its smaller child up) until its children >= 31

Source: Muangsin / Weiss


Deletemin2

14

16

19

21

19

19

68

16

68

19

21

14

32

26

65

26

32

65

31

31

deleteMin

Source: Muangsin / Weiss


Deletemin3

14

68

16

14

19

19

26

21

21

19

68

16

19

32

31

32

26

65

65

31

deleteMin

Source: Muangsin / Weiss


Running time

Running Time

  • insert

    • worst case: takes O(log N) time, moves an element from the bottom to the top

    • on average: takes a constant time (2.607 comparisons), moves an element up 1.607 levels

  • deleteMin

    • worst case: takes O(log N) time

    • on average: takes O(log N) time (element that is placed at the root is large, so it is percolated almost to the bottom)

Source: Muangsin / Weiss


Array implementation of binary heap

C

G

F

B

E

D

I

H

A

J

A

B

C

D

E

F

G

H

I

J

0

1

2

3

4

5

6

7

8

9

10

11

12

13

Array Implementation of Binary Heap

left child is in position 2i

right child is in position (2i+1)

parent is in position i/2

Source: Muangsin / Weiss


  • Login