• Tidak ada hasil yang ditemukan

Priority Queue - Heap

N/A
N/A
Protected

Academic year: 2024

Membagikan "Priority Queue - Heap"

Copied!
12
0
0

Teks penuh

(1)

Struktur Data & Algoritme ( Data Structures & Algorithms )

Denny (

[email protected]

) Suryana Setiawan (

[email protected]

)

Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2004/2005

Version 2.0 - Internal Use Only

Priority Queue - Heap

SDA/HEAP-PQ/V2.0/2

A

B C

D E F G

H I J

A B C D E F G H I J

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

Review

„

Complete binary tree:

„

the tree is completely filled, with possible exception of the bottom level, which is filled from left to right.

Objectives

„

understand the definition and operations of a priority queue

„

understand binary heap data structure

Outline

„

Priority Queue

„

Definition

„

Operations

„

Binary Heap

„

Operations

„

Properties

„

Representation

„

Heap Sort

(2)

SDA/HEAP-PQ/V2.0/5

A Priority Queue

„

A special kind of queue that features slightly different rules:

„

enqueue operation doesn’t always add new items to the rear of the queue.

„

Instead, it insert each new item into the queue as determined by its priority.

SDA/HEAP-PQ/V2.0/6

Binary Heap

„

Access allowed to the minimum item only.

„

Basic operations:

„

insert a new item in logarithmic worst-case time

„

delete the minimum item in logarithmic worst-case time

„

find the minimum item in constant time

SDA/HEAP-PQ/V2.0/7

„

Structure Property

„

Data stored in complete binary tree

⇒the tree is balanced

⇒all operations are guaranteed O(log n) worst case

⇒can be stored in array or java.util.Vector

„

Ordering Property

„

Heap Order:

• Parent ≤Child

Properties

P

X

P ≤ X

SDA/HEAP-PQ/V2.0/8

1 2

2 3

1 2 3

2

1 3

2 2

1 2

2 3

13

21 16

6 31 19 68

65 26 32

Mana yang Binary Heap?

(3)

SDA/HEAP-PQ/V2.0/9

„ Root at location 0

„ Left child of i is at location 2i + 1

„ Right child of i is at location 2i + 2 = 2(i + 1)

„ Parent of i is at location (i - 1) / 2

-1

0 1

43 3 3 2

65 58 40 42 4

0 1 43 3 3 2 65 58 40

-1 42 4

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

Heap Representation

SDA/HEAP-PQ/V2.0/10

40 42 -1

0 1

43 5 3 8

65 58 4 2

0 1 43 5 3 8 65 58 40

-1 42 4

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

2 3

1 -1

Insertion

„

Insert 2 (Percolate Up)

40 42 -1

0 1

43 5 2 8

65 58 4 3

0 1 43 5 3 8 65 58 40

-1 42 4

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

3 2

1 -1

Insertion

„

Insert 2 (Percolate Up)

„

Insert 14

Insertion

14 13

21 16

24 31 19 68

65 26 32

(4)

SDA/HEAP-PQ/V2.0/13

„

Insert 14

Insertion

31 13

21 16

24 14 19 68

65 26 32

SDA/HEAP-PQ/V2.0/14

„

Insert 14

Insertion

31 13

14 16

24 21 19 68

65 26 32

SDA/HEAP-PQ/V2.0/15

0 1 43 3 3 2 65 58 40

-1 42 4

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

-1

0 1

43 3 3 2

65 58 40 42 4

14

Delete Minimum

„

Percolate Down

SDA/HEAP-PQ/V2.0/16

0 1 43 3 3 2 65 58 40

4 42

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

4

0 1

43 3 3 2

65 58 40 42

14

Delete Minimum

(5)

SDA/HEAP-PQ/V2.0/17

3 1 43 4 3 2 65 58 40

0 42

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

0

3 1

43 4 3 2

65 58 40 42

14

Delete Minimum: Completed

SDA/HEAP-PQ/V2.0/18

13

14 16

19 21 19 68

65 26 32 31

Delete Min (2)

14 16

19 21 19 68

65 26 32 31

Delete Min (2)

„

Percolate Down

Delete Min (2)

14

16

19 21 19 68

65 26 32 31

(6)

SDA/HEAP-PQ/V2.0/21

14

19 16

21 19 68

65 26 32 31

Delete Min (2)

SDA/HEAP-PQ/V2.0/22

Delete Min (2)

14

19 16

26 21 19 68

65 32 31

SDA/HEAP-PQ/V2.0/23

Delete Min (2)

14

19 16

26 21 19 68

65 31 32

SDA/HEAP-PQ/V2.0/24

Heap

„

Operation sequence:

„

Insert

• 40 20 5 55 76 31 3

„

Delete Min

„

Delete Min

„

Insert

• 10 22

„

Delete Min

„

Delete Min

(7)

SDA/HEAP-PQ/V2.0/25

Heap Constructor

