Priority Queue Sorting

Size: px
Start display at page:

Download "Priority Queue Sorting"

Transcription

1 Priority Queue Sorting We can use a priority queue to sort a list of comparable elements 1. Insert the elements one by one with a series of insert operations 2. Remove the elements in sorted order with a series of removemin operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) n Input list S, comparator C for the elements of S n Output list S sorted in increasing order according to C P priority queue with comparator C while S.isEmpty () e S.remove(S.first ()) P.insert (e, ) while P.isEmpty() e P.removeMin().getKey() S.addLast(e) 2014 Goodrich, Tamassia, Goldwasser Heaps 1

2 Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: 1. Inserting the elements into the priority queue with n insert operations takes O(n) time 2. Removing the elements in sorted order from the priority queue with n removemin operations takes time proportional to n + n Selection-sort runs in O(n 2 ) time 2014 Goodrich, Tamassia, Goldwasser Heaps 2

3 Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () 2014 Goodrich, Tamassia, Goldwasser Heaps 3

4 Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort: n Inserting the elements into the priority queue with n insert operations takes time proportional to n n Removing the elements in sorted order from the priority queue with a series of n removemin operations takes O(n) time Insertion-sort runs in O(n 2 ) time 2014 Goodrich, Tamassia, Goldwasser Heaps 4

5 Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) (g) (2,3,4,5,7,8,9) () 2014 Goodrich, Tamassia, Goldwasser Heaps 5

6 In-place Insertion-Sort Instead of using an external data structure, we can implement selectionsort and insertion-sort inplace A portion of the input sequence itself serves as the priority queue For in-place insertion-sort n We keep sorted the initial portion of the sequence n We can use swaps instead of modifying the sequence Goodrich, Tamassia, Goldwasser Heaps 6

7 Heap Sort Consider a priority queue with n items implemented by means of a heap n n the space used is O(n) methods insert and removemin take O(log n) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort n methods size, isempty, and min take time O(1) time Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selectionsort 2014 Goodrich, Tamassia, Goldwasser Heaps 7

8 Heap Sort Create a heap Do removemin repeatedly till head becomes empty To do an in place sort, we move deleted element to end of heap Heaps 8

9 Heap Sort Heaps 9

10 Heap Sort Heaps 10

11 Heap Sort Heaps 11

12 Heap Sort Heaps 12

13 Heap Sort Heaps 13

14 Heap Sort Heaps 14

15 Heap Sort Heaps 15

16 Heap Sort Heaps 16

17 Heap Sort Heaps 17

18 Heap Sort Heaps 18

19 Heap Sort Heaps 19

20 Heapsort - Analysis Create a heap - O(n) Do removemin repeatedly till head becomes empty n O(log n) + O(log n-1) + O(log n-2) + O(1) = O(n log n) To do an in place sort, we move deleted element to end of heap Heaps 20

21 Maps

22 Maps A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting, and deleting items Multiple entries with the same key are not allowed Applications: n address book n student-record database 2014 Goodrich, Tamassia, Goldwasser Maps 22

23 The Map ADT get(k): if the map M has an entry with key k, return its associated value; else, return null put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k remove(k): if the map M has an entry with key k, remove it from M and return its associated value; else, return null size(), isempty() entryset(): return an iterable collection of the entries in M keyset(): return an iterable collection of the keys in M values(): return an iterator of the values in M 2014 Goodrich, Tamassia, Goldwasser Maps 23

24 Example Operation Output Map isempty() true Ø put(5,a) null (5,A) put(7,b) null (5,A),(7,B) put(2,c) null (5,A),(7,B),(2,C) put(8,d) null (5,A),(7,B),(2,C),(8,D) put(2,e) C (5,A),(7,B),(2,E),(8,D) get(7) B (5,A),(7,B),(2,E),(8,D) get(4) null (5,A),(7,B),(2,E),(8,D) get(2) E (5,A),(7,B),(2,E),(8,D) size() 4 (5,A),(7,B),(2,E),(8,D) remove(5) A (7,B),(2,E),(8,D) remove(2) E (7,B),(8,D) get(2) null (7,B),(8,D) isempty() false (7,B),(8,D) 2014 Goodrich, Tamassia, Goldwasser Maps 24

25 A Simple List-Based Map We can implement a map using an unsorted list n We store the items of the map in a list S (based on a doubly linked list), in arbitrary order header nodes/positions trailer 9 c 6 c 5 c 8 c entries 2014 Goodrich, Tamassia, Goldwasser Maps 25

