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

Similar documents
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. Summer Assignment 4. Due: Monday, June 11, 2012 in class

Lecture Notes on Interfaces

You must include this cover sheet. Either type up the assignment using theory3.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 theory4.tex, or print out this PDF.

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

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

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

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

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 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

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

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

Lecture Notes on Priority Queues

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

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

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Lecture Notes on Generic Data Structures

HEAPS & PRIORITY QUEUES

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

CSCE 2014 Final Exam Spring Version A

Recitation 9. Prelim Review

CS 307 Final Spring 2009

CSE 2123: Collections: Priority Queues. Jeremy Morris

Lecture Notes on Binary Search Trees

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

Exam Data structures DIT960/DAT036

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

Lecture Notes on Binary Search Trees

CSL 201 Data Structures Mid-Semester Exam minutes

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Lecture 15 Binary Search Trees

Lecture 15 Notes Binary Search Trees

Section 1: True / False (1 point each, 15 pts total)

PROBLEM 1 : (And the winner is...(12 points)) Assume you are considering the implementation of a priority queue that will always give you the smallest

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Course Review. Cpt S 223 Fall 2009

Lecture 17 Notes Priority Queues

Lecture Notes on Binary Search Trees

PRIORITY QUEUES AND HEAPS

Lecture 17 Priority Queues

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Sorting and Searching

Lecture Notes on Generic Data Structures

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

CS301 - Data Structures Glossary By

Programming II (CS300)

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Sorting and Searching

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

Recall: Properties of B-Trees

Programming Abstractions

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

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

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Practice Midterm Exam Solutions

Algorithms and Data Structures

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Summer Final Exam Review Session August 5, 2009

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

Name CPTR246 Spring '17 (100 total points) Exam 3

Second Examination Solution

CSE 214 Computer Science II Searching

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

Lecture 18 Restoring Invariants

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Fall, 2015 Prof. Jungkeun Park

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS171 Final Practice Exam

PRIORITY QUEUES AND HEAPS

Heaps, stacks, queues

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Lecture Notes on Binary Search Trees

Heaps. Heaps. A heap is a complete binary tree.

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Data Structures Question Bank Multiple Choice

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

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

MLR Institute of Technology

Computer Science Foundation Exam

Course Review for Finals. Cpt S 223 Fall 2008

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

UNIT III BALANCED SEARCH TREES AND INDEXING

2) Which data structure is most suitable for search operations? A) Linked List B) Queue C) Hash table D) Sorted array

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Chapter 20: Binary Trees

Exam Data structures DAT036/DAT037/DIT960

The questions will be short answer, similar to the problems you have done on the homework

Binary Heaps in Dynamic Arrays

Lecture Notes on Tries

Priority queues. Priority queues. Priority queue operations

Transcription:

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: The written portion of this weeks homework will give you some practice working with hash tables, heaps and priority queues. 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 7 3 8 4 6 Total: 25 You must include this cover sheet. Either type up the assignment using 15-122-theory5.tex, or print out this PDF.

