Multiple-choice (35 pt.)

Similar documents
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

Module 2: Classical Algorithm Design Techniques

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

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

CPSC 320 Midterm 2 Thursday March 13th, 2014

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Midterm 2. Replace by cover page.

CSci 231 Final Review

The divide and conquer strategy has three basic parts. For a given problem of size n,

Midterm solutions. n f 3 (n) = 3

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

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

CS240 Fall Mike Lam, Professor. Quick Sort

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Problem Set 6 Due: 11:59 Sunday, April 29

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Data Structures and Algorithms Week 4

Pseudo code of algorithms are to be read by.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

EECS 2011M: Fundamentals of Data Structures

Solutions to Midterm 2 - Monday, July 11th, 2009

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

CS521 \ Notes for the Final Exam

II (Sorting and) Order Statistics

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CS61BL. Lecture 5: Graphs Sorting

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Solutions for Algorithm Design Exercises and Tests

CISC 320 Midterm Exam

Lecture 5: Sorting Part A

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

Randomized Algorithms: Selection

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Divide-and-Conquer. Dr. Yingwu Zhu

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Exercise 1. D-ary Tree

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

CSC236 Week 5. Larry Zhang

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Data Structures and Algorithms Chapter 4

Classic Data Structures Introduction UNIT I

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

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

STA141C: Big Data & High Performance Statistical Computing

Sorting is a problem for which we can prove a non-trivial lower bound.

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

STA141C: Big Data & High Performance Statistical Computing

CS 561, Lecture 1. Jared Saia University of New Mexico

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

INSTITUTE OF AERONAUTICAL ENGINEERING

Examination Questions Midterm 2

4. Sorting and Order-Statistics

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

CPSC W2 Midterm #2 Sample Solutions

Lecture 2: Getting Started

Section 05: Solutions

Divide and Conquer 4-0

Final Exam in Algorithms and Data Structures 1 (1DL210)

1 Probabilistic analysis and randomized algorithms

COMP 251 Winter 2017 Online quizzes with answers

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

Test 1 Review Questions with Solutions

CS 411 Analysis of Algorithms, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, October 17, 2012.

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

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

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

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

Lower Bound on Comparison-based Sorting

Sample Solutions to Homework #2

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

University of Waterloo CS240, Winter 2010 Assignment 2

Questions Total Points Score

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

Elementary maths for GMT. Algorithm analysis Part II

UNIT 1 ANALYSIS OF ALGORITHMS

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

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

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

COT 3100 Spring 2010 Midterm 2

Algorithms and Data Structures

Quiz 1 Practice Problems

CSC 273 Data Structures

CSE wi: Practice Midterm

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Transcription:

CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need to be tight. Circle all that apply. (A) O(n 2 n) (B) O(n 3 ) (C) Ω(n 2 n) (D) Ω(n 3 ) (E) Θ(n 3 ). 2. (4 pt.) Consider the following iterative algorithm that sorts an input list A. def mystery(a): n = len(a) # Iterates through the last n-1 elements of the list for i in range(1, n): cur_value = A[i] j = i - 1 while j >= 0 and A[j] > cur_value: A[j+1] = A[j] j -= 1 A[j+1] = cur_value (a) Which of the following asymptotic bounds describes the best-case runtime of this algorithm (i.e. runtime of the algorithm on a best-case input). The bounds do not necessarily need to be tight. Circle all that apply. (A) O(n) (B) O(n 2 ) (C) Ω(n) (D) Ω(n 2 ) (E) Θ(n log(n)). (b) Which of the following asymptotic bounds describes a tight bound for the worst-case runtime of this algorithm (i.e. runtime of the algorithm on a worst-case input). Circle all that apply. (A) O(n) (B) O(n 2 ) (C) Ω(n) (D) Ω(n 2 ) (E) Θ(n log(n)). 1