26 The get(k) Algorithm Algorithm get(k): B = S.positions() {B is an iterator of the positions in S} while B.hasNext() do p = B.next() { the next position in B } if p.element().getkey() = k then return p.element().getvalue() return null {there is no entry with key equal to k} 2014 Goodrich, Tamassia, Goldwasser Maps 26

27 The put(k,v) Algorithm Algorithm put(k,v): B = S.positions() while B.hasNext() do p = B.next() if p.element().getkey() = k then t = p.element().getvalue() S.set(p,(k,v)) return t {return the old value} S.addLast((k,v)) n = n + 1 {increment variable storing number of entries} return null { there was no entry with key equal to k } 2014 Goodrich, Tamassia, Goldwasser Maps 27

28 The remove(k) Algorithm Algorithm remove(k): B =S.positions() while B.hasNext() do p = B.next() if p.element().getkey() = k then t = p.element().getvalue() S.remove(p) n = n 1 {decrement number of entries} return t {return the removed value} return null {there is no entry with key equal to k} 2014 Goodrich, Tamassia, Goldwasser Maps 28

29 Performance of a List-Based Map Performance: n n n put takes O(1) time since we can insert the new item at the beginning or at the end of the sequence get and remove take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key Average case time? The unsorted list implementation is effective only for maps of small size or for maps in which puts are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation) Maps 29

30 Different Data Structures to Implement Map ADT arrays, linked lists (inefficient) Binary Trees Hash Tables Red/Black Trees AVL Trees B-Trees Java n java.util.dictionary - abstract class n java.util.map - interface Maps 30

31 Binary Search 2014 Goodrich, Tamassia, Goldwasser Maps 31

32 Direct Addressing an array that is indexed by the key n O(1) - time n O(r) space - where r is the range of numbers n wastage of space A B C D XXXX-XXXX XXXX-XXXX XXXX-XXXX Maps 32

33 Hash Tables

34 Intuitive Notion of a Map Intuitively, a map M supports the abstraction of using keys as indices with a syntax such as M[k]. As a mental warm-up, consider a restricted setting in which a map with n items uses keys that are known to be integers in a range from 0 to N 1, for some N n Goodrich, Tamassia, Goldwasser Hash Tables 34

35 Hash Table Solution O(1) - expected time O(n+m) - space, where m is size of the table instead of a one-to-one map between the key values and array locations, find a function to map the large range into one which we can manage n e.g., key value modulo size of array, and use that as an index n Insert ( , C) into a hashed array of size 5, mod 5 = 3 XXXX-XXXX XXXX-XXXX XXXX-XXXX XXXX-XXXX C Hash Tables 35

36 More General Kinds of Keys But what should we do if our keys are not integers in the range from 0 to N 1? n Use a hash function to map general keys to corresponding indices in a table. n For instance, the last four digits of a Identification number Goodrich, Tamassia, Goldwasser Hash Tables 36

37 Hash Functions and Hash Tables A hash function h maps keys of a given type to integers in a fixed interval [0, N - 1] Example: h(x) = x mod N is a hash function for integer keys The integer h(x) is called the hash value of key x A hash table for a given key type consists of n Hash function h n Array (called table) of size N When implementing a map with a hash table, the goal is to store item (k, o) at index i = h(k) 2014 Goodrich, Tamassia, Goldwasser Hash Tables 37

38 Example (1,D) (25,C) (3,F) (14,Z) (6,A) (39,C) (7,Q) Collision Two different keys resulting in the same hash value Hash Tables 38

39 Hash Functions Need to choose a good hash function n quick to compute n uniform distribution of keys throughout the table n good hash functions are very rare. How to deal with hashing non-integer keys n find some way to turn keys into integers n use standard hash functions on these integers Hash Tables 39

40 Hash Functions (2) A hash function is usually specified as the composition of two functions: Hash code: h 1 : keys integers Compression function: h 2 : integers [0, N - 1] The hash code is applied first, and the compression function is applied next on the result, i.e., h(x) = h 2 (h 1 (x)) The goal of the hash function is to disperse the keys in an apparently random way 2014 Goodrich, Tamassia, Goldwasser Hash Tables 40

41 Hash Codes Integer cast: n n n We reinterpret the bits of the key as an integer Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in Java) For keys of length greater than the number of bits of the integer type, ignore the exceeding bits Component sum: n n n We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in Java) Not a good choice for strings Hash Tables 41