public class VectorHeap implements PriorityQueue {

protected Vector data;

public VectorHeap() { data = new Vector(); } public VectorHeap(Vector v) {

int i;

data = new Vector(v.size());

// we know ultimate size

for (i = 0; i < v.size(); i++) { // add elements to heap

add((Comparable) v.elementAt(i));

} }

SDA/HEAP-PQ/V2.0/26

Heap’s Methods

protected static int parentOf(int i) { return (i - 1) / 2;

}

protected static int leftChildOf(int i) { return 2 * i + 1;

}

protected static int rightChildOf(int i) { return 2 * (i + 1);

}

public Comparable peek() { // findMin

return (Comparable) data.elementAt(0);

}

Removal & Insertion

public Comparable remove() {

Comparable minVal = peek();

data.setElementAt (

data.elementAt (data.size() - 1), 0);

data.setSize (data.size() - 1);

if (data.size() > 1) pushDownRoot(0);

return minVal;

}

public void add(Comparable value) { data.addElement (value);

percolateUp (data.size() - 1);

}

Percolate Down

protected void pushDownRoot (int root) {

int heapSize = data.size();

Comparable value = (Comparable) data.elementAt (root);

while (root < heapSize) {

int childpos = leftChildOf(root);

if (childpos < heapSize) { // choose the bigger child

if ((rightChildOf(root) < heapSize) &&

(((Comparable) (data.elementAt(

childpos+1))).compareTo ((Comparable) (data.elementAt(

childpos))) < 0)) {

childpos++;

}

(8)

SDA/HEAP-PQ/V2.0/29

Percolate Down

if (((Comparable)(data.elementAt(

childpos))).compareTo (value) < 0) {

data.setElementAt (

data.elementAt(childpos), root);

root = childpos; // keep moving down } else { // found right location

data.setElementAt(value, root);

return;

}

} else { // at a leaf! insert and halt data.setElementAt (value, root);

return;

} }

}

SDA/HEAP-PQ/V2.0/30

Percolate Up

protected void percolateUp (int leaf) {

int parent = parentOf(leaf);

Comparable value =

(Comparable)(data.elementAt(leaf));

while (leaf > 0 && (value.compareTo

((Comparable)(data.elementAt(parent)))

< 0)) {

data.setElementAt (data.elementAt (parent), leaf);

leaf = parent;

parent = parentOf(leaf);

}

data.setElementAt(value,leaf);

}

SDA/HEAP-PQ/V2.0/31

Heap’s Methods

public boolean isEmpty() {

return data.size() == 0;

}

public int size() {

return data.size();

}

public void clear() {

data.clear();

}

public String toString() {

return "<VectorHeap: " + data + ">";

}

SDA/HEAP-PQ/V2.0/32

Fix Heap / Heapify

„

The fixHeap operation takes a complete tree that does not have heap order and reinstates it.

„

N insertions could be done in O(n log n)

„

We could make a fix heap that takes O(n) time

20

61 17

12

55 37

45

25 64

63

83 73

92

47 21

(9)

SDA/HEAP-PQ/V2.0/33

Fix Heap / Heapify

„

Percolate down 6

20

61 17

12

55 37

45

25 64

63

83 73

92

47 21

SDA/HEAP-PQ/V2.0/34

Fix Heap / Heapify

„

Percolate down 5

20

61 17

12

55 37

45

25 64

63

83 73

92

47 21

25 45

Fix Heap / Heapify

„

Percolate down 4

20

61 17

12

55 37

25

45 64

63

83 73

92

47 21

Fix Heap / Heapify

„

Percolate down 3

20

61 17

12

55 37

25

45 64

63

83 73

92

47 21

20

17

(10)

SDA/HEAP-PQ/V2.0/37

Fix Heap / Heapify

„

Percolate down 2

17

61 20

12

55 37

25

45 64

63

83 73

92

47 21

SDA/HEAP-PQ/V2.0/38

Fix Heap / Heapify

„

Percolate down 1

17

61 20

12

55 37

25

45 64

63

83 73

92

47 21

12 37

47

SDA/HEAP-PQ/V2.0/39

Fix Heap / Heapify

„

Percolate down 0

17

61 20

37

55 47

25

45 64

63

83 73

92

12 21

17 20

92

12

SDA/HEAP-PQ/V2.0/40

97

53 59

26 41 58 31

16 21 36

53 59 26 41 58 31 16 21 36 97

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

Max Heap

(11)

SDA/HEAP-PQ/V2.0/41

59

53 58

26 41 36 31

16 21

53 58 26 41 36 31 16 21 97 59

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

97

Heap after the first deleteMax

SDA/HEAP-PQ/V2.0/42

58

53 36

26 41 21 31

16

53 36 26 41 21 31 16 59 97 58

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

97 59

Heap after second deleteMax

Heap Sort

1.

Create a heap tree

2.

Remove the root elements from the heap one at a time, recomposing the heap

Summary

„

Priority queue can be implemented using binary heap

„

Properties of binary heap

„

structure property

• complete binary tree

„

ordering property

• Heap order: Parent ≤Child

„

Operations of binary heap

„

insertion: takes O(log n) time in worst case

„

find min: takes O(1) time

„

delete min: takes O(log n) time in worst case

„

Binary heap can be used for sorting

(12)

SDA/HEAP-PQ/V2.0/45

Further Reading

„

Chapter 20

„

Programming Pearls. Column 14: Heaps

„

http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/resources/others/cs016/PriorityQueuesHe ap2x2.pdf

SDA/HEAP-PQ/V2.0/46

What’s Next

„

Graph

Referensi

Dokumen terkait

The results of FGD activity concluded that there were only 5 five elements that needed to be followed up as a research instrument to determine the priority elements considered by

3.2 Analysis Summary Cartesian quadrant and PGCV index of both IPA and PGCV methods are used in the combinations to determine the improvement priority ranking based on the potential

Indeed, later research demonstrates that PEG’s solubility properties can be used to remove ruthenium form olefin metathesis product mixtures.18,32 Summary This chapter described the