CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

Similar documents
CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

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

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

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE 332, Spring 2010, Final Examination 8 June 2010

CSE373 Spring 2015: Final

CSE332 Summer 2012 Final Exam, August 15, 2012

CSE373 Winter 2014, Midterm Examination January 29, 2014

CSE 373: Practice Final

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE 373: Data Structures and Algorithms

CSE332, Spring 2012, Final Examination June 5, 2012

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE 332: Analysis of Fork-Join Parallel Programs. Richard Anderson Spring 2016

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Lecture 29: Parallelism II

Final Examination CSE 100 UCSD (Practice)

1. AVL Trees (10 Points)

CSE 332: Data Structures & Parallelism Lecture 14: Introduction to Multithreading & Fork-Join Parallelism. Ruth Anderson Winter 2018

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 1 Introduction to Multithreading & Fork-Join Parallelism

CSE373: Data Structures & Algorithms Lecture 22: Parallel Reductions, Maps, and Algorithm Analysis. Kevin Quinn Fall 2015

Course Review for Finals. Cpt S 223 Fall 2008

CSE 373: Data Structures & Algorithms Introduction to Parallelism and Concurrency. Riley Porter Winter 2017

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

Computer Science Spring 2005 Final Examination, May 12, 2005

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

Section 05: Solutions

CS/ENGRD2110: Final Exam

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

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

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

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

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

Computer Science E-22 Practice Final Exam

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

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

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

Sample Exam Problems For the Material in A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency

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

CSE 332 Autumn 2016 Final 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)

CSE351 Winter 2016, Final Examination March 16, 2016

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

About this exam review

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 2 Analysis of Fork-Join Parallel Programs

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

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

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

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

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

ECE 250 Algorithms and Data Structures

CLASSIC DATA STRUCTURES IN JAVA

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

CSE351 Spring 2018, Final Exam June 6, 2018

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

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

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

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

Solutions to Exam Data structures (X and NV)

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

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

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Practice Midterm Exam Solutions

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

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

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

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

CSE 373 Final Exam 3/14/06 Sample Solution

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

CSE 332: Data Structures & Parallelism Lecture 15: Analysis of Fork-Join Parallel Programs. Ruth Anderson Autumn 2018

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

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Data Structures and Algorithm Analysis in C++

Lecture Summary CSC 263H. August 5, 2016

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

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

CS 4349 Lecture October 18th, 2017

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

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

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

CSE 373, Winter 2013 Final Exam, Tuesday, March 19, Good luck! You can do it!

COMP3121/3821/9101/ s1 Assignment 1

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions

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

Review of course COMP-251B winter 2010

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

CSci 231 Final Review

CS61B Spring 2016 Guerrilla Section 6 Worksheet

Quiz 2 Solutions. (a) T F Topological sort requires Ω(V lg V ) if the edge weights are unbounded. Explain:

Transcription:

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly at 4:20. There are 98 points total, distributed unevenly among 10 questions (many with multiple parts): Question Max Earned 1 7 2 7 3 8 4 20 5 8 6 8 7 16 8 8 9 7 10 9 Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. But clearly circle your final answer. The questions are not necessarily in order of difficulty. Skip around. Make sure you get to all the problems. If you have questions, ask. Relax. You are here to learn.