42 Hash Codes (2) Polynomial accumulation: n n n We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) a 0 a 1 a n-1 We evaluate the polynomial p(z) = a 0 + a 1 z + a 2 z2 + + a n-1 z n-1 at a fixed value z, ignoring overflows Especially suitable for strings (e.g., the choice z = 33 gives at most 6 collisions on a set of 50,000 English words) Polynomial p(z) can be evaluated in O(n) time using Horner s rule: n The following polynomials are successively computed, each from the previous one in O(1) time p 0 (z) = a n-1 p i (z) = a n-i-1 + zp i-1 (z) (i = 1, 2,, n -1) We have p(z) = p n-1 (z) 2014 Goodrich, Tamassia, Goldwasser Hash Tables 42

43 Compression Functions Division: n Use the remainder n h 2 (y) = y mod N w y is the key w N is size of the table n how to choose N? Consider (200, 205,210,215,220, 600) N = 100 N = 101 Hash Tables 43

44 Compression Functions (2) Division: n Use the remainder n h 2 (y) = y mod N w y is the key w N is size of the table n how to choose N? n N = b x (bad) Consider (200,205,210,215,220, 600) N = 100 N = 101 w N is a power of 2, h 2 (y) gives the x least significant bits of y. w all keys with the same ending go to the same place n N is prime (good) w helps ensure uniform distribution Hash Tables 44

45 Compression Functions (3) Multiply, Add and Divide (MAD): n h 2 (y) = [(ay + b) mod p ]mod N n a and b are nonnegative integers such that a mod N 0 n Otherwise, every integer would map to the same value b n p is a prime number larger than N n a and b are chosen at random from the interval [0, p-1], with a > 0 Hash Tables 45

46 Compression Functions (4) For any choice of hash function, there always exists a bad set of keys You could choose only these keys, resulting in all of them getting mapped to the same slot. n reduction in performance. Solution n collection of hash functions n a random hash function n choose a hash function that is independent of the keys Hash Tables 46

47 Collision Handling Collisions occur when different elements are mapped to the same cell Separate Chaining: let each cell in the table point to a linked list of entries that map there n complexity depends on load factor Separate chaining is simple, but requires additional memory outside the table Goodrich, Tamassia, Goldwasser Hash Tables 47

48 Map with Separate Chaining Delegate operations to a list-based map at each cell: Algorithm get(k): return A[h(k)].get(k) Algorithm put(k,v): t = A[h(k)].put(k,v) if t = null then n = n + 1 return t Algorithm remove(k): t = A[h(k)].remove(k) if t null then n = n - 1 return t {k is a new key} {k was found} 2014 Goodrich, Tamassia, Goldwasser Hash Tables 48

49 Linear Probing Open addressing: the colliding item is placed in a different cell of the table n load factor is at most 1 Linear probing: handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a probe Colliding items lump together, causing future collisions to cause a longer sequence of probes Hash Tables 49

50 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 50

51 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 51

52 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 52

53 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 53

54 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 54

55 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 55

56 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 56

57 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 57

58 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod Goodrich, Tamassia, Goldwasser Hash Tables 58

59 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod Goodrich, Tamassia, Goldwasser Hash Tables 59

60 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 60

61 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order Goodrich, Tamassia, Goldwasser Hash Tables 61

62 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod Goodrich, Tamassia, Goldwasser Hash Tables 62

63 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod 13 w h(x) = (x+2) mod Goodrich, Tamassia, Goldwasser Hash Tables 63

64 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod 13 w h(x) = (x+2) mod Goodrich, Tamassia, Goldwasser Hash Tables 64

65 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod 13 w h(x) = (x+2) mod Goodrich, Tamassia, Goldwasser Hash Tables 65

66 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod 13 w h(x) = (x+2) mod Goodrich, Tamassia, Goldwasser Hash Tables 66

67 Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order w h(x) = (x+1) mod 13 w h(x) = (x+2) mod Goodrich, Tamassia, Goldwasser Hash Tables 67

68 Search with Linear Probing Consider a hash table A that uses linear probing get(k) n n We start at cell h(k) We probe consecutive locations until one of the following occurs w An item with key k is found, or w An empty cell is found, or w N cells have been unsuccessfully probed Algorithm get(k) i h(k) p 0 repeat c A[i] if c = return null else if c.getkey () = k return c.getvalue() else i (i + 1) mod N p p + 1 until p = N return null 2014 Goodrich, Tamassia, Goldwasser Hash Tables 68

69 Search with Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order n Search for Hash Tables 69

70 Updates with Linear Probing To handle insertions and deletions, we introduce a special object, called DEFUNCT, which replaces deleted elements remove(k) n We search for an entry with key k n n If such an entry (k, o) is found, we replace it with the special item DEFUNCT and we return element o Else, we return null Hash Tables 70

