Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Nick Harvey & Kevin Zatloukal
Tirgul 9 Hash Tables (continued) Reminder Examples.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Week 10: Heap and Priority queue. Any feature here?
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Spring 2015 Lecture 6: Hash Tables
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Chapter 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
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 21 Priority Queue: Binary Heap Saurav Karmakar.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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:
Internal and External Sorting External Searching
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS 367 Introduction to Data Structures Lecture 8.
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)
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
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.
"Teachers open the door, but you must enter by yourself. "
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Tree Representation Heap.
"Teachers open the door, but you must enter by yourself. "
Heap Sort CSE 2011 Winter January 2019.
Topic 5: Heap data structure heap sort Priority queue
Amortized Analysis and Heaps Intro
Presentation transcript:

Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples

The heap property & heapify A heap is a complete binary tree, in which each node is larger than both his sons. The largest element is the root of the tree. Notice that this does not mean, however, that the two sons of the root are the 2nd two largest in the heap.

The heap property & heapify Heapify: assumes that both subtrees of the root are heaps, but the root may be smaller than one of its children: Heapify(Node x) largest = max {left(x), right(x)} if ( largest > x ) exchange (largest, x) heapify (x)

The heap property & heapify The node 3 is replaced with 16, then with 12, and then with 8.

Priority Queue Priority Queue applies First In Highest Priority Out. Supports the operations: insert, maximum, extract-maximum. It is simple to implement it using heaps: maximum: just return the root extract-maximum: save the root, move the last leaf to be the root, perform heapify and return the saved root. insert: add the node as a leaf and then move it up until its value is lower than its parent ’ s value.

Priority Queue Example for Priority Queue insert operation. When inserting 11, first add it as a leaf, then exchange it with 5, then with 9.

Performance Priority Queue using heaps: maximum operation takes O(1). extract-max operation takes O(log n). insert operation takes O(log n). Priority Queue using ordered list: maximum operation takes O(1). extract-max operation takes O(1). insert operation takes O(n).

Insert versus Build Heap What is the difference between insert operation of Priority Queue and Build_Heap? for downto 1 do while there’s some x not yet in heap

Insert versus Build Heap and sometimes create different heaps. For example, consider the sequence 1,2,3,4 build-heap will create 4,2,3,1, Insert-build-heap will create 4,3,2,1. Run time Build-heap = O(n). Insert-build-heap = O(nlogn) in the worst-case.

Questions How to implement a queue/stack with a priority queue? What are the differences in running times? How to implement an increase-key operation, which increases the value of some node? How to delete a given node from the heap in O(logn)?

Dictionary / Map ADT This ADT stores pairs of the form (in java: “ value ” instead of “ data ” ). Supports the operations insert(key, data), find(key), and delete(key). One way to implement it is by search trees. The standard operations take O(log n) this way. Can we achieve better performance for the standard operations?

Direct addressing Say the keys comes from a (large) set U. one way to have fast operations is by allocating an array of size |U|. This is of course a waste of memory, since most entries in the array will remain empty. For example, A Hebrew dictionary (e.g. Even- Shushan) holds less than 100,000 words whereas the number of possible combinations of Hebrew letters is much bigger (22 5 for 5-letter words only). It ’ s impractical to try and allocate all this space that will never be used.

Hash table In a hash table, we allocate an array of size m, which is much smaller than |U|. We use a hash function h() to determine the entry of each key. When we want to insert/delete/find a key k we look for it in the entry h(k) in the array. Notice that this way, it is not necessary to have an order among the elements of the table.

Example of usage Take for example the login names of the students in the dast course. There are about 300 login names. If we will use a binary search tree, a tree of height 8 will be created. We can store the login names in a hash table of 100 entries using h(k)=k mod 100 hash function. The next slide presents a possible spread of login names on entry.

Example of usage The x-axis describes the number of items in an entry. The y-axis describes how many entries with this load exist.

Example of usage Notice that even if the spread of login names was perfect, there would have been 3 names in an entry. (This spread considered to be good.) Searching a login name with binary search tree: Since half of the elements are in the leaves it would take 8 operations to find them. Searching a login name with the hash table: The worst case is still 8, but the search for most of the elements (about 80% of them) will take about half than that.

Example of usage Two questions arise from the example: What is the best hash functions to use, and how do we find it? What happens when several keys have the same entry? (clearly it might happen, since U is much larger than m).

How to choose hash functions The crucial point: the hash function should “ spread ” the keys of U equally among all the entries of the array. Unfortunately, since we don ’ t know in advance the keys that we ’ ll get from U, this can be done only approximately. Remark: the hash functions usually assume that the keys are numbers. We ’ ll discuss next class what to do if the keys are not numbers.

The division method If we have a table of size m, we can use the hash function Some values of m are better than others: Good m ’ s are prime numbers not too close to 2 p. Bad choice is 2 p - the function uses only the p less significant bits of k. Likewise - if keys are decimal using 10 p is bad. A bad choice example: if m=100 and key=3674 a decimal number then gives 74.

The division method A good choice example: if we have |U|=2000, and we want each search to take (on average) 3 operations, we can choose the closest primal number to 2000/3, m= , , …

More About Hash Tables Next class