COSC 2011 - 2004F - Exam

COSC 2011 - 2004F - Exam - g UNIVERSITE _ EUNIVERSITY...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: g UNIVERSITE _ EUNIVERSITY Department of Computer Science and Engineering case 2011: Fundamentals of Data Structures (Fall 2004) Instructors: G. Turpin and N. Vlajic Final Examination December 16, 2004 ' Instructions: may be used. There are 8 questions. The points for each question are given in square brackets. next to the question title. The overall maximum score is 100. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearty indicate that and label the continuation with the question number. Question Points FIRSTNAME: BEN 1 ’ I10 2 1,; re LASTNAME: DOVER 3 M H4 4' ' r10 STUDENTitI: 314159265 5 ,( us I 5 9, I15 SECTION (circleone): 7 lo [20 8 I10 Total 1. True/False Choice [10 points] Indicate whether each of the following statements is true or false. No marks will be deducted for QR.“ incorrect answers. The space requirement of a vector-based implementation of an ill-balanced binary tree 2 Insertion in an AVL tree is "commutative". That is. inserting x and then y into an AVL trees leaves the same tree as inserting y and then _x. 6mm ow. L i .4 . The fifth smallest element of a min—heap stored in a complete binary tree will have a depth of four or less. 53-i— ' - nary search tree with n elements can be converted into a heap in O(n) time. The worst case running time of 'insert’ operation in _a_hash table with chaining can be as low as 0(1). n addressin works well even keys than can be stored directly in the hash table. QKDSECK add r easing. Let G=(V,E) be a weighted graph and let M be a UPS spanning tree of G. The path in M between' any pair of vertices v1 and v2 must be their shortest path in G. A graph algorithm with 8(m-n) running time is asymptotically better than an algorithm with 9(n-n) running time for a ‘free tree‘ graph with n vefiic nd ed es. {rice idea: conned-ed {mega he aides (I? [1 point] [1 point] [1 point] [1 point] [1 point] \\d—B\\ f... 2. Algorithm Running Time [6 points] (a) [2 points] What is the execution running time of add 8 n) (see the code below). as a function of n (n 2 0). (Express the running time using big—O notation, you do not need to provide an exact analysis. int add(a,b) . i if (a==0) return b; ff a 5" 0 else if§a<0) return add(a+1, b-l); {g 0 Q We!" ) else return may} .emw “0+ dependent on lo or (AA) ( sea trauma MA tempo g (vi-2r R MW '5 (we? - "L- “*U aedlh n+9) Md L0, 1w 3’) tux gamercfl is 003) bT‘ ) {1 0(2). 0003"”? massc/m. n20 41”! / 70 (b) [2 points] f '1 I +£«ea1 ou can OCR) Consider the function add from Question 2.a again. and xpress the execution runni 9 time of “Mn add(n,8). cult- £st tosptugeu O C“) '4]wa Ismwct Aloe Proflrovw runs WK @ 1* awe?” owl n=o._ v; _______________. (c) [2 points] . What is the running time of the following algorithm. as a function of n. (Express the running time using big-e notation; you do not need to provide an exact analysis.) Assume n 2 1. int i=1; int sum=0; while (iinj) { for (int_.'L=_n2;_J'_>__1i j=j/2){ [0 if; he, "' sum++; "'— Z/o n } 3 ’ - i += 2; f } ______ W9(2’°7M) a W?” a QM MADAME MOOM ’3' - In»; a I 3. Implementation [14 points] (a) [7 POMS] Palindromes are words or phrases that appear the same whether read backwards or forwards, e.g. “eye”, “madam”, etc. Complete the Boolean method, isPalindromeQS(Queue q). which takes a queue of Characters as its input variable, and returns true if the text contained in the given queue is a palindrome. The method uses only one additional data structure. namely a Stack, to accomplish its main function. 37 M 3 g boolean isPalindromeqsiQueue q) { K M“ Stack s = new Stack“; JQUZ ;E L' Tob'l C15 $3: Lien sgiuejser, ‘3 Lt} 3++33 3, Push L q.&eqoeuc03} 3 4% is; (catcle 3 q, deaoeue CY: 444115 Va viable is. (same! I‘UL “WM am Swab. mommy L we (and acid .‘i—dbslqclc _ using peelc.g!q'll Tl—laesqu '3=°) Hung. move see/2 some QJLLB “L - (a! v i-FQL 5 POQQ) equals (at de‘IUMCBBBi C. . F . {mire 2" fé—U—‘(n Faisal fig?» % discard MM:- ‘ F TDD? “Orr (b) [7 points] Fill in the blanks below in the resursive countDlvisible method. countDivfsiQLe takes three Qosifive integers, a, b. and Q. and returns how many integers between a and b (inclusive) are divisible by c. For example, countDivisible(9, 18, 5) returns 2 since 10,15 are divisible by 5. (Hint: x % y = 0 if x is divisible by y.) at 1‘5 5- ilDilKB public int countDivisible(int a, int b, int c) { if ((oL==Lb+0))/¥;b Ma 4 i 6 yet “LEE return 0; COM/ago! ‘K/ "9 (.01 l 1'5 i1?) /xmmé Im— kgn mag” 43 W q‘é H if(C8 “(ch—_—=0 ) (aw/01g =lé° l “1 Ga Ctozlq’ig) return (mun-tblvfsmh LQHJ‘DN‘AT: to qé ‘q fl): by 4W mméer IV/ [o°/°§:=O else . ,- —)(u,i%,§‘)+l return cauntb'wlsiun GEMINI?) I\/ ll 75 I"? H m lT—“fo } _) a! / - Assuw'vkj 1W“? -' < {#:é')‘ CI 1% S M —9 lo Ml: mam {for Cmgczs',‘ L filo} {H33 lQQiWoc—B ==o> {5'5" emit) {q COUwJ‘J'H 77% 4. AVL Tree [10 points] The following is an AVL tree with node labels A - N used to identify the nodes (the node labels have noting to do with ordering/searching the tree). Redraw this AVL tree as .it would look like if the node with label D were deleted. {blower : BEHAMIM Filo K011, /’ 5. Heaps- [15 points] The vector show below represents the underlying array of a min—heap. O l 3 9 Inflam- . l Suppose the following operations are performed (in this order!) on the-given heap: f W): RemoveMinO: |nsert(38); Insert(15). 233 K‘Ffi\ Show the heap (Le. underlying array) as it would look after each operation! //Z%\K_/g/J+ \ Space for rough work is provided on the next page. - W RemoveMinO RemoveMinO @ 0 1 2 3 4 5 6 7 8 9 In. 3d ’32 3C lnsert(38) o 1 2 3 4 5 ' 6 * 7 I 3’2. '9‘? “3% 8 9 tnsert(15) 0 l 2 3 4 5 6 'i 8 9 .- 31 3C 3% '57” Hid/38 see, back if PLO/(Cd!) 1’) 7,? 3% 31 C(14- 36: 8‘? 3? 1—4" G) lq’ n@\ / \ p/ 73 3'4 37' 3H / \ / \ fig. \ / \ 92. cm- 36 S“?— qg’ 3" 3"“ /\/\/\/\ .4 31 33’ © / \ 6. Hash Table [15 points] (a) [9 points] Consider a map implemented using a hash table with 7 buckets. Quadratic probing and a hash fu ' = k % 7 Show the table as it would look after each of the following put insert statements in order. a put(7, "A") . ":1 1*‘u3 _ Paw D ) a+q_6 __é:3 Wham") '55? “’0 3i :2 Q. 3+q=“/ 17% Z. 0 (D) [6 points] ' Assume that you have a hash table with n slots (indexed from 0 to (n—1)). For each of the following functions h(k), where k is a key. answer two questions: a Is the function acceptable as a hash function. i.e. can it be used to index into the array? - o If so, is it a good hash function? Why of why not? Z Wigwam/for (b.1) h(k)=|</n, wherekandnare egers More dxiioktj Mendel: ¥Mtkl‘b\’\. \K- can swag be used +0 fwdex {Vt-b Wanaffata but pig: at: slink cam be pow—watt; WM 53* ‘0 "‘- SW “2000- we Nils. NraiaLUQS évosek— bclow 2,000 glint g If +00 Mask-co $401 b.2 =1 ( i ( J kaess we are Loo.ch +0 great: mess, waver Wis. it's not mug r but» one cs toeHcr offf 05m an anaj ti 61" (Unwed (Mimi: fiatde @me Jag-‘05 [flag‘fl Wm:- HflWé‘A M65. Kegardlw 0F W_K is! au gimme wfl‘ 10¢ bj deg—0E,» aim—1% i. Imagine Jim rest.— «m etements Cn2mu3§m+ $19. (b.3) h(k) = (k + Ran om (n)) mod n _ Assume that the function Random(n) returns a random integer between 0 and (n—1) Incluswe. Maflbe. This (would resemb’b {05F linear Pm 5C CLOSE”. (23" ‘YQMW} PlaCC/fim FCJH'ULj would be defiant because Metal um W MM ' aw: VIM “bebokfifg l-i search 4441, ham 4am; “than. gLe 1mm apt—(m 7. Graph [20 points] Assume a graph G=(V,E) with: Ix container of all vertices V={1,2,3,4,5.6.7.8}, Adiaeent‘o‘ertieeeffiafasL jg. Adjacenwefiticesiiflafl. K WK ' 4}. MEWS) X Adiaeenwemcesfinfia}, KNOTE, here AdjacentVertices[k] represents the list of vertices adjacent to vertex[k]. Answer the following: (a) [4 points] Write down the adjacency matrix correSponding to G. Merelm fiWIOI'Zji-lftqg essoMtt/Lj 4—Wa- c: ' Malaya b33111 a I. a MA- Courtech l—bt‘ ’ - - ‘ lat . [Ni O. 4—hol5‘4 3 L n 1 —-) -H»u_s Pale its nob necessavg. 5' g ‘ led 9“ W”) ‘- a (; lvalr betrcuc + 4 :2 WW 6/? rl's easier ’59 ‘ Ska-O (IQ-l'efle( 4W“ S'bll'FfiiS all UMUQL. TL“; chanjeg .51; fig .6 (b) [5 points] Draw a representation of G using circles for vertices (with vertex number in the circle) and arrows for edges. ls G directed? Circle the right answer. Is G connected? Circle the right answer. “mu/c9 connede +o e,- no Cb“ M search algorithm DFS(G. v) ( answer these questions. (c) [3 points] Find the number of connected components in a graph G- ’1 I " - / Simpg SUb’n (J.th are. marked W‘s-fired gm bar of? tier/412255. m +ka+ are. ' (d) [3 points] Determine whether the connected graph G has any cycles. 4°01 {3r 0L cigar— 5w» gave 93: a. po- CB‘FS) +rauerse or go WW —iF we. FNM' ’WQ SW-{PA Qk— am —— note: 0W5 “Have an'I all (613% have: keen ' - not Time? Vrgrkd/ L (No.96 War/G’— £d¢h V6 (dis. covered ’and arm/9W [i=5 9, Seeg‘f'r'ég (4 WV 9&- Besstea would be 1% 0/5" «new be; hovered) dzxmvcy / uy'red/ «(1-) +5 see "-F 60300 n3dbnc1¢fbakaififr b )0 8. Data Structures Applied ' [‘l 0 points] Consider the following problem: Given N elements in a data structure (N is odd, and all elements are distinct). find the middle element in sorted order. (Note. the elements are not necessarily in sorted order unless the data structure forces them to be.) For example. if the numbers are: 34 76 87 12 8 96 83. then the middle element in sorted order is 76. lf the elements had been sorted, 76 would be in the middle. For each of the following data structures assume the N elements are already in the data structure, and 1) describe briefly an efficient algorithm to find the middle element in sorted order (do not write code), 2) give the worst case running time of the algorithm. Your algorithms should be as efficient as possible for full credit. (3) Data structure1: an unsorted array \ Best. modd probabkj "to {heat-G; 4% arrab e\emen‘c$ one-63 -—c>~oc {won a P®~ Chilean} WW and “new using remove MW» (W564 WM indelch Than 3034; / l 4: L“) WM“ 105“) lair-Le Hoe, Niacfie elem.er - ~ Asstmg loll Mama/tic“ 4‘“‘“’3"‘ “A Am ("a Wimp) 4.00;? MJ {Marl-:4) [0314 and ((MMMIVL filoflfiu). SC): 0 (when) .l— OCMiD-gu) :‘0 CM [oat/t (c) Data structure 2: a sorted linked list (here the elements are already sorted) wag 1's «in Haven-9. l‘D 4'43 middle demon-l" and DOM»? Quail/«W3 {L96 -—C—2. ingoki'od. lMi‘b Amati/er ADT' Cal-POM W <1th OCqugL'l --so inert-rib Jest use “My. LL “motel [711.2 out? 02, OCflE-t'“ :- / (c) Data structure 3: a min heap _—) cowl?ch +rge halal/[Jr Of‘ My: (9100904)) 122% 16 “final Como renal/aids) 44%- would “we’d” H“ m w— k ' 1rd he 00410 “J 56W“ 3—2 mun uan j j: be chamfl__mm [S no (d) Data structure 4: an AVL tree —) 65 7" . a) “Mm-ed “6'91"? -—2 WW old 165 OJ. nodes. how elements cure son/led #wc wrfl (aque’k) +raueuc +144 thle huh Mlc— oCM).~?©mW (1M poseibg £14564 £404 demewf Mb :04 arrag 0(a) .— 5614: Mak— OLMIOJ'M) ~ao~d 4440A final W \ W‘Idd‘e' POSS'H‘J 44""— WOYSE {3r finding €lcmm43 ’WtW knDWl'flj HI value. mole: m is Most mo.te less Mn 11, MW CW: mtg, elemenk New should be ma +° rs Sig-n om) bur Mafia?» 65 000 «M ]{l "A CW 9”?)711 I Mulde amis OOH) but A; Umpamfdc, W'Yg' -fiU-. ...
View Full Document

Page1 / 14

COSC 2011 - 2004F - Exam - g UNIVERSITE _ EUNIVERSITY...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online