71 Update with Linear Probing (2) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order n Remove 57 n Search for Hash Tables 71

72 Update with Linear Probing (3) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order n Remove 57 n Search for X Hash Tables 72

73 Updates with Linear Probing (4) To handle insertions and deletions, we introduce a special object, called DEFUNCT, which replaces deleted elements put(k, o) n We throw an exception if the table is full n We start at cell h(k) n We probe consecutive cells until one of the following occurs w A cell i is found that is either empty or stores DEFUNCT, or w N cells have been unsuccessfully probed n We store (k, o) in cell i 2014 Goodrich, Tamassia, Goldwasser Hash Tables 73

74 Update with Linear Probing (5) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order n Remove 57 n Search for 32 n Insert X Hash Tables 74

75 Update with Linear Probing (5) Example: n h(x) = x mod 13 n Insert keys 18, 41, 22, 44, 57, 32, 31, 73, in this order n Remove 57 n Search for 32 n Insert Hash Tables 75

76 Double Hashing Double hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series (i + jd(k)) mod N for j = 0, 1,, N - 1 The secondary hash function d(k) cannot have zero values The table size N must be a prime to allow probing of all the cells 2014 Goodrich, Tamassia, Goldwasser Hash Tables 76

77 Example of Double Hashing Consider a hash table storing integer keys that handles collision with double hashing n N = 13 n h(k) = k mod 13 n d(k) = 7 - k mod 7 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order k h (k ) d (k ) Probes Goodrich, Tamassia, Goldwasser Hash Tables 77

78 Performance of Hashing In the worst case, searches, insertions and removals on a hash table take O(n) time The worst case occurs when all the keys inserted into the map collide The load factor α = n/n affects the performance of a hash table Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is 1 / (1 - α) The expected running time of all the dictionary ADT operations in a hash table is O(1) In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables: n n n small databases compilers browser caches 2014 Goodrich, Tamassia, Goldwasser Hash Tables 78

Data Structures Lecture 12

Data Structures Lecture 12 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 12 Advance ADTs Maps and Hash Tables Maps A map models a searchable collection

More information

Maps, Hash Tables and Dictionaries. Chapter 10.1, 10.2, 10.3, 10.5

Maps, Hash Tables and Dictionaries. Chapter 10.1, 10.2, 10.3, 10.5 Maps, Hash Tables and Dictionaries Chapter 10.1, 10.2, 10.3, 10.5 Outline Maps Hashing Dictionaries Ordered Maps & Dictionaries Outline Maps Hashing Dictionaries Ordered Maps & Dictionaries Maps A map

More information

CSED233: Data Structures (2017F) Lecture10:Hash Tables, Maps, and Skip Lists

CSED233: Data Structures (2017F) Lecture10:Hash Tables, Maps, and Skip Lists (2017F) Lecture10:Hash Tables, Maps, and Skip Lists Daijin Kim CSE, POSTECH dkim@postech.ac.kr Maps A map models a searchable collection of key-value entries The main operations of a map are for searching,

More information

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map This lecture Iterators Hash tables Formal coursework Iterators ( 5.4) An iterator abstracts the process of scanning through a collection of elements Methods of the ObjectIterator ADT: object object() boolean

More information

Dictionaries-Hashing. Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2)

Dictionaries-Hashing. Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2) Dictionaries-Hashing Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2) Dictionary The dictionary ADT models a searchable collection of key-element entries The main operations of a dictionary are searching,

More information

Hash Tables Hash Tables Goodrich, Tamassia

Hash Tables Hash Tables Goodrich, Tamassia Hash Tables 0 1 2 3 4 025-612-0001 981-101-0002 451-229-0004 Hash Tables 1 Hash Functions and Hash Tables A hash function h maps keys of a given type to integers in a fixed interval [0, N 1] Example: h(x)

More information

Dictionaries and Hash Tables

Dictionaries and Hash Tables Dictionaries and Hash Tables 0 1 2 3 025-612-0001 981-101-0002 4 451-229-0004 Dictionaries and Hash Tables 1 Dictionary ADT The dictionary ADT models a searchable collection of keyelement items The main

More information

Elementary Data Structures 2

Elementary Data Structures 2 Elementary Data Structures Priority Queues, & Dictionaries Priority Queues Sell 00 IBM $ Sell 300 IBM $0 Buy 00 IBM $9 Buy 400 IBM $8 Priority Queue ADT A priority queue stores a collection of items An

