CSE 326: Data Structures Lecture #5 Heaps More

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
Advertisements

Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #9 Big, Bad B-Trees Steve Wolfman Winter Quarter 2000.
1 Chapter 6: Priority Queues, AKA Heaps. 2 Queues with special properties Consider applications –ordering CPU jobs –searching for the exit in a maze (or.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Priority Queues
CSE 326: Data Structures Priority Queues (Heaps)
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332 Data Abstractions B-Trees
Binary Search Trees.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
B+-Trees.
B+-Trees.
October 30th – Priority QUeues
Programming Abstractions
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Hashing Exercises.
CS Anya E. Vostinar Grinnell College
Binary Search Trees Why this is a useful data structure. Terminology
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
CSE 326: Data Structures: Midterm Review
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326 Heaps and the Priority Queue ADT
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
CMSC 341 Lecture 14 Priority Queues & Heaps
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332: Data Structures Priority Queues – Binary Heaps
B- Trees D. Frey with apologies to Tom Anastasio
Heaps and the Heapsort Heaps and priority queues
CSE 214 – Computer Science II B-Trees
CSE 332: Data Abstractions Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
CSE 326: Data Structures Lecture #5 Political Heaps
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 326: Data Structures Lecture #18 Fistful of Data Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 326: Data Structures Lecture #12 Hashing II
CSE 326: Data Structures Lecture #8 Balanced Dendrology
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #3 Mind your Priority Qs
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
Priority Queues (Chapter 6):
CSE 373: Data Structures and Algorithms
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
CSE 326: Data Structures Lecture #10 B-Trees
CSE 326: Data Structures Lecture #14
326 Lecture 9 Henry Kautz Winter Quarter 2002
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE 326: Data Structures Lecture #5 Heaps More Steve Wolfman Winter Quarter 2000 OK, today we’ll get to and get through much of heaps. So, we have a lot of material to cover! But, there is one thing we need to touch on briefly before we continue.

We get slide printouts every class 4 pages of notes per day  50 students  3 lectures per week  10 weeks per quarter  engineer’s fudge factor  10,000 pages one 60 foot pine tree  80,000 pages Frankye Jones’s frantic effort = one paycheck for Steve paycheck + www.americanforests.org = 1 tree / $1 How about two trees per person? Some of you may remember that I was considering not printing out slides every class period. I decided it was important to do so, but I did some math and some web searching and discovered that we can, as usual, buy our way out of guilt! So, given that I’m now getting paid, how does about two trees per person in the class sound to everyone?

Today’s Outline Things Steve Didn’t Finish on Wednesday (Heaps) Extra heap operations d-Heaps Return Quizzes OK, back to comupter science. Today we’ll cover everything I missed on Wed. That’s heaps of stuff, and then move on to some extra heap operations and d-heaps. I’ll hand the quizzes back right now, however. You MUST hand in your score sheet along with your homework!

Other Priority Queue Operations decreaseKey given a pointer to an object in the queue, reduce its priority value increaseKey given a pointer to an object in the queue, increase its priority value remove given a pointer to an object in the queue, remove it buildHeap given a set of items, build a heap Why do I insist on a pointer to each item in decreaseKey, increaseKey, and remove? Because it’s hard to find an item in a heap; it’s easy to delete the minimum, but how would you find some arbitrary element? This is where the Dictionary ADT and its various implementations which we will study soon come up. What about build heap? Is that really a necessary operation? It turns out that the necessity for this is based _purely_ on the fact that we can do it faster than the naïve implementation!

DecreaseKey, IncreaseKey, and Remove void decreaseKey(int obj) { assert(size >= obj); temp = Heap[obj]; newPos = percolateUp(obj, temp); Heap[newPos] = temp; } void increaseKey(int obj) { newPos = percolateDown(obj, temp); void remove(int obj) { assert(size >= obj); percolateUp(obj, NEG_INF_VAL); deleteMin(); } The code for these is pretty simple. Notice that these are called just after the key is changed, they do not include the new value (just assume it’s there). This is different from the book’s version. And, it’s not necessarily better.

BuildHeap Floyd’s Method. Thank you, Floyd. 12 5 11 3 10 6 9 4 8 1 7 2 pretend it’s a heap and fix the heap-order property! 12 5 11 Pull other slide back up and ask how long it takes. O(n log n) worst case, but O(n) average case. Can we get a worst case O(n) bound? Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 3 10 6 9 4 8 1 7 2

Build(this)Heap 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11

Finally… 1 3 2 4 5 6 9 How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. … In other words n * Sum I=0 to n of I/2i But, this is (as n->inf) just 2. See chapter I to see why. So, the runtime is O(n). 12 8 10 7 11 runtime:

Thinking about Heaps Observations Realities finding a child/parent index is a multiply/divide by two operations jump widely through the heap each operation looks at only two new nodes inserts are at least as common as deleteMins Realities division and multiplication by powers of two are fast looking at one new piece of data sucks in a cache line with huge data sets, disk accesses dominate Now, let’s look at some facts about heaps and see if we can’t come up with a new priority queue implementation.

Solution: d-Heaps Each node has d children 1 Each node has d children Still representable by array Good choices for d: optimize performance based on # of inserts/removes choose a power of two for efficiency fit one set of children in a cache line fit one set of children on a memory page/disk block 3 7 2 4 8 5 12 11 10 6 9 D-heaps address all these problems. 12 1 3 7 2 4 8 5 12 11 10 6 9

One More Operation Merge two heaps. Ideas? Anyone have a way to merge two heaps? We’ll find out tomorrow that we can actually do this in O(log n) time!

To Do Turn in Project I (due today) Start on Homework II (due Jan 20th) Read chapter 6 in the book

Coming Up Mergeable heaps Dictionary ADT and Self-Balancing Trees First project due (January 14th) A day off (January 17th)! Second homework assignment due (January 20th) Again, turn in that next homework assignment with your score sheet!