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

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

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

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

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

Lecture Notes on Interfaces

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

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.

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 13 Notes Sets

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

Basic Idea of Hashing Hashing

Lecture 13 Notes Sets

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

: Principles of Imperative Computation. Fall Assignment 4: Text Editor - UPDATE 2. Due: Friday, October 19, 2012 by 23:59 [Updated]

Lecture Notes on Binary Search Trees

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.

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

Lecture Notes on Binary Search Trees

Lecture 13 Hash Dictionaries

Lecture Notes on Tries

Lecture 10 Notes Linked Lists

Lecture Notes on Tries

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

Lecture 15 Notes Binary Search Trees

: Principles of Imperative Computation. Fall Assignment 5: Interfaces, Backtracking Search, Hash Tables

Data Structure and Algorithm Homework #3 Due: 1:20pm, Thursday, May 16, 2017 TA === Homework submission instructions ===

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

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

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

Lecture 10 Notes Linked Lists

Lecture Notes on Queues

Lecture Notes on Memory Layout

: Principles of Imperative Computation, Fall Written Homework 1 Solutions

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Lecture Notes on Binary Search Trees

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

15 122: Principles of Imperative Computation. Practice Exam August 4, 2015

Lecture 11 Unbounded Arrays

Lecture Notes on Generic Data Structures

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

CS 106X Sample Final Exam #2

Pointers, Pointers, Pointers!

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

Lecture 15 Binary Search Trees

Practice Midterm Exam Solutions

CS 315 Data Structures mid-term 2

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Lecture 16 More on Hashing Collision Resolution

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

ECE242 Data Structures and Algorithms Fall 2008

Lecture 10 Linked Lists

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

15-122: Principles of Imperative Computation, Spring 2013

Singly linked lists in C.

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Lecture 12 Hash Tables

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Lecture Notes on Hash Tables

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

BBM 201 DATA STRUCTURES

Dynamic Data Structures (II)

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

Übungen zur Vorlesung Informatik II (D-BAUG) FS 2017 D. Sidler, F. Friedrich

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

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Lecture 12 Notes Hash Tables

CS 104 (Spring 2014) Final Exam 05/09/2014

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

ECE 15B COMPUTER ORGANIZATION

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Programm- & Systemverifikation

Final assignment: Hash map

Introduction to Programming, Aug-Dec 2006

Lecture Notes on Generic Data Structures

15-122: Principles of Imperative Computation, Spring Written Homework 12. Due: Sunday 15 th April, 2018 by 10pm. Name: Andrew ID: Section:

Data Structure and Algorithm Midterm Reference Solution TA

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

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

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

CS61B, Fall 1995 Midterm #2 K. Yelick

15-110: Principles of Computing, Spring 2018

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Introduction to Data Structures

PRACTICE MIDTERM EXAM #1

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Introduction to Algorithms

QUIZ. What are 3 differences between C and C++ const variables?

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Computer Programming

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

Lecture Notes on Priority Queues

Transcription:

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 of this weeks homework will give you some practice working with amortized analysis, memory management, hashtables, and recursion. You can either type up your solutions or write them neatly by hand in the spaces provided. You should submit your work in class on the due date just before lecture or recitation begins. Please remember to staple your written homework before submission. Question Points Score 1 4 2 2 3 4 4 6 5 4 Total: 20

15-122 Assignment 4 Page 2 of 8 1. Hash Tables. In Java, strings are hashed using the following function: (s[0] 31 n 1 +s[1] 31 n 2 +...+s[n 2] 31 1 +s[n 1] 31 0 ) % m where s[i] is the ASCII code for the ith character of string s, n is the length of the string, and m is the hash table size. (2) (a) If 15122 strings were stored in a hash table of size 4401, what would the load factor of the table be? (2) (b) Using the hash function above with a table size of 4401, give an example of two strings that would collide and would be stored in the same chain. Show your work. (Think of short strings please!)

15-122 Assignment 4 Page 3 of 8 (2) 2. Invariant of Hashtables. In lecture, we wrote a hash table specification function, is_ht, to check that a given ht is actually a hash table. bool is_ht(ht H) { if (H == NULL) return false; if (!(H->size > 0)) return false; return true; } This specification function is incomplete. Extend is_ht from above, implementing code to check that every element of a chain in the table hashes to the index of that chain.

15-122 Assignment 4 Page 4 of 8 3. Amortized Analysis. (2) (a) There are n ECE students who want to get into the Gates-Hillman building after 6pm. Unfortunately 122 TAs control the building and charge a toll for entrance. The toll policy is the following: the i-th student is charged k 2 when i 0 mod k, or zero otherwise. What is the amortized cost per student for entering the building? Assume that n > k. Explain your answer.

15-122 Assignment 4 Page 5 of 8 (2) (b) Let us recall that when we analyze the amortized cost we are averaging the running time of an algorithm over a worst-case sequence of executions. In this task you don t need to compute the amortized cost but rather provide an example of such sequence. Consider a linked list of items, where searching for k-th element takes O(k) cost (this is the cost of traversing the list from the head to the kth location in the list). We shall use n to denote the number of items in the list. Our goal is to find a simple rule for updating the list (by performing exchanges) that will make the total cost of a sequence of n operations as small as possible. Famous Fred Hacker suggests the following rule: after accessing an item in the list, exchange it with the immediately preceding item. He claims that such technique will improve the amortized cost of single searching to O(1). To prove him wrong, you need to create a counterexample, namely, find a sequence of n search operations such that the amortized cost of a single search (under Hacker s rule) will be linear. Explain your answer.

15-122 Assignment 4 Page 6 of 8 4. Memory Management. Consider the following two implementations of a stack. The first implementation uses a linked list to create the stack data structure struct list_stack { struct list_node* top; struct list_node* bottom; struct list_node { char data; struct list_node* next; and the second implementation uses an unbounded array: struct uba_stack { struct ubarray* top; struct ubarray { int limit; /* limit > 0 */ int size; /* 0 <= size && size <= limit */ char[] elems; /* \length(elems) == limit */ Recall that in C 0, a char is represented using 8 bits a pointer is represented using 64 bits an int is represented using 32 bits An array of characters of size n takes exactly 8n+64 bits, where 8n is for the characters and 32 bits each are for two ints: one to record the length of the array and one to record the size of its elements. The size of a struct is the sum of the sizes of its elements. Consider a program that uses a stack of characters and can allocate only 1 MB (2 20 bytes, or 8 2 20 bits) of memory for this stack. Answer the questions below.

15-122 Assignment 4 Page 7 of 8 (3) (a) What is the maximum number of characters that can be stored using a list_stack? Show your work. (3) (b) What is the maximum number of characters that can be stored using a uba_stack? Show your work.

15-122 Assignment 4 Page 8 of 8 (4) 5. Recursion. Recursive functions can be used to construct data structures of recursive type, like linked lists. For the following exercise, you assume a linked list defined by typedef struct list_node* list; struct list_node { int data; list next; Write a recursive function deleteall matching the following specification: list deleteall(int key, list start, list end) The function deletes all occurrences of key in the given linked list between start and end nodes inclusively. If deletion cannot be performed (e.g., the item to delete is not in the list), it should leave the list unchanged.