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

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

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 Binary Search Trees

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

Lecture Notes on Binary Search Trees

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

Lecture Notes on Binary Search Trees

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

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

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

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

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

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

Lecture 18 Restoring Invariants

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

Lecture Notes on Binary Search Trees

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

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

Lecture 16 Notes AVL Trees

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

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

Lecture 15 Notes Binary Search Trees

Lecture Notes on Sorting

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

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

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

Lecture 15 Binary Search Trees

Lecture Notes on Tries

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

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

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

Lecture Notes on Quicksort

Lecture Notes on Tries

Lecture 16 AVL Trees

Lecture Notes on Priority Queues

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

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

Total Score /1 /20 /41 /15 /23 Grader

Lecture 4 Searching Arrays

Lecture Notes on Linear Search

Lecture 17 Notes Priority Queues

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

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Lecture Notes on Queues

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

Sorting 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

COS 226 Midterm Exam, Spring 2009

Final Examination CSE 100 UCSD (Practice)

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

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

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

Lecture 17 Priority Queues

Lecture Notes on Quicksort

Midterm solutions. n f 3 (n) = 3

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

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Lecture Notes on Memory Layout

CS102 Binary Search Trees

Practice Midterm Exam Solutions

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

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

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

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

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Solution to CSE 250 Final Exam

CS 315 Data Structures mid-term 2

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

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

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Second Examination Solution

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.

Lecture Notes on AVL Trees

Lecture 11 Unbounded Arrays

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

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

CSL 201 Data Structures Mid-Semester Exam minutes

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

ECE250: Algorithms and Data Structures Midterm Review

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

CSE373 Winter 2014, Midterm Examination January 29, 2014

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

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

Lecture Notes on Tries

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Lecture 5 Sorting Arrays

Computational Geometry

CSE 100 Practice Final Exam

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 8 Data Structures

CSE 373 Spring Midterm. Friday April 21st

Transcription:

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. You have 80 minutes to complete the exam. There are 4 problems. Read each problem carefully before attempting to solve it. Do not spend too much time on any one problem. Consider if you might want to skip a problem on a first pass and return to it later. Consider writing out programs or diagrams on scratch paper first. Unbounded Arrays Heaps BSTs Rotations Prob 1 Prob 2 Prob 3 Prob 4 Total Score 30 25 25 20 100 Max 30 25 25 20 100 Grader???????? 1

1. Unbounded Arrays (30 pts) In this problem we reconsider unbounded arrays, as introduced in lecture. Recall the basic idea: we begin by allocating a fixed size array, adding new elements at the end. When the array is full, we create a new array of twice size of the old array, copy the existing elements over, and continue to work with the new array. In our analysis we count the number of write operations to arrays. Task 1 (2 pts). What is a worst-case bound on the number of array writes that have to be performed during a single insert operation, assuming there are already n elements in the unbounded array? Express your answer precisely in terms of n and not in big-o notation. You do not need to justify your answer. n+1 Task 2 (3 pts). What is a worst-case bound on the total number of array writes that have to be performed during a sequence of n insert operations, starting from an empty array. Express your answer precisely in terms of n and not in big-o notation. You do not need to justify your answer; we obtained it in lecture by amortized analysis. 3*n Next we discuss a technique to eliminate the potentially long pauses when we have to double the size of the array. Instead of copying the whole array at once, we copy it element by element. For this to work we need two arrays: a short one and a long one. The elements of the unbounded array are distributed over these two arrays. We only ever add elements to the long array. Whenever we do so, we also copy one element from the short array to the long array. Besides the usual index next that indicates the next insertion point, we also maintain an index jump where the lookup function jumps from the short to the long array. For example, after adding strings "a" through "d" to the empty array, we are in the following situation, where blank array elements are irrelevant. short a b limit/2 long 0 jump c d next limit If we now add "e" we also copy "b" from the short to the long array, adjusting indices as appropriate. In order to simplify boundary conditions, we do not use index 0 of the underlying arrays, but start at index 1. 2

