University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Similar documents
University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Second Examination Solution

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Final Examination CSE 100 UCSD (Practice)

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Course Review for Finals. Cpt S 223 Fall 2008

Computer Science Spring 2005 Final Examination, May 12, 2005

CPSC W1: Final Exam

Solutions to Exam Data structures (X and NV)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

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

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Largest Online Community of VU Students

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

COS 226 Algorithms and Data Structures Fall Midterm

Course Review. Cpt S 223 Fall 2009

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Lecture Summary CSC 263H. August 5, 2016

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Total Score /15 /20 /30 /10 /5 /20 Grader

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

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

Total Score /15 /20 /30 /10 /5 /20 Grader

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373: Practice Final

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

INSTITUTE OF AERONAUTICAL ENGINEERING

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

CSE 100: GRAPH ALGORITHMS

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

CSE 100: GRAPH ALGORITHMS

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

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

CISC 320 Midterm Exam

1. AVL Trees (10 Points)

lecture29: Shortest Path Algorithms

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Final exam. CS 2110, December 15, 2016, 9:00AM

Recitation 9. Prelim Review

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

CS61BL. Lecture 5: Graphs Sorting

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

Course Review. Cpt S 223 Fall 2010

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Outline. Graphs. Divide and Conquer.

Table of Contents. Chapter 1: Introduction to Data Structures... 1

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Lecture 6 Basic Graph Algorithms

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Data Structure (CS301)

CPSC 331 Term Test #2 March 26, 2007

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

COS 226 Algorithms and Data Structures Fall Final

Computer Science E-22 Practice Final Exam

Homework Assignment #3 Graph

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CS-301 Data Structure. Tariq Hanif

Transcription:

University of Illinois at Urbana-Champaign Department of Computer Science Final Examination CS 225 Data Structures and Software Principles Fall 2009 7-10p, Tuesday, December 15 Name: NetID: Lab Section (Day/Time): This is a closed book and closed notes exam. No electronic aids are allowed. You should have 9 problems total on 19 pages. The last two sheets are scratch paper; you may detach them while taking the exam, but must turn them in with the exam when you leave. Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can, and (3) assume the best possible design of a particular implementation is being used. Please put your name at the top of each page. Problem Points Score Grader 1 20 2 10 3 20 4 15 5 20 6 20 7 20 8 15 9 20 Total 160

1. [Choices, Choices! 20 points]. MC1 (2pts) Suppose your goal is determine whether or not a graph contains a vertex that is connected to no other vertices. How long does the best possible algorithm take if the graph is implemented using adjacency lists? an adjacency matrix? (As always, assume there are n vertices and m edges.) (a) O(1) for lists and O(n) for the matrix. (b) O(deg(v)) for lists and O(n) for the matrix. (c) O(n) for lists and O(n 2 ) for the matrix. (d) O(m) for lists and O(n 2 ) for the matrix. (e) None of these answers is correct. MC2 (2pts) When should a pointer parameter p be a reference parameter? (That is, when would it be more appropriate for a parameter list to be (mytype * & p) rather than (mytype * p)?) (a) When the function changes p, and you want the change to affect the actual pointer argument. (b) When the function changes p, and you do NOT want the change to affect the actual pointer argument. (c) When the function changes *p, and you want the change to affect the object that is pointed at. (d) When the function changes *p, and you do NOT want the change to affect the object that is pointed at. (e) When the pointer points to a large object. MC3 (2pts) Suppose we have implemented the Stack ADT as a singly-linked-list with head and tail pointers and no sentinels. Which of the following best describe the running times for the functions push and pop, assuming there are O(n) items in the list, and that the bottom of the stack is at the head of the list (all pushing and popping occurs at the tail)? (a) O(1) for both functions. (b) O(n) for both functions. (c) O(1) for push and O(n) for pop. (d) O(n) for push and O(1) for pop. (e) None of these is the correct choice.

MC4 (2pts) Which of the following statements is true for a B-tree of order m containing n items? (i) The height of the B-tree is O(log m n) and this bounds the total number of disk seeks. (ii) A node contains a maximum of m 1 keys, and this bounds the number of disk seeks at each level of the tree. (iii) Every Binary Search Tree is also an order 2 B-Tree. Make one of the following choices. (a) Only item (i) is true. (b) Only item (ii) is true. (c) Only item (iii) is true. (d) Two of the above choices are true. (e) None of choices (i), (ii), or (iii) are true. MC5 (2pts) In our uptree disjoint sets implementation, suppose we employ Union-by-Size and no path compression. The running times of the relevant functions are: (a) O(1) for setunion, and O(log n) for Find. (b) O(n) for setunion, and O(log n) for Find. (c) O(log n) for setunion, and O(log n) for Find. (d) O(1) for setunion, and O(log n) for Find. (e) None of the above is correct. MC6 (2pts) Suppose we run depth first search on a connected, undirected graph with n vertices and 5n+2 edges. How many edges will be labelled back edges? (a) 3n + 4 (b) 4n + 3 (c) 5n + 2 (d) 6n + 1 (e) The answer cannot be determined from the information given.

