Lecture 13 Notes Sets
|
|
- Elinor Griffith
- 5 years ago
- Views:
Transcription
1 Lecture 13 Notes Sets : Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons 1 Introduction In this lecture, we will discuss the data structure of hash tables further and use hash tables to implement a very basic interface of sets. With this lecture, we will also begin to discuss a new separation of concerns. Previously, we have talked a great deal about the distinction between a library s interface (which the client can rely on) and a library s implementation (which should be able to change without affecting a correctly-designed client). The interface defines not only the types, but also the available operations on them and the pre- and post-conditions for these operations. For general data structures it is also useful to note the asymptotic complexity of the operations so that potential clients can decide if the interface serves their purpose. One wrinkle we have not yet discussed is that, in order for a library to provide its services, it may in turn require some operations provided by the client. Hash tables provide an excellent example for this complication, so we will discuss the interface to hash tables in details before giving the hash table implementation. For the purposes of this lecture we call the data structures and the operations on them provided by an implementation the library and code that uses the library the client. Relating to our learning goals, we have Computational Thinking: We discuss the separation of client interfaces and client implementations. Algorithms and Data Structures: We discuss algorithms for hashing strings. Programming: We revisit the char data type and use it to consider string hashing.
2 Sets L Generic Data Structures So far, all the data structures that we ve considered, have always had particular type information that seemed irrelevant. In the implementation of queues, why is it important that we have a queue of strings in particular? // typedef * queue_t; bool queue_empty(queue_t Q) /* O(1) */ /*@requires Q!= queue_t queue_new() /* O(1) */ /*@ensures \result!= void enq(queue_t Q, string x) /* O(1) */ /*@requires Q!= string deq(queue_t S) /* O(1) */ /*@requires Q!= NULL ; It s both wasteful and a potential source of errors to have to rewrite our code if we want our program to use integers (or chars, or pointers to structs, or arrays of strings,... ) instead of strings. A way we deal with this is by creating a type, elem, that is used by the library but not defined in the library: /*** Client interface ***/ // typedef elem; /*** Library interface ***/ // typedef * queue_t; bool queue_empty(queue_t Q) /* O(1) */ /*@requires Q!= queue_t queue_new() /* O(1) */ /*@ensures \result!= void enq(queue_t Q, elem x) /* O(1) */ /*@requires Q!= elem deq(queue_t Q) /* O(1) */ /*@requires Q!= NULL ; The underscores in the library interface, before queue_t, mean that the client doesn t know how the abstract type queue_t is implemented beyond knowing that it is a pointer. The underscores in the library interface, before queue_t, mean that the client doesn t know how the abstract type queue_t is implemented. The library is therefore free to change this and that the library is free to change this implementation without breaking any
3 Sets L13.3 (interface-respecting) client code. The underscores in the client interface mean that the library doesn t know how the abstract type elem is implemented, which means that the client is free to change this implementation without breaking the library. The library s implementation just refers to the elem type, which it expects the client to have already defined, whenever it needs to refer to client data. This approach is still not perfect, because any given program only supports a single type of queue element. We ll start working on that problem in the next lecture. 3 Generic Hash Sets When we implement the set interface with a hash table, we ll call it a hash set or hset. When we implement the dictionary interface with a hash table, we ll call it a hash dictionary or hdict. Our hash set implementation will be generic; it will work regardless of the type of keys or elements to be stored in the table. We need to think carefully about which types and functions are provided by the client of the hash set, and which are provided by the library itself. Clearly, the library should determine the type of hash sets: /* library side types */ // typedef * hset_t; That is really the only type provided by the implementation. In addition, the library interface is supposed to provide a few functions: /* library side functions */ hset_t hset_new(int capacity) /* O(1) */ /*@requires capacity > /*@ensures \result!= ; bool hset_contains(hset_t H, elem x) /* O(1) avg. */ /*@requires H!= ; void hset_add(hset_t H, elem x) /* O(1) avg. */ /*@requires H!= /*@ensures hset_contains(h, ; The function hset_new(int capacity) takes the initial capacity of the hash table as an argument (which must be strictly positive) and returns a new hash set without any elements.
4 Sets L13.4 The function hset_contains(hset_t H, elem x) answers the question of whether the element x has been added to the set already. The last function, hset_add(hset_t H, elem x), ensures that x is now in the set. From these decisions we can see that the client must provide the type of elements. Only the client can know what these might be in any particular use of the library. In this implementation, we don t need to know anything about the type elem, which we indicate by using only underscores: /* client-side types */ // typedef elem; Does the client also need to provide any functions? Yes! The hash table implementation needs functions that can operate on values of the types elem so that it can hash elements and so that it can determine whether they are equal. Since the library is supposed to be generic, the library implementer cannot write these functions; we require the client to provide them. There are two of these client-side functions. First, and most obviously, we need a hash function which maps keys to integers. /* client-side functions */ int elem_hash(elem x); The result, the hash value, can be any integer, so our hash table implementation will have to take both this arbitrary integer and m, the size of the hash table s table, into consideration when figuring out which index of the table the element hashes to. For the hash table implementation to achieve its advertised (average-case) asymptotic complexity, the hash function should have the property that its results are evenly distributed between 0 and m. The hash set implementation will work correctly (albeit slowly) even if it maps every key to 0. Hash table operations also need to be able to check for the equality of elements in order to be able to tell whether two objects that collide are actually the same or not. /* client-side functions */ bool elem_equiv(elem x, elem y); This completes the interface which we now summarize.
5 Sets L13.5 /************************/ /*** Client interface ***/ /************************/ // typedef * elem; bool elem_equiv(elem x, elem y); int elem_hash(elem x); /*************************/ /*** Library interface ***/ /*************************/ // typedef * hset_t; hset_t hset_new(int capacity) /*@requires capacity > /*@ensures \result!= ; elem hset_contains(hset_t H, elem x) /*@requires H!= ; void hset_add(hset_t H, elem x) /*@requires H!= /*@ensures hset_contains(h, ;
6 Sets L A Tiny Client One sample application is to count word occurrences say, in a corpus of Twitter data or in the collected works of Shakespeare. In this application, the keys are the words, represented as strings. Data elements are pairs of words and word counts, the latter represented as integers. 1 /******************************/ 2 /* client-side implementation */ 3 /******************************/ 4 struct wcount { 5 string word; 6 int count; 7 }; typedef struct wcount* elem; 11 int elem_hash(elem x) 12 //@requires x!= NULL; 13 { 14 return hash_string(x->word); /* from hash-string.c0 */ 15 } bool elem_equiv(elem x1, elem x2) 18 //@requires x1!= NULL && x2!= NULL; 19 { 20 return string_equiv(x1->word, x2->word); 21 } 5 A Universal Hash Function One question we have to answer is how to hash strings, that is, how to map strings to integers so that the integers are evenly distributed no matter how the input strings are distributed. We can get access to the individual characters in a string with the function string_charat(s, i), and we can get the integer ASCII value of a char with the function char_ord(c); both of these are defined in the C0 string library. Therefore, our general picture of hashing strings looks like this:
7 Sets L int hash_string(string s) { 24 int len = string_length(s); 25 int h = 0; 26 for (int i = 0; i < len; i++) 27 //@loop_invariant 0 <= i; 28 { 29 int ch = char_ord(string_charat(s, i)); 30 // Do something to combine h and ch 31 } 32 return h; 33 } Now, if we don t add anything to replace the comment, the function above will still allow the hash table to work correctly, it will just be very slow because the hash value of every string will be zero. A slightly better idea is combining h and ch with addition or multiplication: 26 for (int i = 0; i < len; i++) 27 //@loop_invariant 0 <= i; 28 { 29 int ch = char_ord(string_charat(s, i)); 30 h = h + ch; 31 } This is still pretty bad, however. We can see how bad by running entering the n = 45, 600 news vocabulary words from Homework 2 into a table with m = 22, 800 chains (load factor is 2) and running ht_stats: Hash table distribution: how many chains have size : : : : : : : : : : : 835 Longest chain: 176
8 Sets L13.8 Most of the chains are empty, and many of the chains are very, very long. One problem is that most strings are likely to have very small hash values when we use this hash function. An even bigger problem is that rearranging the letters in a string will always produce another string with the same hash value so we know that "cab" and "abc" will always collide in a hash table. Hash collisions are inevitable, but when we can easily predict that two strings have the same hash value, we should be suspicious that something is wrong. To address this, we can manipulate the value h in some way before we combine it with the current value. Some versions of Java use this as their default string hashing function. 26 for (int i = 0; i < len; i++) 27 //@loop_invariant 0 <= i; 28 { 29 int ch = char_ord(string_charat(s, i)); 30 h = 31*h; 31 h = h + ch; 32 } This does much better when we add all the news vocabulary strings into the hash table: Hash table distribution: how many chains have size : : : : : : : : : : : 1 Longest chain: 10 We can try adding a bit of randomness into this function in a number of different ways. For instance, instead of multiplying by 31, we could multiply by a number generated by the pseudo-random number generator from C0 s library: 26 rand_t r = init_rand(0x1337beef);
9 Sets L for (int i = 0; i < len; i++) 28 //@loop_invariant 0 <= i; 29 { 30 int ch = char_ord(string_charat(s, i)); 31 h = rand(r) * h; 32 h = h + ch; 33 } If we look at the performance of this function, it is comparable to the Java hash function, though it is not actually quite as good more of the chains are empty, and more are longer. Hash table distribution: how many chains have size : : : : : : : : : : : 7 Longest chain: 11 Many other variants are possible; for instance, we could try directly applying the linear congruential generator to the hash value at every step: 26 for (int i = 0; i < len; i++) 27 //@loop_invariant 0 <= i; 28 { 29 int ch = char_ord(string_charat(s, i)); 30 h = * h ; 31 h = h + ch; 32 } The key goals are that we want a hash function that is very quick to compute and that nevertheless achieves good distribution across our hash table. Handwritten hash functions often do not work well, which can significantly affect the performance of the hash table. Whenever possible, the use of randomness can help to avoid any systematic bias.
10 Sets L A Fixed-Size Implementation of Hash Tables The implementation of hash tables we wrote in lecture did not adjust their size. This requires that we can a priori predict a good size, or we will not be able to get the advertised O(1) average time complexity. Choose the size too large and it wastes space and slows the program down due to a lack of locality. Choose the size too small and the load factor will be high, leading to poor asymptotic (and practical) running time. We start with the type of lists to represent the chains of elements, and the hash table type itself. 1 /*******************************/ 2 /* library-side implementation */ 3 /*******************************/ 4 struct chain_node { 5 elem data; /* data!= NULL */ 6 struct chain_node* next; 7 }; 8 typedef struct chain_node chain; 9 10 struct hset_header { 11 int size; /* size >= 0 */ 12 int capacity; /* capacity > 0 */ 13 chain*[] table; /* \length(table) == capacity */ 14 }; The first thing after the definition of a data structure is a function to verify its invariants. Besides the invariants noted above we should check that each data value in each chain in the hash table should be non-null and the hash value of the key of every element in each chain stored in A[i] is indeed i. (This is_hset function is incomplete.) 16 bool is_hset(hset H) { 17 return H!= NULL 18 && H->capacity > 0 19 && H->size >= 0 20 && is_table_expected_length(h->table, H->capacity); 21 /* && each element is non-null */ 22 /* && there aren t equal elements */ 23 /* && the number of elements matches the size */ 24 /* && every element in H->table[i] hashes to i */ 25 }
11 Sets L13.11 Recall that the test on the length of the array must be inside an annotation, because the \length function is not available when the code is compiled without dynamic checking enabled. In order to check that the elements of a hash set hash to the correct index, we need a way of mapping the hash value returned by elem_hash to an index of the table. This is a common enough operation that we ll write a helper function: 27 int elemhash(hset H, elem x) 28 //@requires H!= NULL && H->capacity > 0; 29 //@requires x!= NULL; 30 //@ensures 0 <= \result && \result < H->capacity; 31 { 32 return abs(elem_hash(x) % H->capacity); 33 } Allocating a hash table is straightforward. 35 hset hset_new(int capacity) 36 //@requires capacity > 0; 37 //@ensures is_hset(\result); 38 { 39 hset H = alloc(struct hset_header); 40 H->size = 0; 41 H->capacity = capacity; 42 H->table = alloc_array(chain*, capacity); 43 return H; 44 } Equally straightforward is searching for an element with a given key. We omit an additional loop invariant and add an assertion that should follow from it instead. 46 bool hset_contains(hset H, elem x) 47 //@requires is_hset(h); 48 //@requires x!= NULL; 49 { 50 int i = elemhash(h, x); 51 for (chain* p = H->table[i]; p!= NULL; p = p->next) { 52 if (elem_equiv(p->data, x)) return true; 53 } 54 return false; 55 }
12 Sets L13.12 We can extract the key from the element l->data because the data cannot be null in a valid hash table. (Think: how would we phrase this as a loop invariant?) Inserting an element follows generally the same structure as search. If we find an element in the correct chain with the same key we replace it. If we find none, we insert a new node at the beginning of the chain. 57 void hset_insert(hset H, elem x) 58 //@requires is_hset(h); 59 //@requires x!= NULL; 60 //@ensures is_hset(h); 61 //@ensures x == hset_contains(h, x); 62 { 63 int i = elemhash(h, x); 64 for (chain* p = H->table[i]; p!= NULL; p = p->next) 65 { 66 if (elem_equiv(p->data, x)) { 67 p->data = x; 68 return; 69 } 70 } // prepend new element 73 chain* p = alloc(chain); 74 p->data = x; 75 p->next = H->table[i]; 76 H->table[i] = p; 77 (H->size)++; 78 }
13 Sets L13.13 Exercises Exercise 1. Extend the hash table implementation so it dynamically resizes itself when the load factor exceeds a certain threshold. When doubling the size of the hash table you will need to explicitly insert every element from the old hash table into the new one, because the result of hashing depends on the size of the hash table. Exercise 2. Redo the library implementation for a different client interface that has a function elem_hash(key k, int m) that returns a result between 0 (inclusive) and m (exclusive). Exercise 3. Extend the hash table interface with a new function ht_tabulate that returns an array with the elements in the hash table, in some arbitrary order. Exercise 4. Extend the hash table interface with a new function to delete an element with a given key from the table. To be extra ambitious, shrink the size of the hash table once the load factor drops below some minimum, similarly to the way we could grow and shrink unbounded arrays.
Lecture 13 Notes Sets
Lecture 13 Notes Sets 15-122: Principles of Imperative Computation (Fall 2015) Frank Pfenning, Rob Simmons 1 Introduction In this lecture, we will discuss the data structure of hash tables further and
More informationLecture 13 Hash Dictionaries
Lecture 13 Hash Dictionaries 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, Rob Simmons, Iliano Cervesato In this lecture, we will discuss the data structure of hash tables further
More informationLecture Notes on Interfaces
Lecture Notes on Interfaces 15-122: Principles of Imperative Computation Frank Pfenning Lecture 14 October 16, 2012 1 Introduction The notion of an interface to an implementation of an abstract data type
More informationLecture 14 Notes Generic Data Structures
Lecture 14 Notes Generic Data Structures 15-122: Principles of Imperative Computation (Fall 2015) Rob Simmons 1 Introduction Our story about client interfaces in the previous lecture was incomplete. We
More informationLecture 12 Notes Hash Tables
Lecture 12 Notes Hash Tables 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons 1 Introduction In this lecture we re-introduce the dictionaries that were implemented
More informationLecture 12 Hash Tables
Lecture 12 Hash Tables 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons Dictionaries, also called associative arrays as well as maps, are data structures that are
More informationLecture Notes on Hash Tables
Lecture Notes on Hash Tables 15-122: Principles of Imperative Computation Frank Pfenning Lecture 13 February 24, 2011 1 Introduction In this lecture we introduce so-called associative arrays, that is,
More informationLecture 11 Unbounded Arrays
Lecture 11 Unbounded Arrays 15-122: Principles of Imperative Computation (Spring 2018) Rob Simmons, Frank Pfenning Arrays have efficient O(1) access to elements given an index, but their size is set at
More informationLecture 10 Notes Linked Lists
Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to
More informationLecture 10 Notes Linked Lists
Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to
More informationLecture 15 Notes Binary Search Trees
Lecture 15 Notes Binary Search Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, André Platzer, Rob Simmons 1 Introduction In this lecture, we will continue considering ways
More informationMidterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.
Midterm 2 Exam 15-122 Principles of Imperative Computation Tuesday 31 st March, 2015 Name: Andrew ID: Recitation Section: Instructions This exam is closed-book with one sheet of notes permitted. You have
More informationLecture 8 Data Structures
Lecture 8 Data Structures 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture we introduce the idea of imperative data
More informationLecture 15 Binary Search Trees
Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to
More informationYou must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.
15-122 Homework 4 Page 1 of 13 15-122 : Principles of Imperative Computation, Summer 1 2014 Written Homework 4 Due: Thursday, June 12 before recitation Name: Andrew ID: Recitation: In this assignment,
More informationLecture Notes on Tries
Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina Notes by Frank Pfenning Lecture 24 April 19, 2011 1 Introduction In the data structures implementing associative arrays
More informationLecture Notes on Memory Layout
Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,
More informationLecture 18 Restoring Invariants
Lecture 18 Restoring Invariants 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we will implement heaps and operations on them. The theme of this lecture is reasoning
More informationLecture 10 Linked Lists
Lecture 10 Linked Lists 15-122: Principles of Imperative Computation (Spring 2017) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to implement
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 16 March 17, 2015 1 Introduction In this lecture, we will continue considering ways
More informationLecture 9 Stacks & Queues
Lecture 9 Stacks & Queues 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, André Platzer, Rob Simmons In this lecture we introduce queues and stacks as data structures, e.g., for
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 1 Introduction In the previous two lectures we have seen how to exploit the structure of binary
More informationLecture Notes on Generic Data Structures
Lecture Notes on Generic Data Structures 15-122: Principles of Imperative Computation Frank Pfenning Lecture 22 November 15, 2012 1 Introduction Using void* to represent pointers to values of arbitrary
More information: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class
15-122 Assignment 4 Page 1 of 8 15-122 : Principles of Imperative Computation Summer 1 2012 Assignment 4 (Theory Part) Due: Monday, June 11, 2012 in class Name: Andrew ID: Recitation: The written portion
More informationLecture Notes on Generic Data Structures
Lecture Notes on Generic Data Structures 15-122: Principles of Imperative Computation Frank Pfenning, Penny Anderson Lecture 21 November 7, 2013 1 Introduction Using void* to represent pointers to values
More informationLecture Notes on Queues
Lecture Notes on Queues 15-122: Principles of Imperative Computation Frank Pfenning and Jamie Morgenstern Lecture 9 February 14, 2012 1 Introduction In this lecture we introduce queues as a data structure
More informationLecture Notes on Tries
Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina, Frank Pfenning, Rob Simmons, Penny Anderson Lecture 22 June 20, 2014 1 Introduction In the data structures implementing
More informationThe assignment is due by 1:30pm on Tuesday 5 th April, 2016.
15-122 : Principles of Imperative Computation, Spring 2016 Written Homework A/B Due: Tuesday 5 th April, 2016 Name:q1 Andrew ID: q2 Section:q3 This written homework covers heaps and priority queues. The
More informationLecture Notes on Queues
Lecture Notes on Queues 15-122: Principles of Imperative Computation Frank Pfenning Lecture 9 September 25, 2012 1 Introduction In this lecture we introduce queues as a data structure and linked lists
More informationLecture 6 Binary Search
Lecture 6 Binary Search 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning One of the fundamental and recurring problems in computer science is to find elements in collections, such
More informationMost of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.
15-122 : Principles of Imperative Computation, Spring 2016 Written Homework 7 Due: Monday 7 th March, 2016 Name:q1 Andrew ID: q2 Section:q3 This written homework covers amortized analysis and hash tables.
More informationLecture 9 Stacks & Queues
Lecture 9 Stacks & Queues 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, André Platzer, Rob Simmons In this lecture we introduce queues and stacks as data structures, e.g.,
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 15 March 6, 2014 1 Introduction In this lecture, we will continue considering associative
More informationMidterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011
Midterm I Exam 15-122 Principles of Imperative Computation Frank Pfenning February 17, 2011 Name: Sample Solution Andrew ID: fp Section: Instructions This exam is closed-book with one sheet of notes permitted.
More informationMost of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.
15-122 : Principles of Imperative Computation, Spring 2016 Written Homework 5 6 Due: Monday 22 nd February, 2016 Name:q1 Andrew ID:q2 Section:q3 This written homework covers big-o notation, some reasoning
More informationMidterm 1 Exam Principles of Imperative Computation. Thursday 6 th October, This exam is closed-book with one sheet of notes permitted.
Midterm 1 Exam 15-122 Principles of Imperative Computation Thursday 6 th October, 2016 Name: Andrew ID: Recitation Section: Instructions This exam is closed-book with one sheet of notes permitted. You
More informationMidterm 1 Solutions Principles of Imperative Computation. Thursday 6 th October, 2016
Midterm 1 Solutions 15-122 Principles of Imperative Computation Thursday 6 th October, 2016 Name: Harry Bovik Andrew ID: bovik Recitation Section: S Instructions This exam is closed-book with one sheet
More informationLecture Notes on Tries
Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina, Frank Pfenning, Rob Simmons, Penny Anderson Lecture 21 November 10, 2014 1 Introduction In the data structures implementing
More informationLecture 17 Priority Queues
Lecture 17 Priority Queues 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons In this lecture we will look at priority queues as an abstract type and discuss several
More informationLecture 16 More on Hashing Collision Resolution
Lecture 16 More on Hashing Collision Resolution Introduction In this lesson we will discuss several collision resolution strategies. The key thing in hashing is to find an easy to compute hash function.
More information15-122: Principles of Imperative Computation, Fall 2015
15-122 Programming 5 Page 1 of 10 15-122: Principles of Imperative Computation, Fall 2015 Homework 5 Programming: Clac Due: Thursday, October 15, 2015 by 22:00 In this assignment, you will implement a
More informationYou must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.
15-122 Assignment 4 Page 1 of 12 15-122 : Principles of Imperative Computation Fall 2012 Assignment 4 (Theory Part) Due: Thursday, October 18, 2012 at the beginning of lecture Name: Andrew ID: Recitation:
More informationPrint out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.
15-122 : Principles of Imperative Computation, Fall 2015 Written Homework 5 Due: Monday, October 5, 2015 by 6PM Name: Andrew ID: Section: This written homework covers big-o notation, some reasoning about
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Tom Cortina Lecture 15 October 14, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure
More informationLecture 17 Notes Priority Queues
Lecture 17 Notes Priority Queues 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons 1 Introduction In this lecture we will look at priority queues as an abstract type
More information: Principles of Imperative Computation, Spring Homework 3 Theory [Update 1]
15-122 Homework 3 Page 1 of 15 15-122 : Principles of Imperative Computation, Spring 2013 Homework 3 Theory [Update 1] Due: Thursday, February 21, 2013, at the beginning of lecture Name: Andrew ID: Recitation:
More informationLecture 16 Notes AVL Trees
Lecture 16 Notes AVL Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning 1 Introduction Binary search trees are an excellent data structure to implement associative arrays,
More informationLecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than
More informationYou must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.
15-122 Assignment 5 Page 1 of 11 15-122 : Principles of Imperative Computation Fall 2012 Assignment 5 (Theory Part) Due: Tuesday, October 30, 2012 at the beginning of lecture Name: Andrew ID: Recitation:
More informationMidterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011
Midterm II Exam 15-122 Principles of Imperative Computation Frank Pfenning March 31, 2011 Name: Sample Solution Andrew ID: fp Section: Instructions This exam is closed-book with one sheet of notes permitted.
More informationLecture Notes on Quicksort
Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 September 20, 2012 1 Introduction In this lecture we first sketch two related algorithms for sorting that
More informationLecture Notes on Types in C
Lecture Notes on Types in C 15-122: Principles of Imperative Computation Frank Pfenning, Rob Simmons Lecture 20 April 2, 2013 1 Introduction In lecture 18, we emphasized the things we lost by going to
More informationLecture 18. Collision Resolution
Lecture 18 Collision Resolution Introduction In this lesson we will discuss several collision resolution strategies. The key thing in hashing is to find an easy to compute hash function. However, collisions
More informationAAL 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 informationLecture 9 Notes Stacks & Queues
Lecture 9 Notes tacks & Queues 15-122: Principles of Imperative Computation (ummer 1 2015) Frank Pfenning, André Platzer, Rob immons 1 Introduction In this lecture we introduce queues and stacks as data
More informationQUIZ. What are 3 differences between C and C++ const variables?
QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,
More informationLecture Notes on Quicksort
Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve
More informationWeek 5, continued. This is CS50. Harvard University. Fall Cheng Gong
This is CS50. Harvard University. Fall 2014. Cheng Gong Table of Contents News... 1 Buffer Overflow... 1 Malloc... 6 Linked Lists... 7 Searching... 13 Inserting... 16 Removing... 19 News Good news everyone!
More informationCITS2200 Data Structures and Algorithms. Topic 15. Hash Tables
CITS2200 Data Structures and Algorithms Topic 15 Hash Tables Introduction to hashing basic ideas Hash functions properties, 2-universal functions, hashing non-integers Collision resolution bucketing and
More informationYou must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.
15-122 Assignment 3 Page 1 of 12 15-122 : Principles of Imperative Computation Fall 2012 Assignment 3 (Theory Part) Due: Thursday, October 4 at the beginning of lecture. Name: Andrew ID: Recitation: The
More informationData Structure Series
Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after
More informationLecture Notes on Memory Management
Lecture Notes on Memory Management 15-122: Principles of Imperative Computation Frank Pfenning Lecture 22 November 11, 2010 1 Introduction Unlike C0 and other modern languages like Java, C#, or ML, C requires
More informationLecture Notes on Dynamic Programming
Lecture Notes on Dynamic Programming 15-122: Principles of Imperative Computation Frank Pfenning Lecture 23 November 16, 2010 1 Introduction In this lecture we introduce dynamic programming, which is a
More informationCOSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26
COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.
More informationLecture 1: Overview
15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant
More informationCSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)
CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) HW6 NOTE: Do not use the STL map or STL pair for HW6. (It s okay to use them for the contest.)
More informationprintf( Please enter another number: ); scanf( %d, &num2);
CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful
More informationLecture 20 Notes C s Memory Model
Lecture 20 Notes C s Memory Model 15-122: Principles of Imperative Computation (Fall 2015) Frank Pfenning, Rob Simmons 1 The C0 and C Memory Model When we talk about memory in C0, C1, and C, that memory
More informationLecture Notes on Priority Queues
Lecture Notes on Priority Queues 15-122: Principles of Imperative Computation Frank Pfenning Lecture 16 October 18, 2012 1 Introduction In this lecture we will look at priority queues as an abstract type
More information15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00
15-122 Programming 7 Page 1 of 8 15-122: Principles of Imperative Computation, Spring 2016 Programming homework 7: Text Buers Due: Thursday, March 10, 2016 by 22:00 For the programming portion of this
More informationSorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha
Lecture Notes on Sorting 15-122: Principles of Imperative Computation Frank Pfenning Lecture 7 February 1, 2011 1 Introduction We have seen in the last lecture that sorted arrays drastically reduce the
More informationLecture 16 AVL Trees
Lecture 16 AVL Trees 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning Binary search trees are an excellent data structure to implement associative arrays, maps, sets, and similar
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationMidterm Review (overview of fall 2005 midterm)" Jennifer Rexford!
Midterm Review (overview of fall 2005 midterm)" Jennifer Rexford! 1 1. Modulo Arithmetic and Character I/O" void f(unsigned int n) { do { } putchar( 0 + (n % 10)); } while (n /= 10); putchar( \n ); What
More informationCMSC 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 informationLecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationCS 161 Computer Security
Wagner Spring 2014 CS 161 Computer Security 1/27 Reasoning About Code Often functions make certain assumptions about their arguments, and it is the caller s responsibility to make sure those assumptions
More informationLecture 3 Notes Arrays
Lecture 3 Notes Arrays 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, André Platzer 1 Introduction So far we have seen how to process primitive data like integers in imperative
More informationCSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists
CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,
More informationLecture Notes on Contracts
Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #29 Arrays in C (Refer Slide Time: 00:08) This session will learn about arrays in C. Now, what is the word array
More informationBasic Idea of Hashing Hashing
Principles of Imperative Computation V. Adamchik CS 5-22 Carnegie Mellon University Hashing Data Structure Searching Worst-case runtime complexity Unsorted Array Sorted Array Unsorted Linked List Sorted
More informationLecture 20 C s Memory Model
Lecture 20 C s Memory Model 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons 1 The C0 and C Memory Model When we talk about memory in C0, C1, and C, that memory is
More informationCS2 Algorithms and Data Structures Note 4
CS2 Algorithms and Data Structures Note 4 Hash Tables In this lecture, we will introduce a particularly efficient data structure for the Dictionary ADT. 4.1 Dictionaries A Dictionary stores key element
More informationData Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada
CS102 Hash Tables Prof. Tejada 1 Vectors, Linked Lists, Stack, Queues, Deques Can t provide fast insertion/removal and fast lookup at the same time The Limitations of Data Structure Binary Search Trees,
More informationCuckoo Hashing for Undergraduates
Cuckoo Hashing for Undergraduates Rasmus Pagh IT University of Copenhagen March 27, 2006 Abstract This lecture note presents and analyses two simple hashing algorithms: Hashing with Chaining, and Cuckoo
More informationPrint out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.
15-122 : Principles of Imperative Computation, Fall 2015 Written Homework 6 Due: Monday, October 12, 2015 Name: Andrew ID: Section: This written homework covers structs, pointers, and linked lists. Print
More informationLecture 24 Notes Search in Graphs
Lecture 24 Notes Search in Graphs 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, André Platzer, Rob Simmons, Penny Anderson 1 Introduction In this lecture, we will discuss the
More informationMidterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010
Midterm I Exam 15-122 Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas September 30, 2010 Name: Andrew ID: Instructions This exam is closed-book with one sheet of notes permitted.
More informationLecture 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 informationTypes, Expressions, and States
8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties
More informationLecture 5 Sorting Arrays
Lecture 5 Sorting Arrays 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons We begin this lecture by discussing how to compare running times of functions in an abstract,
More informationLecture Notes on Arrays
Lecture Notes on Arrays 15-122: Principles of Imperative Computation July 2, 2013 1 Introduction So far we have seen how to process primitive data like integers in imperative programs. That is useful,
More informationLecture 24 Search in Graphs
Lecture 24 Search in Graphs 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, André Platzer, Rob Simmons, Penny Anderson, Iliano Cervesato In this lecture, we will discuss the question
More informationLecture Notes on Binary Decision Diagrams
Lecture Notes on Binary Decision Diagrams 15-122: Principles of Imperative Computation William Lovas Notes by Frank Pfenning Lecture 25 April 21, 2011 1 Introduction In this lecture we revisit the important
More informationData Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 18 Tries Today we are going to be talking about another data
More informationFunctions, Pointers, and the Basics of C++ Classes
Functions, Pointers, and the Basics of C++ Classes William E. Skeith III Functions in C++ Vocabulary You should be familiar with all of the following terms already, but if not, you will be after today.
More informationLecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationLecture 23 Representing Graphs
Lecture 23 Representing Graphs 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, André Platzer, Rob Simmons, Penny Anderson, Iliano Cervesato In this lecture we introduce graphs.
More informationCpt S 223. School of EECS, WSU
Hashing & Hash Tables 1 Overview Hash Table Data Structure : Purpose To support insertion, deletion and search in average-case constant t time Assumption: Order of elements irrelevant ==> data structure
More information