SOME TYPICAL QUESTIONS, THEIR EXPECTED ANSWERS AND THINGS

Similar documents
Lecture Notes on Linear Search

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

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

Lecture 4 Searching Arrays

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

Lecture Notes on Sorting

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

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

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

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

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

Lecture 18 Restoring Invariants

Lecture 5 Sorting Arrays

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

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

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

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

Lecture 11 Unbounded Arrays

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

Lecture 6 Binary Search

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

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

Lecture 9 Stacks & Queues

Faculty of Science FINAL EXAMINATION

Data Structures and Algorithms CSE 465

Lecture 8 Data Structures

Lecture Notes on Quicksort

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Hardware versus software

Lecture Notes on Quicksort

Lecture 15 Notes Binary Search Trees

Lecture 15 Binary Search Trees

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

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

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Preliminary Examination I Computer Science 312, Cornell University 6 March 2003

Hoare Logic: Proving Programs Correct

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

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.

Some Extra Information on Graph Search

Lecture Notes on Binary Search Trees

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Introduction to Data Structure

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

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Binary Search to find item in sorted array

CS 161 Computer Security

Chapter 2: Complexity Analysis

Outline and Reading. Analysis of Algorithms 1

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

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

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

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

UNIVERSITY REGULATIONS

Lecture 2: Algorithm Analysis

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

CIS 341 Final Examination 3 May 2011

Lecture Notes on Loop Optimizations

Lecture 10 Notes Linked Lists

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Lecture 10 Notes Linked Lists

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

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

CS126 Final Exam Review

CPSC 320 Midterm #1. February 4, 2015

ASYMPTOTIC COMPLEXITY

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

CSE 331 Introduction to Algorithm Analysis and Design. Sample Mid-term Exam-I: Fall 2018

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Lecture Notes on Arrays

Stacks and queues (chapters 6.6, 15.1, 15.5)

CSL 201 Data Structures Mid-Semester Exam minutes

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

Review: Hoare Logic Rules

INTRODUCTION TO ALGORITHMS

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

CS 4349 Lecture August 21st, 2017

Data Structures and Algorithms. Part 2

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Induction and Semantics in Dafny

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

EECS Sample Midterm Exam

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

CISC 320 Midterm Exam

Transcription:

SOME TYPICAL QUESTIONS, THEIR EXPECTED ANSWERS AND THINGS TO WATCH OUT FOR ON THE TEST Nivedita Chopra 1: Contracts (The following question is Question 1 on Written Homework 2) Consider the following implementation of the linear search algorithm that finds the first occurrence of x in array A: int find(int x, int[] A, int n) //@requires 0 <= n && n <= \length(a); //@requires is_sorted(a, 0, n); //answer to 1.b goes here int i = 0; while (i < n && A[i] <= x) //answer to 1.a goes here if (A[i] == x) return i; i = i + 1; return -1; The function is sorted has the following signature: bool is_sorted(int[] A, int lower, int upper) //@requires 0 <= lower && lower <= \length(a) - 1; //@requires 0 <= upper && upper <= \length(a); //@requires lower <= upper; ; and returns true if the array A is sorted in increasing order from [lower, upper). You may also use the function is in in any of the following questions. Its signature is: bool is_in(int[] A, int x, int n) //@requires 0 <= n && n <= \length(a); ; and it returns true if A[i] == x for some i in [0, n). 1

Add loop invariants to the while loop in the code and show that they hold for this loop. Be sure that the loop invariants precisely describe the computation in the loop. What we were looking for is: //@loop_invariant 0 <= i && i <= n; //@loop_invariant!is_in(a, x, i); OR //@loop_invariant (i == 0) (A[i-1] < x); It s important to get these correct at this stage because you have to prove that your loop invariant implies the postcondition and if your loop invariant is incorrect or not strong, that part will go wrong Add one or more ensures clause(s) to describe the intended postcondition in a precise manner. (0 <= result && result < n && A[\result] == x &&!is_in(a, x, \result)); @*/ OR (0 <= result && result < n && A[\result] == x && ((\result == 0) (A[\result - 1] < x))); @*/ Both these forms are equivalent. Take a moment to reason why the are equivalent. Even though you re expected to write just one if these, it s crucial to know other ways to say the same thing. The common mistake here was missing out the fact that you were asked to return the first occurrence. Remember to read the question carefully! Also, whenever you have cases in the postcondition, remember to be as specific as possible. A good way to think about it is independently generate the strongest postcondition for each of the possible cases and then combine them together using the operator. (WARNING: While using the operator, remember to take advantage of short-circuiting to prevent errors like array access out of bounds. After writing your post-condition, try it out with a sample output each of both cases, to ensure that it s saying what you intend it to) 2

