36
Datastructuren Datastructuren Data Structures Fenia Aivaloglou Hendrik Jan Hoogeboom Informatica – LIACS Universiteit Leiden najaar 2019

Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

DatastructurenData Structures

Fenia AivaloglouHendrik Jan Hoogeboom

Informatica – LIACSUniversiteit Leiden

najaar 2019

Page 2: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Contents

5 Priority QueuesADT Priority QueueBinary HeapLeftist heapsPairing Heap (niet)Double-ended Priority Queues

Page 3: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Abstract Data Structures

ADT – what, not how

Definition

An abstract data structure (ADT) is a specification of the valuesstored in the data structure as well as the description andsignatures of the operations that can be performed.

no representation or implementation in ADT“mathematical model”

Page 4: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Abstract Data Structures

stl container classes

helper: pair

sequences: contiguous: array (fixed length),vector (flexible length), deque (double ended),linked: forward list (single), list (double)

adaptors: based on one of the sequences:stack (lifo), queue (fifo),based on binary heap: priority queue

associative: based on balanced trees:set, map, multiset, multimap

unordered: based on hash table:unordered set, unordered map,unordered multiset,unordered multimap

Page 5: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Abstract Data Structures

STL priority queueclass Comp {

public:

int operator() ( const paar& p1, const paar& p2 ) {

return p1.second < p2.second;

}

};

int main() {

vector <paar> club // ’modern’ initialization

{ {"Jan", 1}, {"Piet", 6}, {"Katrien", 5}, {"Ramon", 2} };

using pqtype = priority_queue< paar, vector <paar>, Comp > ;

pqtype pq (club.begin(), club.end() );

// wow! converts into priority_queue

while ( !pq. empty() ) {

cout << pq.top().first << " (" << pq.top().second << ") ";

pq.pop();

}

return 0;

}

Piet (6) Katrien (5) Ramon (2) Jan (1)

Page 6: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

dictionary vs. priority queue

Both store a set of (key,value) pairs

{ (’Detra’,17), (’Nova’,84), (’Charlie’,22), (’Henry’,75), (’Elsa’,29) }

both:Insert(’Roxanne’,29)

dictionary:Delete(’Detra’)Find(’Elsa’) returns 29Set(’Henry’,76)

priority queue:FindMax() returns (’Nova’,84)DeleteMax()

Page 7: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

ADT dictionary / map / associative array

Stores a set of (key,value) pairs

Initialize, IsEmpty, Size

Insert: add (key,value) pair, provided key is not yet present

Delete: deletes (key,value) pair, given the key

Find: returns the value associated to a given key

Set: reassigns a new value to a (existing) given key

usually implemented as (balanced) binary serach tree,or hash table “unordered”

Page 8: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

ADT priority queue

Initialize: construct an empty queue.

IsEmpty: check whether there are any elements in the queue.

Size: returns the number of elements.

Insert: given a data element with its priority, it is added tothe queue

DeleteMax: returns a data element with maximal priority,and deletes it.

GetMax: returns a data element with maximal priority.

IncreaseKey: given an element with its position in thequeue it is assigned a higher priority.

Meld, or Union: takes two priority queues and returns anew priority queue containing the data elements from both.

Page 9: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

min & max queues

max-queue ≥

Initialize, IsEmpty, Size, Insert, DeleteMax, GetMax,IncreaseKey, Meld

min-queue ≤

Initialize, IsEmpty, Size, Insert, DeleteMin, GetMin,DecreaseKey, Meld

even opletten welke ordening

er staat vaak ook data (niet alleen prioriteit)

Page 10: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

priority queue - use cases

sorting (heapsort)

graph algorithms (Dijkstra shortest path, Prim’s algorithm)

compression (Huffman)

operating systems: task queue, print job queue

discrete event simulation

Page 11: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

ADT Priority Queue

implementations

Binary Leftist Pairing Fibonacci Brodal

