cs 261 – data structures

66
CS 261 – Data Structures Priority Queue ADT & Heaps CS261 Data Structures

Upload: warren

Post on 22-Feb-2016

51 views

Category:

Documents


0 download

DESCRIPTION

CS 261 – Data Structures. CS261 Data Structures. Priority Queue ADT & Heaps. Goals. Introduce the Priority Queue ADT Heap Data Structure Concepts. Priority Queue ADT. Not really a FIFO queue – misnomer!! Associates a “ priority ” with each element in the collection: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 261 – Data Structures

CS 261 – Data Structures

Priority Queue ADT & Heaps

CS261 Data Structures

Page 2: CS 261 – Data Structures

Goals

• Introduce the Priority Queue ADT• Heap Data Structure Concepts

Page 3: CS 261 – Data Structures

Priority Queue ADT

• Not really a FIFO queue – misnomer!!

• Associates a “priority” with each element in the collection:– First element has the highest priority (typically, lowest

value)

• Applications of priority queues:– To do list with priorities– Active processes in an OS

Page 4: CS 261 – Data Structures

Priority Queue ADT: Interface

• Next element returned has highest priority

void add(newValue + priority);TYPE getMin();void removeMin();

Page 5: CS 261 – Data Structures

Priority Queue ADT: Implementation

Heap: has 2 completely different meanings

1. Classic data structure used to implement priority queues

2. Memory space used for dynamic allocation

We will study the data structure (not dynamic memory allocation)

Page 6: CS 261 – Data Structures

Priority Queue ADT: Implementation

Binary Heap data structure: a complete binary tree in which every node’s value is less than or equal to the values of its children (min heap)

Review: a complete binary tree is a tree in which

1. Every node has at most two children (binary)2. The tree is entirely filled except for the bottom level

which is filled from left to right (complete)– Longest path is ceiling(log n) for n nodes

Page 7: CS 261 – Data Structures

Min-Heap: Example

2

5

8

3

79 10

14 12 11 16

Root = Smallest element

Next open spot

Last filled position(not necessarily the last element added)

Page 8: CS 261 – Data Structures

Maintaining the Heap: Addition

2

5

8

3

79 10

14 12 11 16 4

Place new element in next available position,then fix it by “percolating up”

Add element: 4

New element innext open spot.

Page 9: CS 261 – Data Structures

Maintaining the Heap: Addition (cont.)

Percolating up:while new value is less than parent,

swap value with parent

2

5

8

3

49 10

14 12 11 16 7

2

4

8

3

59 10

14 12 11 16 7

After first iteration (swapped with 7)

After second iteration (swapped with 5)New value not less than parent Done

Page 10: CS 261 – Data Structures

Maintaining the Heap: Removal

• Since each node’s value is less than or equal to the values of its children, the root is always the smallest element

• Thus, the operations getMin and removeMin access and remove the root node, respectively

• Heap removal (removeMin):What do we replace the root node with?Hint: How do we maintain the completeness of the tree?

Page 11: CS 261 – Data Structures

Maintaining the Heap: Removal

Heap removal (removeMin):1. Replace root with the element in the last filled

position2. Fix heap by “percolating down”

Page 12: CS 261 – Data Structures

Maintaining the Heap: Removal

2

5

8

3

79 10

14 12 11 16

Root = Smallest elementremoveMin :1. Move element in last

filled pos into root2. Percolate down

Last filled position

Page 13: CS 261 – Data Structures

Maintaining the Heap: Removal (cont.)

Percolating down:while greater than smallest child

swap with smallest child

16

5

8

3

79 10

14 12 11

3

5

8

16

79 10

14 12 11

Root value removed(16 copied to root and last node removed)

After first iteration (swapped with 3)

Page 14: CS 261 – Data Structures

Maintaining the Heap: Removal (cont.)

3

9

16

9

12

16

After second iteration (moved 9 up)

After third iteration (moved 12 up)Reached leaf node Stop percolating

Percolating down:while greater than smallest child

swap with smallest child

5

8710

14 12 11

8 3

5

8710

14 11

Page 15: CS 261 – Data Structures

Maintaining the Heap: Removal (cont.)

3

5

8

9

712 10

14 16 11

Root = New smallest element

New last filled position

Page 16: CS 261 – Data Structures

Practice

Insert the following numbers into a min-heap in the order given: 54, 13, 32, 42, 52, 12, 6, 28, 73, 36

Page 17: CS 261 – Data Structures
Page 18: CS 261 – Data Structures

Practice

Remove the minimum value from the min-heap5

50

70

6

607 8

Page 19: CS 261 – Data Structures

5

50

70

6

607 8

Page 20: CS 261 – Data Structures

Your Turn

• Complete Worksheet: Heaps Practice

Page 21: CS 261 – Data Structures

CS 261 – Data Structures

Heap Implementation

CS261 Data Structures

Page 22: CS 261 – Data Structures

Goals

• Heap Representation• Heap Priority Queue ADT Implementation

Page 23: CS 261 – Data Structures

Dynamic Array Representation

Complete binary tree has structure that is efficiently represented with an array (or dynamic array)

