You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Similar documents
You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

: Principles of Imperative Computation, Spring Homework 3 Theory [Update 1]

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Lecture Notes on Interfaces

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class

Lecture Notes on Queues

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Lecture 10 Linked Lists

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Lecture Notes on Queues

Lecture 13 Notes Sets

Lecture 13 Notes Sets

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Midterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010

Final Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. December 10, Name: Andrew ID: Section:

Lecture 9 Stacks & Queues

Lecture 13 Hash Dictionaries

Chapter 9 STACK, QUEUE

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

Lecture 9 Stacks & Queues

Lecture 16 More on Hashing Collision Resolution

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Midterm 1 Exam Principles of Imperative Computation. Thursday 6 th October, This exam is closed-book with one sheet of notes permitted.

(b) If a heap has n elements, what s the height of the tree?

Cpt S 223. School of EECS, WSU

The assignment is due by 1:30pm on Tuesday 5 th April, 2016.

Midterm 1 Solutions Principles of Imperative Computation. Thursday 6 th October, 2016

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

Lecture 9 Notes Stacks & Queues

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Section 05: Midterm Review

Lecture Notes on Binary Search Trees

Dictionaries and Hash Tables

More on Hashing: Collisions. See Chapter 20 of the text.

Computer Science Foundation Exam

Lecture 15 Notes Binary Search Trees

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1)

DATA STRUCTURES AND ALGORITHMS

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Lecture Notes on Binary Search Trees

Topic HashTable and Table ADT

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Hash[ string key ] ==> integer value

Standard ADTs. Lecture 19 CS2110 Summer 2009

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Lecture Notes on Generic Data Structures

INSTITUTE OF AERONAUTICAL ENGINEERING

.:: UNIT 4 ::. STACK AND QUEUE

Hashing. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Lecture 18. Collision Resolution

CMPT 125: Practice Midterm Answer Key

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

CS171 Midterm Exam. October 29, Name:

Practice Midterm Exam Solutions

Basic Idea of Hashing Hashing

CSCE 2014 Final Exam Spring Version A

AAL 217: DATA STRUCTURES

Recitation 9. Prelim Review

Hash Tables. Hashing Probing Separate Chaining Hash Function

ECE250: Algorithms and Data Structures Midterm Review

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

1 CSE 100: HASH TABLES

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

CS 61B Spring 2017 Guerrilla Section 4 Worksheet. 11 March 2017

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CSL 201 Data Structures Mid-Semester Exam minutes

CPSC 331 Term Test #2 March 26, 2007

The dictionary problem

III Data Structures. Dynamic sets

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Computer Science 302 Spring 2007 Practice Final Examination: Part I

COMP171. Hashing.

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

Successful vs. Unsuccessful

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Stacks and Queues. CSE Data Structures April 12, 2002

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Lecture 10 March 4. Goals: hashing. hash functions. closed hashing. application of hashing

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function.

CSE 214 Computer Science II Searching

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 19 November 2014

Lecture Notes on Memory Layout

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Tables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries

Transcription:

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, you will work with amortized analysis and hashing. Question Points Score 1 6 2 10 3 9 4 3 5 7 Total: 35 You 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 2 of 13 Part I: Pushing, Popping, and Paying Tokens 1. Amortized Analysis Consider a counter represented as k bits: b k 1 b k 2... b 1 b 0 The cost of flipping the i th bit is 2 i tokens. For example, b 0 costs 1 token to flip, b 1 costs 2, and b 2 costs 4. We wish to analyze the cost of performing n = 2 k increments of this k-bit counter. (Note that k is not a constant.) (2) (a) The worst case for incrementing the counter is when every bit is set to 1. The increment then causes every bit to flip, the cost of which is Show that this cost is O(n). 1 + 2 + 2 2 + 2 3 +... + 2 k 1 (2) (b) Over the course of n increments, how many tokens does it cost to flip the i th bit? (Hint: how many times does the i th bit flip? What is the cost of flipping the i th bit? You may want to work through some examples, such as i = 0 and i = 1, and look for a pattern.) (2) (c) Based on your answer to the previous part, what is the total cost of performing n increments? Based on your answer above, what is the amortized cost of a single increment?