GetMax Θ(1) Θ(1) Θ(1) Θ(1) Θ(1)Insert O(log n) Θ(log n) Θ(1) Θ(1) Θ(1)DeleteMax Θ(log n) Θ(log n) O(log n)† O(log n)† O(log n)

IncreaseKey Θ(log n) Θ(log n) O(log n)† Θ(1)† Θ(1)Meld Θ(n) Θ(log n) Θ(1) Θ(1) Θ(1)† amortized complexity

“. . . is based on heap ordered trees where [. . . ] nodes may violateheap order.” “The data structure presented is quite complicated.”

Page 12: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

binary search tree vs heap order

35

20

10

5 14

30

26

23

45

39 51

56

83

70

10

5 7

30

26

23

45

39 37

3

Page 13: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

representing binary tree with an array

root at index 1, left/right child i at index 2i/2i+1.

1

10 11

100 101 110 111

10001001101010111100

33

42 17

8 24 3 3

98 55 10 19 5

33

1

42

2

17

3

8

4

24

5

3

6

3

7

98

8

55

9

10

10

19

11

5

12

works well for complete binary treeswaste of space when ‘missing’ nodes

Page 14: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

binary heap: three levels

functioning: abstract (priority queue)

understanding: binary tree

implementation: array

internal operations (change key at position):bubble up, trickle down

“To add an element to a heap we must perform an up-heap operation(also known as bubble-up, percolate-up, sift-up, trickle-up, swim-up,heapify-up, or cascade-up), . . . ” What’s in a name? [Wikipedia]

Page 15: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

increasekey / bubble up

98

57 55

42 24 17 3

8 33 10 19 71 13

981

572

553

424

245

176

37

88

339

1010

1911

x

12

711313

98

57 71

42 24 55 3

8 33 10 19 17 13

981

572

713

424

245

556

37

88

339

1010

1911

1712

1313

BubbleUp : swap with parent until heap-ordered

Page 16: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

decreasekey / trickle down

37

57 55

42 24 17 3

8 33 10 19 5 13

x

1

37572

553

424

245

176

37

88

339

1010

1911

512

1313

57

42 55

37 24 17 3

8 33 10 19 5 13

571

422

553

374

245

176

37

88

339

1010

1911

512

1313

TrickleDown : swap with largest child until heap-ordered

Page 17: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

Insert to priority queue

98

57 55

42 24 17 3

8 33 10 19 5 13 29

981

572

553

424

245

176

37

88

339

1010

1911

512

1313 14

29

98

57 55

42 24 17 29

8 33 10 19 5 13 3

981

572

553

424

245

176

297

88

339

1010

1911

512

1313

314

Insert: add as last, BubbleUp

Page 18: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

DeleteMax from priority queue

98 98

57 55

42 24 17 3

8 33 10 19 5 13

981

13572

553

424

245

176

37

88

339

1010

1911

512

1313

x

57

42 55

33 24 17 3

8 13 10 19 5

571

422

553

334

245

176

37

88

139

1010

1911

512

DeleteMax: move last element to root, trickleDown

Page 19: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

heapify (1)

33

42 17

8 24 13 3

98 57 10 19 5 55

331

422

173

84

245

136

37

988

579

1010

1911

512

5513

33

42 17

98 24 55 3

8 57 10 19 5 13

331

422

173

984

245

556

37

88

579

1010

1911

512

1313

TrickleDown new key: swap with parent until heap-ordered

Page 20: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

heapify (2)

33

42 17

98 24 55 3

8 57 10 19 5 13

331

422

173

984

245

556

37

88

579

1010

1911

512

1313

33

98 55

57 24 17 3

8 42 10 19 5 13

331

982

553

574

245

176

37

88

429

1010

1911

512

1313

Page 21: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

heapify (3)

33

98 55

57 24 17 3

8 42 10 19 5 13

331

982

553

574

245

176

37

88

429

1010

1911

512

1313

98

57 55

42 24 17 3

8 33 10 19 5 13

