Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

Similar documents
Quiz 1 Practice Problems

Quiz 1 Practice Problems

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

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

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

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

CS:3330 (22c:31) Algorithms

CSE 101, Winter Discussion Section Week 1. January 8 - January 15

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Section 05: Solutions

TREES AND ORDERS OF GROWTH 7

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

Data Structures Question Bank Multiple Choice

Chapter 2: Complexity Analysis

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

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

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

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 S-02 Algorithm Analysis 1

1. Attempt any three of the following: 15

ECE250: Algorithms and Data Structures Midterm Review

CSE Winter 2015 Quiz 1 Solutions

CS 3114 Data Structures and Algorithms READ THIS NOW!

CSL 201 Data Structures Mid-Semester Exam minutes

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Section 05: Solutions

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

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

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Final Exam. Problem Parts Points Grade Grader Problem Parts Points Grade Grader Total 180. Name: Athena username: Joe WF3a. Joe WF2.

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;

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

Midterm solutions. n f 3 (n) = 3

Practice Midterm Exam Solutions

COMP Analysis of Algorithms & Data Structures

Lecture Summary CSC 263H. August 5, 2016

CSE 373 Spring Midterm. Friday April 21st

Section 05: Midterm Review

DDS Dynamic Search Trees

Math 501 Solutions to Homework Assignment 10

Data Structures and Algorithms

INSTITUTE OF AERONAUTICAL ENGINEERING

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

COS 226 Fall 2015 Midterm Exam pts.; 60 minutes; 8 Qs; 15 pgs :00 p.m. Name:

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Data Structures and Algorithms

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

Second Examination Solution

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

Introduction to Algorithms December 16, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas

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

Design and Analysis of Algorithms - - Assessment

Complexity of Algorithms

Sample Exam 1 Questions

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

Lecture 5: Running Time Evaluation

The growth of functions. (Chapter 3)

CS 171: Introduction to Computer Science II. Binary Search Trees

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

CSE wi: Practice Midterm

CS 303 Design and Analysis of Algorithms

DATA STRUCTURES AND ALGORITHMS

CS102 Binary Search Trees

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

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

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

CMSC351 - Fall 2014, Homework #2

Friday Four Square! 4:15PM, Outside Gates

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

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

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Sample Solutions CSC 263H. June 9, 2016

Basic Data Structures (Version 7) Name:

9/24/ Hash functions

Final Examination CSE 100 UCSD (Practice)

Complexity of Algorithms. Andreas Klappenecker

Recitation 9. Prelim Review

Course Review. Cpt S 223 Fall 2009

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Ceng 111 Fall 2015 Week 12b

Data Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA === Homework submission instructions ===

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Union-Find and Amortization

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

University of Waterloo CS240R Fall 2017 Review Problems

Prelim 2, CS2110. SOLUTION

Data Structures and Algorithms. Part 2

Spring 2013 CSE Qualifying Exam Core Subjects. March 23, 2013

A* Optimality CS4804

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CSE 331 Introduction to Algorithm Analysis and Design. Sample Mid-term Exam-I: Fall 2018

Multiple-choice (35 pt.)

CSCI Analysis of Algorithms I

Lecture Notes for Advanced Algorithms

Transcription:

Introduction to Algorithms October 17, 2007 Massachusetts Institute of Technology 6.006 Fall 2007 Professors Ron Rivest and Srini Devadas Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic Notation [25 points] (5 parts) State whether each statement below is True or False. You must briefly justify all your answers to receive full credit. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) True. Θ is transitive. (b) If f(n) = O(g(n)) and g(n) = O(h(n)), then h(n) = Ω(f(n)) True. O is transitive, and h(n) = Ω(f(n)) is the same as f(n) = O(h(n)) (c) If f(n) = O(g(n)) and g(n) = O(f(n)) then f(n) = g(n) False: f(n) = n and g(n) = n + 1. (d) n 100 = Ω(n) True. n 100 < c n for c = 1 200.

6.006 Quiz 1 Solutions Name 2 (e) f(n) = Θ(n 2 ), where f(n) is defined to be the running time of the program A(n): def A(n): atuple = tuple(range(0, n)) # a tuple is an immutable version of a # list, so we can hash it S = set() for i in range(0, n): for j in range(i+1, n): S.add(atuple[i:j]) # add tuple (i,...,j-1) to set S False: Inside the two for loops, both slicing and hashing take linear time.

