Hash Maps: The point of a hash map is to FIND DATA QUICKLY.

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Advertisements

Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heaps & Priority Queues
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Sorting With Priority Queue In-place Extra O(N) space
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Priority Queues and Heaps
Priority Queue & Heap CSCI 3110 Nan Chen.
Part-D1 Priority Queues
Dr. David Matuszek Heapsort Dr. David Matuszek
i206: Lecture 14: Heaps, Graphs intro.
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Hassan Khosravi / Geoffrey Tien
Computer Science 2 Heaps.
"Teachers open the door, but you must enter by yourself. "
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Heapsort.
Data Structures and Analysis (COMP 410)
Priority Queues & Heaps
Priority Queues (Heaps)
Heapsort.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Heapsort.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
CO 303 Algorithm Analysis and Design
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
EE 312 Software Design and Implementation I
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
Presentation transcript:

Hash Maps: The point of a hash map is to FIND DATA QUICKLY. We use an array because there’s not much faster than finding the kth value in an array DATA ->hash function ->index Hash function used for both STORING AND THEN FINDING LATER

Hash Map Example Student objects: Sobj *stud1 = new Sobj(“Smith”,”Sam”,”Sophomore”,”ELEG”,3.4,214325919) Sobj *stud2 = new Sobj(“Kell”,”Taylor”,”Freshman”,”CISC”,2.9,271132414) Sobj *stud3 = new Sobj(“Smith”,”Taylor”,”Freshman”,”ELEG”,3.4,199321426) (the unique element about each student is their id) Hash function we’ll use is folding with width of 3 Since the original set of data consists of 3 objects, the ideal array size would be 7 (214+325+919)%7 = 2 (271+132+414)%7 = 5 (199+321+426)%7 = 0 Once students are stored in array, now when I want to find info about a particular student, I take their id, fold it, and go to that index in the array (0(1))! E.g., what if I wanted to find Sam Smith’s GPA? (214+325+919)%7 = 2, go to index to and get her GPA! 1 2 3 4 5 6 Taylor Smith, Freshman, ELEG, 3.4, 199321426 Sam Smith, Sophomore, ELEG, 3.4, 214325919 Taylor Kell, Freshman, CISC, 2.9, 271132414

So Far: Arrays: find the kth value, traversing quickly! Linked lists: join, push, pop (stacks) AVL Trees: insert, delete, search in equally efficient time, no extra space HashMap: Finding data quickly!!! (at the cost of extra memory)

Binary Heaps What if we’re concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap is structured so that the node with the most relevant data is the root node, the next most relevant as the children of the root, etc. A heap is not a binary search tree A heap does not order its node A heap is a complete tree. Every level but the leaf level is full Leaf level full from left to right Binary Heap – another way to implement a priority queue!

Definition of a Binary Heap A tree is a binary heap if It is a complete tree Every level is full except the bottom level The value in the root is the largest of the tree (Or smallest, if the smallest value is the most relevant and that is how you choose to structure your tree) Every subtree is also a binary heap Equivalently, a complete tree is a binary heap if Node value > child value, for each child of the node Note: This use of the word “heap” is entirely different from the heap that is the allocation area in C++

Is this a Binary Heap?

Inserting an Item into a Binary Heap Insert the item in the next position across the bottom of the complete tree: preserves completeness Restore “heap-ness”: while new item is not root and is greater than its parent swap new item with its parent

Insert 80? 80 74 80 6 66 80 How many steps? At most, how many steps to insert?

Removing an Item We always remove the top (root) node! Heaps find the largest (or smallest) value in a set of numbers and that number is at the root! Remove the root Leaves a “hole”: Fill the “hole” with the last item (lower right-hand leaf) L Preserve completeness Swap L with largest child, as necessary Restore “heap-ness”

Remove? 89 80 66 66 74 66 How many steps? At most, how many steps to remove? Next: how would we implement a heap?

Implementing a Heap Yeah, yeah, we could use nodes and pointers, but… Recall: a heap is a complete binary tree If we know the number of nodes ahead of time, a complete binary tree fits nicely in an array: The root is at index 0 Children of 0 are at 1 and 2 Children of 1 are at 3 and 4 Children of 2 are at 5 and 6 Children of 3 are at 7 and 8 Children of 4 are at 9 and 10 Is there a formula for figuring out where children of a node are? Parents? Where would we insert the next node (the child of the node containing 7)? 11 11 8 7 2 5 4 1 3 6 7 8 3 8 1 7 2 2 3 5 5 4 4 5