15-122 Homework 4 Page 3 of 13 2. A New Implementation of Queues Recall the interface for stack that stores elements of the type elem: stack stack_new(); /* O(1) */ bool stack_empty(stack S); /* O(1) */ void push(elem e, stack S); /* O(1) */ elem pop(stack S) /* O(1) */ //@requires!stack_empty(s); ; We wish to implement a queue using two stacks, called instack and outstack. When we enqueue an element, we push it on top of the instack. When we dequeue an element, we pop the top off of the outstack. If the outstack is empty when we try to dequeue, then we will first need to move all of the elements from the instack to the outstack. For example, below is one possible configuration of a two-stack queue containing the elements A through E: We will use the following C0 code: struct queue stack instack; stack outstack; ; typedef struct queue* queue; bool is_queue(queue Q) return Q!= NULL; queue queue_new() //@ensures is_queue(\result); queue Q = alloc(struct queue); Q->instack = stack_new(); Q->outstack = stack_new(); return Q;

15-122 Homework 4 Page 4 of 13 (5) (a) Write the function queue empty that returns true if the queue is empty. bool queue_empty(queue Q) //@requires is_queue(q); //@ensures is_queue(q); Write the function enqueue based on the description of the data structure above. void enqueue(elem e, queue Q) //@requires is_queue(q); //@ensures is_queue(q);

15-122 Homework 4 Page 5 of 13 Write the function dequeue based on the description of the data structure above. elem dequeue(queue Q) //@requires is_queue(q); //@requires!queue_empty(q); //@ensures is_queue(q); (2) (b) We now determine the runtime complexity of the enqueue and dequeue operations. Let k be the total number of elements in the queue. What is the worst-case runtime complexity of each of the following queue operations based on the description of the data structure implementation given above? Write ONE sentence that explains each answer. enqueue: O( dequeue: O(

15-122 Homework 4 Page 6 of 13 (3) (c) Using amortized analysis, we can show that the worst-case complexity of a valid sequence of n queue operations is O(n). This means that the amortized cost per operation is O(1), even though a single operation might require more than constant time. In this case, a valid sequence of queue operations must start with the empty queue. Each operation must be either an enqueue or a dequeue. Assume that push and pop each consume one token. How many tokens are required to enqueue an element? State for what purpose each token is used. (Your answer for the number of tokens should be a constant integer since the amortized cost should be O(1).) How many tokens are required to dequeue an element? Once again, you must state for what purpose each token is used. (Your answer for the number of tokens should be a constant integer since the amortized cost should be O(1).)

15-122 Homework 4 Page 7 of 13 3. Dealing with Collisions Part II: A Mash Of Hash In a hash table, when two keys hash to the same location, we have a collision. There are multiple strategies for handling collisions: Separate chaining: each location in the table stores a chain (typically a linked list) of all keys which hashed to that location. Open addressing: each location in the table stores keys directly. We then probe the table to search for keys which may have collided. Suppose our hash function is h, the size of the table is m, and we are attempting to insert or look up the key k: Linear probing: on the i th attempt (counting from 0), we look at the index (h(k) + i) mod m. Quadratic probing: on the i th attempt (counting from 0), we look at the index (h(k) + i 2 ) mod m. For insertion, we are searching for an empty slot to put the key. For lookup, we are trying to find the key itself. (2) (a) You are given a hash table of size m with n inserted keys that resolves collisions using separate chaining. If n = 2m and the keys are not evenly distributed, what is the worst-case Big-O runtime complexity of searching for a specific key? O( ) Under the same conditions, except that now the keys are evenly distributed, what is the worst-case Big-O runtime complexity of searching for a specific key? O( )

15-122 Homework 4 Page 8 of 13 (2) (b) You are given a hash table of capacity m = 13 which resolves collisions using separate chaining. The hash function is h(k) = k. Show how the set of keys below will be stored in the hash table by drawing the final state of each chain of the table after all of the keys are inserted, one by one, in the order shown. If a chain is empty, you may indicate a NULL pointer using a diagram such as where appropriate. 54, 23, 67, 88, 39, 75, 49, 5