6.006 Quiz 1 Solutions Name 3 Problem 2. Weight-Balanced Binary Search Trees [20 points] (2 parts) Recall from class our definition of a weight-balanced binary search tree: it maintains the weight balance of each node x, such that if the weight of x is w(x), the weight of each child is at least α w(x), where α = 0.29. (The weight of x is one more than the number of nodes in the subtree rooted at x.) This property is maintained when a node is inserted by performing rotations and double rotations going up the insertion path to fix any nodes that are not weight balanced. Let T be an arbitrary weight-balanced tree with n nodes, for some n 1. (a) Show that there is a leaf node in T that could be removed without unbalancing any subtrees of T (including T itself). Walk down T from the root, choosing the larger subtree at each step, or choosing one at random if the subtrees are equal in size, until a leaf is reached. Then that leaf can be removed. No nodes will become unbalanced: if the subtrees differed in weight, they became more balanced (α moves toward 0.5), and if they had the same weight k, the new balance is α = k 1 1/3 > 0.29, since k 2. 2k 1 (b) Argue that T could have been created by a sequence of n insertions in such a way that no rotations were ever performed as T was built; i.e. that the growing tree was always weight-balanced. Remove the nodes from T, one at a time. Since a leaf can always be removed from T (as long as it is nonempty), leaving another weight-balanced BST, we can repeat this process until T is empty. After each step, note that we can re-add the removed node, and no rotations need to be performed. Then, just insert the nodes into an empty weight-balanced BST in reverse order from the removals. Note that this problem asks for a reconstruction of the exact tree T, not just any weightbalanced BST on the same keys.

6.006 Quiz 1 Solutions Name 4 Problem 3. Linked List Equivalence [15 points] (1 parts) Let S and T be two sets of numbers, represented as unordered linked lists of distinct numbers. All you have are pointers to the heads of the lists, but you do not know the list lengths. Describe an O(min { S, T })-expected-time algorithm to determine whether S = T. You may assume that any operation on one or two numbers can be performed in constant time. First, check that both sets are the same size. If they are not, then they cannot be equal. To do this check in O(min { S, T }) time, just iterate over both lists in parallel. That is, advance one step in S and one step in T. If both lists end, the lengths are the same. If one list ends before the other, they have different lengths. If both lists are the same size, then we want to check whether the elements are the same. We create a hash table of size Θ( S ) using universal hashing with chaining. We iterate over S, adding each element from S to the hash table. Then we iterate over T. For each element x T, we check whether x belongs to the hash table (that is, whether it is also in S). If not, then we return that the sets are not identical. If so, then continue iterating over T. Any sequence of S = T Insert and Search operations in the table take O( S ) time in expectation (see CLRS p.234), so the total runtime is O(min { S, T }) in expectation.

6.006 Quiz 1 Solutions Name 5 Problem 4. Hash Table Analysis [15 points] (2 parts) You are given a hash table with n keys and m slots, with the simple uniform hashing assumption (each key is equally likely to be hashed into each slot). Collisions are resolved by chaining. (a) What is the probability that the first slot ends up empty? Independently, each key has a 1/m probability of hashing into the first slot, or (m 1)/m probability of not hashing into the first slot. Thus, the probability that no key hashes into the first slot is ( ) n m 1. m (b) What is the expected number of slots that end up not being empty? Let X i be the event that slot i is nonempty. Since X i = 1 when slot i is nonempty and is 0 otherwise, E[X i ] = Pr(X i = 1) is the probability of slot i being nonempty. The number of nonempty slots is X i. By linearity of expectation, the expected number of nonempty slots is E[ X i ] = E[X i ]. From part (a), the probability that the first slot is nonempty, or E[X 1 ], is then 1 )n, and is the same for all slots. Thus, the expected number of nonempty slots is ( m 1 m ( m 1 ( m 1 m ) n ).

6.006 Quiz 1 Solutions Name 6 Problem 5. Dynamic Programming [25 points] (1 parts) You are given a sequence of n numbers (positive or negative): x 1, x 2,..., x n Your job is to select a subset of these numbers of maximum total sum, subject to the constraint that you can t select two elements that are adjacent (that is, if you pick x i then you cannot pick either x i 1 or x i+1 ). Explain how you can find, in time polynomial in n, the subset of maximum total sum. constraint. Let sum i be the maximum sum of the numbers x 1, x 2,..., x i given the adjacency sum 0 = 0 sum 1 = max(0, x 1 ) sum i = max(sum i 2 + x i, sum i 1 ) This last step works because either we include x i, in which case we also want to include the best solution on up to i 2, or we don t include x i, in which case we can just use the best solution on i 1. Our final answer is then just sum n. To calculate the set that gives the max sum, we could simply keep pointers back from i to either i 1 or i 2 depending on which one was bigger (or we could go back and check which was bigger). We follow those pointers, including appropriate numbers.