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

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

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

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

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /5:30-7:00

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

ECE 250 Algorithms and Data Structures

ECE 250 Algorithms and Data Structures

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

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

INSTITUTE OF AERONAUTICAL ENGINEERING

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

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

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

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

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

MLR Institute of Technology

void insert( Type const & ) void push_front( Type const & )

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

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

) $ f ( n) " %( g( n)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

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

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

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

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

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

( ) n 3. n 2 ( ) D. Ο

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Data Structures Question Bank Multiple Choice

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

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Final Examination CSE 100 UCSD (Practice)

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

AP Computer Science 4325

Practice Problems for the Final

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

CS301 - Data Structures Glossary By

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

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

( D. Θ n. ( ) f n ( ) D. Ο%

CS-301 Data Structure. Tariq Hanif

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

Course Review for Finals. Cpt S 223 Fall 2008

Test points UTA Student ID #

( ) 1 B. 1. Suppose f x

Section 05: Solutions

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

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

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

LECTURE 03 LINKED LIST

Solved by: Syed Zain Ali Bukhari (BSCS)

Module 2: Classical Algorithm Design Techniques

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

Computer Science E-22 Practice Final Exam

CSE 373: Practice Final

VALLIAMMAI ENGINEERING COLLEGE

DATA STRUCTURES AND ALGORITHMS

Data Structures Brett Bernstein

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

Data Structures Through C. Student Workbook

SELF-BALANCING SEARCH TREES. Chapter 11

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

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Recitation 9. Prelim Review

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

Question Bank Subject: Advanced Data Structures Class: SE Computer

VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CSL 201 Data Structures Mid-Semester Exam minutes

About this exam review

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

CSE 373 Final Exam 3/14/06 Sample Solution

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

ECE250: Algorithms and Data Structures Final Review Course

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE 250 ALGORITHMS AND DATA STRUCTURES

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

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

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

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

1. Attempt any three of the following: 15

Design and Analysis of Algorithms - - Assessment

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

2. (a) Explain the concept of virtual functions in C++ with suitable examples. (b) Explain the concept of operator overloading in C++.

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Section 05: Midterm Review

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

Transcription:

ECE 25 Data Structures and Algorithms University of Waterloo Department of Electrical and Computer Engineering ECE 25 Data Structures and Algorithms Instructor: Douglas Wilhelm Harder Time: 2.5 hours Aides: none 18 pages Final Examination 27-4-1 Surname Given name(s) Student ID Number Signature You may only ask one question: May I go to the washroom? If you are unsure of a question, write down your assumptions and continue. If you have insufficient space to answer a question, use the back of the previous page. The examination is out of 144 marks. It will be marked out of 134. Page 1 of 18

ECE 25 Data Structures and Algorithms A. Algorithm Analsyis A.1 [3] Describe, in terms of limits, what it means for: a. f(n) = o(g(n)) b. f(n) = Q(g(n)) c. f(n) = W(g(n)) A.2 [2] Using limits, find the asymptotic relationship between ln(n) and each of ln(n 2 ) and ln 2 (n). Use the correct Landau symbol to show the relationship. A.3 [5] Determine the run times of the following loops where bool f(int n) randomly returns true or false and runs in O(n) time. Use the most appropriate Landau symbol in each case. a. for ( int i = ; i < n; ++i ) { for ( int j = i; j < n; ++j ) { ++sum; b. for ( int i = ; i < n; ++i ) { if ( f(1) ) { for ( int j = i; j < n; ++j ) { ++sum; Page 2 of 18

ECE 25 Data Structures and Algorithms c. for ( int i = ; i < n; ++i ) { for ( int j = i; j < n; ++j ) { if ( f(1) ) { ++sum; d. for ( int i = 1; i < n; i *= 2 ) { ++sum; A.4 [3] Suppose a teaching assistant has the choice of sorting a list of marked quizzes by last name, recording the marks, and then returning the examinations, or by simply going through the examinations in the order they were picked up, recording the marks, and then returning them. Assume that there are approximately n = 1 examinations and that students are approximately evenly distributed with respect to their last names across the m = 26 letters A through Z. Discuss the effort required in both cases. Page 3 of 18

ECE 25 Data Structures and Algorithms B. Lists, Stacks, Queues, Deques B.1 [4] Suppose we have a singly-linked list which has only a single member variable, a list_head pointer. What are the best possible run times of the following member functions (the behaviour of which is similar to that described in Project 1): Member Functions void push_front( const Object & ); Run Time void push_back( const Object & ); Object front() const; Object back() const; Object pop_front(); Object pop_back(); bool empty() const; int size() const; B.2 [7] For this question, assume the given change was made to the data structure described in Question B.1 and indicate which functions could be written to run in O(1) time instead of O(n) time. Just give the function name, e.g., empty. a. Assume we add the member variable int count. Which member functions can now be written to run in O(1) time? b. Assume we add the member variable SingleNode<Object> * list_tail. Which member functions can now be written to run in O(1) time? c. Suppose we change the singly-linked list to a doubly-linked list, that is, each node has both a previous and next pointer. Which member functions can now be written to run in O(1) time? d. Suppose that in addition to changing the singly-linked list to a doubly-linked list, we also add the member variable DoubleNode<Object> * list_tail. Which member functions can now be written to run in O(1) time? Page 4 of 18

ECE 25 Data Structures and Algorithms B.3 [6] Suppose we have a singly-linked list as described below which is storing integers but we would like to add the additional member function bool is_sorted() const which returns true if the elements in the list are sorted and false otherwise. It is easy to implement this new function so that it runs in O(n) time (n being the number of elements in the linked list) however, you can implement this function so that all member functions in this class runs in O(1) time? You will have to modify the other mutators. class SingleNode { private: int element; SingleNode * next_node; public: SingleNode( int e, SingleNode * n ):element(e), next_node(n) { int retrieve() const { return element; SingleNode * next() const { return next_node; friend class SingleList; ; class SingleList { private: SingleNode * list head; SingleNode * list_tail; // insert other member variables here public: bool empty() const; // do not implement bool is_sorted() const; int front() const; // do not implement int back() const; // do not implement void push_front(); void push_back(); ; void SingleList::push_front( int n ) { void SingleList::push_back( int n ) { bool SingleList::is_sorted() { Page 5 of 18

ECE 25 Data Structures and Algorithms C. Trees C.1 [3] The following is a memory map of a binary search tree. The first row are the addresses, the second row are the contents of those addresses. Assume that each pointer and each value occupies one address. The member variable root which points to a binary-search-tree node is stored at address 8. Each binary-search-tree node occupies three contiguous spaces, the first being the value, the second being the address of the left sub-node, and the third being the address of the right sub-node. Address is used to represent empty sub-trees. 1 2 3 4 5 6 7 8 9 A B C D E F 21 33 A 15 4 25 1 18 47 1 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 42 E 27 1D 29 a. Draw a graphic representation of the tree. b. Insert the node 3 and assume that the new operator was allocated the memory addresses 12, 13, and 14. Update the above memory map to insert this new node. C.2 [4] List the elements of the tree in the order in which they are visited for a: a. breadth-first traversal: b. pre-order depth-first traversal Page 6 of 18

ECE 25 Data Structures and Algorithms C.3 [6] Implement the member function double_rotate_to_left which performs a double rotation on the given node to the left, as demonstrated in Figure C.3. The member function would be called on the node which is not balanced. You are not required to use the parameter to make this question work. You may implement helper functions on the back of the previous page. Figure C.3. The required double rotation. template <typename T> void AVLNode<T>::double_rotate_to_left( AVLNode<T>::to_this * & ptr ) { Page 7 of 18

ECE 25 Data Structures and Algorithms C.4 [6] For each of the given AVL trees, perform the given operation and perform whatever rotations are necessary to rebalance the tree. If no rotations are necessary, you can simply draw in or delete the appropriate node. If you are removing a full node, make the appropriate selection to the right sub-tree. a. Insert 17. b. Insert 3. c. Remove 45. d. Insert 47. e. Remove 12. Page 8 of 18

ECE 25 Data Structures and Algorithms C.5 [7] For each of the given B-trees, perform the given operation and perform whatever changes are necessary maintain the B-tree structure. You may not use redistribution. You only need redraw those blocks which change. a. Insert 11. b. Insert 68. c. Remove 61. d. Remove 13. C.6 [2] Correct the following statement: If a block on the hard drive can hold a maximum of M next pointers, all internal blocks in a B-tree must point to at least M/2 nodes. Page 9 of 18

ECE 25 Data Structures and Algorithms D. Hash Tables D.1 [2] In the worst case, what is the minimum number of elements which can be stored in a hash table using open addressing for the following three techniques of probing (assume the size of the hash table is M). Probing Technique Linear Probing Worst Case Quadratic Probing Double-hashing D.2 [3] Insert the following seven elements (in the given order) into a hash table of size 7 which uses quadratic probing. Use the least-significant digit to represent the initial bin. Stop after the first exception is thrown as a result of an invalid operation. 22 66 72 44 86 33 9 1 2 3 4 5 6 D.3 [2] We had two options for double hashing: a. The hash table is of size M = 2 m and the skip size must be odd, or b. The hash table is of size p where p is prime and the skip size is in 2,..., p 1. Explain two benefits of using the first model. These benefits may relate to any of the operations which we may wish to perform on a hash table. D.4 [3] What does the following algorithm do? The operator sizeof returns the number of bytes occupied by the variable n. int a = ; int b = 1; for ( int i = ; i < 8*sizeof( n ); ++i ) { if ( n b ) { ++a; b <<= 1; Page 1 of 18

ECE 25 Data Structures and Algorithms E. Heaps E.1 [6] Using the implementation of a min-heap as discussed in class, insert the following numbers (in the order given) into an initially empty min-heap. 6 4 2 5 3 1 Indicate the final state of the min heap in the following array: 1 2 3 4 5 6 E.2 [6] In class, we saw a min-heap with two sub trees. A d-min-heap is a min-heap where each node has d sub trees, each of which is also a d-min-heap. In this case, it is easier to start at the array entry and let the children of i be di + 1, di + 2,..., di + d. Unfortunately, the code below to dequeue the minimum element has some bugs. Correct the code. class DHeap { private: int * array; int count; int d; ; public: void dequeue_min( int ); //... void DHeap::dequeue_min( int n ) { int value = array[]; int i = ; while ( i < count ) { int min = array[i + 1]; int posn = 1; for ( int j = 2; j <= d; ++j ) { if ( array[i + j] < min ) { max = array[i + j]; posn = j; if ( array[count - 1] < min ) { array[i] = array[count 1]; return value; else { array[i] = array[i + j]; i = i + j; return value; Page 11 of 18

ECE 25 Data Structures and Algorithms F. Sorting F.1 [3] Perform one iteration (one pass through the array) of bubble sort on the following unsorted array. Write your answer in the next row. 7 5 3 8 2 9 1 1 6 4 F.2 [4] Convert the unsorted array into a max-heap using the heapify algorithm shown in class. 7 5 3 8 2 9 1 1 6 4 F.3 [3] Perform one iteration of quick sort (after the values are appropriately moved but before quick sort is recursively called on both halves). Use integer division to find the mid point. The first row represents the array entries. Write your answer in the next row. 1 2 3 4 5 6 7 8 9 1 11 43 21 93 87 23 57 19 35 77 54 96 12 F.4 [4] Apply radix sort to the following 3-bit binary numbers: 1 11 111 1 11 11 1 11 Place the intermediate lists (after dequeuing) into the following three tables. The last (sorted) list is given. 1 1 11 1 11 11 11 111 Page 12 of 18

ECE 25 Data Structures and Algorithms G. Graphs G.1 [4] Perform a topological sort on the following graph with the following condition: if there is more than one eligible node which could appear next in the topological sort, choose that one which has the smallest value. Figure G.1 A directed acyclic graph. G.2 [6] The following is an implementation of Prim s algorithm, as done in Project 5. Indicate what must be modified to implement Dijkstra s algorithm to find the minimum distance between two vertices v and w. You should assume that the arguments do not require error checking and that a minimum path exists. double WeightedUndirectedGraph::minimum_spanning_tree( int v ) const { double value =.; double * table = new double[array_size]; bool * visited = new bool[array_size]; for ( int i = ; i < array_size; ++i ) { table[i] = INF; visited[i] = false; table[v] =.; while ( true ) { bool found = false; double max = INF; int posn; for ( int i = ; i < array_size; ++i ) { if (!visited[i] && table[i] < max ) { posn = i; max = table[i]; found = true; if (!found ) { delete [] table; delete [] visited; return value; visited[posn] = true; value += max; for ( int i = ; i < array_size; ++i ) { if (!visited[i] && adjacent( i, posn ) < table[i] ) { table[i] = adjacent( i, posn ); Page 13 of 18

ECE 25 Data Structures and Algorithms G.3 [6] Use Dijkstra s algorithm to find the length of the minimum path from vertex A to vertex K. You must use a separate graph for each visited node in the graph. Indicate the order in which you are using the graphs (1, 2, 3,...) and cross out any unused graphs. You do not have to record the pointers. Use the check box to indicate which vertices have been visited. Page 14 of 18

ECE 25 Data Structures and Algorithms H. Algorithms H.1 [4] Suppose a number of projects are being proposed for the next cycle of a product release. There are n projects, where each is associated with a projected revenue and an expected completion time. Justify heuristically why using a greedy algorithm using the highest cost density (expected revenue over completion time) is better than using a greedy algorithm which uses the highest expected revenue. H.2 [4] For a divide-and-conquer algorithm which has a runtime T(n) = at(n/b) + O(n k ) for n > 1, assume that n = b m and that a < b k. Show how we can simplify to see that T(n) = O(n k ). T m m ( n) = a l= k b a l H.3 [3] Implement a version of factorial which stores the values of n! when n < 1, thus, the second time the function is called with a particular argument, the run time is O(1). Use the given array. int array[1]; array[] = 1; for ( int i = 1; i < 1; ++i ) { array[i] = ; int factorial( int n ) { H.4 [3] Describe the ideal shape of a skip list with 15 entries. Page 15 of 18

ECE 25 Data Structures and Algorithms Page 16 of 18 I. Sparse Matrices I.1 [4] Show how you would store the following matrix using the Harwell-Boeing sparce matrix format by filling in the appropriate tables below. Member Variable 1 2 3 4 5 6 7 8 double_*_value; int * row; Member Variable 1 2 3 4 5 6 7 int_*_column; I.2 [4] Suppose that you are writing a function to return the (i, j)th entry of a matrix stored in the Harwell-Boeing format. Assume the matrix is n n and that the N entries are evenly distributed among the rows and columns. Describe (using pseudo-code) how you could implement the access function so that it runs in O(ln(N/n)) time. 3 1 6 5 1 2 4 1 3

ECE 25 Data Structures and Algorithms J. Unix J.1 [5] Assume that your home directory contains the four files Tester.h TestTester.h TestDriver.cpp t1.in and that you have just logged in. Create a directory called project, copy the files into that directory, change to that directory, edit a new file Test.h using whichever Unix editor you prefer, compile the appropriate tester file (assume it compiled), and then run the resulting executable by redirecting the contents of the t1.in file. Finally, exit the system. {eceunix:1 {eceunix:2 {eceunix:3 {eceunix:4 {eceunix:5 {eceunix:6 {eceunix:7 Page 17 of 18

ECE 25 Data Structures and Algorithms K. Self Study K.1 [5] Describe, in your own words, either splay trees or disjoint sets. You should describe the purpose, appropriate implementations, and properties. Provide references. Page 18 of 18