Now to the implementation. All the crucial invariants are expressed in the comments below. You may assume that they are checked in the is_uba function. Read these invariants carefully. struct uba { int limit; /* limit > 0 */ int next; /* 1 <= next && next <= limit */ int jump; /* jump + next == limit-1 */ elem[] short; /* \length(short) = limit/2 */ elem[] long; /* \length(long) = limit */ ; typedef struct uba* uba; bool is_uba(uba L); Task 3 (5 pts). Complete the following function that returns the element at index i. elem uba_get(uba L, int i) //@requires is_uba(l); //@requires 1 <= i && i < L->next; { if (i > L->jump) return L->long[i]; else return L->short[i]; Task 4 (3 pts). Complete the following function to double the size of the unbounded array. [Hint: let yourself be guided by the data structure invariants.] void uba_double(uba L) //@requires is_uba(l); //@ensures is_uba(l); { L->limit = 2*L->limit; L->short = L->long; L->long = alloc_array(elem, L->limit); L->jump = L->next - 1 ; return; 3

Task 5 (12 pts). Complete the following function to add a new element to the end of the array. [Hint: let yourself be guided by the data structure invariants.] void addend(uba L, elem e) //@requires is_uba(l); //@ensures is_uba(l); { if (L->next == L->limit) uba_double(l); L->long[L->next] = e; L->next++; L->long[L->jump] = L->short[L->jump]; L->jump--; Task 6 (2 pts). In the worst case, how many array write operations do we have to perform for a single insertion (function addend)? Again, give your answer directly, not in big-o notation. 2 (uba_double does not write to any array) Task 7 (3 pts). Would this incremental copying technique work for hash tables that dynamically adjust their size? Justify your answer in one or two sentences. No, at least not in a straightforward way. That s because we have to rehash all elements before inserting them into the larger table, so we wouldn t reliably find the elements we haven t moved yet. 4

2. Heaps (25 pts) In this problem we explore the representation of unbounded priority queues as pointer-based trees, instead using arrays. We use the following type: struct tree { int priority; int size; struct tree* left; struct tree* right; ; typedef struct tree* tree; See Task 3 for an explanation of the size field. Task 1 (2 pts). State the ordering invariant for min-heaps, as discussed in lecture. For every node in the tree, its priority is less or equal to the priority of all of its children. Task 2 (8 pts). Write a function heap_ord to check the ordering invariant for heaps, represented as trees. [Hint: Think recursively.] bool heap_ord(tree T) { if (T == NULL) return true; if (T->left!= NULL && T->left->priority < T->priority) return false; if (T->right!= NULL && T->right->priority < T->priority) return false; return heap_ord(t->left) && heap_ord(t->right); 5

In order to keep the heap as balanced as possible, we maintain a size field in each node. It represents the total number of elements in the tree with that node as its root. We have the following balance invariant that replaces the old shape invariant: Heap Balance Invariant: The number of elements in the left and right subtrees at every node differ by at most 1. Assume the following functions: bool heap_bal(tree T); /* check if tree is heap-balanced, O(n) */ tree leaf(int x); /* construct a leaf node with priority x, O(1) */ int heap_size(tree T); /* return size of tree, O(1) */ Task 3 (15 pts). Write a recursive function heap_insert that inserts a new element into the heap, maintaining both order and balance invariants. It should have asymptotic complexity O(log(n)) where n = heap size(t ). [Hint: Keep in mind that heaps are very different from binary search trees.] tree heap_insert(tree T, int x) //@requires heap_ord(t) && heap_bal(t); //@ensures heap_ord(\result) && heap_bal(\result); { if (T == NULL) return leaf(x); if (T->priority > x) { /* swap x with priority at T */ int tmp = T->priority; T->priority = x; x = tmp; //@assert x >= T->priority; if (heap_size(t->left) <= heap_size(t->right)) T->left = heap_insert(t->left, x); else T->right = heap_insert(t->right, x); T->size++; return T; 6

3. Binary Search Trees (25 pts) In this problem we consider plain binary search trees, without rebalancing. Task 1 (3 pts). Consider the tree below. Give a sequence of numbers that, when inserted in the given order into an empty tree, would construct exactly this tree. 3 11 1 7 5 9 11, 3, 1, 7, 5, 9 Task 2 (5 pts). Assume that we have a sorted array from which we want to construct a binary search tree. Which order of insertion should we follow to obtain a tree that is as balanced as possible? To insert a range A[lower..upper) of an array, start with the middle element A[mid] for mid = lower + (upper lower)/2 and then recursively insert the ranges A[lower..mid) and A[mid + 1..upper), in either order. We are now working with the following types. struct tree { elem data; struct tree* left; struct tree* right; ; typedef struct tree* tree; struct bst { tree root; int size; /* size == tree_size(root), see Task 3 */ ; typedef struct bst* bst; 7

Task 3 (5 pts). Write a function tree_size to compute the total number of elements in a tree. int tree_size(tree T) { if (T == NULL) return 0; else return tree_size(t->left)+tree_size(t->right)+1; Task 4 (12 pts). Next you are given some code to transform a binary search tree to a sorted array of elements. It is your job to deduce how it works. Fill in pre- and post-conditions in the tree2array function below. Your annotations should at least be strong enough to verify that no null pointer dereference or out-of-bounds array access is possible. In addition to tree_size you may also use is_sorted and is_ordtree declared below. bool is_sorted(elem[] A, int lower, int upper) /* A[lower..upper) is sorted */ //@requires 0 <= lower && lower <= upper && upper <= \length(a); ; bool is_ordtree(tree T); /* binary search tree ordering */ int tree2array(tree T, elem[] A, int i) //@requires is_ordtree(t); //@requires 0 <= i && i <= \length(a) - tree_size(t); //@ensures \result == i + tree_size(t); //@ensures is_sorted(a, i, \result); { if (T == NULL) return i; int j = tree2array(t->left, A, i); A[j] = T->data; return tree2array(t->right, A, j+1); 8

4. Rotations (20 pts) Task 1 (5 pts). Complete the picture below with the missing tree on the right. (, + ) y right rota1on at y (, + ) x x (y, + ) (, x) y (, x) (x, y) le5 rota1on at x (x, y) (y, + ) Task 2 (10 pts). Give a sequence of single left or right rotations, and where they are performed, to transform the tree on the left to the tree on the right. For full credit, find the shortest sequence (which requires less than 5 steps). Indicate in the rightmost column if the tree after the rotation satisfies the balance invariant of the AVL trees. 11 7 3 sequence of rota2ons? 3 9 1 7 1 5 11 5 9 Step Left or Right? at AVL? 1 left 3 no 2 right 11 yes 3 right 11 yes 4 5 9

Task 3 (5 pts). Is it possible to transform any binary search tree to any other binary search tree that has exactly the same elements? If not, show a counterexample. If yes, briefly explain how. Yes. First, you identify the root of the target tree in the source and move it to the top with a sequence of left and right rotations. Now the left and right subtrees must have exactly the same elements as the left and right subtrees of the target, so we can transform them recursively into the desired shape. The empty tree is transformed to the empty tree by zero rotations. 10