More information

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager Hash Tables What is a Dictionary? Container class Stores key-element pairs Allows look-up (find) operation Allows insertion/removal of elements May be unordered or ordered Dictionary Keys Must support

More information

HASH TABLES. Goal is to store elements k,v at index i = h k

HASH TABLES. Goal is to store elements k,v at index i = h k CH 9.2 : HASH TABLES 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND

More information

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS 0 1 2 025-612-0001 981-101-0002 3 4 451-229-0004 CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,

More information

Chapter 9: Maps, Dictionaries, Hashing

Chapter 9: Maps, Dictionaries, Hashing Chapter 9: 0 1 025-612-0001 2 981-101-0002 3 4 451-229-0004 Maps, Dictionaries, Hashing Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided

More information

Priority Queues 3/19/14

Priority Queues 3/19/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Priority Queues Priority Queues 1 Priority

More information

csci 210: Data Structures Maps and Hash Tables

csci 210: Data Structures Maps and Hash Tables csci 210: Data Structures Maps and Hash Tables Summary Topics the Map ADT Map vs Dictionary implementation of Map: hash tables READING: GT textbook chapter 9.1 and 9.2 Map ADT A Map is an abstract data

More information

CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms CS2210 Data Structures and Algorithms Lecture 5: Hash Tables Instructor: Olga Veksler 0 1 2 3 025-612-0001 981-101-0002 4 451-229-0004 2004 Goodrich, Tamassia Outline Hash Tables Motivation Hash functions

More information

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015. Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Hash Tables xkcd. http://xkcd.com/221/. Radom Number. Used with permissio uder Creative

More information

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14 Heaps 3// Presentation for use with the textbook Data Structures and Algorithms in Java, th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 0 Heaps Heaps Recall Priority Queue ADT

More information

Stores a collection of elements each with an associated key value

Stores a collection of elements each with an associated key value CH9. PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 201) PRIORITY QUEUES Stores a collection

More information

Topics on the Midterm

Topics on the Midterm Midterm Review Topics on the Midterm Data Structures & Object-Oriented Design Run-Time Analysis Linear Data Structures The Java Collections Framework Recursion Trees Priority Queues & Heaps Maps, Hash

More information

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT COMP0: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Outline Priority Queues Heaps Adaptable Priority Queues Session, 0 http://www.cse.unsw.edu.au/~cs0 Priority Queue ADT Priority Queues

More information

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES 2 5 6 9 7 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley, 2014

More information

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved Introducing Hashing Chapter 21 Contents What Is Hashing? Hash Functions Computing Hash Codes Compressing a Hash Code into an Index for the Hash Table A demo of hashing (after) ARRAY insert hash index =

More information

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques Hashing Manolis Koubarakis 1 The Symbol Table ADT A symbol table T is an abstract storage that contains table entries that are either empty or are pairs of the form (K, I) where K is a key and I is some

More information

Priority Queues Goodrich, Tamassia. Priority Queues 1

Priority Queues Goodrich, Tamassia. Priority Queues 1 Priority Queues Priority Queues 1 Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry

More information

CSE 214 Computer Science II Searching

CSE 214 Computer Science II Searching CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a

More information

CH 8. HEAPS AND PRIORITY QUEUES

CH 8. HEAPS AND PRIORITY QUEUES CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries 2013-2 DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 22, 2013 Advanced

More information

Data Structures and Algorithms " Priority Queues!

Data Structures and Algorithms  Priority Queues! Data Structures and Algorithms " Priority Queues! Outline" Priority Queues! Heaps! Adaptable Priority Queues! Priority Queues" Priority Queue ADT" A priority queue stores a collection of entries! Each

More information

CS 241 Analysis of Algorithms

CS 241 Analysis of Algorithms CS 241 Analysis of Algorithms Professor Eric Aaron Lecture T Th 9:00am Lecture Meeting Location: OLB 205 Business HW5 extended, due November 19 HW6 to be out Nov. 14, due November 26 Make-up lecture: Wed,

More information

Heaps Goodrich, Tamassia. Heaps 1

Heaps Goodrich, Tamassia. Heaps 1 Heaps Heaps 1 Recall Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k

More information

CH. 8 PRIORITY QUEUES AND HEAPS

CH. 8 PRIORITY QUEUES AND HEAPS CH. 8 PRIORITY QUEUES AND HEAPS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps (2017F) Lecture9: Priority Queues and Heaps Daijin Kim CSE, POSTECH dkim@postech.ac.kr Priority Queue ADT A priority queue stores a coll ection of entries Each entry is a pair (key, value) Main methods

More information

Heaps and Priority Queues

