3 Pages

lecture26

Course: COMP 250, Spring 2010
School: McGill
Rating:
 
 
 
 
 

Word Count: 1323

Document Preview

250 COMP Winter 2010 26 - hashing March 17, 2010 Today we look at a particular kind of map, sometimes called a hash map. (Java has a class HashMap for representing these kinds of maps.) Before we understand what a hash map is, though, we need to understand what hashing is. [Note: make sure you also read through the slides. There are many gures in the slides which I have not reproduced here and which illustrate...

Register Now

Unformatted Document Excerpt

Coursehero >> Canada >> McGill >> COMP 250

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
250 COMP Winter 2010 26 - hashing March 17, 2010 Today we look at a particular kind of map, sometimes called a hash map. (Java has a class HashMap for representing these kinds of maps.) Before we understand what a hash map is, though, we need to understand what hashing is. [Note: make sure you also read through the slides. There are many gures in the slides which I have not reproduced here and which illustrate the various maps discussed in this lecture.] Hashing Suppose you have a set of keys K . Dene a hash function to be a mapping: h : K {0, 1, 2, m 1} where m is some positive integer. Specically, for each key k K , the hash function species some integer h(k ). For hash functions, typically m is smaller than the number of keys in K . For example, if the keys are possible social insurance numbers (109 of them), then we might let m be 1000, rather than 109 . So it happens alot that two keys in K to map to the same integer. Also note that the hash function h is a mapping that is dened on the entire set K and not just on a subset of K . (The denition of mapping last class allowed a mapping to only include a subset of keys.). It is very common to design hash functions by writing them as a composition of two maps. The rst map takes keys K to a large set of integers. The second map takes the large set of integers to a small set of integers {0, 1, . . . , m 1}. (The reasons for this will be clear by the end of the lecture.) The rst mapping is called hash coding and the integer chosen for a key k is called the hash code for that key. The second mapping is called compression. Compression maps the hash codes to hash values. In fact, we saw two examples of hash codes last lecture. The keyObject-to-address map, and the string-to-integer map (see lecture 26 page 3). A typical compression function is the modulus function. e.g. suppose i is the hash code for some key k (or the keys K are already integers). Then, the hash value is i mod m. Often one takes m to be a prime number, though this is not necessary. To summarize, we have that a hash function is typically composed of two functions: h : {keys} {hash codes} {hash values}, i.e. the values {0, 1, . . . , m 1} are the hash values. Hash map Lets return to the problem we discussed last class in which we have a keys K and values V and we wish to represent a map M which contains some set of ordered pairs (k, v ). We now wish to use a hash function described above to represent this map. We assume the keys in the map M form a relatively small subset of K . [ASIDE: The values v V of the map M should not be confused with the hash values which are are integers in 0, 1, . . . , m 1. The values v might be Employee records, or entries in an telephone book, for example.] Dene an array called a hash table, which will hold the (k, v ) pairs. This is like the direct mapping array we discussed last lecture, except that it has far fewer slots than the number of keys 1 COMP 250 Winter 2010 26 - hashing March 17, 2010 in our key space K , namely the number of slots in the array is typically just a bit bigger than the number of (k, v ) pairs. Even if we make the number of slots m to be a bit bigger than the number of pairs in the map, it could still happen that there are two keys k1 and k2 that hash to the same hash value, that is, there is an index i in the array such that h(k ) = i for more than one key in the map. this When happens, we say that a collision has occured. This can happen either if two keys have the same hash code or if two keys have a dierent hash code but the compression function maps them to the same slot e.g. 7 mod 5 = 22 mod 5 = 2. To allow for collisions, we can use linked list of pairs (k, v ) at each slot of our hash table array. The entries of a hash table, which now can hold multiple (k, v ) pairs, are called buckets or slots. Storing a list of (k, v ) pairs in each hash bucket is called chaining. Note that we need to store the key k because when use a key k to try to access a value v , we need to know which of the v s stored in a bucket corresponds to which k . When we use a (search) key k to nd a value v , we match the search key k with the keys in the bucket. For example, with social insurance numbers (keys) and employee records (values), there may be multiple employee records stored in each bucket and we need to be able to check which if any corresponds to the given social insurance number. hash table key value In the worst case that all the elements in the collection hash to the same location in the array, then we have one linked list. This is undesirable since the whole point here is to represent a map so that we can access values as quickly as possible. To avoid having such long lists, we would like to choose a hash function so that there is roughly an equal chance of mapping to any of the hash values. (The word hash means to chop/mix up.) In class, several students asked if it is possible to design hash functions that avoid collisions entirely. For this to be possible in any given situation, it is necessary (but not sucient) that the number of buckets of the hash table be greater than or equal to the number of key-value pairs in the map. Hash functions that are designed to avoid collisions are called perfect hash functions. 2 COMP 250 Winter 2010 26 - hashing March 17, 2010 [ASIDE: There is one other term that comes up often: the load factor of a hash table is the ratio of the number of (k, v ) pairs currently in the table to the number of slots in the table (m). In Java, for example, the HashMap and HashSet classes implement hash functions, and the load factor 4 for the hash table is never more than 0.75m. That is, m is chosen to be at least 3 as great as the number of elements: k-v pairs in the case of a HashMap, or set elements in the case of a HashSet. ] hash codes for strings Suppose s is a string, composed out of unicode characters (16 bits each). Dene its hash code to be: s.length1 s[i] h(s) = i=0 where s[i] is the 16-bit unicode value of the character at position i in the string. This will be an integer at most s.length 216 . In this above example, two strings consisting of the same set of characters, such as eat and ate will have the same hash code. A more general hash code for strings can be dened by s.length1 s[i] pi h(s) = i=0 where p is some positive integer. In the last lecture, I gave you a similar formula for the case p = 216 , which gave us unique hash codes i.e. no two strings mapped to the same hash code. In Java, the hashCode() method for the String class is dened s.length1 s[i] ps.lengthi1 h(s) = i=0 with p = 31, and this formula is indeed stated in the Java API in the String.hashCode() method. (Check it out, http://java.sun.com/javase/6/docs/api/java/lang/String.html#hashCode() ) 3
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

McGill - COMP - 250
COMP 250 Winter 201027 - graphs 1: denitionsMarch 19, 2010GraphsYou are familiar with data structures such as arrays and lists (linear), and trees (non-linear). Wenext consider a more general non-linear data structure, known as graphs. Like the previ
McGill - COMP - 250
COMP 250 Winter 201028 - graphs 2: traversalMarch 22, 2010Graph traversalOne problem you often need to solve when working with graphs is whether there is a sequence ofedges (a path) from one vertex to another or, more generally, what is the set of al
McGill - COMP - 250
COMP 250 Winter 201029 - inheritance 1March 24, 2010InheritanceIn our daily lives, we classify the many things around us. We know that the world has objectslike dogs and cars and food and we are familiar with talking about these objects as classes:D
McGill - COMP - 250
COMP 250 Winter 201030 - inheritance 2March 26, 2010Example (of inheritance)See the example from the slides in which we consider a method threaten() in the Dog() class.This method calls showTeeth(), which is dened in the Dog class only, and bark() wh
McGill - COMP - 250
COMP 250 Winter 201031 - polymorphismMarch 31, 2010 [updated April 3]PolymorphismSuppose you have some reference variable. It is is declared to be a certain type. You might assumethat this means that the variable can point only to objects that are of
McGill - COMP - 250
COMP 250 Winter 201032 - interfacesApril 7, 2010Earlier when we discussed Java classes and their inheritance relationships, we pictured a hierarchy where each class (except Object) extends some other unique class (see inheritance diagrambelow on the l
McGill - COMP - 250
COMP 250 Winter 201033 - abstract classes, castingApril 9, 2010Abstract classesIt often occurs that one wants to dene a class with some methods fully specied, but some methodsspecied only by their signature. Think of this as a hybrid between a full c
McGill - COMP - 250
COMP 250 Winter 201034 - modiersApril 12, 2010Java modiers[Note: some of this is taken from the Readings BackgroundJava.pdf which I posted early in thecourse. I have added further details that involve concepts from inheritance.]PackagesA package is
McGill - COMP - 250
COMP 250 Winter 2010lecture 1 addition and multiplicationJanuary 6, 2010Algorithms for addition and multiplicationLets try to remember your rst experience with numbers, way back when you were a child in gradeschool. In grade 1, you learned how to cou
McGill - COMP - 250
COMP 250 Winter 2010lecture 2 binary representationsJanuary 8, 2010Converting from decimal to binaryLast lecture I nished o by asking how to convert from decimal to binary. Here is the standardalgorithm. It may not be obvious to you at rst glance why
McGill - COMP - 250
COMP 250 Winter 2010lecture 3 memory addresses, insertion sortJanuary 11, 2010In COMP 202, you learned about primitive types vs. reference types in Java. You think of avariable that is a primitive type as naming a particular set of bytes in memory, an
McGill - COMP - 250
COMP 250 Winter 2010 lectures 4,5 linked lists [modied Jan. 21]January 13 & 15, 2010Linked listsWe next consider an alternative data structure for ordering a set of objects. Again, we have a setof objects that constructed one-by-one. But now we do not
McGill - COMP - 250
McGill - COMP - 250
COMP 250 Winter 2010lecture 6 linked listsJanuary 18, 2010Abstract Data Types (ADT)In the previous two lectures, I sketched out linked list data structures (singly and doubly) and someof the methods that are used to manipulate them. Notice that it wa
McGill - COMP - 250
COMP 250 Winter 2010lecture 7 stacksJanuary 20, 2010Stack ADTIn this lecture and the next, we look at stacks and queues. Each is an ordered set of objects(a list), where the ordering is determined by when each object was inserted. With a stack,one a
McGill - COMP - 250
COMP 250 Winter 2010lecture 8 queuesJanuary 22, 2010QueuesYou are familiar with queues in daily life. You know that when you have a single resource such asa cashier in the cafeteria, you need to join the end of the line and the person at the front of
McGill - COMP - 250
COMP 250 Winter 2010lecture 9 - Call stack, factorialJan 25, 2010In the next three lectures we will look at recursion. This is a very important topic. Before wediscuss it, though, I will discuss the call stack. I wont give a complete description of th
McGill - COMP - 250
COMP 250 Winter 2010lecture 11 - decimal-to-binary, power, binary searchJan 29, 2010Today we will look at three dierent recursive algorithms which have a similar behavior, in thatall three are (recursively) called approximately logn timesDecimal to b
Apex College - ECON - 105
Dr. OngTze San et al., Int. J. Eco. Res., 2011 2(1), 1-15ISSN:2229-6158A STUDY ON THE PERFORMANCE OF MALAYSIAN REAL ESTATEINVESTMENT TRUSTS FROM 2005-2010 BY USING NET ASSETVALUE APPROACHDr. OngTze San (Senior Lecturer) - Faculty of Economics and Man
Apex College - 12 - 12
differentbetweenpearsonandspearmancorrelationThedifferencebetweenthePearsoncorrelationandtheSpearmancorrelationisthatthe Pearsonismostappropriateformeasurementstakenfroman interval scale,whiletheSpearmanismoreappropriateformeasurementstakenfromordinals
Apex College - ECON - 123
PROJECTINTRODUCTIONOur company is S&T Sdn Bhd which is allocating in Petaling Jaya, Selangor. WemanufacturingintheproductionofSoyaMilkandTaufuFaunderthebrandnameofFSoy.Withthepotentialofourcompany,wedecidetoenlargeourcompanybyinvestinnewmarketwhichist
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go toadd questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go toadd questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E - WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questi
Texas Woman's University - ACCT - 3000-5000
COURSES > ACCOUNTING FOR GOVERNMENTAL AND NONPROFIT ENTITIES:, 15/E- WILSON > CONTROL PANEL > POOL MANAGER > POOL CANVASPool CanvasAdd, modify, and remove questions. Select a question type from the Add Question drop-down list and click Go to add questio
Texas Woman's University - ACCT - 3000-5000
Chapter4Accounting for Governmental Operating Activities-Illustrative Transactions and Financial StatementsMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter studying Chapter 4, you should be able to: Recogni
Texas Woman's University - ACCT - 3000-5000
Chapter5Accounting for General Capital Assets and Capital ProjectsMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter studying Chapter 5, you should be able to: Describe the nature and characteristics of gener
Texas Woman's University - ACCT - 3000-5000
Chapter6Accounting for General Long-Term Liabilities and Debt ServiceMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter studying Chapter 6, you should be able to: Explain what types of liabilities are classif
Texas Woman's University - ACCT - 3000-5000
Chapter7Accounting for the Business-Type Activities of State and Local GovernmentsMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter reading this chapter, you should be able to: Distinguish between the purpos
Texas Woman's University - ACCT - 3000-5000
Chapter8Accounting for Fiduciary Activities- Agency and Trust FundsMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter reading this chapter, you should be able to: Explain how fiduciary funds are used to repor
Texas Woman's University - ACCT - 3000-5000
12Accounting and Reporting for the Federal GovernmentChapterMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter studying Chapter 12, you should be able to: Describe the financial management structure of the fe
Texas Woman's University - ACCT - 3000-5000
16Accounting for Colleges and UniversitiesChapterMcGrawHill 2007 The McGrawHill Companies, Inc. All rights reserved.Learning ObjectivesAfter studying Chapter 16, you should be able to: Distinguish between GAAP for public and private colleges and uni
Texas Woman's University - ACCT - 3000-5000
Chapter4Accounting for Governmental OperatingActivitiesIllustrative Transactionsand Financial StatementsMcGrawHill/IrwinCopyright2010byTheMcGrawHillCompanies,Inc.Allrightsreserved.Learning ObjectivesAfter studying Chapter 4, you should be able to:
Texas Woman's University - ACCT - 3000-5000
Chapter8Accounting for FiduciaryActivitiesAgencyand Trust FundsMcGrawHill/IrwinCopyright2010byTheMcGrawHillCompanies,Inc.Allrightsreserved.Learning ObjectivesAfter studying Chapter 8, you should be able to: Explain how fiduciary funds are used to
Texas Woman's University - ACCT - 3000-5000
Chapter 1The Government andNot-for-Profit EnvironmentLearning ObjectivesAfter studying Chapter 1, you should be able to: Understand the characteristics that distinguishgovernments and not-for-profit organizations frombusinesses (for-profit entities
Texas Woman's University - ACCT - 3000-5000
Chapter TwoFUND ACCOUNTINGChapter 21Learning ObjectivesAfter studying Chapter 2, you should understand: The nature of funds The three major fund types of a state or local government:-Governmental funds,-Proprietary funds, and-Fiduciary funds Th
Texas Woman's University - ACCT - 3000-5000
Chapter 4Recognizing Revenues inGovernmental FundsChapter 41Learning Objectives Why governments focus on current financialresources and use the MA basis? Why governments focus on all economicresources and use the full accrual basis? The key dist
Texas Woman's University - ACCT - 3000-5000
Chapter 11Issues of Reporting, Disclosure,and Financial AnalysisChapter 111Learning Objectives Conversion of Fund Statements to Government-wide Why the make-up of a governments or not-for-profitsreporting entity is an issue The criteria that the
Texas Woman's University - ACCT - 3000-5000
Chapter 12Not-for-Profit OrganizationsChapter 121Learning Objectives Identify the authoritative standards-setting bodies for establishing GAAPfor Not-For-Profits (NFPs). Division of Resources into:-Unrestricted-Temporarily restricted-Permanently
Texas Woman's University - ACCT - 3000-5000
Chapter 13Health Care Providers andColleges and UniversitiesChapter 131Learning Objectives Understand the unique issues faced by not-for-profit healthcare providers and colleges and universities (C&U) Understand accounting and reporting issues for
Texas Woman's University - ACCT - 3000-5000
Chapter 14Managing for ResultsChapter 141Learning Objectives Roles of accountants in the management ofgovernmental and not-for-profit organizations How program budgets overcome limitations oftraditional object-classification budgets The need for,
Texas Woman's University - ACCT - 3000-5000
Chapter 15Auditing Governments andNot-For-Profit OrganizationsChapter 151Learning Objectives Differentiate between auditing in the government andnot-for-profit sectors and in the business sectors. Influence of yellow-book on auditing Types of aud
Texas Woman's University - ACCT - 3000-5000
Chapter 16Federal Government AccountingChapter 161Learning Objectives Understand the unique characteristics of federalgovernment Roles of the main agencies responsible for federalaccounting and reporting Key objectives of federal financial report
Texas Woman's University - ACCT - 3000-5000
B us 4023 Advanced AccountingF all 2011I nst ructor: Dr. RezacO ffice Hou rs: MW 11:00-1:00 T ues 9-12Class:C lass meets: Mon/Wed 1:00-2:20 in ASB 201E mail: r rezac@twu.edu O ffice CFO408Text: Advanced Accounting 10th e d. by Hoyle ISBN 978-0-07-8
Texas Woman's University - ACCT - 3000-5000
Running Head: Information Systems1Information SystemsForrest McCannMGMT Info Systems - BUS 3533Professor Bob WoodsSept 14, 20112Information SystemsData Vs. InformationData as we learned in class is the raw materials i.e., unorganized raw materia
Texas Woman's University - ACCT - 3000-5000
Running Head: Computers1ComputersForrest McCannMGMT Info Systems - BUS 3533Professor Bob WoodsSept 14, 20112ComputersMain Vs. Secondary MemoryMain memory, stores data and information and is usually volatile, its contents are lostwhen the power
Texas Woman's University - ACCT - 3000-5000
Running Head: Information Systems1Information SystemsForrest McCannMGMT Info Systems - BUS 3533Professor Bob WoodsSept 14, 20112Information SystemsData Vs. InformationData as we learned in class is the raw materials i.e., unorganized raw materia
Texas Woman's University - ACCT - 3000-5000
Running Head: Information Systems1Information SystemsForrest McCannMGMT Info Systems - BUS 3533Texas Womans UniversitySept 14, 20112Information SystemsData Vs. InformationData as we learned in class is the raw materials i.e., unorganized raw mat