13 Pages

L22

Course: CPS 100, Fall 2009
School: Duke
Rating:
 
 
 
 
 

Word Count: 1059

Document Preview

Maps,Tries Searching, (hashing) Searching is a fundamentally important operation We want to search quickly, very very quickly Consider searching using Google, ACES, issues? In general we want to search in a collection for a key We've searched using trees and arrays Tree implementation was quick: O(log n) worst/average? Arrays: access is O(1), search is slower If we compare keys, log n is best for...

Register Now

Unformatted Document Excerpt

Coursehero >> North Carolina >> Duke >> CPS 100

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.
Maps,Tries Searching, (hashing) Searching is a fundamentally important operation We want to search quickly, very very quickly Consider searching using Google, ACES, issues? In general we want to search in a collection for a key We've searched using trees and arrays Tree implementation was quick: O(log n) worst/average? Arrays: access is O(1), search is slower If we compare keys, log n is best for searching n elements Lower bound is (log n), provable Hashing is O(1) on average, not a contradiction, why? Tries are O(1) worstcase!! (ignoring length of key) CompSci 100E 22.1 From Google to Maps If we wanted to write a search engine we'd need to access lots of pages and keep lots of data Given a word, on what pages does it appear? This is a map of words>web pages In general a map associates a key with a value Look up the key in the map, get the value Google: key is word/words, value is list of web pages Anagram: key is string, value is words that are anagrams Interface issues Lookup a key, return boolean: in map or value: associated with the key (what if key not in map?) Insert a key/value pair into the map 22.2 CompSci 100E Interface at work: MapDemo.java Key is a string, Value is # occurrences Interface in code below shows how Map class works while (scanner.hasNext()) { String s = (String) scanner.next(); Counter c = (Counter) map.get(s); if (c != null) c.increment(); else map.put(s, new Counter()); } What clues are there for prototype of map.get and map.put? What if a key is not in map, what value returned? What kind of objects can be put in a map? CompSci 100E 22.3 Accessing all values in a map (e.g., print) Access every key in the map, then get the corresponding value Get an iterator of the set of keys: keySet().iterator() For each key returned by this iterator call map.get(key) ... Get an iterator over (key,value) pairs, there's a nested class called Map.Entry that the iterator returns, accessing the key and the value separately is then possible To see all the pairs use entrySet().iterator() CompSci 100E 22.4 External Iterator without generics The Iterator interface accesses elements Source of iterator makes a difference: cast required? Iterator it = map.keySet().iterator(); while (it.hasHasNext()){ Object value = map.get(it.next()); } Iterator it2 = map.entrySet().iterator(); while (it2.hasNext()){ Map.Entry me = (Map.Entry) it.next(); Object value = me.getValue(); } CompSci 100E 22.5 External Iterator with generics Avoid Object, we know what we have a map of Is the syntax worth it? Iterator<String> it = map.keySet().iterator(); while (it.hasHasNext()){ Object value = map.get(it.next()); } Iterator<Map.Entry<String,Counter>> it2 = map.entrySet().iterator(); while (it2.hasNext()){ Map.Entry<String,Counter> me = it.next(); Object value = me.getValue(); } CompSci 100E 22.6 Hashing: Log (10100) is a big number Comparison based searches are too slow for lots of data How many comparisons needed for a billion elements? What if one billion webpages indexed? Hashing is a search method: average case O(1) search A hash function generates the number from the key Worst case is very bad, but in practice hashing is good Associate a number every with key, use the number to store the key o Like catalog in library, given book title, find the book Goal: Efficient to calculate Goal: Distributes keys evenly in hash table CompSci 100E 22.7 Hashing details There will be collisions, two keys will hash to the same value 0 1 2 3 n-1 Several ways to handle collisions, in general array/vector used We must handle collisions, still have efficient search What about birthday "paradox": using birthday as hash function, will there be collisions in a room of 25 people? Linear probing, look in next spot if not found o Hash to index h, try h+1, h+2, ..., wrap at end o Clustering problems, deletion problems, growing problems Quadratic probing o Hash to index h, try h+12, h+22 , h+32 , ..., wrap at end o Fewer clustering problems Double hashing o Hash to index h, with another hash function to j o Try h, h+j, h+2j, ... 22.8 CompSci 100E Chaining with hashing With n buckets each bucket stores linked list Compute hash value h, look up key in linked list table[h] Hopefully linked lists are short, searching is fast Unsuccessful searches often faster than successful o Empty linked lists searched more quickly than nonempty Potential problems? Hash table details Size of hash table should be a prime number Keep load factor small: number of keys/size of table On average, with reasonable load factor, search is O(1) What if load factor gets too high? Rehash or other method 22.9 CompSci 100E Hashing problems Linear probing, hash(x) = x, (mod tablesize) Insert 24, 12, 45, 14, delete 24, insert 23 (where?) 12 24 45 2 3 14 4 5 6 7 8 9 10 0 1 Same numbers, use quadratic probing (clustering better?) 12 0 1 24 2 14 3 4 45 5 6 7 8 9 10 What about chaining, what happens? CompSci 100E 22.10 What about hash functions Hashing often done on strings, consider two alternatives public static int hash(String s) { int k, total = 0; for(k=0; k < s.length(); k++){ t...

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:

Duke - CPS - 100
Data CompressionCompression is a highprofile application .zip, .mp3, .jpg, .gif, .gz, . What property of MP3 was a significant factor in what made Napster work?Why do we care? Secondary storage capacity doubles every year Disk space fi
Duke - CPS - 100
Intro to SortingSorting &quot;Ideal&quot; Computer Science Topic Theory and Practice meet Efficient Sorting Saves MoneyFirst look at some simple (quick and dirty?) algorithms Selection Sort1. 2. 3.Find smallest; swap with element [0] Consider r
Duke - CPS - 100
Sorting: From Theory to PracticeWhy do we study sorting?Because we have to Because sorting is beautiful Example of algorithm analysis in a simple, useful settingThere are n sorting algorithms, how many should we study?O(n), O(log n), . Why d
Duke - CPS - 100
Sorting: From Theory to PracticeSorting out sortsWhy do we study sorting? Simple, O(n2) sorts - for sorting n elements Because we have to Because sorting is beautiful Example of algorithm analysis in a simple, useful settingSelection
Duke - CPS - 100
Other N log N SortsBinary Tree SortBasic Recipe o Insert into binary search tree (BST) o Do Inorder Traversal Complexity o Create: O(N log N) o Traversal O(N) Not usually used for sorting unless you need BST for other reasonsCompSci 100E
Duke - CPS - 100
From bits to bytes to intsAt some level everything is stored as either a zero or a one A bit is a binary digit a byte is a binary term (8 bits) We should be grateful we can deal with Strings rather than sequences of 0's and 1's. We should b
Duke - CPS - 100
On the Limits of ComputingReasons for Failure1.2. 3.Runs too long o Real time requirements o Predicting yesterday's weather Non-computable ! Don't know the algorithm Time SpaceComplexity, NTractable and IntractableCompSci 100E36.1
Duke - CPS - 100
On the Limits of ComputingOn the Limits of ComputingReasons for Failure1.Intractable Algorithms 2. 3.Runs too long o Real time requirements o Predicting yesterday's weather Non-computable ! Don't know the algorithm Time SpaceComputer
Duke - CPS - 100
Intro to GraphsDefinitions and Vocabulary A graph consists of a set of vertices (or nodes) and a set of edges (or arcs) where each edge connects a pair of vertices. If the pair of vertices defining an edge is ordered, then it is a dire
Duke - CPS - 100
Graph AlgorithmsTopological SortProduce a valid ordering of all nodes, given pair-wise constraints Solution usually not unique When is solution impossible?Topological Sort Example: Getting an AB in CPSExpress prerequisite structure This exam
Duke - CPS - 100
Graph AlgorithmsTopological SortTopological Sort Topological Sort Algorithm1. 2. 3. Produce a valid ordering of all nodes, given pair-wise constraints Solution usually not unique When is solution impossible? Express prerequisite structur
Duke - CPS - 100
Memory ModelFor this course: Assume Uniform Access Time Memory Hierarchy (in order of decreasing speed) All elements in an array accessible with same time cost Reality is somewhat different Registers On (cpu) chip cache memory Off
Berkeley - EE - 232
EE 232 11/6/2007Midterm Examination-2 EE 232 Lightwave DevicesFall 2007 Prof. Ming Wu If you need additional conditions to solve a problem, please write down your assumptions. Please put a square box around your final answers for each proble
Duke - CPS - 100
Seating chart Front of the roomGroup 1 Group 9Group 2 Group 10Group 3 Group 11Group 4 Group 12Group 5 Group 13Group 6 Group 14Group 7 Group 15Group 8 Group 16 Back of the room Group 1
Duke - CPS - 100
Seating chart Front of the roomGroup 1 Group 9Group 2 Group 10Group 3 Group 11Group 4 Group 12Group 5 Group 13Group 6 Group 14Group 7 Group 15Group 8 Group 16 Back of the room Group 1
Texas Tech - GERM - 3313
German 3313 Northern Myths and Legends Welcome to German 3313 Northern Myths and Legends No Prerequisites! Syllabus is online at:http:/www.languages.ttu.edu/courses/germ3313/ Instructor: Dr. Charles A. Grair FL 265 Charles.Grair@ttu.edu Office
Texas Tech - GERM - 3313
Njla The Saga of Burnt Njal Njala was composed around 1280, dealing with events from 930 1020 AD. It is the longest, the most sophisticated, and perhaps the finest example of saga art considered by some to be the national epic of Iceland. Over
Texas Tech - GERM - 3313
Njla 95-97. Flosi, a powerful chieftain, is introduced. He has a brother called Starkad, niece harsh-natured Hildigunn, sons Thorgeir, Stein, Kolbein, Egil. Flosi's father-in-law is Godi Hall of Sida. Hoskuld is now grown; Njal suggests that he m
Texas Tech - GERM - 3313
Review of Lecture 1What is a myth? A myth is a traditional story of ostensibly historical past events and gods or godlike beings that is used to relate the world view of a people or to explain a practice, a belief or a natural phenomenon. There ar
Texas Tech - GERM - 3313
The Kalevala &quot;The Kalevala, or old Karelian poemsabout ancient times of the Finnish people&quot; First published by Elias Lnnrot (18021884) on 28 February 1835. 28 February later became Kalevala Day in Finnland the National Cultural Holiday. The Ka
Texas Tech - GERM - 3313
The Vinland Sagas Two short Icelandic Sagas discuss the settlement of Greenland and the first voyages to the New World: Saga of the Greenlanders (Grnlendinga saga) Eirik the Red's Saga (Eirks saga raua) Both sagas refer to events from 970-1030,
Texas Tech - GERM - 3313
&quot;Sayings of the High One&quot;Havamal A collection of sayings rather than a single poem. Attributed to Odin, the wisest of the Norse gods, but many of the sayings represent folk wisdom rather than divine lore. Written in ljodahattr, the customary ver
Texas Tech - GERM - 3313
Scandinavians in AmericaEmigration by Norwegians to North America began in 1825; over five million Scandinavians arrived in this country before the first world war. This picture shows emigrants in Oslo leaving for America.The Historical Society F
Texas Tech - GERM - 3313
Secrets of the Runes According to Havamal, Odin learned the secrets of the runes while he hung on a tree:&quot;I know that I hung on a windy tree nine long nights Wounded with a spear, dedicated to Odin, Myself to myself (.) No bread did they give me no
Texas Tech - GERM - 3313
Vafthrudnismal and Grimnismal Vafthrudnir's and Grimnir's Sayings Both poems begin with a contest or argument between Odin and his wife, Frigg. In each poem, Odin sets off in disguise to test an adversary, the Giant Vafthrudnir or the King Geirro
Texas Tech - GERM - 3313
Celt ic M yt h and L egend The Celts are a linguistic group in the IndoEuropean language family. Today, only small populations in Scotland, Wales, Ireland, Isle of Man, and Brittany still speak Celtic languages (Gaelic et al.). Historically, Celti
Texas Tech - GERM - 3313
The Mighty ThorThe ChallengeMarvel Comic Groups, 1973The Mighty ThorLoki incites Hymir to challenge Thor, whose honor compels him to accept.The Mighty ThorThor survives the first challenge, to bring back a fish from the depths.The Mighty
Texas Tech - GERM - 3313
Der Nibelunge Nt The Nibelungenlied is one of the few great heroic epics in world literature. It was likely composed around 1200 in southern Germany, between Passau and Vienna. It was composed from the same oral literature as the Volsunga saga, bu
Texas Tech - GERM - 3313
Der Nibelunge Nt The second half of the Nibelungenlied was probably based on a separate work and only later combined with the Siegfried material to form a single epic. Many of the characters are introduced a second time after Chapter 19: Kriemhild,
Texas Tech - GERM - 3313
Der Ring des Nibelungen Richard Wagner (1813 1883). Wanted to be a dramatist, heard Beethoven and changed his mind: music-drama! First marriage, Minna, infidelity and debt. 1848 Revolution, exile in Zrich, new operatic style with Tannhuser, Lohe
Texas Tech - GERM - 3313
The Legend of Faust Early Modern Period in Germany (13501600) also gave rise to legends. Early Modern Legends are folk heroes, not warriors or chieftains. Different society. Like earlier legends, folk heroes also exist on the margins of recorded h