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

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

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

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 E&CE 250 ALGORITHMS AND DATA STRUCTURES

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

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

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

ECE 250 Algorithms and Data Structures

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 ALGORITHMS AND DATA STRUCTURES Total:

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

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

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

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

ECE 250 Algorithms and Data Structures

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

INSTITUTE OF AERONAUTICAL ENGINEERING

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

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

COMP 103. Data Structures and Algorithms

CMSC132, Practice Questions

CSE 214 Computer Science II Heaps and Priority Queues

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

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

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

DIT960 Datastrukturer

CS61BL: Data Structures & Programming Methodology Summer 2014

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

CSE332 Summer 2012 Final Exam, August 15, 2012

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

COMP 250 Midterm #2 March 11 th 2013

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

CS171 Midterm Exam. October 29, Name:

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

AP Computer Science 4325

CSE wi: Practice Midterm

Computer Science 136 Exam 2

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

Final Examination CSE 100 UCSD (Practice)

CSE 100: GRAPH ALGORITHMS

n 2 ( ) ( ) + n is in Θ n logn

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

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS61BL. Lecture 5: Graphs Sorting

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

Final Exam. CS2110 Spring May 12, Total. Max Score

CS200 Midterm 2 Fall 2007

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

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

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

CS302 Data Structures using C++

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

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

About this exam review

SOLUTIONS (INCOMPLETE, UNCHECKED)

CS 314 Final Fall 2012

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

( ) 1 B. 1. Suppose f x

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

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

Section 05: Solutions

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CSE373 Spring 2015: Final

CS 161 Fall 2015 Final Exam

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

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

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

CSE 373 Final Exam 3/14/06 Sample Solution

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

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

MLR Institute of Technology

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

CSE 373: Practice Final

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

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

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CmpSci 187: Programming with Data Structures Spring 2015

Computer Science E-22 Practice Final Exam

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

CS200 Final Exam Fall 2007

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

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

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

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

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

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

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

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

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

Practice Problems for the Final

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

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Transcription:

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Final Examination (17 pages) Instructor: Douglas Harder April 14, 2004 9:00-12:00 Name (last, first) Student ID Do all problems. If you are writing a supplemental exam, DO NOT attempt the question on Dijkstra s algorithm (I4). If you are writing the supplemental exam, circle the following: WRITING SUPPLEMENTAL EXAM The number in brackets denotes the weight of the question. If information appears to be missing from a problem, make a reasonable assumption, state it, and proceed. If the space to answer a question is insufficient, use the back of the previous page. You may use diagrams to supplement (but not replace) sentence answers. Closed book. No calculators. Question Mark Question Mark A F /9 /5 B G /7 /9 C H /6 /4 D I /12 And Bonus (2) /15 or /7 * E Total /13 /80 or /72 * * Total weight for students writing the supplemental exam. ECE 250 W04 Page 1 of 17