Inserting into a Heap Insert new item at end; set child to curr_size-1 Set parent to (child – 1)/2 while (parent ≥ 0 and arr[parent] < arr[child]) Swap arr[parent] and arr[child] Set child equal to parent // so child is now (child – 1)/2 Set parent to (child – 1) / 2 How do we delete?

Deleting from a Heap Set arr[0] to arr[curr_size-1], and shrink curr_size by 1 Set parent to 0 flag = true while (flag) Set leftchild to 2 * parent + 1, and rightchild to leftchild + 1 if leftchild ≥ curr_size, flag is false else: Set maxchild to leftchild If rightchild < curr_size and arr[rightchild] > arr[leftchild] set maxchild to rightchild If arr[parent] ≥ arr[maxchild], Flag is false Swap arr[parent] and arr[maxchild]; set parent to maxchild

Performance of Heap A complete tree of height h has: Less than 2h nodes (why?) At least 2h-1 nodes Thus complete tree of n nodes has height O(log n) Insertion and deletion at most are O(log n), always Heap is useful for priority queues

Which of the following is (are) a heap? 1 2 3 4 5 6 7 8 9 42 21 58 12 31 48 64 14 29 A 1 2 3 4 5 6 7 8 9 42 41 35 39 37 24 12 38 36 B 1 2 3 4 5 6 7 8 9 thunder storm mud squirrels grass flowers dandelions petunias spring baby birds C

Try: 18’s parent is? Perform a delete 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 58 45 56 20 33 50 18 19 24 41 18’s parent is? (7-1)/2, or 20 (at location 3) Perform a delete Remove 58 41 goes to position 0 Bubble 41 down 1 2 3 4 5 6 7 8 9 10 11 12 13 14 41 45 56 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 41 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 50 20 33 41 18 19 24

Heapsort Heapsort Idea: Works in place: no additional storage Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1

Heapsort Picture

Algorithm for In-Place Heapsort Build heap starting from unsorted array While the heap is not empty Remove the first item from the heap: Swap it with the last item Restore the heap property

Heapsort Analysis Insertion cost is log n for heap of size n This is O(n log n) Removal cost is also log n for heap of size n Total removal cost = O(n log n) Total cost is O(n log n)

Heapsort Picture

Making Heap: Could: OR: Insert each number in a sequence into the heap As you insert, bubble each number up OR: Just insert all numbers into array. Check each node at leaf level with its parent. Make a bunch of tiny heaps Then check each node at height of 2 with its parent Make heaps by switching just max child and parent Continue until you get to root Quicker

Make a heap (efficiently!): 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25

Example of making a heap: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 19 15 16 20 46 44 47 41 42 23 22 40 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays

Example of making a heap: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 19 15 47 42 46 44 16 41 20 23 22 40 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15

Example of making a heap: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 47 46 41 42 40 44 16 19 20 23 22 15 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15 Round 3: Level 1 and 2: switch 47 and 37, then 42 and 37

Example of making a heap: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 47 42 46 41 37 40 44 16 19 20 23 22 15 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15 Round 3: Level 1 and 2: switch 47 and 37, then 42 and 37

E.G.: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 H:4 H:3 H:2 H:1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 19 15 16 20 46 44 47 41 42 23 22 40 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 19 15 47 42 46 44 16 41 20 23 22 40 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 47 46 41 42 40 44 16 19 20 23 22 15 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 42 46 41 37 40 44 16 19 20 23 22 15 25 Now we have a heap!

Example: Selection problem (Kth largest) If we had a set of unordered numbers, how would we determine the kth largest (or smallest, by reversing the process)? E.g., 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 What if we wanted the 5th smallest (largest) element? How would we do this? How long would it take?

Can we do better? Of course. 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 (for this, pretend as if we’re looking for the kth smallest element) 1. build a heap with the array with the smallest value at the top. 2. delete k elements from the heap. Running time? 11 10 5 3 8 2 4 8 1 1 2 3 4 10 11 2 5 6 8 3 8 4 7 11 3 6 8 10 5 7 8 9 11 6 8 10 8 So 5 is the 5th smallest element in the list. Can we do better than this????

Better: 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 build a heap (largest elements at top) with just the first k elements. 11 4 2 6 5 8 7 7 2 4 4 8 1 3 7 4 2 2. Compare rest of elements with heap. If the new element is smaller than the root, we insert the new element and remove the root. 3. Otherwise we ignore. Note: we are finding the kth smallest element. To find the kth largest element, we would make a heap with the smallest number as the root, and ascend as we move down. Then new elements would be inserted if they were larger than the root.

Analysis: We’re finding the kth smallest in an array of data K must be smaller than n All n elements must at a minimum be compared to the root, In the worst case, must bubble down log k Worst case analysis: n log k