3. (3 pt.) Consider the following recursive implementation of ternary search that splits the input list into thirds instead halves, like binary search. def ternary_search(a, needle): if len(a) == 0: raise Exception("needle does not exist!") n = len(a) one_third = n/3 two_thirds = 2*n/3 if A[one_third] == needle: return one_third elif A[two_thirds] == needle: return two_thirds elif A[one_third] > needle: return ternary_search(a[:one_third], needle) elif A[two_thirds] > needle: return one_third + 1 + ternary_search(a[one_third+1:two_thirds], needle) else: return two_thirds + 1 + ternary_search(a[two_thirds+1:], needle) Assuming the input list A can always evenly be divided into thirds, which of the following recurrence relations describes the worst-case runtime of this algorithm? (A) T (n) = T (n/3) + O(1) (B) T (n) = T (n/3) + O(log(n)) (C) T (n) = T (2n/3) + O(1) (D) T (n) = T (2n/3) + O(log(n)) (E) T (n) = T (n/3) + O(n) 4. (4 pt.) Consider the following recurrence relation: T (n) = 3T (n/3) + O(n 2 ). (a) According to Master method, which of the following describes a closed-form expression for T (n)? (A) O(n) (B) O(n log(n)) (C) O(n 2 ) (D) O(n 2 log(n)) (E) O(n 2 log 2 (n)). (b) Is the recursion tree resulting from this recurrence relation top-heavy, balanced, or bottom-heavy? (A) Top-heavy (B) Balanced (C) Bottom-heavy. 5. (4 pt.) Recall the recurrence relation for linear-time selection: T (n) = T (n/5) + T (7n/10) + Θ(n). Which of the following statements about the runtime of this algorithm are correct? Circle all that apply. (A) The T (n/5) term in the recurrence relation represents the time required to find the median-ofmedians. (B) The T (n/5) term in the recurrence relation represents the time required to find the k th -smallest element in the recursed-upon sublist. (C) The Θ(n) term in the recurrence relation represents the time required to find the median-ofmedians. (D) The recurrence relation can be solved with Master method for a = 2, b = 10/7, and d = 1. (E) The T (7n/10) term in the recurrence relation represents the time required to partition the list around the pivot. 2

6. (5 pt.) Consider the following algorithm that outputs a random number from 0 to n 1. def pick(n): A = random.shuffle(list(range(n))) max_so_far = -1 counter = 0 for a in A: if a > max_so_far: counter += 1 max_so_far = a return counter (a) What is the expected runtime of the algorithm? (A) Θ(log(n)) (B) Θ( n) (C) Θ(n) (D) Θ(n n) (E) Θ(n 2 ). (b) What is the expected value returned from the algorithm? (A) Θ(log(n)) (B) Θ( n) (C) Θ(n) (D) Θ(n n) (E) Θ(n 2 ). 7. (4 pt.) Consider a hash family consisting of a single hash function that hashes a universe U of whole numbers less than 1 million to one of n = 3 buckets according to: h(key) is the sum of the digits of the key mod 3. Which of the following must be true about this hash family? Circle all that apply. (A) This hash family is universal for U. (B) This hash family is universal for the following subset of U: {0, 1, 2, 10, 11, 12} (C) There exists a specific worst-case input of n = 3 elements from U, such that a randomly-drawn hash function from this hash family hashes all of the elements to the same bucket. (D) For a randomly selected hash function h from the hash family, we have that P (h(123) = h(321)) = 1. (E) For a randomly selected hash function h from the hash family, we have that P (h(1) = h(2)) = 1. 8. (3 pt.) Recall h a,b (x) = ax + b mod p mod n where a [1, p 1] and b [0, p 1] with prime p U describes a universal family of hash functions mapping a universe of keys U to values {1, 2,..., n}. Let s call this hash family H mod. For some u x, u y U, such that u x u y, the number of hash functions from this family for which u x = u y is at most: (A) U /n (B) n/ H mod (C) n/ U (D) 1/n (E) H mod /n. 3

9. (3 pt.) Consider the following binary search tree. A B E C D F Which of the following vertices must be black in any valid red-black coloring? Circle all that apply. (A) A (B) B (C) C (D) D (E) E (F ) F. 10. (3 pt.) You run dfs from A. Assuming a vertex s neighbors will be visited in alphabetical order, what will be the ordering of end times from lowest to highest. A B C D E (A) A, B, C, D, E (B) A, B, E, C, D (C) A, B, D, E, C (D) E, D, B, C, A (E) E, B, D, C, A 4

