CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

Similar documents
CS 11 C track: lecture 1

CIS 121. Introduction to Trees

Homework 1 Solutions MA 522 Fall 2017

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Data Structures Week #5. Trees (Ağaçlar)

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

CMPT 125 Assignment 2 Solutions

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Lecture 5. Counting Sort / Radix Sort

Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18

Computational Geometry

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

Priority Queues. Binary Heaps

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Abstract Data Types (ADTs) Stacks. The Stack ADT ( 4.2) Stack Interface in Java

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Fundamental Algorithms

Data Structures Week #9. Sorting

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

condition w i B i S maximum u i

Lower Bounds for Sorting

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

COMP Parallel Computing. PRAM (1): The PRAM model and complexity measures

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Analysis of Algorithms

Exercise 6 (Week 42) For the foreign students only.

Solutions to Final COMS W4115 Programming Languages and Translators Monday, May 4, :10-5:25pm, 309 Havemeyer

From last week. Lecture 5. Outline. Principles of programming languages

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

top() Applications of Stacks

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

Chapter 3 Classification of FFT Processor Algorithms

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Examples and Applications of Binary Search

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

6.854J / J Advanced Algorithms Fall 2008

Lecture 9: Exam I Review

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure.

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

FREQUENCY ESTIMATION OF INTERNET PACKET STREAMS WITH LIMITED SPACE: UPPER AND LOWER BOUNDS

How do we evaluate algorithms?

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows:

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Inductive Definition to Recursive Function

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

CS553 Lecture Reuse Optimization: Common SubExpr Elim 3

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Algorithm. Counting Sort Analysis of Algorithms

Python Programming: An Introduction to Computer Science

Data Structures and Algorithms Part 1.4

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Module 8-7: Pascal s Triangle and the Binomial Theorem

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Lecture 5: Recursion. Recursion Overview. Recursion is a powerful technique for specifying funclons, sets, and programs

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

Lecture 1: Introduction and Strassen s Algorithm

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

Data Structures and Algorithms. Analysis of Algorithms

. Written in factored form it is easy to see that the roots are 2, 2, i,

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Reliable Transmission. Spring 2018 CS 438 Staff - University of Illinois 1

Priority Queues and Heaps (Ch 5.5) Huffman Coding Trees (Ch 5.6) Binary Search Trees (Ch 5.4) Lec 5: Binary Tree. Dr. Patrick Chan

Structuring Redundancy for Fault Tolerance. CSE 598D: Fault Tolerant Software

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

High-Order Language APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Abstract Syntax Trees. AST Data Structure. Visitor Interface. Accept methods. Visitor Methodology for AST Traversal CS412/CS413

The Magma Database file formats

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

6.851: Advanced Data Structures Spring Lecture 17 April 24

Polynomial Functions and Models. Learning Objectives. Polynomials. P (x) = a n x n + a n 1 x n a 1 x + a 0, a n 0

CSE 417: Algorithms and Computational Complexity

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Combination Labelings Of Graphs

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Threads and Concurrency in Java: Part 2

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Transcription:

CS11 Fall 003 Prelim Solutios ad Gradig Guide Problem 1: (a) obj = obj1; ILLEGAL because type of referece must always be a supertype of type of object (b) obj3 = obj1; ILLEGAL because type of referece must always be a supertype of type of object (c) obj3 = obj; ILLEGAL because type of referece must always be a supertype of type of object (d) I1 b = obj3; LEGAL because C3 is a subclass of C1 which implemets I1 (e) I c = obj1; ILLEGAL because type of referece must always be a supertype of type of object Gradig (total 5 poits): For each part -1 : wrog coclusio or reaso

