CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

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

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

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

CSE332 Summer 2010: Final Exam

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

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

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

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

CS171 Final Practice Exam

CS171 Final Practice Exam

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

CSE332, Spring 2012, Final Examination June 5, 2012

CSE332 Summer 2012 Final Exam, August 15, 2012

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

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

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

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

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

Final Examination CSE 100 UCSD (Practice)

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

CSE 332, Spring 2010, Final Examination 8 June 2010

CSE 373: Practice Final

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

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

COMP 251 Winter 2017 Online quizzes with answers

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

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

Chapter 9 Graph Algorithms

CS521 \ Notes for the Final Exam

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

Chapter 9 Graph Algorithms

1. AVL Trees (10 Points)

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Exam Sample Questions CS3212: Algorithms and Data Structures

Chapter 9 Graph Algorithms

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Course Review for Finals. Cpt S 223 Fall 2008

Data Structures Question Bank Multiple Choice

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

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

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

INSTITUTE OF AERONAUTICAL ENGINEERING

CISC 320 Midterm Exam

CSE332 Summer 2010: Midterm Exam Sample Solutions

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

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

Lecture Summary CSC 263H. August 5, 2016

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

CSE373 Spring 2015: Final

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

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

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

(a) Write code to do this without using any floating-point arithmetic. Efficiency is not a concern here.

CSE 373 Final Exam 3/14/06 Sample Solution

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

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

Solutions to Exam Data structures (X and NV)

COS 226 Midterm Exam, Spring 2009

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

Computer Science E-22 Practice Final Exam

Practice Midterm Exam Solutions

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

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. Final Examination

CS/ENGRD2110: Final Exam

CS301 - Data Structures Glossary By

AP Computer Science 4325

CS61BL. Lecture 5: Graphs Sorting

6.006 Final Exam Name 2. Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Data Structures Brett Bernstein

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

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Design and Analysis of Algorithms - - Assessment

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

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

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

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

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?

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

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

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Problem Score Maximum MC 34 (25/17) = 50 Total 100

UNIT III BALANCED SEARCH TREES AND INDEXING

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

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

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

Section 1: True / False (1 point each, 15 pts total)

Student number: Datenstrukturen & Algorithmen page 1

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

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

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

CSE 373: Data Structures and Algorithms

Transcription:

Name: Sample Solution Email address (UWNetID): CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We may give partial credit based on the work you write down, so show your work! Use only the data structures and algorithms we have discussed in class so far. Writing after time has been called will result in a loss of points on your exam. Note: For questions where you are drawing pictures, please circle your final answer. You have 1 hour and 50 minutes, work quickly and good luck! Total: Time: 1 hr and 50 minutes. Question Max Points 1 12 2 12 3 10 4 10 5 14 6 10 7 10 8 11 9 11 Total 100 Score

1) [12 points total] Hash Tables For a) and b) below, insert the following elements in this order: 50, 21, 29, 10, 39, 19. For each table, TableSize = 10, and you should use the primary hash function h(k) = k%10. If an item cannot be inserted into the table, please indicate this and continue inserting the remaining values. a) Quadratic probing hash table 0 50 1 21 2 3 39 4 10 5 6 7 8 19 9 29 b) Separate chaining hash table use a linked list for each bucket where the values are ordered by increasing value 0 1 2 3 4 5 6 7 8 9 c) What is the load factor in Table b)? 0.6 d) In a sentence or two, describe double hashing. The first hash function determines the original location where we should try to place the item. If there is a collision, then the second hash function is used to determine the probing step distance as 1*h2(key), 2*h2(key), 3*h2(key) etc. e) What is one advantage of double hashing over quadratic probing, be specific. Double hashing avoids secondary clustering that quadratic probing can have. Double hashing does not suffer from the case where quadratic probing may not find a viable bucket when the table is more than half full. f) What is the big-o worst case runtime of a find operation on a table like table b? O(N) g) What is the big-o worst case runtime of an Insert in a separate chaining hash table containing N elements where each bucket points to an AVL tree? O(log N) 2 of 12

