1 Lecture 12 More on Hashing Multithreading. 2 Speakers!

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
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:
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Skip List & Hashing CSE, POSTECH.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Hashing as a Dictionary Implementation
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Maps, Dictionaries, Hashtables
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
COSC 2007 Data Structures II
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
P p Chapter 11 discusses several ways of storing information in an array, and later searching for the information. p p Hash tables are a common approach.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Hashing as a Dictionary Implementation Chapter 19.
CS201: Data Structures and Discrete Mathematics I Hash Table.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashing CS 110: Data Structures and Algorithms First Semester,
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
1 Chapter 9 Searching And Table. 2 OBJECTIVE Introduces: Basic searching concept Type of searching Hash function Collision problems.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
CS203 Lecture 14. Hashing An object may contain an arbitrary amount of data, and searching a data structure that contains many large objects is expensive.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Multithreading / Concurrency
Review Graph Directed Graph Undirected Graph Sub-Graph
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
Hash Table.
Chapter 28 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
TreeMap & HashMap 7/16/2009.
Multithreading.
Multithreaded Programming
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Lecture 12 More on Hashing Multithreading

2 Speakers!

3 Hashing An object may contain an arbitrary amount of data, and searching a data structure that contains many large objects is expensive suppose your collection of Strings stores the text of various books, you are adding a book, and you need to make sure you are preserving the Set definition – ie that no book already in the Set has the same text as the one you are adding. A hash function maps each datum to a value to a fixed and manageable size. This reduces the search space and makes searching less expensive Hash functions must be deterministic, since when we search for an item we will search for its hashed value. If an identical item is in the list, it must have received the same hash value

4 Hashing Any function that maps larger data to smaller ones must map more than one possible original datum to the same mapped value Diagram from Wikipedia When more than one item in a collection receives the same hash value, a collision is said to occur. There are various ways to deal with this. The simplest is to create a list of all items with the same hash code, and do a sequential or binary search of the list if necessary

5 Hashing Hash functions often use the data to calculate some numeric value, then perform a modulo operation. This results in a bounded-length hash value. If the calculation ends in % n, the maximum hash value is n-1, no matter how large the original data is. It also tends to produce hash values that are relatively uniformly distributed, minimizing collisions. Modulo may be used again within a hash-based data structure in order to scale the hash values to the number of keys found in the structure

6 Hashing Sparse: A AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ Not Sparse: Juror 1 Juror 2 Juror 3 Juror 4 The more sparse the data, the more useful hashing is

7 Hashing Hashing is used for many purposes in programming. The one we are interested in right now is that it makes it easier to look up data or memory addresses in a table

8 Why Hashing? The preceding chapters introduced search trees. An element can be found in O(logn) time in a well-balanced search tree. Is there a more efficient way to search for an element in a container? This chapter introduces a technique called hashing. You can use hashing to implement a map or a set to search, insert, and delete an element in O(1) time.

9 Map Recall that a map stores entries. Each entry contains two parts: key and value. The key is also called a search key, which is used to search for the corresponding value. For example, a dictionary can be stored in a map, where the words are the keys and the definitions of the words are the values. A map is also called a dictionary, a hash table, or an associative array.

10 What is Hashing? If you know the index of an element in the array, you can retrieve the element using the index in O(1) time. So, can we store the values in an array and use the key as the index to find the value? The answer is yes if you can map a key to an index. The array that stores the values is called a hash table. The function that maps a key to an index in the hash table is called a hash function. Hashing is a technique that retrieves the value using the index obtained from key without performing a search.

11 Hash Function and Hash Codes A typical hash function first converts a search key to an integer value called a hash code, and then compresses the hash code into an index to the hash table.

12 What is Hashing? A collision occurs when multiple values receive the same hash code. There are several standard ways to deal with collisions. The first approach, open addressing, involves ways of finding available addresses in the table. The simplest form of this is linear probing, which checks whether the key is found at the expected place in the array. If not, it checks the next spot, etc. There are more complex variants of open addressing, which you can read about in the textbook. The second approach, separate chaining, uses a list at each location in the hash table. When looking up a value by hash, search the list of all nodes with that hash value.

13 Linear Probing

14 Handling Collisions Using Separate Chaining The separate chaining scheme places all entries with the same hash index into the same location, rather than finding new locations. Each location in the separate chaining scheme is called a bucket. A bucket is a container that holds multiple entries.

15 Implementing Map Using Hashing Run TestMyHashMap MyHashMap MyMap

Multithreading Multithreading is just what it sounds like: concurrent running of multiple tasks. Multithreading does not always involve parallel execution, in which multiple operations are executed simultaneously. It can be implemented in a situation in which multiple threads have access to the CPU at different times 16

Multithreading Multithreading has been available in programming for a long time, even when it was unusual to run an application on multiple processors. Multithreading offers a way to run multiple tasks at once without blocking, ie making tasks wait for another task which has not completed because it is waiting for user input, for some resource to become available, for a set amount of time to pass, etc. Multithreading offers a way to give higher-priority tasks access to resources when needed without starving lower-priority ones Event driven programming with GUI components often uses multiple threads, some of which are waiting for user input. 17

Multithreading Multithreading also makes it easier for an operating system or other scheduling mechanism to spread work among different processors. This is increasingly important as multiple cores have become the norm and as distributed applications, ones running on multiple computers on a network, become more prevalent. 18

Threads Concept 19 Multiple threads on multiple CPUs Multiple threads sharing a single CPU

Creating Tasks and Threads 20 Write a class that implements Runnable Instantiate a Thread, sending an instance of your Runnable implementation to the constructor Call start() on the Thread

The Thread Class 21

The Static sleep(milliseconds) Method The sleep(long mills) method puts the thread to sleep for the specified time in milliseconds. For example, consider this code: int lastNum = 15; for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); try { Thread.sleep(i* i * i); } catch (InterruptedException ex) { } Every time a number i is printed, the current thread is put to sleep for i 3 milliseconds. 22

The following example uses a.wav file which is available at Any other very short.wav file would work too. 23

package week8; import javax.sound.sampled.*; import java.io.File; import java.io.IOException; import javax.sound.sampled.LineEvent.Type; public class WavPlayer { File file; AudioListener listener; AudioInputStream audioInputStream; public WavPlayer(String fileName) { file = new File(fileName); } public void playClip() throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException { listener = new AudioListener(); audioInputStream = AudioSystem.getAudioInputStream(file); try { Clip clip = AudioSystem.getClip(); clip.addLineListener(listener); clip.open(audioInputStream); try { clip.start(); listener.waitUntilDone(); } finally { clip.close(); } } finally { audioInputStream.close(); } private class AudioListener implements LineListener { private boolean done = public synchronized void update(LineEvent event) { Type eventType = event.getType(); if (eventType == Type.STOP || eventType == Type.CLOSE) { done = true; // notifyAll() tells all objects or threads that are waiting for // the line to do something that its state has changed notifyAll(); } public synchronized void waitUntilDone() throws InterruptedException { while (!done) { wait(); } 24

package week8; public class Rhythm implements Runnable { private int interval; private WavPlayer player; public Rhythm(String wav, int intervalIn) { interval = intervalIn; player = new WavPlayer(wav); public void run() { while (true) { try { player.playClip(); Thread.sleep(interval); } catch (Exception e) { e.printStackTrace(); } 25

package week8; public class RhythmDriver { public static void main(String[] args) { Rhythm r1 = new Rhythm("cowbell2.WAV", 400); Rhythm r2 = new Rhythm("cowbell2.WAV", 600); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } 26