MC7 (2pts) Depth first search, when run on the following graph, beginning at node A, classifies edge (c, f) as. Assume that the edge iterators are set up to process adjacent vertices in the (left to right) order given in this adjacency list representation of the graph. (a) back (b) cross (c) discovery (d) predecessor (e) None of these is the correct choice. MC8 (2pts) Suppose we implement Kruskal s algorithm using a sorted array as our priority queue and an adjacency matrix graph. What is the tightest worst case running time of the algorithm? (As usual, V = n, E = m, and you may assume disjoint set functions run in constant time.) (a) O(n 2 ) (b) O(n + m) (c) O(m log n) (d) O(n 2 + m log n) (e) None of the above accurately describes the running time of Kruskal s algorithm.

MC9 (2pts) Suppose we implement an algorithm to determine whether or not a simple undirected graph is connected using a modification of a graph algorithm we saw in class. What is the tightest worst case asymptotic running time of the best algorithm to do this? (a) O(n 2 ) (b) O(n + m) (c) O(n log n + m log n) (d) O(n) MC10 (2pts) Which of the following array-based representations of a forest of up-trees could not possibly result from a sequence of union and find operations if union-by-size and path compression are employed? (a) (b) (c) (d) 0 1 2 3 4 5 6 7-1 -1-1 -1-1 -1-1 -1 0 1 2 3 4 5 6 7-8 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7-2 0-2 2-2 4-2 6 0 1 2 3 4 5 6 7-8 0 1 2 3 4 5 6 (e) All of these are valid forests of up-trees.

2. [A Little C++ 10 points]. (a) (5 points) Complete the following (silly) function implementation so that no memory is leaked. void facebook() { int linkedin = 7; int * hi5 = &linkedin; int * orkut = new int[4]; int * friendster = orkut; int ** bebo = new int * [3]; bebo[0] = &friendster[3]; bebo[1] = new int; bebo[2] = bebo[0]; // add code to free each piece of dynamically // allocated memory exactly once } (b) (3 points) Write the line of C++ code that declares a variable named album whose type is a vector of dynamic arrays of BMPs. (c) (2 points) Briefly describe the two instances when the destructor is called.

3. [1D-Search 20 points]. In this problem you will describe a C++ class that supports nearest-neighbor search among 1-dimensional points consisting of a single real value. The abstract data type corresponding to the class definition of OneDSearch: OneDSearch(): creates an empty 1-dimensional search structure. insert(double x): inserts the real number x into the structure. query(double y): returns the real number in the data structure that is closest to y. If the data structure is empty, return a large number constant called BIG. Implementation notes: Your data structure should perform each operation in worst-case O(log n) time. Your answers will be graded on correctness, efficiency, clarity, and succinctness. (a) (7 points) Write the entire public portion of the class definition for OneDSearch, paying particular attention to const correctness and, depending on your implementation, responsible memory management. class OneDSearch { public: private: }; // you don t have to write this

(b) (3 points) The best implementation of this class most nearly resembles which of the following data structures from our course? BST AVL Tree Hash Table SkipList Heap KD-Tree DFS BFS (c) (7 points) Write the C++ code for public member function query(double y). Be sure to comment your code so we know what you re doing. You may write a private helper function, if you d like. (d) (3 points) Suppose you want to add a function rangequery(int a, int b) that returns a list of all the values in the structure between a and b (inclusive). The tightest, worstcase running time of rangequery in terms of n, the number of values in the structure is... O(1) O(log n) O(n) O(n log n) O(n 2 )

4. [Hashing 15 points]. (a) (6 points) Suppose the following keys, k, are inserted in some order into an initially empty linear probing hash table of size 7, using the following table of hash values (h(k)): k h(k) A 3 B 1 C 4 D 1 E 5 F 2 G 5 Circle the table(s) below that could be the result of a sequence of insertions of the data above. I. II. III. 0 1 2 3 4 5 6 G B D F A C E 0 1 2 3 4 5 6 B G D F A C E 0 1 2 3 4 5 6 E G F A B C D (b) (9 points) Suppose a hash table of size N handles collisions by separate chaining, and that it employs periodic resizing whenever necessary. In order to argue that lookup times are constant time operations on average, we have to make some assumptions. List those assumptions here: (assumption 1 about the hash function) (assumption 2 about the hash function) (assumption 3 about the hash function) (assumption 1 about the resizing scheme) (assumption 2 about the resizing scheme)

5. [Skip Lists 20 points]. (a) (5 points) In class we examined a particular scheme for creating the towers of a skip list using coin flips. What is the probability we see a sequence of coin flips corresponding to a tower containing exactly 3 pointers? (b) (5 points) Suppose we want to insert the key 35 into the structure below, and we ve flipped coins to determine that the tower containing 35 should have exactly 3 pointers. In the diagram, neatly mark the nodes that are compared when the function call insert(35) is made. Please mark each node in every level at which a comparison takes place (so that some nodes will have multiple comparisons). In addition, place a small x on the pointers in the original structure that have to be reassigned in order to insert the new node. (c) (3 points) Suppose you are flipping coins to create a tower and that each flip is the last one with probability p, and with probability (1-p) you keep going. Then what is the probability a tower contains exactly k pointers? (d) (3 points) Let random variable X denote the number of pointers in a single tower. Compute E[X], the expected number of pointers in a tower if the probabilities are as described in part c). Show your work!