Heaps and Priority Queues Heaps and Priority Queues Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce Priority Queue ADT To discuss and illustrate Priority Queues for sorting

More information

Entry and Priority Queue ADTs

Entry and Priority Queue ADTs Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Adaptable Priority Queues 3 a 5 g 4 e Adaptable

More information

Logic, Algorithms and Data Structures ADT:s & Hash tables. By: Jonas Öberg, Lars Pareto

Logic, Algorithms and Data Structures ADT:s & Hash tables. By: Jonas Öberg, Lars Pareto Logic, Algorithms and Data Structures ADT:s & Hash tables M7 By: Jonas Öberg, Lars Pareto Others Queues Priority queues Trees Arrays.. Lots of lists Some example properties of a list: access(l, n) :

More information

Introduction to Hashing

Introduction to Hashing Lecture 11 Hashing Introduction to Hashing We have learned that the run-time of the most efficient search in a sorted list can be performed in order O(lg 2 n) and that the most efficient sort by key comparison

More information

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 21 (Based on Paul Kube course materials) CSE 100 Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table cost

More information

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum Table ADT and Sorting Algorithm topics continuing (or reviewing?) CS 24 curriculum A table ADT (a.k.a. Dictionary, Map) Table public interface: // Put information in the table, and a unique key to identify

More information

Hash Tables. Gunnar Gotshalks. Maps 1

Hash Tables. Gunnar Gotshalks. Maps 1 Hash Tables Maps 1 Definition A hash table has the following components» An array called a table of size N» A mathematical function called a hash function that maps keys to valid array indices hash_function:

More information

Data Structures Lecture 7

Data Structures Lecture 7 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 7 Recap We have talked about object oriented programing Chapter 1, 2,

More information

Dictionaries. 2/17/2006 Dictionaries 1

Dictionaries. 2/17/2006 Dictionaries 1 Dictionaries < 6 > 1 4 = 8 9 /17/006 Dictionaries 1 Outline and Reading Dictionary ADT ( 9.3) Log file ( 9.3.1) Binary search ( 9.3.3) Lookup table ( 9.3.3) Binary search tree ( 10.1) Search ( 10.1.1)

More information

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9

More information

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1 Hash Tables Outline Definition Hash functions Open hashing Closed hashing collision resolution techniques Efficiency EECS 268 Programming II 1 Overview Implementation style for the Table ADT that is good

More information

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end Adding a Node to (Min) Heap (F) Lecture16: Heap Sort Takes log steps if nodes are added Step 1: Add node at the end Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Step 2: Make swap if parent is bigger Step

More information

Understand how to deal with collisions

Understand how to deal with collisions Understand the basic structure of a hash table and its associated hash function Understand what makes a good (and a bad) hash function Understand how to deal with collisions Open addressing Separate chaining

More information

Priority queues. Priority queues. Priority queue operations

Priority queues. Priority queues. Priority queue operations Priority queues March 30, 018 1 Priority queues The ADT priority queue stores arbitrary objects with priorities. An object with the highest priority gets served first. Objects with priorities are defined

More information

Lecture 7: Efficient Collections via Hashing

Lecture 7: Efficient Collections via Hashing Lecture 7: Efficient Collections via Hashing These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. 1 Announcements Lab 6 due Friday Lab 7 out tomorrow

More information

Data Structures and Algorithms. Chapter 7. Hashing

Data Structures and Algorithms. Chapter 7. Hashing 1 Data Structures and Algorithms Chapter 7 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Hash Table and Hashing

Hash Table and Hashing Hash Table and Hashing The tree structures discussed so far assume that we can only work with the input keys by comparing them. No other operation is considered. In practice, it is often true that an input

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 2015 Goodrich and Tamassia

More information

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Introduction hashing: a technique used for storing and retrieving information as quickly as possible. Lecture IX: Hashing Introduction hashing: a technique used for storing and retrieving information as quickly as possible. used to perform optimal searches and is useful in implementing symbol tables. Why

More information

CS1020 Data Structures and Algorithms I Lecture Note #15. Hashing. For efficient look-up in a table

CS1020 Data Structures and Algorithms I Lecture Note #15. Hashing. For efficient look-up in a table CS1020 Data Structures and Algorithms I Lecture Note #15 Hashing For efficient look-up in a table Objectives 1 To understand how hashing is used to accelerate table lookup 2 To study the issue of collision

More information

COMP171. Hashing.

