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

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

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

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CMPT 125 Assignment 2 Solutions

Big-O Analysis. Asymptotics

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

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

Analysis of Algorithms

Big-O Analysis. Asymptotics

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

Homework 1 Solutions MA 522 Fall 2017

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

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

Analysis of Algorithms

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

Lecture 5. Counting Sort / Radix Sort

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

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

Lecture 1: Introduction and Strassen s Algorithm

Data Structures and Algorithms. Analysis of Algorithms

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

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

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

CSE 417: Algorithms and Computational Complexity

How do we evaluate algorithms?

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

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

1.2 Binomial Coefficients and Subsets

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

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

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

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

top() Applications of Stacks

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

CS473-Algorithms I. Lecture 2. Asymptotic Notation. CS 473 Lecture 2 1

Analysis of Algorithms

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

The isoperimetric problem on the hypercube

Elementary Educational Computer

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

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

Lower Bounds for Sorting

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

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

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

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

CIS 121. Introduction to Trees

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

Mathematical Stat I: solutions of homework 1

Computational Geometry

CS 111: Program Design I Lecture 16: Module Review, Encodings, Lists

EVALUATION OF TRIGONOMETRIC FUNCTIONS

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

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

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Python Programming: An Introduction to Computer Science

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

Data Structures Week #9. Sorting

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

6.854J / J Advanced Algorithms Fall 2008

Our Learning Problem, Again

Examples and Applications of Binary Search

Ch 9.3 Geometric Sequences and Series Lessons

Math Section 2.2 Polynomial Functions

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

Alpha Individual Solutions MAΘ National Convention 2013

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

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

CS 683: Advanced Design and Analysis of Algorithms

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CS 11 C track: lecture 1

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

K-NET bus. When several turrets are connected to the K-Bus, the structure of the system is as showns

Ones Assignment Method for Solving Traveling Salesman Problem

Python Programming: An Introduction to Computer Science

ENGI 4421 Probability and Statistics Faculty of Engineering and Applied Science Problem Set 1 Descriptive Statistics

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

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

Design and Analysis of Algorithms Notes

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

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

CSE 111 Bio: Program Design I Class 11: loops

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

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

15 UNSUPERVISED LEARNING

Algorithm. Counting Sort Analysis of Algorithms

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

Guide to Applying Online

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

condition w i B i S maximum u i

Lecture 18. Optimization in n dimensions

Octahedral Graph Scaling

3. b. Present a combinatorial argument that for all positive integers n : : 2 n

Algorithm Design Techniques. Divide and conquer Problem

IMP: Superposer Integrated Morphometrics Package Superposition Tool

Descriptive Statistics Summary Lists

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

1 Graph Sparsfication

Transcription:

CS 604 Data Structures Midterm Sprig, 00 VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Istructios: Prit your ame i the space provided below. This examiatio is closed book ad closed otes. No calculators or other computig devices may be used. Aswer each questio i the space provided. If you eed to cotiue a aswer oto the back of a page, clearly idicate that ad label the cotiuatio with the questio umber. If you wat partial credit, justify your aswers, eve whe justificatio is ot explicitly required. There are 6 questios, priced as marked. The maximum score is 100. Whe you have completed the test, sig the pledge at the bottom of this page ad tur i the test. Note that either failig to retur this test or discussig its cotet with a studet who has ot take it is a violatio of the Hoor Code. Do ot start the test util istructed to do so! Name Solutio prited Pledge: O my hoor, I have either give or received uauthorized aid o this examiatio. siged Page 1 of 8

CS 604 Data Structures Midterm Sprig, 00 1. [0 poits] Circle TRUE or FALSE accordig to whether the statemet is true or false: a) 1000 7 + is O( ) TRUE FALSE Directly from a theorem i the otes, 1000 7 + ^ is O(^) sice that's the domiat term. But big-o is just a upper boud, ad clearly ^ is O(^). Or, usig its we ca show it this way: 1000 7 1000 7 0 b) 5 + + is ( (log ) ) TRUE FALSE From the same theorem, 5^ + + is O(^) ad we should suspect it's Theta(^). That's easily verified by takig the it of (5^ + + )/(^) as goes to ifiity ad seeig it's 5. But how does that help, sice this questio is about Omega? Well, rememberig that Omega is about lower bouds, ad rememberig the hierarchy of complexity classes from the otes, is Omega(log ) ad so ^ is Omega( (log )^ ). Alteratively, use the it theorem ad l'hopital's Rule several times: log 0 (l10) 5 10 (log ) /( l10) 10 l10 0 log l10 / l10 c) + 100( log ) is ( ) TRUE FALSE First you eed to expad this as ^ + 100 ^ (log )^. Clearly the secod term is domiatig the first sice ^ domiates ^. So our theorem would say this is O(^ (log )^ ), which is Omega(^), ad so this caot be Theta(^). Or, you ca use its agai: 100 log 100log d) 17( + ) is ( ) TRUE FALSE Multiplyig it out we have 17 ^ + 17^. Now from our list of complexity classes, ^ will domiate ay power of ad so this is O( ^) ad that is clearly Omega(^). Or, oce agai, usig its: Page of 8