(e) (2 points) Define a skip list to have height (H) equal to k if the maximum level of any tower is level k 1, and suppose towers are created by flipping fair coins. We have derived an upper bound on the height of an n item skip list: P (H > k) Circle the correct bound: n 1 ( n 2 k 2 )k ( 1 k )n (1 1 kn ) ( 1 2 )nk (f) (2 points) Use the result from part e) to give a bound on the probability that a skip list containing 2 20 keys (approx. 1m) has height greater than 60?

6. [Binary Heaps 20 points]. (a) (8 points) Consider the following minheap. A C B K H F T P L M J The minheap above was the result of a sequence of insert and removemin operations. Assume that the last operation was an insert. Which of the following keys could have been the one inserted last? Circle all possible keys. A B C F H J K L M P T (b) In many graph algorithms we need to be able to update or change the value of a key in a heap. Function changekey(int index, const ktype & newkey) changes the key in position index to value newkey and restores the heap property. The partial interface for the Heap class is here: template <class ktype> class Heap{ public:... void insert(const ktype & k); ktype & removemin(); private: vector <ktype> theheap; int size; int leftchild(int i); int rightchild(int i); int parent(int i); }; void heapifyup(int i); void heapifydown(int i);...

i. (7 points) Write function changekey. Be sure to comment your code! void Heap<kType>::changeKey(int index, const ktype & newkey) { } ii. (3 points) What is the tightest bound on the worst case running time of changekey? O(1) O(log n) O(n) O(n log n) O(n 2 ) iii. (2 points) Is changekey a public or private member function of the Heap class?

7. [Priority Queues 20 points]. Consider the following code fragment: minpq <Integer> pq; for (int i = 0; i < n; i++) { pq.insert(a[i]); if (pq.size() > k) pq.removemin(); // <3 <3 <3 <3 <3 } for (int i = 0; i < k; i++) cout << pq.removemin() << endl; Assume that a[] is an array containing n integers, n k 1, and minpq is implemented using a binary heap. (a) (5 points) What does the code output? (b) (5 points) What is the tightest bound on its worst case running time? O(k log k) O(k log n) O(n log k) O(n log n) O(nk) O(n 2 ) Now suppose we remove the line marked <3 <3 <3 <3 <3, and we implement minpq as an unsorted array. (c) (5 points) What does the code output? (d) (5 points) What is the tightest bound on its worst case running time? O(k log k) O(k log n) O(n log k) O(n log n) O(nk) O(n 2 )

8. [Landmark Paths 15 points]. Given an undirected graph G = (V, E), and a landmark vertex x, your task is to design an algorithm that finds the length of the shortest path from one vertex v to another vertex w passing through vertex x. One such landmark path from v to w is illustrated in the graph below, but notice that it s not the shortest one! (Note that the path is allowed to traverse an edge more than once, and each traversal is counted once in the path length, so that the total path length in the graph below is 9.) X V W (a) (3 points) The solution to this problem involves employing one of the graph algorithms we ve discussed in class. Circle the appropriate one: DFS BFS Prim s MST Kruskal s MST (b) (2 points) How many times would you employ the algorithm you chose in part (a)? (c) (2 points) All of the algorithms in part (a) have to start somewhere! Circle the vertex or vertices from which you would start the algorithm. V W X anywhere (d) (5 points) Very briefly describe how you would compute and report the shortest landmark path length. (e) (3 points) What is the worst case running time of the best algorithm for finding the shortest landmark path?

9. [Algorithms 20 points]. Below is partial pseudocode from an algorithm we discussed in class. Assume it is a member function of a graph class consisting of weighted, undirected edges. Also assume that any instance of the graph class contains a collection of vertices V with V = n and a collection of edges E with E = m. graph & graph:: ( vertex s ) { for each vertex v in V { d[v] = INFINITY; labelled [v] = false; predecessor[v] = empty; } d[s] = 0; for n iterations { vertex v = minimum distance vertex among unlabelled vertices; // (12) labelled[v] = true; } for each vertex w adjacent to v if (!labelled[w]) if (weight(v,w) < d[w]) { d[w] = weight(v,w); // (18) predecessor[w] = v; } graph returngraph; returngraph.v = V; for each vertex v in V if (v!= s) returngraph.addedge(v,predecessor[v]); } return returngraph; (a) (4 points) What is the name of the function described above, and what does it return? (b) (3 points) In the worst case, how many times does is the assignment statement d[w] = weight(v,w); made over the execution of the entire algorithm?

(c) Suppose that the degree of every vertex in the graph is no more than 10. i. (5 points) Which graph implementation gives the most efficient running time? ii. (5 points) Which implementation of a priority queue should be used in lines 12 and 18? iii. (3 points) What is the running time if you implement the function this way? (Your answer should be in terms of n and/or m, and should be as accurate as possible.)

(scratch paper, page 1)

(scratch paper, page 2)