Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #4 Mind Your Priority Queues

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #4 Mind Your Priority Queues"— Presentation transcript:

1 CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
Steve Wolfman Winter Quarter 2000 Today we’re taking a bit of a breather. You’ve had your first assignment which was hard because we didn’t know what we were doing on it. You’re working on your first project which is hard because you don’t all know UNIX and g++. Finally, you’re trying to follow what’s going on in class which is hard because your blood sugar level is too low. So, let’s take a bit of a break.

2 Today’s Outline 20 (minutes of) Questions Trees Priority Queues Heaps
Today, I’m going to start by taking questions for about 20 minutes. That’s our break. Ask about the assignment, grading policies, late days, or last but definitely not least, ask about content from the lectures. Before we start, let me just say one thing; some people are getting very stressed about this assignment and are worried about using a late day. THERE ARE ONLY FOUR PROJECTS AND YOU HAVE FOUR LATE DAYS! THIS IS A GREAT TIME TO USE A LATE DAY!!

3 Trees 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 Non-recursive procedure call chains Anyone not seen a tree? I mean the kind that grows _down_ from the root. The computer science tree. Here are some applications. What do these have in common? They have a root. Each entry is either a leaf or it has a number of children. Each child is also a tree. Trees are defined recursively! A tree is a leaf or an internal node with one or more children. Each child is a tree.

4 Tree Terminology A root: leaf: child: parent: sibling: ancestor:
descendent: subtree: B C D E F G H I Let’s review the words: root: A leaf: DEFJKLMNI child:A - C or H - K leaves have no children parent: C - A or L - H the root has no parent sibling: D - E or F or J - K,L,M, or N grandparent: G to A grandchild: C to H or I ancestor: the node itself or any ancestor’s parent descendent: the node itself or any child’s descendent subtree: a node and all its descendents J K L M N

5 More Tree Terminology A depth: height: degree: branching factor: B C D
Depth: the number of edges along the path from the root to the node height: the number of edges along the longest path from the node to a leaf degree: the number of children of the node branching factor: the maximum degree of any node (or sometimes the average) preorder traversal: running through all the nodes in the tree, starting with the parent, then all the children postorder traversal: run through all the nodes starting with the children and then the parents J K L M N

6 One More Tree Terminology Slide
binary: n-ary: complete: A B C Binary: each node has at most two children n-ary: each child has at most n children complete: Each row of the tree is filled in left to right before the next one is started HOW DEEP CAN A COMPLETE TREE BE? D(N)  D(N/2) + 1 D E F G H I J

7 Tree Calculations Find the longest undirected path in a tree Might be:
OK, let’s get a bit of practice working with trees. Almost anything we do on a tree will be most easily thought of as recursive (even if we implement it iteratively) Let’s try to find the longest undirected path through the tree. That is, the path goes from node to node, never repeating a node, and it can either go up a link or down it. Thinking recursively, what might the longest path in this example tree be? EITHER: the longest path in one of the subtrees OR: the longest path that goes through t To implement that, we could keep returning the height of the tree and the longest path in the tree. The height overall is the max height + 1. The longest path is max(longest subtree path, 2max(heights)+1

8 Tree Calculations Example
B C D E F G OK, what’s the longest path in this tree? H I J K L M N

9 Back to Queues Some applications Problems? ordering CPU jobs
simulating events picking the next search site Problems? short jobs should go first earliest (simulated time) events should go first most promising sites should be searched first Alright, now we all remember what a tree is. Let’s harken back to queues now. Here are some possible applications of a queue which aren’t quite right. All of these want to favor certain entries in the Queue. How do we handle that?

10 Priority Queue ADT Priority Queue operations
Remember ADTs? 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) We need a new ADT for this. One that returns the best (which we’ll generally define as lowest priority value) node next. I’m often going to represent priority queues as collections of priorities; remember that they are collections of data with priorities. MOREOVER, some priority queues don’t even need priorities, just the ability to compare priorities. (all of the ones we’ll talk about fall in this category).

11 Applications of the Priority Q
Hold jobs for a printer in order of length Store packets on network routers in order of urgency Simulate events Select symbols for compression Sort numbers Anything greedy There are, of course, gobs of applications for priority queues. Here are just a few. A - Z

12 Naïve Priority Q Data Structures
Unsorted list: insert: deleteMin: Sorted list: OK, let’s look at some ways we might implement priority Qs. Unsorted lists give us fast inserts, but we have to search the whole list to delete. Sorted lists give us fast delete (it’s just the first element!), but we have to either search the whole list (for ll) or move the whole list (for array) on an instert.