1. (7 points) Given this code, write client code using BankAccount such that a call to getownerage encounters a NullPointerException. Then explain briefly how to rewrite one of the methods in BankAccount to avoid this problem. class Person { public String name; public Date birthdate; // has a Date(int year, int month, int day) constructor class BankAccount { private Person owner; private float balance; public BankAccount(Person o, float b) { if (o == null o.birthdate == null) { throw new IllegalArgumentException(); owner = o; balance = b; public long getownerage() { Date now = new Date(); // initialized to be the current date long millisecondsperyear = 365 * 24 * 60 * 60 * 1000; // the Date gettime() method returns the date as the number // of milliseconds since January 1, 1970, 00:00:00 GMT return (now.gettime() - owner.birthdate.gettime()) / millisecondsperyear; Person p = new Person(); p.name = "Bob"; p.birthdate = new Date(1988, 10, 17); BankAccount acct = new BankAccount(p, 10.0); p.birthdate = null; acct.getownerage(); The constructor of BankAccount should do a deep copy of the Person object passed in.

2. (7 points) Your friend wrote the code below to compute the maximum value from an array of integers. Unfortunately, your friend is not seeing the speedup they were expecting. Briefly explain why this is the case (i.e. identify the mistake they made). Then modify their code to take full advantage of the available parallelism. Indicate which lines of the original you are replacing. class MaxThread extends java.lang.thread { int lo; int hi; int[] arr; // arguments int ans = Integer.MIN_VALUE; // result MaxThread(int[] a, int l, int h) {... public void run() { // override if(hi - lo < SEQUENTIAL_CUTOFF) for(int i=lo; i < hi; i++) if (arr[i] > ans) ans = arr[i]; else { MaxThread left = new MaxThread(arr,lo,(hi+lo)/2); MaxThread right= new MaxThread(arr,(hi+lo)/2,hi); left.run(); right.run(); ans = Math.max(left.ans, right.ans); int max(int[] arr){ MaxThread t = new MaxThread(arr,0,arr.length); t.run(); return t.ans; The current code is entirely sequential because a separate thread of execution is never created (i.e. start() is never called). Replace code in else block with: MaxThread left = new MaxThread(arr,lo,(hi+lo)/2); MaxThread right= new MaxThread(arr,(hi+lo)/2,hi); left.start(); right.run(); left.join(); ans = Math.max(left.ans, right.ans);

3. (8 points) You are at a summer internship working on a program that currently takes 12 minutes to run on a 2-processor machine. Two-thirds of the execution time (8 minutes) is spent running sequential code on one processor and the other third is spent running parallel code on both processors. Assume the parallel code enjoys perfectly linear speedup for any number of processors. Note/hint/warning: This does not mean two-thirds of the work is sequential. Two-thirds of the running time is spent on sequential code. Your manager has a budget of $2,000 to speed up the program. She figures that is enough money to do only one of the following: Buy a 8-processor machine. Hire a CSE373 graduate to parallelize more of the program under the highly dubious assumptions that: Doing so introduces no additional overhead Afterwards, there will be 1 minute of sequential work to do and the rest will enjoy perfect linear speedup. Which approach will produce a faster program? Show your calculations, including the total work done by the program and the expected running time for both approaches. This is an application of Amdahl s Law. Let S be the running time for the sequential portion, L be the running time for the parallel portion on 1 processor and P be the number of processors. Then T p = S + L/P. Initially T P is 12 minutes, S is 8 minutes and P = 2, which means L is 8 minutes. So the total work is 8 + 8 = 16 minutes. Therefore, T 8 = 8 + 8/8 is 9 minutes and that s the buy an 8-processor option. Under the hire an intern option S becomes 1 and L becomes 15 minutes. So the total time is 1+15/2 which is 8.5 minutes. So the better choice is to hire the intern. Hurrah for interns!

4. (20 points) Below are five computational tasks. For each one, choose the data structure or abstract data type you feel best suited to the task from the following list: Stack, Queue, Hashtable, AVL Tree, Priority Queue, Union-Find. In addition, list the basic operations for the data structure or ADT that you chose and give asymptotic worst-case running times for those operations. No additional explanation is required. (a) While processing a list of objects, check if you have processed a particular object before. (b) Store a list of students and their grades. You must also provide an efficient way for a client to see all students sorted in alphabetical order by name. Give the running time for this operation as well. (c) Process a digital image to divide the image up into groups of pixels of the same color. (d) Compute a frequency analysis on a file. That is, count the number of times each character occurs in the file, and store the results. (e) Store the activation records (i.e. objects containing the return address and local variable associated with a function call) for nested function calls.

(a) Hashtable insert, lookup, and remove all have a O(1) running times (credit also given for O(n), which accounts for worst-case hash collisions) (b) AVL Tree insert, find, and remove all have O(log n) running times. Getting all students in sorted order take O(n) time (doing an in-order traversal). (c) Union-Find Find has a O(log n) running time (credit also given for O(log n) or almost O(1)). Union has a O(1) running time. (d) Hashtable insert, lookup, and remove all have a O(1) running times (credit also given for O(n), which accounts for worst-case hash collisions) (e) Stack Pop has a O(1) running time and push has a O(n) running time (if array-based) (or a O(1) amortized running time) or a O(1) running time (if linked-list based).

5. (8 points) Suppose we use radix sort to sort the numbers below, using a radix of 10. Show the state of the sort after each of the first two passes, not after the sorting is complete. If multiple numbers are in a bucket, put the numbers that come first closer to the top. Numbers to sort (in their initial order): 17, 45, 877, 31, 7, 222, 42, 43, 301, 2525, 9, 27, 64 Put the result after the first pass here: 0 1 2 3 4 5 6 7 8 9 Put the result after the second pass here: 0 1 2 3 4 5 6 7 8 9 After first pass: bucket: 0 1 2 3 4 5 6 7 8 9 contents: 31 222 43 64 45 17 9 301 42 2525 877 7 27 After second pass: bucket: 0 1 2 3 4 5 6 7 8 9 contents: 301 17 222 31 42 64 877 7 2525 43 9 27 45

6. (8 points) For each of the following situations, name the best sorting algorithm from among those we studied. There may be more than one answer deserving full credit, but you only need to give one answer for each. (a) You need a very fast sort on average, and you can only use a constant amount of extra space. (b) The array is in perfect sorted order. (c) You have a large data set, but you know all the values are between 0 and 999. (d) Copying your data is very fast, but comparisons are relatively slow. Please provide an example of each of the following (or say NONE if no example exists) from among the sorting algorithms we studied. (e) A stable comparison sort with a worst-case O(n 2 ) running time (f) A stable comparison sort with a worst-case O(n) running time (g) An efficient (i.e. O(n log n)) stable comparison sort (h) A stable sort with a worst-case running time linear in n (a) Quicksort (b) Insertion sort (c) Radix sort or bucket sort (d) Merge sort (e) Insertion sort (f) NONE (g) Merge sort (h) Radix sort or bucket sort

7. (16 points) Below are four graph-based computational tasks. For each one, specify the type of graph most appropriate for the data in question in terms of undirected or directed, and unweighted or weighted. In addition, choose the graph algorithm from the following list best suited to computing a solution: Breadth-First Search, Minimum Spanning Tree (e.g. Prim s or Kruskal s), Dijkstra s Algorithm, Topological Sort, Depth-First Search. No additional explanation is required. (a) You have data for the rail transport (i.e. train track) network in North America, including the length of each section of track. You need to compute the shortest (in terms of distance) route for a train traveling from New York City to Atlanta, GA. (b) You need to lay water lines for a new housing development (i.e. pipes to carry water to and from each house). You have data on all the places it s possible to build pipes, and how much pipe would be required in each case. From the many potential pipeline options, you must compute which pipes to actually build such that you use the least amount of pipe overall. (c) You are compiling a program from source code and you know the dependencies between source files (i.e. for each file F, you know which other files, if any, can t be compiled until F is compiled). You need to compute a valid compilation order such that each file is compiled after all of its dependencies are compiled. (d) You have data about friend relationships from a social network, and you want to model the spread of a rumor based on these relationships. Your model is that a rumor starts with a single person, who then tells all of their friends. Then at each subsequent step, everyone who knows about the rumor spreads it to all of their friends who don t know it. You want to compute how many people know about the rumor after k steps. (a) Undirected, weighted Dijkstra s algorithm (b) Directed, weighted Minimum spanning tree (c) Directed, unweighted Topological sort (d) Undirected, unweighted Breadth-first search for k steps or depth-first search to a depth of k

8. (8 points) (a) Why might it be faster to access all the elements of an array-based queue than to access all the elements of a linked-list-based queue? i. temporal locality ii. spatial locality iii. it is faster asymptotically iv. none of the above (b) Why might the second version of f be faster than the first? public void f(string[] strings) { for (int i = 0; i < strings.length; i++) { strings[i] = strings[i].trim(); // omits trailing/leading whitespace for (int i = 0; i < strings.length; i++) { strings[i] = strings[i].touppercase(); public void f(string[] strings) { for (int i = 0; i < strings.length; i++) { strings[i] = strings[i].trim(); // omits trailing/leading whitespace strings[i] = strings[i].touppercase(); i. temporal locality ii. spatial locality iii. it is faster asymptotically iv. none of the above (c) In any graph, why might checking if an edge exists be faster when using an adjacency matrix to represent the graph instead of an adjacency list? i. temporal locality ii. spatial locality iii. it is faster asymptotically iv. none of the above (d) In a dense graph, why might finding all the vertices adjacent to a particular vertex be faster when using an adjacency matrix to represent the graph instead of an adjacency list? i. temporal locality ii. spatial locality iii. it is faster asymptotically iv. none of the above (a) i. spatial locality, (b) ii. temporal locality, (c) iii. it is faster asymptotically, (d) ii. spatial locality

9. (7 points) (a) A data race is situation where the behavior of an application depends on the sequence or timing of parallel threads. That is, the application behavior may depend on which thread gets there first. Which of the Java Thread class methods can be used to help prevent data races? i. run ii. join iii. hashcode iv. start (b) For each of the following, indicate whether it can be computed with: A parallel map operation A parallel reduce operation Neither i. Increment each integer in an array. ii. Compute the mode (i.e. the most frequently occurring value) of an array of integers. iii. Convert an array of strings to an array of integers by parsing each string as an integer. iv. Compute the average of an array of numbers. v. Find the minimum element in an array of numbers. (a) ii. join (b) i. map ii. neither iii. map iv. reduce v. reduce

10. (9 points) (a) Which of the following would generally be the best choice for computing the hashcode of an object with multiple fields? i. Use the product of the hashcodes for each field. ii. Take the maximum of the hashcodes for all the fields and multiply it by a large prime number. iii. Sum the hashcodes for all the fields, multiplying by a prime number before each addition. iv. Randomly choose a field and use its hashcode. (b) Java expects a particular relationship between the equals and hashcode methods of any object. What is that relationship? (c) Briefly describe a good approach for generating a hashcode from a string. (d) Why is quicksort not a good choice for an external sorting algorithm given data stored on traditional hard drives? (e) Suppose we can randomize the order of an array of elements in O(n) time. For which sorting algorithms (if any) could it be worthwhile to randomize the input array before performing the sort? (a) iii. Sum the hashcodes for all the fields, multiplying by a prime number before each addition. (b) if a.equals(b) then a.hashcode() == b.hashcode(). (c) Sum integer values corresponding to each character, multiplying by a prime before every addition. (d) Quicksort requires lots of non-sequential accesses to the input array (i.e. jumps around the array), and on traditional hard drives non-sequential access is much slower than sequential access. (e) Quicksort