TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT 20065.1 TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,

Slides:



Advertisements
Similar presentations
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:
Advertisements

Skip List & Hashing CSE, POSTECH.
Hashing as a Dictionary Implementation
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Hashing Techniques.
Hashing CS 3358 Data Structures.
Maps, Dictionaries, Hashtables
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT TDDB56 – TDDB57 Lecture 5 Trees Terminology, Implementation Binary Search Trees.
TDDB56 DALGOPT-D – TDDB57 DALG-C Lecture 4 Jan Maluszynski - HT TDDB56 TDDB57 Lecture 4 Maps and Dictionaries Binary Search, Hashing, Skip Lists.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables1 Part E Hash Tables  
TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search.
Hash Tables1 Part E Hash Tables  
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Lecture 10: Search Structures and Hashing
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Hash Tables1   © 2010 Goodrich, Tamassia.
CS121 Data Structures CS121 © JAS 2004 Tables An abstract table, T, contains table entries that are either empty, or pairs of the form (K, I) where K is.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
Data Structures and Algorithms Lecture (Searching) Instructor: Quratulain Date: 4 and 8 December, 2009 Faculty of Computer Science, IBA.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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:
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Advanced Associative Structures
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Ch Hash Tables Array or linked list Binary search trees
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map, ADT Dictionary Binary Search, Hashing, Binary Search Trees

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Content: ADTs Map/Dictionary GT p , p.389 Implemented as: –Arrays/binary search GT –Hash tables GT 9.2, 9.3.2, –Binary Search Trees GT

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT ADT Map, ADT Dictionary Domain: sets of pairs where k key, i information, ADT Map: a key may appear in at most one entry ! size, isEmpty, get(k), put(k,v), remove(k) Example: examination list: personal number used as key, ADT Dictionary: a key may appear in different entries ! size, isEmpty, find(k), findAll(k), insert(k,v), remove(k,v). Example: Telephone directory (several numbers per person)

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Implementations: Map, Dictionary Table/Array – seq. of memory chunks of equal size –Unorderedno particular order between T[i] and T[i+1] –Ordered T[i] < T[i+1] Linked Lists –Unordered –Ordered Hashing [Goodrich/Tamassia 9.2] Binary Search Trees [Goodrich/Tamassia 10.1]

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Array representations of Set and Dictionary For unordered keys: (unordered set / dictionary) lookUp by linear search unsuccessful lookup: n comparisons  O(n) time successful lookup, but worst case: n comparisons  O(n) time successful lookup, average case with uniform distribution of requests : comparisons  O(n) time

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Array representations of... (cont.) For ordered keys: (ordered set / dictionary) LookUp by binary search: Function binLookup(table T[min..max], key k) if (min>max) then return NULL mid   min+max)/2  if k = key(T[mid]) then return T[mid] else if k < key(T[mid]) then return binLookup(T[min..mid-1) else return binLookup(T[mid+1..max)

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Analysis of binary search All possible executions represented by a binary tree (a binary decision tree): The max height of the tree is the max no of iterations of the algorithm Height is O(lg(n)) Worst case = max un-balance, searched for key in deepest leaf...

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Hash Tables To implement ADT Map/ADT –Use a table T [0..m] –Find a (hash) function h : key  i  [0..m] such that k 1  k 2  h(k 1 )  h(k 2 ) –Store each key-element pair as in T[ h(k 1 ) ] –Commonly used: h(k) = k mod m [G&T] terminology: h(k) = g(hc(k)) hash code hc: Keys  Int; compression function g: Int  [0.. m ]

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Hash Table In practice, hash functions do not give unique values h(k 1 ) = h(k 2 ) for some k 1  k 2 We need conflict resolution

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Hash Table – Collision Resolution Two principles for handling collisions: 1.Chaining: keep conflicting data in linked lists –Separate Chaining: Keep a linked list of the colliding ones outside the table –Coalesced Chaining: Store all items inside the table 2.Open Addressing: Store all items inside the table, and the index to use at collision is determined by an algorithm

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Hashing with Separate Chaining: Example Hash table of size: 13 Hash function h with h(k) = k mod 13 Store 10 integer keys: 54, 10, 18, 25, 28, 41, 38, 36, 12, 90

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Hashing with Separate Chaining: LookUp Given: key k, hash table T, hash function h 1.compute h(k) 2.search for k in the list pointed by T[h(k)] Notation: probe = one access to the linked list data structure 1 probe for accessing the list header (if nonempty) 1+1 probes for accessing the contents of the first element 1+2 probes for accessing the contents of the second element... A probe (just pointer dereferencing) takes constant time. How many probes P are needed to retrieve a hash table entry?

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Separate Chaining: Unsuccessful LookUp n data items m positions in the table Worst case: all items have the same hash value: P=1+n Average case: Hash values equally distributed among m : Average length  of the list:  n / m  n/m is called the load factor P=1+ 

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Separate Chaining: Successful LookUp Average case: Expected number P of probes for given key k… Access to T[h(k)] (beginning of a list L ): 1 Traversing L  k found after: (| L| +1)/2 Expected (or average) | L | corresponds to , thus: P = 

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Coalesced Chaining: items inside table (1) First step: store first element in table keep rest in separate lists The increase in space consumption is acceptable if key fields are small or hash table is quite full

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Coalesced Chaining: items inside table (2) Place data items in the table Extend them with pointers Resolve collisions by using the first free slot Chains may contain keys with different hash values… …but all keys with the same hash value appears in the same chain + better space utilization - table may become full

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Open Addressing Store all elements inside the table Use a fix algorithm to find a free slot Sequential / Linear Probing desired hash index j = h(k) in case of conflict go to the next free position If at end of the table, go to the beginning… -Close positions rapidly filled (primary clustering) -How to remove(k) ??

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Open Addressing – how to remove The element to remove may be part of a collision chain: can’t be directly removed! Two approaches: –Scan elements after, re-hash –Mark as “deleted” (if the next slot is non-empty)

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Double Hashing Second hashing function h2 computes increments in case of conflicts Increment beyond the table is taken modulo m = tableSize Linear probing is double hashing with h 2 (k) = 1 Requirements on h 2 : h 2 (k)  0 …for all k For each k, h 2 (k) has no common divisor with m  all table positions can be reached A common choice: h 2 (k)= q – (k mod q) for q < m prime, m prime (i.e., pick a prime less than the table size!)

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Binary Search Tree A binary search tree (BST) is a binary tree such that: Information associated with a node includes a key,  linear ordering of nodes determined by keys. The key of each node is: greater than (or equal) the keys of all left descendents, and smaller than (or equal) the keys of all right descendents. The leaves do not store any information Variant in [Goodrich &Tamassia]

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT ADT Map as Binary Search Tree… lookUp( k,v) : comparison controlled traversal if key ( v) = k then return k else if k < key(v) then LookUp (k, leftChild(v)) fails if no leftChild else LookUp (k, rightChild(v)) fails if no rightChild worst-case: height(T)+1 comparisons put( k, x) : add ( k, x) as new leaf on lookUp failure or update the node on lookUp success worst-case: height(T)+1 comparisons remove( k) : lookUp, then… if v is a leaf, remove v if v has one child u, replace v by u if v has two children, replace v by its inorder successor (alternatively: by its inorder predecessor) worst-case: height(T)+1 comparisons

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT BST:s are not unique Same data may generate different BST put : 1,2,4,5,8 put: 5, 2, 1, 4, 8

TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT Successful LookUp: Worst and Average case Worst Case BST BST degenerated to a linear sequence expected number of comparisons is ( n+1)/ 2 Balanced BST The depths of leaves differ by at most 1  O(log 2 n) comparisons.