Problem (a): abstract class Exp { abstract it eval(); class BiExp exteds Exp { protected char op; protected Exp left; protected Exp right; public BiExp(char op, Exp l, Exp r) { this.op = op; this.left = l; this.right = r; public it eval() { switch(op) { case '+': retur left.eval() + right.eval(); case '*': retur left.eval() * right.eval(); default: System.out.pritl("ERROR: Ukow op"); retur -1; public char get() { retur op; public Exp getleft() { retur left; public Exp getright() { retur right; class NumExp exteds Exp { protected it ; public NumExp(it ) { this. = ; public it get() { retur ; public it eval() { retur ; Gradig (total 10 poits): The solutio for this part would vary widely. But at a miimum, a correct solutio must have all the class defiitios with variable declaratios, costructors ad getter methods. Setter methods are ot required. -7 : o separate class for umbers ad biary operators -4 : icorrect derivatio of classes (e.g. NumExp should ot be a subclass of BiExp) -3 : BiExp class stores itegers -3 : NumExp class stores operators -3 : o costructor for BiExp for directly settig left, right childre -3 : ot eough getter methods

Problem (b): public static it eval(exp root) { if (root==ull) { System.out.pritl("ERROR: Tree ot iitialized"); retur -1; retur root.eval(); Gradig (total 10 poits): This part would greatly deped o the solutio for part (a). At a miimum, it should implemet a recursive method that evaluates the tree passed. - : o error checkig for root == ull -3 : does ot work if root is just a NumExp ode -5 : illegal dowcast if eval() implemeted exterally ad Exp objects ot checked for type before dowcastig -3 : returs wrog result - : has ay sort of parsig code (this problem does ot require parsig expressios)

Problem 3(a):, log,,,! (i icreasig order of asymptotic complexity) Gradig (total 7 poits): - : ot smallest - :! ot largest - : smaller tha - : log smaller tha - : smaller tha, log, or - : wrote i reverse order Problem 3(b): = oe valid witess pair: ( 1,0) TRUE: O( 3 ) FALSE: 3 = O( ) Proof: Assume O( ) 3 =. Therefore there exists a witess pair ( ) 3 c. for all. I other words: 0 c, 0 such that 3 c 0 3 But the limit (as + ) is = +. Therefore, it is ot possible to have a costat upper 3 boud o. This implies our iitial assumptio of the existece of a witess pair was false. 3 = O is also false. Therefore, the statemet ( ) Gradig (total 8 poits): -4 : first statemet cocluded FALSE - : first statemet cocluded TRUE but ivalid witess pair -4 : secod statemet cocluded TRUE - : secod statemet cocluded FALSE but o relevat argumet (iformal good eough)

Problem 3(c): No. Here is a couter example: ( ) g = ( ) O( g( ) ) Let f = ad ( ). We ca easily show that (, 0). Now, f ( ) = = 4 ad, ( ) g = f = usig the witess pair By the same process that we used to show that 3 = O( ) is false, we ca prove that 4 = O( ) f ( ) g ( ) is also false. Therefore, if f ( ) = O( g( ) ) it does ot imply that = O( ). Gradig (total 5 poits): -5 : wrog coclusio (aswered yes istead of o) -3 : if couter example (or other proof) ot valid

Problem 4: [Breadth-first] a) ABDCE b) Not uique. Aother possibility: ADBCE [Depth-first] c) ABCED d) Not uique. Aother possibility: ADECB e) Yes. Graph with oe ode (A) or, (A) (B), or a graph that looks like a "liked list" i geeral, amog may other possibilities. Gradig (total 10 poits): poits for each part: a) - if wrog sequece b) - if aswered uique -1 if aswered ot uique but gave wrog sequece c) - if wrog sequece d) - if aswered uique -1 if aswered ot uique but gave wrog sequece e) - if aswered o -1 if aswered yes but gave wrog example

Problem 5: public static boolea Valid(Strig s) { if (s==ull) retur false; retur Valid(s,0,s.legth()-1); public static boolea Valid(Strig s, it low, it high) { if (low > high) retur true; if (low == high) retur false; else retur(s.charat(low) == '(') && (s.charat(high) == ')') && (Valid(s,low+1,high-1)); Gradig (total 15 poits): - : fuctio does ot retur Boolea - : fails if s is ull -5 : does ot work for empty strig -3 : extremely iefficiet (e.g. scas strig from begiig i each iteratio) -7 : does ot work for strigs of odd legth (i.e. either crashes or returs true) - : icorrect use of s.charat(i) -10 : o recursio -3 : bad algorithm -7 : allows ivalid strig -1 : returs true if iput is ull

Problem 6: class Hashley implemets SearchStructure { protected ListCell[] spie; protected it size; private fial it buckets = 10; public Hashley() { spie = ew ListCell[buckets]; for (it i=buckets; i<buckets; i++) spie[i] = ull; public void isert(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell l = ew ListCell(o,spie[idex]); spie[idex] = l; ++size; retur; public void delete(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell curr = spie[idex]; ListCell prev = ull; while (curr!= ull && ((Comparable) curr.getdatum()).compareto(o)!= 0) { prev = curr; curr = curr.getnext(); if (curr == ull) retur; if (prev == ull) spie[idex] = curr.getnext(); else prev.setnext(curr.getnext()); --size; retur; public boolea search(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell curr = spie[idex]; while (curr!= ull) { if (((Comparable) curr.getdatum()).compareto(o) == 0) retur true; curr = curr.getnext(); retur false; public it size() { retur size;

Gradig (total 30 poits): -3 : class header does ot have implemets SearchStructure -5 : spie is ot declared as a array -5 : spie array ot allocated (o ew) before first use -3 : object ot type-casted to Iteger before callig itvalue() - : isert() does ot icremet size -5 : deletio of first ode i a list fails -5 : deletio of itermediate odes fail - : delete() does ot decremet size -3 : objects ot compared correctly -5 : iefficiet search if all lists are traversed to look for a object -3 : tries to call methods o a ull poiter (o checkig i while loops etc.) -3 : does ot keep a size variable - : each idex i spie iitialized to poit to empty ListCell s -5 : fuctio headers do t match iterface