2) [12 points total] Graphs! a) [2 points] What is the big-o running time of Dijkstra s algorithm (assuming an adjacency list representation) if: (i) A priority queue is used? O(E log V + Vlog V) or just O(E log V) (ii) An unsorted list is used? O(EV + V 2 ) b) [2 points] Which implementation of Dijkstra s (priority queue vs. unsorted list) is likely to be faster if the graph is known to be dense? Explain your answer in ~one sentence for any credit. Priority Queue is better than unsorted list regardless of whether the graph is dense or sparse. O(log V) is always better than O(V) whether E is close to V or V 2. c) [2 points] Give a Minimum Spanning Tree (MST) of the graph below by highlighting the edges that would be part of the MST. 2 C A 3 d) [6 points] For (ii) and (iv) below, you are given a perfect binary tree of height h containing n nodes. Your answer should be an exact formula, such as 3/2 log h, or 5 3 n, not big-o notation. (i) Depth First Search: What is the name of the data structure used in DFS? stack (ii) What is the maximum size of that data structure during a DFS? h + 1 1 4 F 3 2 D B 4 5 1 2 G E 2 1 (iii)breadth First Search: What is the name of the data structure used in BFS? queue (iv) What is the maximum size of that data structure during a BFS? 2 h 3 of 12

3) [10 points total] More Graphs! a) [4 points] Draw a picture of a connected directed graph with 5 nodes that has the largest possible number of topological sorts. How many different topological sorts does it have? Either 4 nodes all pointing to one or one node pointing to 4 nodes How Many topo sorts? 4! Or 24 sorts b) [6 points total] Given a weighted, undirected graph with V nodes, answer the following. Assume all weights are non-negative. (i) [2 points] If each edge has weight w, what can you say about the cost of an MST? Your answer should give a lower bound, or an upper bound on the cost of the MST, e.g. the cost of the MST is 2 w+n, or the cost of the MST is log(w log n). The cost of the MST is ( V - 1)w (since there are V - 1 edges in an MST, and each can have weight w) (ii) [2 points] If the cost of an MST is c, what can you say about the shortest distances returned by Dijkstra s algorithm when run with an arbitrary vertex s as the source? You should give a lower bound or an upper bound for the distance between arbitrary vertices u and v. The shortest distance between any two points c, since any two points are connected in the MST (which is undirected) (iii)[2 points] If there exists a Hamiltonian circuit of cost c, then what can you say about the cost of the minimum spanning tree? The cost of the MST c, since we can remove one edge (which might have weight 0) from the Hamiltonian circuit to get a (not necessarily minimum) spanning tree. 4 of 12

4) [10 points] Parallel Prefix CountNegatives: a) Given the following array as input, perform the parallel prefix algorithm to fill the output array with the number of negative values contained in all of the cells to the left (including the value contained in that cell) in the input array. Fill in the values for negs, and fromleft in the tree below. Do not use a sequential cutoff. Note: This is NOT sum! lo:0 hi:8 negs:5 fromleft:0 lo:0 hi:4 negs:3 fromleft:0 lo:4 hi:8 negs:2 fromleft:3 lo:0 hi:2 negs:1 fromleft:0 lo:2 hi:4 negs:2 fromleft:1 lo:4 hi:6 negs:1 fromleft:3 lo:6 hi:8 negs:1 fromleft:4 l:0 h:1 ng:1 fl:0 l:1 h:2 ng:0 fl:1 l:2 h:3 ng:1 fl:1 l:3 h:4 ng:1 fl:2 l:4 h:5 ng:0 fl:3 l:5 h:6 ng:1 fl:3 l:6 h:7 ng:0 fl:4 l:7 h:8 ng:1 fl:4 Index 0 1 2 3 4 5 6 7 Input -3 5-4 -7 2-8 9-5 Output 1 1 2 3 3 4 4 5 b) How is the fromleft value computed for a node in the tree? Specifically, if you have a node with negs & fromleft computed, how do you compute fromleft for its left & right children (both of which have negs already computed). Left child s fromleft: Parent s fromleft (or fromleft, if viewed from parent) Right child s fromleft: Parent s fromleft + sibling s neg (or fromleft + left.negs, if viewed from parent) 5 of 12