A. Hash Tables Every object in Java has a method with the signature int hashvalue(). This method returns an integer which may be considered to be a hash of the object. 1. [7] Design a chained hash table class ChainedHashTable which implements the HashTable interface. Use the division method to convert the int returned by the hashvalue method into an appropriate hash value. You may assume that the value returned by hashvalue() is greater than or equal to 0. The argument arraysize indicates the number of entries in the array of the hash table. You should use the LinkedLinked class, the signature of which is given below. Your implementation should assume that elements which are most recently inserted into the hash table are also the ones most likely to be referenced in the future. public interface HashTable { public HashTable( int arraysize ); public void insert( Object obj ); public void extract( Object obj ); public class LinkedList { public LinkedList(); public void append( Object obj ); public void assign( LinkedList list ); public void extract( Object obj ); public Object getfirst(); public LinkedList.Element gethead(); public Object getlast(); public LinkedList.Element gettail(); public boolean isempty(); public void prepend( Object obj ); public void purge(); ECE 250 W04 Page 2 of 17

2. [2] Why is it appropriate that we can use the division method to map the integer returned by hashvalue() onto the appropriate range? ECE 250 W04 Page 3 of 17

B. Trees 1. [3] Taking into account only the instance variables of the various classes and their super classes, what is the memory requirement for a general tree with n nodes, as implemented and discussed in class. Recall that each int and object reference requires 4 bytes. 2. [4] Suppose data is stored in a binary search tree. For each of the four traversals, breadth-first traversal, and pre-, post- and, in-order depth-first traversals, determine if the order in which the data are visited depends on the order in which the data are stored. For example, the data 1, 2, and 3 may be stored in a binary search tree in any one of the following five orders: Which ones depend on the order in which the data are stored: Which ones do not depend on the order in which the data are stored: ECE 250 W04 Page 4 of 17

C. AVL Trees [6] The tree shown in Figure 1 is an AVL tree of characters sorted using lexicographical (i.e., alphabetical) ordering. Starting with Figure 1, insert the letters A, C, E, and K individually into this tree and perform whatever rotations are necessary to keep the tree AVL balanced. For each step, indicate whether no rotations are necessary (i.e., the tree remains AVL balanced) or if a single rotation or a double rotation is necessary to rebalance the tree. Warning: Do not add A, and then, into this result, add C, etc., that is, you should end up with four different trees, each with ten nodes. Figure 1. ECE 250 W04 Page 5 of 17

D. Heaps 1. [4] For each of the following three parts (a, b, and c) the array represents a min-heap implemented as a complete binary tree. For each heap, either enqueue into or dequeue from the heap, as specified. The resulting representation should continue to be a complete binary tree. Each question is provided with a table to enter the resulting heap. a. Enqueue the element 2 into the the heap represented by: 1 3 5 7 4 6 9 b. Dequeue an element from the heap represented by: 1 3 5 7 4 6 9 c. Dequeue an element from the heap represented by the following tree. 1 2 3 12 9 4 7 19 15 11 13 6 2. [5] Assume you are implementing a priority queue using the data structures listed in the first column of Table 1. Given that there are n objects in the queue, fill in the asymptotic run times for the following methods: Enqueue Dequeue Get Head Complete Binary Tree (Heap) O( ) O( ) O( ) Ordered Linked list O(1) O(1) O( ) Ordered Array O(n) O(1) O( ) AVL Tree O( ) O( ) O( ) Table 1. One mark is taken off for each incorrect answer. ECE 250 W04 Page 6 of 17

3. Suppose you are implementing the class HeapAsArray (again, using a complete binary tree) where the size of the array is both fixed with 2 n 1 entries. You have three ways of dealing with the problem of inserting an object into a full heap: a. Throw an exception which is not a run-time exception. b. Find the lowest value/priority object in the heap and replace it if the new object has a higher value/priority. c. Discard the object being inserted. i. [1] Which way has the slowest run time (a, b, or c)? ii. [1] Which way forces the programmer using this class to write extra code to deal with a full heap (a, b, or c)? iii. [1] If this class is to be used for a priority queue, which method (a, b, or c) will most likely be the cause of problems? If you think this question is ambiguous, you may justify your answer. ECE 250 W04 Page 7 of 17

E. Backtracking Algorithms Consider the following problem. Given a set of weights and a scale, we would like to find a combination of weights which is both balanced and maximizes the weight on the scale. For example, given the weights 1, 3, 5, 5, and 7, placing the weights 7 and 3 on one scale, and the weights 5 and 5 on the other is both balanced and maximizes the weight (10) on the scale. In this case, at any node of the decision tree if we have a balanced partial solution, we may treat this as a complete solution by assuming that all other weights may optionally be left off of either arm of the scale. Thus, before we start any problem, we already have one candidate for a potential solution: leave both arms of the scale empty, in which case, the scale is balanced. Denote the weight of the current optimal solution (initially 0) by the value m opt. 1. [4] Write down the two mathematical equations (one for each restriction) which define the constraints of our problem. You may represent the weights by w i and the set of weights on the left- and right-hand arms of the scale by L and R, respectively. 2. [1] One condition which allows us to prune a branch of the decision tree may be stated in English as if the weight on one arm of the scale together with all unplaced weights are less than the weight on the other arm, then no solution can be found. Restate this mathematically. You may represent the set of all unplaced weights by W. 3. [2] There is another condition which depends on m opt. State this condition mathematically for full marks, or in works for partial marks. ECE 250 W04 Page 8 of 17

4. [6] Given five weights with masses 14, 5, 5, 2, and 1, draw the decision tree which finds the optimal solution to the problem described above. You need not draw any branches of the decision tree which are pruned by the conditions you give in parts 2 or 3 of this question. If you did not get part 3 of this question, do not despair; most pruning comes from the condition described in part 2. The first four nodes of the tree are given. Hints: a. Place the heaviest weights first, as is shown below. b. You may use symmetry to eliminate some of the nodes. c. The full decision tree has 364 nodes. Using the previous two hints and your conditions, the number of nodes which must be visited may be reduced to less than 30. When you are pruning, place an X through the node which is being pruned and indicate if the pruning depends on the condition stated in part 2, part 3, or symmetry. ECE 250 W04 Page 9 of 17

F. [5] Heap Sorting Given the following integer (i.e., int) MinHeap and MaxHeap classes, implement the method heapsort which takes an array of integers and returns a new array of integers sorted in ascending order. Your answer should not be much more than five additional lines of code. The constructor argument size indicates the maximum size of the heap. public class MaxHeap { public MaxHeap ( int size ); public void enqueue( int x ); public int dequeue(); public class MinHeap { public MinHeap ( int size ); public void enqueue( int x ); public int dequeue(); public int[] heapsort ( int[] list ) { int [] sorted = return sorted; ECE 250 W04 Page 10 of 17

G. Divide-and-Conquer Sorting 1. [4] Sort the following data using a binary merge sort. Each step of subdividing and merging should occupy a separate line and subdivisions should be indicated by bold lines between the different lists. Continue subdividing until the size of the list is two, at which point you may simply order the two elements. 7 3 6 1 4 5 0 2 2. [2] What shortcoming of quick sort does merge sort avoid? Use one or two sentences. 3. [3] Is the reasoning given in the following statement correct, yes or no? If yes, explain why. If no, what is the problem with the author s reasoning? The standard binary merge sort takes log 2 (n) steps to sort a list of n objects. If I divided the list into four sublists, sort each of these, and then merge these four sublists back into a single list, then this will take log 4 (n) steps. Since it is true for all values of n that log 4 (n)/log 2 (n) = ½, only half as many steps will be required, and therefore this modified merge sort will be twice as fast as the standard binary merge sort. ECE 250 W04 Page 11 of 17

H. Bucket Sort and Radix Sort 1. [4] Perform a increasing radix sort on 323, 312, 321, 112, 231, 123, 322 by using the following 3x5 tables, which may act as queues. For each step, enter the partial solution in the entry in the 1x7 table. The 6 th table should be the sorted list. 1 2 3 1 2 3 1 2 3 ECE 250 W04 Page 12 of 17

I. Graph Algorithms (and Bonus) For parts 1, 2, and 3 of this question, you do not have to know anything about graphs to answer the questions. Consider a directed graph (V, E) defined by a set of vertices V and a set of edges E, each edge being an ordered pair of vertices, e.g., (v 1, v 2 ). A path of length n is a nonempty ordered sequence of vertices (a 0, a 1,..., a n ) such that each ordered pair (a i - 1, a i ) where i = 1,..., n is an edge. A path is simple if all the vertices are different with the one exception: the first and last verticies may be equal. Consider the following implementation of the class Path together with some additional classes. public class Path { LinkedList path; int length; public Path( Vertex v ) { path = new LinkedList(); path.append( v ); length = 0; public void append( Vertex v ) { if ( isedge( v, path.getlast().getdatum() ) ) { length++; path.append( v ); else throw new RuntimeException( The new vertex is + not adjacent to the last element the path ); ; public boolean issimple() {... public class Vertex { Object key; public Vertex( Object key ) { this.key = key; public class LinkedList { public LinkedList(); public LinkedList.Element gethead(); public Object getfirst(); public class Element { public Element( Object key ); public Element getnext(); public Object getdatum(); ECE 250 W04 Page 13 of 17

1. [5] Implement the method issimple which returns true if the path is simple and false otherwise. You may, if you wish, use the class ChainedScatterTable, the signatures of which are given below. Your method must run in O(n) time where n is the length of the path. You may assume that two vertices with the same key will be equal when compared to each other. public class ChainedScatterTable { public ChainedScatterTable( int length ); Object find( Object obj ); void insert( Object obj ); boolean isfull(); boolean ismember( Object obj ); void purge(); void withdraw( Object obj ); public class Path {... public boolean issimple() { Hints: Implement code which ensures that all the edges are different and then add additional code to check for the special case that the first and last elements may be equal. If you are significantly exceeding 10 lines of code, you should reconsider your strategy. ECE 250 W04 Page 14 of 17

2. [2] Suppose instead that one additional instance variable, boolean simple, is added to the class Path. This instance variable is initially set to true. Each time another vertex is added to the path, the resulting path is tested and if it is no longer simple, this variable is set to false. In this case, the method issimple simplifies to public boolean issimple() { return simple; What must the asymptotic (big-o) run time of adding n vertices to the path be, assuming that the method isedge runs in constant time? 3. (Bonus) [2] If, in addition to adding the variable simple (as described in part 2), suppose we add a chained hash table which stores all vertices in the path. In this case, it is very easy to test if the new vertex already occurs in the linked list and therefore the run time of adding n vertices into the path is O(n). What rule (described in class) describing the relationship between run-time and memory used does this reflect? ECE 250 W04 Page 15 of 17

4. Dijkstra s Algorithm IF YOU ARE WRITING A SUPPLEMENTAL EXAM, DO NOT DO THIS QUESTION. In finding the length of the shortest path from a given a vertex A in a graph to all other vertices in the directed graph the edges of which have positive weights, Dijkstra s algorithm is fast and efficient. The vertex A is given the weight 0 and all other nodes are given a weight of infinity. Each node is marked as unvisited. Select one of the unvisited vertices which have the smallest current weight. Mark this vertex as visited. For each unvisited vertex adjacent to this selected vertex, if the sum of the weight of the selected vertex and the edge joining the two vertices is less than the current weight of the adjacent vertex, update the weight of the adjacent vertex. Repeat this process until all vertices have been marked as visited. After all vertices have been visited, the weights on the vertices are the shortest distance to those vertices from the given vertex A. a. [6] Evaluate Dijkstra s algorithm on the directed graph described on the next page. The first iteration of the algorithm has already been performed. Use one image of the graph for each iteration. b. [2] Dijkstra s algorithm was described as being greedy. Underline the sentence in the description of Dijkstra s algorithm which classifies the algorithm as greedy. ECE 250 W04 Page 16 of 17

ECE 250 W04 Page 17 of 17