Short-answers (15 pt.) 1. (6 pt.) Using the definition of Big-Ω, formally prove the following statement: n is not Ω(n 2 ) [We are expecting: A short but rigorous proof, using the definition of Big-Ω.] 2. (9 pt.) Using substitution method, formally prove the recurrence relation T (n) = T (n/3) + T (2n/3) + O(n) evaluates to O(n log(n)). [We are expecting: A short but rigorous proof, using substitution method.] 5

Problems (50 pt.) 1. (Exponentiator) (12 pt.) The function exponentiator(x, n) takes as input positive integers x and n and returns the value x n. For example, given x = 4 and n = 3, your algorithm should return 4 3 = 64. (a) (6 pt.) Design an O(log(n))-time divide-and-conquer algorithm for exponentiator. Note that basic arithmetic operations (addition, subtraction, multiplication, division) require O(1)- time. [We are expecting: Pseudocode for the algorithm.] def exponentiator(x, n): (b) (3 pt.) Give the recurrence relation that describes the worst-case runtime of your algorithm. [We are expecting: A recurrence relation.] (c) (3 pt.) Prove that your algorithm takes O(log(n))-time. [We are expecting: A runtime analysis using recursion tree, substitution method, or Master method.] 6

2. (Majority Tree) (22 pt.) Consider a complete ternary tree T i.e. a tree comprised of two types of vertices: (1) internal (not-leaf) vertices with exactly three children and (2) leaf vertices distance h from the root, where h represents the height of the tree. Note that no vertices can be added to this tree without increasing its height. The function majority_tree_root_value(t, M) takes as input the complete ternary tree and a map M of its n = 3 h leaves mapped to n = 3 h boolean values, and returns the boolean value associated with the root. To determine the boolean value of a leaf v, index into the map M[v]. To determine the boolean value associated with an internal vertex or the root, take the majority of its children. 1 1 0 0 1 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 Given the boolean values specified for the top complete ternary tree above, the function should return 1 for the boolean value associated with the root. To determine the boolean value associated with the root, notice that 1 is the majority element of the root s children (its left and middle children are 1 while only its right child is 0), each of which was determined by taking the majority elements of their respective children. (a) (6 pt.) Design a divide-and-conquer algorithm that implements this function that always inspects all of the leaves exactly once (i.e. for each leaf, your algorithm should index into M exactly once). [We are expecting pseudocode.] 7

(b) (4 pt.) Sometimes it s possible to short-circuit recursive calls to avoid inspecting all of the leaves. For example, in the example tree, after obtaining the values for its left and middle children, the root need not recurse to its right child since its majority element is guaranteed to be 1. Design a divide-and-conquer algorithm that implements this short-circuiting. [We are expecting pseudocode.] (c) (6 pt.) Design a randomized algorithm that inspects O(n 0.9 ) leaves on worst-case inputs in expectation. [We are expecting pseudocode.] 8

(d) (2 pt.) Justify that the algorithm in part (c) inspects O(n 0.9 ) leaves in expectation. [We are expecting a convincing mathematical argument.] (e) (2 pt.) For any implementation of majority_tree_root_value, including your algorithms from parts (a)-(c), there exists a set of boolean values for the leaves (in part (c), this is not a deterministic input) that requires the algorithm to inspect the boolean values of all 3 h leaves. Why must this be the case? [We are expecting two to three sentences of explanation.] (f) (2 pt.) Describe the advantage of your randomized algorithm from part (c), compared to your divide-and-conquer algorithm from part (b). [We are expecting two to three sentences of explanation.] 9

3. (Detecting Odd Cycles) (16 pt.) An odd cycle of a graph is a cycle with an odd number of vertices. (a) (12 pt.) Design an O( V + E )-time algorithm that takes as input a connected undirected graph G and returns true if the graph contains an odd cycle and false otherwise. Given a vertex v, you can access its list of neighbors with v.neighbors in O(1)-time. You can also add meta-information to the vertices. [We are expecting: Either an English description or pseudocode of the algorithm.] def contains_odd_cycle(g): # You can call G.vertices to get the set of vertices; for each vertex v, # you can call v.neighbors to get its list of neighbors. (b) (4 pt.) Prove that your algorithm takes O( V + E )-time. [We are expecting: An English justification of why it takes O( V + E )-time.] 10