15-122 Homework 4 Page 9 of 13 (2) (c) Show where the sequence of keys shown below are stored in the hash table if they are inserted one by one, in the order shown, with h(k) = k and m = 13, using linear probing to resolve collisions. 54, 23, 67, 88, 39, 75, 49, 5 (2) (d) Show where the sequence of keys shown below are stored in the hash table if they are inserted one by one, in the order shown, with h(k) = k and m = 13, using quadratic probing to resolve collisions. 54, 23, 67, 88, 39, 75, 49, 5 (1) (e) Quadratic probing suffers from one problem that linear probing does not. In particular, given a non-full hashtable, insertions with linear probing will always succeed, while insertions with quadratic probing might not (i.e. they may never find an open spot to insert). Using h(k) = k as your hash function and m = 6 as your table capacity, give an example of a non-full hashtable and a key that cannot be successfully inserted using quadratic probing. (Hint: start by inserting the keys 36, 78, 12, 90.) Key to insert:

15-122 Homework 4 Page 10 of 13 4. Strings as Keys In a popular programming language, strings are hashed using the following function: h(s) = 31 p 1 s 0 + 31 p 2 s 1 +... + 31 1 s p 2 + 31 0 s p 1 where s i is the ASCII code for the i th character of string s, and p is the length of the string. (Also, don t forget that the result of the hash function is modded by the size of the hash table.) (1) (a) If 15105 strings were stored in a hash table of size 3021 using separate chaining, what would the load factor of the table be? If the strings above were equally distributed in the hash table, what does the load factor tell you about the chains? (2) (b) Using the hash function above with a table size of 3021, give an example of two different valid C0 strings that would collide in the hash table and would be stored in the same chain. Show your work. (Use short strings please!)

15-122 Homework 4 Page 11 of 13 5. Hash Tables: Data Structure Invariants Refer to the C0 code below for is ht that checks that a given hash table ht is a valid hash table. struct chain_node elem data; struct chain_node* next; ; typedef struct chain_node chain; struct ht_header chain*[] table; int m; // m = capacity = maximum number of chains table can hold int n; // n = size = number of elements stored in hash table ; typedef struct ht_header* ht; bool is_ht(ht H) if (H == NULL) return false; if (!(H->m > 0)) return false; if (!(H->n >= 0)) return false; //@assert H->m == \length(h->table); return true; An obvious data structure invariant of our hash table is that every element of a chain hashes to the index of that chain. This specification function is incomplete, then: we never test that the contents of the hash table hold to this data structure invariant. That is, we test only on the struct ht, and not the properties of the array within. You may assume the existence of the following client functions as discussed in class: int hash(key k); bool key_equal(key k1, key k2); key elem_key(elem e) //@requires e!= NULL; ;

15-122 Homework 4 Page 12 of 13 (5) (a) Extend is ht from above, adding code to check that every element in the hash table matches the chain it is located in, and that each chain is non-cyclic. bool is_ht(ht H) if (H == NULL) return false; if (!(H->m > 0)) return false; if (!(H->n >= 0)) return false; //@assert H->m == \length(h->table); int nodecount = 0; for (int i = 0; i < ; i++) // set p equal to a pointer to first node // of chain i in table, if any chain* p = ; while ( ) elem e = p->data; if ((e == NULL) (!= i)) return false; nodecount++; if (nodecount > ) return false; p = ; if ( ) return false; return true;

15-122 Homework 4 Page 13 of 13 (2) (b) Consider the ht lookup function given below: elem ht_lookup(ht H, key k) //@requires is_ht(h); int i = abs(hash(k) % H->m); chain* p = H->table[i]; while (p!= NULL) //@loop_invariant is_chain(p, i, H->m); //@assert p->data!= NULL; if (key_equal(elem_key(p->data), k)) return p->data; else p = p->next; /* not in chain */ return NULL; Give a simple postcondition for this function. /*@ensures \result == @*/ key_equal(k, );