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