– Children of node i are stored at 2i + 1 and 2i + 2– Parent of node i is at floor((i - 1) / 2)

a

b c

d e f0a

1b

2c

3d

4e

5f

6 7

Root

Why is this a bad idea if tree is not complete?

Page 24: CS 261 – Data Structures

Dynamic Array Implementation (cont.)

If the tree is not complete (it is thin,

unbalanced, etc.), the DynArr implementation will be full of holes

a

b c

f

d e

0a

1b

2c

3 4d

5 6e

7 8 9 10 11 12 13f

14 15

Big gaps where the level is not filled!

Page 25: CS 261 – Data Structures

Heap Implementation: add

• Where does the new value get placed to maintain completeness?

• How do we guarantee the heap order property?– How do we compute a parent index?– When do we ‘stop’

• Complete Worksheet #33 – heapAdd( )

Page 26: CS 261 – Data Structures

Write : addHeap

void addHeap (struct dyArray * heap, TYPE newValue) {

}

Page 27: CS 261 – Data Structures

1016

Heap Implementation: removeMin

void removeMinHeap(DynArr *heap){ int last;

last = sizeDynArr(heap) – 1;putDynArr(heap, 0, getDynArr(heap, last)); /* Copy the last element to the first */removeAtDynArr(heap, last); /* Remove last element. */

_adjustHeap(heap, last , 0); /* Rebuild heap */}

02

13

24

39

410

55

68

714

812

911

Last element(last)

First element(data[0])

13

24

39

410

55

68

714

812

911

07

117

1016

11

Percolates down from Index 0 to last (not including last…which is one beyondthe end now!)

Last element(last)

Page 28: CS 261 – Data Structures

Heap Implementation: removeMin

2

4

8

3

59 10

14 12 11 16 7 last

13

24

39

410

55

68

714

812

911

02

117

1016

Page 29: CS 261 – Data Structures

Heap Implementation: removeMin (cont.)

7

4

8

3

59 10

14 12 11 16

13

24

39

410

55

68

714

812

911

07

111016

New root

last = sizeDynArr(heap) – 1;putDynArr(heap, 0, getDynArr(heap, last)); /* Copy the last element to the first */removeAtDynArr(heap, last);_adjustHeap(heap, last, 0);

Last element(last)

Page 30: CS 261 – Data Structures

Heap Implementation: _adjustHeap

7

4

8

3

59 10

14 12 11 16

13

24

39

410

55

68

714

812

911

07

1016

Smallest child(min = 3)

11

last

_adjustHeap(heap, last , 0); _adjustHeap(heap, upTo , start);

Page 31: CS 261 – Data Structures

Heap Implementation: _adjustHeap

3

4

8

7

59 10

14 12 11 16

17

24

39

410

55

68

714

812

911

03

1016

smallest child 11

last

_adjustHeap(heap, last , 1);

Page 32: CS 261 – Data Structures

Heap Implementation: _adjustHeap

3

4

8

7

59 10

14 12 11 16

17

24

39

410

55

68

714

812

911

03

1016

smallest child 11

last

current is less thansmallest child so _adjustHeap exitsand removeMin exits

Page 33: CS 261 – Data Structures

Write: _adjustHeap

void _adjustHeap (struct dyArray * heap, int max, int pos) {

}

Page 34: CS 261 – Data Structures

Recursive _adjustHeap

void _adjustHeap(struct DynArr *heap, int max, int pos) { int leftIdx = pos * 2 + 1; int rghtIdx = pos * 2 + 2;

if (rghtIdx < max) { /* Have two children? */

/* Get index of smallest child (_minIdx). *//* Compare smallest child to pos. *//* If necessary, swap and call _adjustHeap(max, minIdx). */

} else if (leftIdx < max) {

/* Have only one child. */ /* Compare child to parent. */ /* If necessary, swap and call _adjustHeap(max, leftIdx). */ } /* Else no children, we are at bottom done. */}

Page 35: CS 261 – Data Structures

Useful Routines

void swap(struct DynArr *arr, int i, int j) { /* Swap elements at indices i and j. */ TYPE tmp = arr->data[i]; arr->data[i] = arr->data[j]; arr->data[j] = tmp;}

int minIdx(struct DynArr *arr, int i, int j) { /* Return index of smallest element value. */ if (compare(arr->data[i], arr->data[j]) == -1) return i; return j;}

Page 36: CS 261 – Data Structures

Priority Queues: Performance Evaluation(ave)

So, which is the best implementation of a priority queue?

(What if wrote a getMin() for AVLTree ?)

SortedVector SortedList Heap

addO(n)

Binary searchSlide data up

O(n)Linear search

O(log n)Percolate up

getMin O(1)get(0)

O(1)Returns firstLink val

O(1)Get root node

removeMinO(n)

Slide data downO(1): Reverse Order

O(1)removeFront()

O(log n)Percolate down

Page 37: CS 261 – Data Structures

Priority Queues: Performance Evaluation

• Recall that a priority queue’s main purpose is rapidly accessing and removing the smallest element!

• Consider a case where you will insert (and ultimately remove) n elements:–ReverseSortedVector and SortedList:

