cs-2852 data structures lecture 13a andrew j. wozniewicz image copyright © 2010 andyjphoto.com

27
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Upload: ralf-chapman

Post on 12-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852Data StructuresLECTURE 13A

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

Page 2: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Agenda• Heap– Tree Implementation– Array Implementation

• Priority Queue

Page 3: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

What is a Heap?

• Max-Heap: key(P) >= key(C)• Min-Heap: key(P) <= key(C)

• Not to be confused with memory “heap” (except in early LISP)

(Max-)Heap is a data structure that satisfies the “heap property”: if C is a child node of P, then key(P) >= key(C)

DEFINITION

Page 4: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

What is a Heap?

A Heap is a complete binary tree with the following properties:• The value in the root is the

smallest (largest)• Every subtree is a heap.

DEFINITION

Page 5: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Full, Perfect, and Complete BT

• Full:– All nodes have either 0 or 2 children

• Perfect– Full, with 2height-1 nodes

• Complete– Perfect through level height-1

Page 6: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Max-Heap Example

19

17 3 25

100

36

1

2 7

Page 7: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Min-Heap Example

18

20 28 39

6

29

66

37 26 76 32 76

Page 8: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations

• create• findMax | findMin• deleteMax | deleteMin• insert• merge

Page 9: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Applications• Priority Queue– Maximally efficient implementation– java.util.PriorityQueue<E>

• Graph algorithms – Dijkstra’s; shortest path

• Heap Sort– Sort an array in-place

• Selection algorithms– Finding min/max in O(1)– Median; k-th largest element in O(n)

Page 10: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Implementations

18

20 28 39

6

29

66

37 26 76 32 76

6 18 29 20 28 39 66 37 26 76 32 76

TREE

ARRAY

Page 11: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Insert

• Insert the new item in the next available position at the bottom of the heap

• While new item not at root, AND smaller than its parent:– Swap the new item with its parent

Page 12: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Insert (Example)

18

20 28 39

6

29

66

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11][12] [13] [14]

6 18 29 20 28 39 66 37 26 76 32 76[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

Page 13: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Insert (Example)

6 18 29 20 28 39 66 37 26 76 32 76 7[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

18

20 28 39

6

29

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12][13] [14]7

66

Page 14: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Formulas

• Children of Node p:c1(p) = 2p+1

c2(p) = 2p+2

• Parent of Node c:p(c) = (c-1) / 2

Page 15: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Insert (Example)

18

20 28

6

29

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12][13] [14]

7 66

39

6 18 29 20 28 3966 37 26 76 32 767[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

Page 16: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Insert (Example)

18

20 28

6

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12][13] [14]

66

39

29

7

6 18 2920 28 3966 37 26 76 32 767[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

Page 17: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Remove

• Remove root item by replacing with last item in the heap (LAST)

• While LAST has children, AND LAST > child:– Swap LAST with smaller child

Page 18: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Remove

18

20 28

6

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12]

[13] [14]

66

39

29

7

6 18 2920 28 3966 37 26 76 32 767

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

Page 19: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Remove

18

20 28

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12]

[13] [14]

6629

7

639

39 18 2920 28 66 37 26 76 32 767

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

Page 20: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Remove

18

20 28

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12]

[13] [14]

6629

7

39

7 18 2920 28 66 37 26 76 32 7639

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

Page 21: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Heap Operations: Remove

18

20 28

37 26 76 32 76

[0]

[1] [2]

[3] [4] [5] [6]

[7] [8] [9] [10] [11] [12]

[13] [14]

66

7

39

29

7 18 3920 28 66 37 26 76 32 7629

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

Page 22: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Time-Complexity of Heap Operations

• Insertion

O(log n)• Removal

O(log n)

Page 23: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Priority Queue

• An Abstract Data Type• Each element associated with a

“priority”• Elements are removed

Highest-Priority-First (VIP)• Operations:– insert_with_priority– pull_highest_priority

Page 24: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Inefficient (BAD) Implementations

• Unsorted “bag” (list)– Insertion: O(1)– Removal: O(n)

• Sorted list– Insertion: O(n)– Removal: O(1)

TERRIBLE IMPLEMENTATIONSDON’T EVER DO THIS!

Priority Queue

Page 25: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Typical (GOOD) Implementations

• Heap– Insertion: O(log n)– Removal: O(log n)

• Self-Balancing Tree– Insertion: O(log n)– Removal: O(log n)

Priority Queue

TYPICALLY ALREADY IMPLEMENTEDDO NOT RE-INVENT THE WHEEL!

Page 26: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Summary• Heap– Tree Implementation– Array Implementation

• Priority Queue

Page 27: CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Questions?

Image copyright © 2010 andyjphoto.com