Show that the loop invariant is strong enough by using the loop invariant to prove that the postconditions hold when the function returns. You do not need to prove the loop invariant s correctness (you already did that in 1.a); that is, you assume it is correct for this problem. Make sure to deal with the fact that find can return in two different ways. Consider the loop invariant //@loop_invariant 0 <= i && i <= n; //@loop_invariant!is_in(a, x, i); and the postcondition (0 <= \result && \result < n && A[\result] == x &&!is_in(a, x, \result)); @*/ So we ll have two cases here: When we return i from within the loop: Then, by the loop invariant, we have 0 <= \result && \result <= n and!is_in(a, x, \result) Also, from the loop body, we know that we return when A[i] == x, so A[\result] == x Thus the second clause in the postcondition, (0 <= \result && \result < n && A[\result] == x &&!is_in(a, x, \result)) holds true and since the clauses in the postcondition are connected by an OR, the postcondition holds true When we return 1 after the loop: Now, we know that the loop condition is false but the invariant is true So what we know is (i >= n) ((i < n) && A[i] > x) In either case, since the array is sorted and we know that!is_in(a, x, i), we can conclude that!is_in(a, x, n) (I m not justifying this here, but it would be a good idea to) The important part here is proving the entire postcondition true The common mistake originated from the earlier parts. If your loop-invariant and postcondition are not strong enough, your proof won t be what we re looking for 3

2: Big O Analysis You are given the following expression - f(n) = 3n 2 + 4n + log n + 4 Give a tight Big O bound on the asymptotic complexity of this expression f(n) O(n 2 ) Using your answer from the previous part, prove that f(n) O(g(n)) using the formal definition of Big O You should know the formal definition of Big O. it s really important and we may not give it to you in the exam. If you have trouble memorizing it, be sure to put it on your cheat sheet! Anyhow, here s the definition If f(n) O(g(n)) then, constants c > 0 and n 0 0 such that n n 0, f(n) cg(n) So, what is this question asking you to do? Basically you need to find suitable values of c and n 0 for which f(n) c(n 2 ), whenever n n 0 You need to come up with this c and n 0 by observation (or any means that you may like) All that is expected is that you state your values for c and n 0 and then show that they hold Here, lets take c = 8 and n 0 = 2 So we must show that f(n) 8(n 2 ), n 2 Base Case: n = 2 f(2) = 3 4 + 4 2 + log 2 + 4 = 25 < 8 4 Induction Step: Assume that f(k) < 8k 2, for some k 2 To show f(k + 1): f(k + 1) = 3 (k + 1) 2 + 4(k + 1) + log(k + 1) + 4 = f(k + 1) = 3k 2 + 6k + 3 + 4k + 4 + log(k + 1) + 4 = f(k + 1) = (3k 2 + 4k + log(k + 1) + 4) + 6k + 7 log(k + 1) <= 2 log(k) = f(k + 1) (3k 2 + 4k + log(k) + 4) + 6k + 7 + log(k) = f(k + 1) 8k 2 + 6k + 7 + k Add (9k + 1) on the RHS, since k > 2 = f(k + 1) 8k 2 + 16k + 8 = f(k + 1) 8(k + 1) 2 4

3: Queues and Stacks Complete the following code for a function that takes in a queue and returns a reversed queue. Assume that you have all the functions in the interface of stacks and queues. queue reverse(queue Q) //@requires ; //@ensures ; ; (HINT: allocate a temporary data structure) while ( ) ; (HINT: create a new queue) while ( ) ; (HINT: look at the function prototype) The basic idea here is to store the elements on the stack and then add them on to a new queue. Then we return the newly made queue. Remember that in such cases, you are usually expected to create a new queue and not modify the original queue. The contracts involved are checking that the data structures are valid (is stack(s) and is queue(q)). This is important, especially in the loop-invariants as it is completely possible that certain statements within the loop body modify the data structure in a way that it is no longer valid. 5