CS 604 Data Structures Midterm Sprig, 00 17 17 17 17 Now the first term goes to zero (usig l'hopital's Rule) ad the secod goes to ifiity, so this it is ifiite. e) Σ i log is ( ) TRUE FALSE i=1 You really eed to work out the summatio to be sure of this oe. As far as the summatio itself goes, the is a costat so you ca write it as (log ) * Sum(i) ad the apply the summatio formula: log i1 i log 1 Now this is goig to have the domiat term ^4 (log ) which is clearly Omega(^). This ca also be show, tediously, usig its. Page of 8

CS 604 Data Structures Midterm Sprig, 00. [0 poits] The followig algorithm prits a iteger as a fuctio of a iput iteger, assumed to be positive. read ; x = 7 * ; y = x + 5 * ; while (x + y > 0) { x = x 6; y = y + ; prit x * y; // 1 operatio // operatios // operatios // operatios for loop test // operatios // operatios // operatios We charge for operatios at a rate of oe arbitrary time uit for each assigmet, arithmetic operatio, compariso, iteger iput, ad iteger output. Assume that is a positive iteger. Compute the time complexity fuctio T() for the above algorithm. Also, determie the asymptotic (big-θ) of T() as a simple fuctio of. The oly tricky thig here is the umber of times the loop body is executed. Give the secod ad third statemets, iitially x + y equals 19. The cumulative effect of the two statemets iside the loop body is to decrease x + y by o each pass. So, x + y will have the values 19, 19, 19 6, etc. Roughly speakig, the loop body will execute 19/ times. If 19 is a multiple of, that will be exact. Otherwise, the correct value would be 19/ + 1 (assumig a iteger divide is used). Fortuately there's a easy way to express this precisely the ceilig fuctio. If you just add up the costs idicated above you get: T() = 1 + + + ceilig(19/)*6 + = 8 + 6*ceilig(19/) If you assume 19/ is a iteger the you have T() = 8 + 8. The aalysis above igores the "extra" executio of the loop test that causes the loop to termiate. Coutig that as well just adds operatios. Obviously, either way you aalyze it, this is Theta(). Page 4 of 8

CS 604 Data Structures Midterm Sprig, 00. [15 poits] Cosider the LikedList ad NodeT template iterfaces give below: template <class Data> class LikedList { private: NodeT<Data>* Head; NodeT<Data>* Tail; public: bool Isert(cost Data&); bool Apped(cost Data&); bool Delete(cost Data&); LikedList SubList(Data&); Data* Fid(cost Data&); ~LikedList(); ; template <class Data> class NodeT { public: Data Elemet; NodeT<Data>* Next; ; NodeT(Data Elem = Data(), NodeT<Data>* N = NULL); Write the body of the public fuctio Fid(), which returs a poiter to the first data elemet i the list that matchig its parameter or NULL if there is o such ode. Your implemetatio must coform to the iterface give below. You may assume that the implemetatio of Data provides overloads for ay relatioal operators you eed. template <class Data> Data* LikedList<Data>::Fid(cost Data& Target) { NodeT<Data> *Curr = Head; while ( Curr!= NULL ) { // slap a temp poiter o first ode // quit whe walk off ed of list if ( Target == Curr->Elemet ) // check for match retur ( &(Curr->Elemet) ); // retur address of data elemet Curr = Curr->Next; // step to ext ode, if ay retur NULL; // o match, so retur NULL as spec'd Notes: Nothig above says that the list is sorted. Nothig implies that the head ad/or tail odes are dummy odes (i.e., ot storig data). You MUST hadle the case where the list is empty, i which case Head would be NULL. You MUST NOT violate the ecapsulatio of the list by returig a poiter to a NODE rather tha a poiter to a data elemet. Of course, the specified retur type for Fid() should have made that moot; ufortuately it did ot. Page 5 of 8

CS 604 Data Structures Midterm Sprig, 00 4. [15 poits] Cosider the LikedList ad NodeT template iterfaces give below (same as problem ): template <class Data> class LikedList { private: NodeT<Data>* Head; NodeT<Data>* Tail; public: bool Isert(cost Data&); bool Apped(cost Data&); bool Delete(cost Data&); LikedList SubList(Data&); Data* Fid(cost Data&); ~LikedList(); ; template <class Data> class NodeT { public: Data Elemet; NodeT<Data>* Next; ; NodeT(Data Elem = Data(), NodeT<Data>* N = NULL); Write the body of the public fuctio SubList(), which returs a ew list that is a copy of the sublist of the origial list startig at the first ode holdig data matchig the give parameter ad goig to the ed of the origial list. The origial list is ot modified. template <class Data> LikedList LikedList<Data>::SubList(cost Data& Target) { NodeT<Data> *Curr = Head; // slap a temp poiter o head ode LikList<Data> slist; // create empty list while ( (Curr!= NULL ) && // stop if walk off ed of list (Curr->Elemet!= Target) ) { // OR if match is foud Curr = Curr->Next; // step to ext ode, if ay while ( Curr!= NULL ) { slist.apped(curr->elemet); Curr = Curr->Next; retur slist; // stop if walk off ed of list // apped ode holdig curret data // elemet to the sublist // step to ext ode, if ay // retur the sublist you built Notes: Agai, you must deal with the possibility the list is empty. You must also deal with the possibility that the Target is ot foud, i which case you retur a EMPTY LIST, ot NULL. You caot call Fid() to do the search ad set Curr because Fid() returs a poiter to the DATA elemet, NOT to the NODE. You could call Fid() to determie if Target is preset i the list, ad abort if it's ot; however, that is iefficiet sice you'd just have to redo the search if Target was preset. We did't exactly say what the differece was betwee Isert() ad Apped(), but you should have chose Apped() to add elemets to the sublist simply o the basis of its ame. You should NOT be addig odes to the sublist maually; i.e., NOT usig the LikedList iterface to do it. Page 6 of 8

CS 604 Data Structures Midterm Sprig, 00 5. Assume that a iteger requires 4 bytes ad a poiter requires 4 bytes. Assume that Data (e.g., problem ) requires D bytes. We wish to implemet a list of N records of Data. a. [5 poits] How much space would the liked list of problem require? Each NodeT<Data> object would require 4 bytes for a poiter ad D bytes for the data elemet, ad there would be N such ode objects. AND, the liked list itself stores two more poiters so the total space cost would be: S(N) = N(D + 4) + 8. b. [5 poits] How much space would a oe-dimesioal array implemetatio require? Each array cell would require D bytes, ad there would be N cells. ALSO, the array ame is really a poiter to the data (eve if it's declared statically), ad so that would require a additioal 4 bytes: S(N) = ND + 4. c. [10 poits] Assume N = 6. Estimate how much space a (radomized) skip list would require. For simplicity I'm assumig that there is o dummy tail ode. Allowig for oe would oly chage the results slightly. Each skip list ode, icludig the head ode, would have space for oe data elemet (D bytes), a it to hold the ode level umber (4 bytes) ad a poiter for the Forward array (4 bytes). So that gives us a estimate of: S(N) = 64*(D + 8) + space for Forward arrays How much space will the Forward arrays require? Recall that a skip list would expect to have 1/ of its odes i level 0, 1/4 i level 1, 1/8 i level, ad so forth. Applyig that here, with 6 data odes, we'd expect to have the followig data odes: Level 0 1 4 5 # Nodes 16 8 4 1 size of Forward[] 4 8 1 16 0 4 The head ode would also have to be i level 5. If we add all this up, we get a estimate of: S(N) = 64*(D + 8) + *4 + 16*8 + 8*1 + 4*16 + *0 + *4 = 64D + 1016 Page 7 of 8

CS 604 Data Structures Midterm Sprig, 00 6. [10 poits] Suppose we kow that R is a trasitive relatio ad that the followig ordered pairs are i R: (C,A), (D,C), (A,C), (E,A), (E,D) Which of the followig ordered pairs ca we prove are also i R? (Circle all that apply.) a. (A,A) (A, C) ad (C, A), so trasitivity implies (A, A) b. (A,E) c. (D,E) d. (C,C) (C, A) ad (A, C), so trasitivity implies (C, C) e. (E,C) (E, A) ad (A, C), so trasitivity implies (E, C) f. (C,E) g. (D,D) h. (E,E) i. (A,D) j. (E,B) Noe of the others ca be established usig what is give. If you had symmetry you could show some of the others; but you do't. Page 8 of 8