Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.

Similar presentations


Presentation on theme: "CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1."— Presentation transcript:

1 CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1

2 Learning Goals Provide examples of appropriate applications for priority queues. Describe efficient implementations of priority queues. Relate heaps to binary trees, binary search trees, and arrays. 2 It is not a goal for this term to be able to manipulate heaps by hand.

3 Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 3

4 Back to Queues Some applications –ordering CPU jobs –simulating events –picking the next search site when searching {a maze, a game tree, a room, the WWW} Problems? –short jobs should go first –earliest (simulated time) events should go first –most promising sites should be searched first 4

5 Priority Queue ADT Priority Queue operations –create –destroy –insert –deleteMin –is_empty Priority Queue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9)C(3) 5

6 Applications of the Priority Q Call queues for help lines (or don’t you think so?) Hold jobs for a printer in order of length Simulate events (hold in order of time) Sort numbers Store packets on network routers in order of urgency Select symbols for compression (hold in order of frequency) Anything greedy: an algorithm that makes the “locally best choice” at each step (hold in order of quality) 6 Your call will not be answered in the order it was received.

7 Naïve Priority Q Data Structures Unsorted array (or linked list): –insert: –deleteMin: Sorted array: –insert: –deleteMin: 7 a.O(lg n) b.O(n) c.O(n lg n) d.O(n 2 ) e.Something else

8 Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 8

9 How Can We Efficiently Implement a PQ? Stack? Queue? Linked List –Singly, doubly, … Array –Circular, resizing, … BST AVL Tree Priority value is _______ insert is ________ delete_min is _______ 9

10 AVL Tree as PQ How well does this work in terms of the number of priority values/data in the tree, n? Runtime of insert ? Runtime of delete_min ? 10

11 Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 11

12 Binary Heap Priority Q Data Structure 201412911 81067 54 2 Heap-order property –parent’s key is less than or equal to children’s keys –result: minimum is always at the top Structure property –“nearly complete tree” –result: depth is always O(log n); next open location always known WARNING: this has NO SIMILARITY to the “heap” you hear about when people say “things you create with new go on the heap”. And, this is a binary tree but is NOT a binary search tree. Look! Invariants! 12

13 201412911 81067 54 2 24576 8119121420 123456789101112 0 12 3 45 6 7 8 910 11 Nifty Storage Trick 0 13 So, we get the speed and “spatial locality” of arrays. (Also the occasional expensive resizes of arrays.)

14 DeleteMin 201412911 81067 54 ? 2 201412911 81067 54 2 pqueue.deleteMin() Invariants violated! DOOOM!!! 14

15 Percolate Down 201412911 81067 54 ? 201412911 81067 5? 4 201412911 810?7 56 4 201420911 810127 56 4 15

16 Finally… 1420911 810127 56 4 16 Algorithm intuition: move the rightmost, bottom element to the root; then, fix the invariant downward until stable.

17 Insert 201412911 81067 54 2 201412911 81067 54 2 pqueue.insert(3) 3 Invariant violated! What will we do? Intuition: insert at the bottom-right; then, fix invariant upward until stable. 17

18 Insert Result 201412911 8567 34 2 10 18 201412911 81067 54 2 3

19 Closer Look at Creating Heaps To create a heap given a list of items: Create an empty heap. For each item: insert into heap. Time complexity? a.O(lg n) b.O(n) c.O(n lg n) d.O(n 2 ) e.None of these 111210 35 3 9, 4, 8, 1, 7, 2 19

20 A Better BuildHeap Floyd’s Method. Thank you, Floyd. 511310694817212 pretend it’s a heap and fix the heap-order property! 27184 96103 115 12 Invariant violated! Where can the order invariant be violated in general? a.Anywhere b.Non-leaves c.Non-roots 20

21 Build(this)Heap: fix from bottom to top 67184 92103 115 12 671084 9213 115 12 1171084 9613 25 12 1171084 9653 21 12 21

22 Finally… 11710812 9654 23 1 runtime: 22

23 Build(any)Heap This is as many violations as we can get. How do we fix them? Let’s play colouring games! 23 “amortized analysis!”

24 Other Uses for Trees (besides BSTs and heaps) Family Trees Organization Charts Classification trees –what kind of flower is this? –is this mushroom poisonous? File directory structure –folders, subfolders in Windows –directories, subdirectories in UNIX Function call tree (i.e., a record of everything that goes in the call stack) 24

25 To Do Read: KW Section 8.5 25

26 Coming Up Sorting, sorting, and more sorting! 26

27 Tree Terminology Reference A E B DF C G IH LJMKN root: the single node with no parent leaf: a node with no children child: a node pointed to by me parent: the node that points to me sibling: another child of my parent ancestor: my parent or my parent’s ancestor descendent: my child or my child’s descendent subtree: a node and its descendents We sometimes use degenerate versions of these definitions that allow NULL as the empty tree. (This can be very handy for recursive base cases!) 27


Download ppt "CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1."

Similar presentations


Ads by Google