COMP171. Hashing. COMP171 Hashing Hashing 2 Hashing Again, a (dynamic) set of elements in which we do search, insert, and delete Linear ones: lists, stacks, queues, Nonlinear ones: trees, graphs (relations between elements

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

Implementations III 1

Implementations III 1 Implementations III 1 Agenda Hash tables Implementation methods Applications Hashing vs. binary search trees The binary heap Properties Logarithmic-time operations Heap construction in linear time Java

More information

Lecture 4. Hashing Methods

Lecture 4. Hashing Methods Lecture 4 Hashing Methods 1 Lecture Content 1. Basics 2. Collision Resolution Methods 2.1 Linear Probing Method 2.2 Quadratic Probing Method 2.3 Double Hashing Method 2.4 Coalesced Chaining Method 2.5

More information

Final Exam. CSE 2011 Prof. J. Elder Last Updated: :41 PM

Final Exam. CSE 2011 Prof. J. Elder Last Updated: :41 PM Final Exam Ø Tue, 17 Apr 2012 19:00 22:00 LAS B Ø Closed Book Ø Format similar to midterm Ø Will cover whole course, with emphasis on material after midterm (maps, hashing, binary search trees, sorting,

More information

Chapter 2: Basic Data Structures

Chapter 2: Basic Data Structures Chapter 2: Basic Data Structures Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority Queues and Heaps Dictionaries and Hash Tables Spring 2014 CS 315 2 Two

More information

CS 350 : Data Structures Hash Tables

CS 350 : Data Structures Hash Tables CS 350 : Data Structures Hash Tables David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Hash Tables Although the various tree structures

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Final Exam. EECS 2011 Prof. J. Elder - 1 - Final Exam Ø Wed Apr 11 2pm 5pm Aviva Tennis Centre Ø Closed Book Ø Format similar to midterm Ø Will cover whole course, with emphasis on material after midterm (maps and hash tables, binary search, loop

More information

Data Structures and Algorithms. Roberto Sebastiani

Data Structures and Algorithms. Roberto Sebastiani Data Structures and Algorithms Roberto Sebastiani roberto.sebastiani@disi.unitn.it http://www.disi.unitn.it/~rseba - Week 07 - B.S. In Applied Computer Science Free University of Bozen/Bolzano academic

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 4: Sets, Dictionaries and Hash Tables Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo)

More information

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES Final Examination Instructors: RESeviora and LTahvildari 3 hrs, Apr, 200 Name: Student ID:

More information

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation Announcements HW1 PAST DUE HW2 online: 7 questions, 60 points Nat l Inst visit Thu, ok? Last time: Continued PA1 Walk Through Dictionary ADT: Unsorted Hashing Today: Finish up hashing Sorted Dictionary

More information

CS 2412 Data Structures. Chapter 10 Sorting and Searching

CS 2412 Data Structures. Chapter 10 Sorting and Searching CS 2412 Data Structures Chapter 10 Sorting and Searching Some concepts Sorting is one of the most common data-processing applications. Sorting algorithms are classed as either internal or external. Sorting

More information

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE Exam Datastrukturer DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE Day: 2017-05-31, Time: 8:30-12.30, Place: SB-MU Course responsible Alex Gerdes, tel. 031-772 6154. Will visit at around 9:30 and 11:00.

More information

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Hash Open Indexing Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up Consider a StringDictionary using separate chaining with an internal capacity of 10. Assume our buckets are implemented

More information

Merge Sort Goodrich, Tamassia Merge Sort 1

Merge Sort Goodrich, Tamassia Merge Sort 1 Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Merge Sort 1 Review of Sorting Selection-sort: Search: search through remaining unsorted elements for min Remove: remove

More information

The java.util.list ADT

The java.util.list ADT Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Lists and Iterators 2014 Goodrich, Tamassia,

More information

Data Structures And Algorithms

Data Structures And Algorithms Data Structures And Algorithms Hashing Eng. Anis Nazer First Semester 2017-2018 Searching Search: find if a key exists in a given set Searching algorithms: linear (sequential) search binary search Search

More information

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40 Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

More information

Lists ADT and Iterators

Lists ADT and Iterators Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Lists ADT and Iterators 2014 Goodrich, Tamassia,

More information

Hashing Techniques. Material based on slides by George Bebis

Hashing Techniques. Material based on slides by George Bebis Hashing Techniques Material based on slides by George Bebis https://www.cse.unr.edu/~bebis/cs477/lect/hashing.ppt The Search Problem Find items with keys matching a given search key Given an array A, containing

More information

CS/COE 1501

CS/COE 1501 CS/COE 1501 www.cs.pitt.edu/~lipschultz/cs1501/ Hashing Wouldn t it be wonderful if... Search through a collection could be accomplished in Θ(1) with relatively small memory needs? Lets try this: Assume

More information

HASH TABLES. Hash Tables Page 1

HASH TABLES. Hash Tables Page 1 HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric

More information

Arrays. Array Definition

Arrays. Array Definition Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Arrays Arrays 1 Array Definition An array

More information

Algorithms and Data Structures

Algorithms and Data Structures Priority Queues and Heaps Page 1 BFH-TI: Softwareschule Schweiz Priority Queues and Heaps Dr. CAS SD01 Priority Queues and Heaps Page 2 Outline Priority Queues Heaps Heap-Based Priority Queues Priority

More information

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5. 5. Hashing 5.1 General Idea 5.2 Hash Function 5.3 Separate Chaining 5.4 Open Addressing 5.5 Rehashing 5.6 Extendible Hashing Malek Mouhoub, CS340 Fall 2004 1 5. Hashing Sequential access : O(n). Binary

More information

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort Sorting Hiroaki Kobayashi 1 Outline Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort Merge Sort Lower Bound on Comparison-Based Sorting Bucket Sort and Radix Sort Hiroaki

More information

Hash Table. Ric Glassey

Hash Table. Ric Glassey Hash Table Ric Glassey glassey@kth.se Overview Hash Table Aim: Describe the map abstract data type with efficient insertion, deletion and search operations Motivation: List data structures are divided

More information

Practical Session 8- Hash Tables

Practical Session 8- Hash Tables Practical Session 8- Hash Tables Hash Function Uniform Hash Hash Table Direct Addressing A hash function h maps keys of a given type into integers in a fixed interval [0,m-1] 1 Pr h( key) i, where m is

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

Lecture 7. Transform-and-Conquer

Lecture 7. Transform-and-Conquer Lecture 7 Transform-and-Conquer 6-1 Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance simplification)

More information

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES Final Examination Instructor: R.E.Seviora 9-12 AM, Dec 14, 2002 Name (last, first) Student

More information

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4.

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4. More Data Structures Priority Queues and Heaps Priority Queues, Comparators, Locators, Dictionaries More Data Structures v. More Data Structures v. Priority Queue ADT (.4.) Total Order Relation A priority

More information

( ) n 5. Test 1 - Closed Book

( ) n 5. Test 1 - Closed Book Name Test 1 - Closed Book Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each 1. During which operation on a leftist heap may subtree swaps be needed? A. DECREASE-KEY

More information

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods 1 2 Given : Stack A, Stack B 3 // based on requirement b will be reverse of

More information

Measuring Input size. Last lecture recap.

Measuring Input size. Last lecture recap. Measuring Input size Last lecture recap. Linear data structures Compiled from http://www.eagle.tamut.edu/faculty/igor/cis-305.htm Abstract Data Type Data Structures. Main Notions and Definitions. Data

More information

HO #13 Fall 2015 Gary Chan. Hashing (N:12)

HO #13 Fall 2015 Gary Chan. Hashing (N:12) HO #13 Fall 2015 Gary Chan Hashing (N:12) Outline Motivation Hashing Algorithms and Improving the Hash Functions Collisions Strategies Open addressing and linear probing Separate chaining COMP2012H (Hashing)

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Merge Sort

Merge Sort Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conuer Divide-and conuer is a general algorithm design paradigm: n Divide: divide the input data S in two disjoint subsets S 1 and

More information

Hashing (Κατακερματισμός)

Hashing (Κατακερματισμός) Hashing (Κατακερματισμός) Manolis Koubarakis 1 The Symbol Table ADT A symbol table T is an abstract storage that contains table entries that are either empty or are pairs of the form (K, I) where K is

More information

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far Chapter 5 Hashing 2 Introduction hashing performs basic operations, such as insertion, deletion, and finds in average time better than other ADTs we ve seen so far 3 Hashing a hash table is merely an hashing

More information

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management Hashing Symbol Table Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management In general, the following operations are performed on

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

Hash Tables. Hashing Probing Separate Chaining Hash Function

Hash Tables. Hashing Probing Separate Chaining Hash Function Hash Tables Hashing Probing Separate Chaining Hash Function Introduction In Chapter 4 we saw: linear search O( n ) binary search O( log n ) Can we improve the search operation to achieve better than O(

More information

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators) _ UWNetID: Lecture Section: A CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will give

More information

AAL 217: DATA STRUCTURES

AAL 217: DATA STRUCTURES Chapter # 4: Hashing AAL 217: DATA STRUCTURES The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions, and finds in constant average

More information