981

572

553

424

245

176

37

88

339

1010

1911

512

1313

Page 22: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Binary Heap

complexity heapify

Lemma∑hd=0 d2d = (h− 1)2h+1 + 2

n levels, N = 2n − 1 keys

top-down∑n−1`=0 2`` = (n− 2)2n = N lgN (ongeveer)

bottom-up∑n−1`=0 2`(n− 1− `) =

∑n−1`=0 2`(n− 1) +

∑n−1`=0 2`` = 2n − n− 1

which is O(N)

Page 23: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

leftist heaps

“bladafstand”npl(x) nil path length, shortest distance to external leaf

Definition (Leftist tree)

An (extended) binary tree where for each internal node x,npl(left(x)) ≥ npl(right(x)).

Definition (Leftist heap)

A leftist tree where the priorities satisfy the heap order.

structure vs. node order

Page 24: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

leftist tree (structure)

npl(left(x)) ≥ npl(right(x))

3

2

1

1

2

1

1

2

1 1

2

1 1

1

3

2

2

2

1 1

1

1

1

1

2

1 1

1

Page 25: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

basic (internal) operation: ZIP

a b

T1 T2 T3 T4

︷ ︸︸ ︷Zipa

bT1

T2

T3 T4

︷ ︸︸ ︷Zipa ≥ b

Page 26: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

example (step 1: recursive Zipping)

38

37 25

29 10

35

31 32

28 30

Zip︷ ︸︸ ︷38

37

29

25

10

35

31 32

28 30

Zip︷ ︸︸ ︷ 38

37

29 25

10

35

31

28

32

30

Zip︷ ︸︸ ︷

Page 27: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

example (step 2: bottom-up swapping)

382

371

352

29 311

322

28 301

251

101

38

3735

293132

2830 25

10

Page 28: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

complexity

Lemma

Let T be a leftist tree with root v such that npl(v) = k, then(1) T contains at least 2k − 1 (internal) nodes, and(2) the rightmost path in T has exactly k (internal) nodes.

3

2 2

1 1 2 1

2 1

2 1

. . . . . .

Page 29: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

priority queue operations: Insert

Zip︷ ︸︸ ︷38

37 25

29 10

27

38

2737

29 25

10

38

2737

29 25

10

Page 30: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Leftist heaps

priority queue operations: DeleteMax

38

37 25

29 10

38

37 25

29 10

︷ ︸︸ ︷ 37

29 25

10

Page 31: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

dual structure min-max heap

3

11 5

14 15 9

31

4

112

5

53

6

144

2

155

1

96

3

-

7

15

14 9

3 11 5

151

5

142

4

93

6

34

1

115

2

56

3

-

7

Pointer from min-heap item to same item in max-heap

Insertion: as in ordinary heap, but twice: once in each heap

Deletion: find item to delete in other heap using pointer,move last element to that position and do normal deletion

Page 32: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

interval heap

2-92

8-80 11-75

17-69 42-70 44-73 14-39

24-33 23-65 55-60 44-50 54-57 61

[8,80] ⊆ [2,92]

Page 33: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

interval heap: insert

2-92

11-75

44-73 14-39

54-57 6180

2-92

11-75

44-73 14-39

54-57 61-80

2-92

11-80

44-75 14-39

54-57 61-73

Page 34: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

embedded min&max heap

2

8 11

17 42 44 14

24 23 55 44 54 61

92

80 75

69 70 73 39

33 65 60 50 57

Page 35: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

Double ended priority queue - use case

wikipedia

One example application of the double-ended priority queue isexternal sorting. In an external sort, there are more elementsthan can be held in the computer’s memory.

Page 36: Datastructuren - Data Structuresliacs.leidenuniv.nl/~hoogeboomhj/dat/ohp/dat-present-5.pdf · the queue DeleteMax: returns a data element with maximal priority, and deletes it. GetMax:

Datastructuren

Priority Queues

Double-ended Priority Queues

end.