15-122 Assignment 5 Page 2 of 11 1. Hash Tables using Separate Chaining. Refer to the C 0 code below for is_ht that checks that a given hash table ht is a valid hash table. struct list_node elem data; struct list_node* next; ; typedef struct list_node list; struct ht_header list*[] 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. (4) (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. You may assume the existence of the following client functions as discussed in class: int hash(key k, int m) //@requires m > 0; //@ensures 0 <= \result && \result < m; ; bool key_equal(key k1, key k2); key elem_key(elem e) //@requires e!= NULL; ;

15-122 Assignment 5 Page 3 of 11 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 numnodes = 0; for (int i = 0; i < ; i++) // set p equal to a pointer to first node // of list i in table, if any list* p = ; while ( ) elem e = p->data; if ((e == NULL) (!= i)) return false; numnodes++; if (numnodes > ) return false; p = ; if ( ) return false; return true;

15-122 Assignment 5 Page 4 of 11 2. Collision Resolution. Consider an alternate implementation of hash tables that uses an array only (no chains) and linear probing to resolve collisions. Recall that in linear probing, if a collision occurs, we search through the array linearly (with wraparound if necessary) for the first available position. (2) (a) Assume that we hash a set of integer keys into a hash table of capacity m = 11 using a hash function hash(k) = k modulo 11 using linear probing to resolve collisions. Show where the following sequence of keys are stored in the hash table if they are inserted in the order shown using linear probing to resolve collisions. 54, 67, 23, 88, 39, 98, 72 0 1 2 3 4 5 6 7 8 9 10 ------------------------------------------------------ ------------------------------------------------------ (2) (b) Suppose we have a hash table that resolves collisions using linear probing. Each table cell either holds a pointer to an element or NULL if the table cell is not occupied. Besides the insert and lookup functions, we provide a delete function: if the element is referenced in the table, we replace the pointer to the element to be deleted with NULL. Explain why this implementation fails. Hint: Consider what happens after a number of elements are inserted that all collide, and then one of them is removed from the hash table.

15-122 Assignment 5 Page 5 of 11 (3) (c) Consider an update to the hash table that adds a boolean array to the hash table data structure such that cell i is true if the corresponding table cell is or has been occupied, and false otherwise. When a new hash table is created, all cells in the occupied array are initially false. struct ht_header elem[] table; bool[] occupied; int m; // m = capacity ; typedef struct ht_header* ht; Complete the functions below for ht_insert, ht_lookup and ht_delete for the hash table that uses linear probing to resolve collisions and the occupied array as described above. You may assume there is an appropriate is_ht function already defined, and you may assume the existence of the client functions hash, elem_key, and key_equal as described in problem 1. void ht_insert(ht H, elem e) //@requires is_ht(h); //@requires e!= NULL; //@ensures is_ht(h); key k = elem_key(e); int i = hash(k, H->m); int j = 0; while (j < H->m) return; if ( ) // write missing condition below in solution box H->table[i] = e; H->occupied[i] = true; return; i = (i + 1) % m; j++; // table full, element gets dropped

15-122 Assignment 5 Page 6 of 11 elem ht_lookup(ht H, key k) //@requires is_ht(h); //@ensures \result == NULL key_equal(elem_key(\result), k); int i = hash(k, H->m); int j = 0; while (j < H->m) elem e = H->table[i]; if ( ) return e; i = (i + 1) % m; j++; return NULL; // write missing condition below in solution box void ht_delete(ht H, key k) //@requires is_ht(h); //@ensures is_ht(h); int i = hash(k, H->m); int j = 0; while (j < H->m) elem e = H->table[i]; if ( ) // write missing condition #1 below in solution box return; condition 1: if ( ) H->table[i] = NULL; return; i = (i + 1) % m; j++; // write missing condition #2 below in solution box condition 2:

15-122 Assignment 5 Page 7 of 11 3. Heaps. We represent heaps, conceptually, as trees. For example, take the min-heap below. (1) (a) What is the result of inserting an element with value 29 into the min-heap shown above using the standard algorithm for heap insertion as discussed in class? Draw your answer as a tree, of the same form.

15-122 Assignment 5 Page 8 of 11 (1) (b) Using the original min-heap from the previous page, what is the result of deleting the minimum element from the min-heap using the standard O(log n) algorithm for heap deletion as discussed in class? Draw your answer as a tree, of the same form. (2) (c) Assume a heap is stored in an array as discussed in class where the root is stored at index 1. Using the original min-heap on the previous page, at what index is the element with value 32 stored? At what index is its parent stored? At what indices are its left and right children stored? The value 32 is stored at index. The parent of value 32 is stored at index. The left child of value 32 is stored at index. The right child of value 32 is stored at index. (1) (d) A heap is a binary tree. The root is at level 1, the children of the root are at level 2, etc. For a heap with 10 levels, what is the maximum number of elements it can store?

15-122 Assignment 5 Page 9 of 11 (1) (e) Suppose we have a non-empty min-heap of integers of size n and we wish to find the maximum integer in the heap. Describe precisely where the maximum must be in the min-heap. (You should be able to answer this question with one short sentence.) (1) (f) Using the following C0 definition for a heap of integers (position 0 in the array is not used): struct heap_header int limit; // size of the array of integers int next; // next available array position for an integer int[] value; ; typedef struct heap_header* heap; Write the C0 function find_max that takes a non-empty min-heap and returns the maximum value. Your code should examine only those cells that could possibly hold the maximum. int find_max(heap H) //@requires is_heap(h); //@requires H->next > 1; (1) (g) What is the worst-case runtime complexity in big O notation of your find_max function on a non-empty min-heap of n elements from the previous problem?

15-122 Assignment 5 Page 10 of 11 4. Priority Queues. In a priority queue, each element has a priority value which is represented as an integer. Recall that the lower the integer is, the higher the priority is. When we perform a delmin operation, we remove the element with the highest priority (i.e. lowest int value for priority). (1) (a) If we used an unsorted array to represent the priority queue, what is the worst-case runtime complexity using big O notation for insert and delmin operations on a priority queue with n elements? insert: delmin: (1) (b) If we used a sorted array to represent the priority queue, where the elements are stored from lowest priority to highest priorty, what is the worst-case runtime complexity using big O notation for insert and delmin operations on a priority queue with n elements? insert: delmin: (1) (c) If we used a heap to represent the priority queue, what is the worst-case runtime complexity using big O notation for insert and delmin operations on a priority queue with n elements? insert: delmin: (1) (d) Which implementation above is preferable if the number of insert operations and delmin operations are relatively balanced? Explain in one sentence.

15-122 Assignment 5 Page 11 of 11 (1) (e) Under what specific condition does a priority queue behave like a FIFO queue if it is implemented using a heap? (1) (f) Under what specific condition does a priority queue behave like a LIFO stack if it is implemented using a heap?