13 Binary Search Tree Priority Q Data Structure (that’s a mouthful)
8 insert: deleteMin: 5 11 2 6 10 12 A binary search tree is a binary tree in which all nodes in the left subtree of a node have lower values than the node. All nodes in the right subtree of a node have higher value than the node. We’ll talk more about these later. For now, let’s get a feel for how well they work as PQ data structures. How long might an insert take here? (log n) How about deleteMin? It’s always the leftmost node in the tree, right? So, it might take log n time to find the node. What about worst case? The book says that these lopsided deletes don’t cause a problem. Ask me about that via if you’re curious why not. 4 7 9 14 13

14 Binary Heap Priority Q Data Structure
Heap-order property parent’s key is less than children’s keys result: minimum is always at the top Structure property complete tree with fringe nodes packed to the left result: depth is always O(log n); next open location always known 20 14 12 9 11 8 10 6 7 5 4 2 Alright, there are two problems with binary search trees as pqs. First, they’re overkill. Why keep everything ordered? We just need to know the least at any given time. Second, they’re not guaranteed to be complete, so we have worst case O(n) times. How do we find the minimum?

15 Nifty Storage Trick Calculations: child: parent: root: next free: 20
1 20 14 12 9 11 8 10 6 7 5 4 2 2 3 4 5 6 7 8 9 It so happens that we can store a complete tree in an array and still be able to find children and parents easily. We’ll take advantage of this. Child: left = 2*node right = 2*node + 1 parent = floor(node/2) nextfree = length + 1 root = 1 12 10 11 1 2 3 4 5 6 7 8 9 10 11 12 12 2 4 5 7 6 10 8 11 9 12 14 20

16 DeleteMin pqueue.deleteMin() 2 20 14 12 9 11 8 10 6 7 5 4 2 ? 4 5 7 6
OK, to delete, we start by plucking out that value in O(1) time. Now, we have a hole at the top, and a node that isn’t proper for a complete tree at the bottom. So, we fix it by putting the value in the hole, and moving the hole until we restore the heap-order property. 11 9 12 14 20

17 Percolate Down ? 4 4 5 ? 5 7 6 10 8 7 6 10 8 11 9 12 14 20 11 9 12 14 20 4 4 6 5 6 5 7 ? 10 8 7 12 10 8 11 9 12 14 20 11 9 20 14 20

18 Finally… 4 6 5 7 12 10 8 11 9 20 14

19 DeleteMin Code runtime: Object deleteMin() { assert(!isEmpty());
returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size+1]); Heap[newPos] = Heap[size + 1]; return returnVal; } int percolateDown(int hole, Object val) { while (2*hole <= size) { left = 2*hole; right = left + 1; if (right <= size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } break; return hole; Note that there are three things going on here: find the smaller child if the smaller child is still smaller than the moving value, move the smaller child up otherwise, we’ve found the right spot, and stop. runtime:

20 Insert pqueue.insert(3) 20 14 12 9 11 8 10 6 7 5 4 2 2 4 5 7 6 10 8 11
Inserting works similarly. We tack a hole on at the end of the tree, and move that hole _up_ until it’s in the right place for the new value. 11 9 12 14 20 ?

21 Percolate Up 2 2 4 5 4 5 3 7 6 10 8 7 6 ? 8 3 11 9 12 14 20 ? 11 9 12 14 20 10 2 2 3 4 ? 4 3 7 6 5 8 7 6 5 8 11 9 12 14 20 10 11 9 12 14 20 10

22 Insert Code runtime: void insert(Object o) { assert(!isFull());
size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; runtime: Notice that this code is a _lot_ easier.

23 To Do Finish Project I Read chapter 6 in the book

24 Coming Up More Priority Queue Operations Mergeable Priority Queues
Next Quiz (January 13th) First project due (January 14th) A day off (January 17th)! Note that the next quiz will be during quiz section. This is not to harass you; it’s to give you an EXTRA day to finish the homework. We’ll get graded quizzes back to you in class on Friday. Remember that the first project is due on Friday. REMEMBER ALSO THAT ONE LATE DAY GIVES YOU UNTIL 10PM MONDAY TO FINISH THE PROJECT!! That’s 3 FULL EXTRA DAYS!


Download ppt "CSE 326: Data Structures Lecture #4 Mind Your Priority Queues"

Similar presentations


Ads by Google