5) [14 points] In Java using the ForkJoin Framework, write code to solve the following problem: Input: An int k, and an array of ints of size n containing values in the range 0 k-1 Output: the value (between 0 and k-1) occurring most often in the Input array. If there is a tie for the most frequently occurring value, return the smallest number. For example, if k=7 and input array of size 9 is {0, 2, 5, 2, 6, 5, 4, 5, 2, the output would be 2. Do not employ a sequential cut-off: the base case should process one element. (You can assume the input array will contain at least one element.) Assume k is small (e.g. less than 50) Fill in the function findmostcommon below. Give a class definition, FindComTask, on the next page. You may not use any global data structures or synchronization primitives (locks). import java.util.concurrent.forkjoinpool; import java.util.concurrent.recursivetask; class Main{ static final ForkJoinPool fjpool = new ForkJoinPool(); // Returns the most common value in the array input. // Values in input are in the range 0 to k. int static findmostcommon (int k, int[] input) { int[] counts = fjpool.invoke(new FindComTask(k, input, 0, input.length)); int maxcount = 0; int maxcountindex = 0; for (int i = 0; i < k; i++){ if (counts[i] > maxcount) { maxcount = counts[i]; maxcountindex = i; return maxcountindex; Please fill in the function above and write your class on the next page. 6 of 12

5) (Continued) Write your class on this page. public static class FindComTask extends RecursiveTask<int[]> { int k; int[] input; int lo; int hi; public FindComTask(int k, int[] input, int lo, int hi) { this.k = k; this.input = input; this.lo = lo; this.hi = hi; public int[] compute() { if (hi lo <= 1) { int[] counts = new int[k]; counts[input[lo]] = 1; return counts; else { int mid = lo + (hi lo)/2; FindComTask left = new FindComTask(k, input, lo, mid); FindComTask right = new FindComTask(k, input, mid, hi); right.fork(); int[] leftresult = left.compute(); int[] rightresult = right.join(); for (int i = 0; i < k; i++) { leftresult[i] += rightresult[i]; return leftresult; 7 of 12

6) [10 points] Concurrency: Once again, we are helping out with the Aviation Management System. This time they have a data structure that keeps track of whether there is a flight from one airport to another. If there is a flight from airport x to airport y, then we should be able to assume that there is also a flight from airport y to airport x. This information is being kept in an array (indexed by unique airport number), where each location in the array points to a list of the airport numbers that can be reached from this airport by a single flight (an AirportList). We would like to allow as much concurrent access to this data structure as possible, while assuring that each thread always sees a consistent state of the data structure. We attempt this by having a different lock on each of the AirportLists. a) Our first attempt at the removeroute method is below. Assume hasflightto and removeflight are methods on an AirportList. removeflight will throw an exception if the specified flight is not present. void removeroute1(int x, int y, AirportList[] airports) { synchronized(airports[x]) { synchronized(airports[y]) { if(airports[x].hasflightto(y)) { airports[x].removeflight(y); airports[y].removeflight(x); i. Does the code above have (circle all that apply): potential for deadlock, a data race, a race condition, none of these ii. If possible, show (as done in class) an interleaving of two or more threads calling removeroute1 that demonstrates a concurrency error. If not possible, explain why not. Thread 1: Thread 2: removeroute1(a, b, airports) { removeroute1(b, a, airports) { synchronized(airports[a]) { synchronized(airports[b]) { (waiting on airports[b]) (waiting on airports[a]) 8 of 12

6) (Continued) b) Our second attempt at the removeroute method is below. void removeroute2(int x, int y, AirportList[] airports) { synchronized(airports[x]) { if(!(airports[x].hasflightto(y))) { return; synchronized(airports[x]) { airports[x].removeflight(y); synchronized(airports[y]) { airports[y].removeflight(x); i. Does the code above have (circle all that apply): potential for deadlock, a data race, a race condition, none of these ii. If possible, show (as done in class) an interleaving of two or more threads calling removeroute2 that demonstrates a concurrency error. If not possible, explain why not. Thread 1: Thread 2: removeroute2(a, b, airports) { synchronized(airports[a]) { if(!(airports[a].hasflightto(b))) { return; removeroute2(b, a, airports) { // Entire method runs, removing // flight from b->a and a->b synchronized(airports[a]) { airports[a].removeflight(b); // THROWS AN EXCEPTION c) Finally, the developers consider scrapping the one-lock-per AirportList strategy in favor of a single lock, locking the entire array of AirportLists. One benefit of a single-lock locking all airports: Less risk of programmer creating a concurrency error, easier to implement. One drawback of a single-lock locking all airports: Less concurrency (no concurrency!) 9 of 12

7) [10 points] Sorting You are given a list of AVL trees. The keys in the AVL trees are ages of people. Each AVL tree represents the ages for people in a different community. Your task is to sort the AVL trees such that tree X comes before tree Y if and only if: The minimal age in tree X is less than the minimal age in tree Y, or The minimal ages are the same, but the maximal age in tree X is less than the maximal age in tree Y Otherwise, ties are broken arbitrarily. You may assume that: There are k trees Each tree has n keys in it The range of ages is fixed (0-127) a) [5 points] Describe in a few sentences or numbered steps how you could use Mergesort to sort these trees efficiently in the worst case. What is the running time in terms of k and n? 1. Find the minimum and maximum age for each tree. Time O(klogn), since each tree is balanced. 2. Perform a mergesort where the comparison is based on using the min element for each tree, with the max element as a tie breaker. Time is O(klogk). Total worst case time O(klogn + klogk) Note: Another way of doing this was to just find the min and max values when doing the comparison in mergesort which would be O(k lg k * lg n). Running Time: O(klogn + klogk) b) [5 points] Describe in a few sentences or numbered steps how you could use ideas from Radixsort to sort these trees efficiently in the worst case. What is the running time in terms of k and n? 1. Find the minimum and maximum as above. Worst case O(klogn). 2. Perform a BinSort using the maximum age of each tree (It is critical that you sort on the least significant the part I care about least, first!). Time O(k+128) = O(k) 3. BinSort on the minimum age of each tree. Time O(k). Total time O(k + klogn) = O(klogn). Running Time: O(klogn) 10 of 12

8) [11 points] More Sorting a) [3 points] Give the recurrence for Mergesort (parallel sort & sequential merge) best case span: (Note: We are NOT asking for the closed form.) Answer: T(N) = T(n/2) + O(n) b) [3 points] In the spaces below, order these sorts from slowest to fastest in terms of big-o runtimes. For parallel sorts, use the span. Draw a circle around any sorts whose big-o runtimes are the same. You do not have to give the runtimes, just list the sort letters. A) Mergesort (sequential) worst case B) Quicksort (parallel sort & parallel partition) best case span O(log 2 n) C) Quicksort (sequential) best case D) Quicksort (sequential) worst case O(n 2 ) E) Quicksort (parallel sort & parallel partition) worst case span D(n 2 ) ACE are ties at (n log n) Slowest B(log 2 n)_ Fastest c) [2 points] Suppose we choose the median of five items as the pivot in quicksort. If we have an N element array, then we find the median of the elements located at the following positions: left (= 0), right (= N 1), center (the average of left and right, rounded down), leftofcenter (the average of left and center, rounded down), and rightofcenter (the average of right and center, rounded down). The median of these elements is the pivot. What is the worst case running time of this version of quicksort? O(N 2 ) d) [1 point] Any algorithm for sorting must take (N log N) time in the worst-case. TRUE FALSE e) [2 points] What does it mean for a sort to be stable? In case of ties during the sort, the original ordering of values is preserved. 11 of 12

9) [11 points] P, NP, NP-Complete a) [2 points] NP stands for Non-deterministic polynomial b) [2 points] What does it mean for a problem to be in NP-complete? These problems are in NP (Given a candidate solution, we can verify whether the solution is correct in polynomial time.) and they are the hardest problems in NP. NPcomplete problems can be reduced to other NP-complete problems in poly-time. We are pretty sure these problems do not have polynomial time solutions. If any one NPcomplete problem could be solved in polynomial time, then all NP-complete problems could be solved in polynomial time. c) [5 points] For the following problems, circle ALL the sets they belong to: Determining if a chess move is the best move on an N x N board NP P NP-complete None of these Finding the maximum value in an array NP P NP-complete None of these Finding a cycle that visits each vertex in a graph exactly once NP P NP-complete None of these Finding a cycle that visits each edge in a graph exactly once NP P NP-complete None of these Determining if a program will ever stop running NP P NP-complete None of these d) [1 point] If there exists a polynomial time algorithm to solve Euler Circuit, then there exists a polynomial time algorithm to solve SAT. TRUE FALSE e) [1 point] If there exists a polynomial time algorithm to solve Hamiltonian Circuit then any problem in NP can be solved by some polynomial time algorithm. TRUE FALSE 12 of 12