Insertions: n * n = n2

Removals: n * 1 = nTotal time: n2 + n = O(n2)

–Heap:Insertions: n * log nRemovals: n * log nTotal time: n * log n + n * log n = 2n log n = O(n log n)

How do they compare in terms of space requirements?

Page 38: CS 261 – Data Structures

Your Turn

• Complete Worksheet #33 - _adjustHeap( .. )

Page 39: CS 261 – Data Structures

CS 261 – Data Structures

BuildHeap and Heapsort

CS261 Data Structures

Page 40: CS 261 – Data Structures

Goals

• Build a heap from an array of arbitrary values• HeapSort algorithm• Analysis of HeapSort

Page 41: CS 261 – Data Structures

BuildHeap

• How do we build a heap from an arbitrary array of values???

Page 42: CS 261 – Data Structures

BuildHeap: Is this a proper heap?

12

5

4

7

68 3

9 11 10 2

Are any of the subtrees guaranteed to be proper heaps?

012

17

25

38

43

56

64

79

811

910

102

Page 43: CS 261 – Data Structures

BuildHeap: Leaves are proper heaps

12

5

4

7

68 3

9 11 10 2

012

17

25

38

43

56

64

79

811

910

102

Size = 11Size/2 – 1 = 4

11

First non-leaf

Page 44: CS 261 – Data Structures

BuildHeap

• How can we use this information to build a heap from a random array?

• _adjustHeap: takes a binary tree, rooted at a node, where all subtrees of that node are proper heaps and percolates down from that node to ensure that it is a proper heap

void _adjustHeap(struct DynArr *heap,int max, int pos)

Adjust up to(not inclusive) Adjust from

Page 45: CS 261 – Data Structures

BuildHeap

• Find the first non-leaf node,i, (going from bottom to top, right to left)

• adjust heap from it to max• Decrement i and repeat until you process the

root

Page 46: CS 261 – Data Structures

BuildHeap: Leaves are proper heaps

12

5

4

7

68 3

9 11 10 2

012

17

25

38

43

56

64

79

811

910

102

Size = 11Size/2 – 1 = 4

11

First non-leaf

Page 47: CS 261 – Data Structures

HeapSort

• BuildHeap and _adjustHeap are the keys to an efficient , in-place, sorting algorithm– in-place means that we don’t require any extra

storage for the algorithm• Any ideas???

Page 48: CS 261 – Data Structures

HeapSort

1. BuildHeap – turn arbitrary array into a heap

2. Swap first and last elements

3. Adjust Heap (from 0 to the last…not inclusive!)

4. Repeat 2-3 but decrement last each time through

Page 49: CS 261 – Data Structures

HeapSort Simulation: BuildHeap

09

13

28

34

45

57

Size = 6i=Size/2 – 1 = 2

9

83

74 5

i=2_adjustHeap(heap,6,2)

Page 50: CS 261 – Data Structures

HeapSort Simulation: BuildHeap

09

13

27

34

45

58

Size = 6i=1

9

73

84 5

i=1_adjustHeap(heap,6,1)

Page 51: CS 261 – Data Structures

HeapSort Simulation: BuildHeap

09

13

27

34

45

58

Size = 6i= 0

9

73

84 5

i=0_adjustHeap(heap,6,0)

Page 52: CS 261 – Data Structures

HeapSort Simulation: BuildHeap

03

14

27

39

45

58

3

74

89 5

i=-1Done…with BuildHeap ….now let’s sort it!

Page 53: CS 261 – Data Structures

HeapSort

1. BuildHeap

2. Swap first and last

3. Adjust Heap (from 0 to the last)

4. Repeat 2-3 but decrement last

Page 54: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

03

14

27

39

45

58

3

74

89 5

i=5 Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 1

Page 55: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

08

14

27

39

45

53

8

74

39 5

i=5Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 1

Page 56: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

04

15

27

39

48

53

4

75

39 8

i=4Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 2

Page 57: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

08

15

27

39

44

53

8

75

39 4

i=4Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 2

Page 58: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

05

18

27

39

44

53

5

78

39 4

i=3Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 3

Page 59: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

09

18

27

35

44

53

9

78

35 4

i=3Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 3

Page 60: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

07

18

29

35

44

53

7

98

35 4

i=2Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 4

Page 61: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

09

18

27

35

44

53

9

78

35 4

i=2Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 4

Page 62: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

08

19

27

35

44

53

8

79

35 4

i=1Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 5

Page 63: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

09

18

27

35

44

53

9

78

35 4

i=1Swap(v, 0, i)_adjustHeap(v, i, 0);

Iteration 5

Page 64: CS 261 – Data Structures

HeapSort Simulation: Sort in Place

09

18

27

35

44

53

9

78

35 4

i=0DONE

Page 65: CS 261 – Data Structures

HeapSort Performance

• Build Heap:

n/2 calls to _adjustHeap = O(n log n)

• HeapSort:

n calls to swap and adjust = O(n log n)

• Total:

O(n log n)

Page 66: CS 261 – Data Structures

Your Turn

• Worksheet 34 – BuildHeap and Heapsort