lecture 8 : priority queue bong-soo sohn assistant professor school of computer science and...

18
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Upload: lora-richard

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Lecture 8 : Priority Queue

Bong-Soo Sohn

Assistant ProfessorSchool of Computer Science and Engineering

Chung-Ang University

Page 2: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Priority Queues

An efficient ADT to keep a dynamic set S of elements x to support the following operations : getMax(S) : return the element with the highest priority extractMax(S) : remove and return the element with

the highest priority insert(x,S) : insert element x into S increaseKey(S,x,k) – increase x’s priority to k

Priority queues are implemented using a heap, which is a tree structure with special properties.

Page 3: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Heap a tree that satisfies the heap property

Heap property If B is a child node of A, then key(A)>=key(B) can be reverse (max heap , min heap)

Heaps are used to implement priority queue

Efficiency of heap operations is crucial in some graph algorithms

Page 4: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Binary Heap

Has following properties shape property

all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.

heap property

<ex. Binary max heap>

Page 5: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Operations (in max heap)

Insert(x,S)1. Add the element on the bottom level of the

heap2. Compare the added element with its parent;

if they are in the correct order, stop3. If not, swap the element with its parent and

return to the previous step.

Page 6: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Operations (in max heap)

getMax(S) : trivial - O(1) extractMax(S) : O(lg n) – tree height

1. Replace root node with the last element on the last level

2. repeat swapping parent-child until heap property becomes valid

Page 7: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Building a Heap

O(n lg n) : A heap can be built by successive insertions - not optimal

O(n) algorithm randomly putting the elements on a binary tree

if all the subtrees starting at some height h (measured from the bottom) have already been "heapified", the trees at depth h+1 can be heapified by sending their root down, along the path of maximum children

Page 8: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: building a heap

4 1 3 2 16 9 10 14 8 7 1 2 3 4 5 6 7 8 9 10

4

1

2 16

7814

3

9 10

1

2 3

4 5 6 7

8 9 10

Page 9: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Sorting with heaps: Heap-Sort

We can use the heap structure to sort an array A[i] of n elements in place:

Since the maximum element of the array is its first element, A[1], it can be put in its correct final position at the end of the array, in A[n].

We can now recursively fix the heap of the sub-tree rooted at node 1 and containing n – 1 elements with Max-Heapify until two elements are left.

Each call to Max-Heapify takes O(lg n), and it is called once for each element in the array, so the running time is O(n lg n) always (best, average, and worst case) with O(n) space.

Page 10: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

10

Heap-Sort

Heap-Sort(A)1. Build-Max-Heap(A) 2. heapsize[A] length(A) 3. for i length[A] downto 2 4. do Exchange(A[1],A[i]) 5. heapsize[A] length(A) –1

6. Max-Heapify(A,1)

put maximum at the root

Page 11: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort

16

14

8 7

142

10

9 3

1

2 3

4 5 6 7

8 9 10

16 14 10 8 7 9 3 2 4 1

Page 12: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (2)

14

8

4 7

1612

10

9 3

1

2 3

4 5 6 7

8 9 10

Page 13: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (3)

10

8

4 7

16142

9

1 3

1

2 3

4 5 6 7

8 9 10

Page 14: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (4)

9

8

4 7

161410

3

1 2

1

2 3

4 5 6 7

8 9 10

Page 15: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (5)

7

4

1 2

161410

3

8 9

1

2 3

4 5 6 7

8 9 10

Page 16: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (6)

4

2

1 7

161410

3

8 9

1

2 3

4 5 6 7

8 9 10

Page 17: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (7)

1

2

4 7

161410

3

8 9

1

2 3

4 5 6 7

8 9 10

Page 18: Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Example: Heap-Sort (8)

1

2

4 7

161410

3

8 9

1

2 3

4 5 6 7

8 9 10

1 2 3 4 7 8 9 10 14 16