CSE 21 Practice Exam for Midterm 2 Fall 2017

Similar documents
Binomial Coefficient Identities/Complements

Analysis of Algorithms CMPSC 565

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CPSC 320 Sample Final Examination December 2013

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Lecture 4. Quicksort

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Central Algorithmic Techniques. Iterative Algorithms

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Recursion: Introduction and Correctness

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Probability (Devore Chapter Two)

Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109. Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101

Lecture 5: Loop Invariants and Insertion-sort

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Week 5: Quicksort, Lower bound, Greedy

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

CIS 2033 Lecture 5, Fall

N/4 + N/2 + N = 2N 2.

COMP 250 Fall Midterm examination

F 99 Final Quiz Solutions

Lecture 3. Big-O notation, more recurrences!!

An analogy from Calculus: limits

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Fundamental Algorithms

Fundamental Algorithms

COS 341: Discrete Mathematics

STAT 201 Chapter 5. Probability

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 16 Notes. Class URL:

Lecture 3: Probability

Data Structures and Algorithms CSE 465

Why should you care?? Intellectual curiosity. Gambling. Mathematically the same as the ESP decision problem we discussed in Week 4.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

ICS141: Discrete Mathematics for Computer Science I

5 ProbabilisticAnalysisandRandomized Algorithms

With Question/Answer Animations. Chapter 7

(2) Generalize De Morgan s laws for n sets and prove the laws by induction. 1

Chapter 8 Sequences, Series, and Probability

MATH 243E Test #3 Solutions

Introduction to Probability 2017/18 Supplementary Problems

Chapter Generating Functions

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

STA Module 4 Probability Concepts. Rev.F08 1

Randomization in Algorithms

9/6. Grades. Exam. Card Game. Homework/quizzes: 15% (two lowest scores dropped) Midterms: 25% each Final Exam: 35%

Math 140 Introductory Statistics

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Note 14

COMP Analysis of Algorithms & Data Structures

MATH STUDENT BOOK. 12th Grade Unit 9

Time Analysis of Sorting and Searching Algorithms

1 Terminology and setup

MATH 3C: MIDTERM 1 REVIEW. 1. Counting

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

COMP Analysis of Algorithms & Data Structures

1 Divide and Conquer (September 3)

Linear Selection and Linear Sorting

CS 361, Lecture 14. Outline

Probability Notes (A) , Fall 2010

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CS173 Running Time and Big-O. Tandy Warnow

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

CS280, Spring 2004: Final

Recitation 6. Randomization. 6.1 Announcements. RandomLab has been released, and is due Monday, October 2. It s worth 100 points.

Math 140 Introductory Statistics

CS 361: Probability & Statistics

Discrete Structures Prelim 1 Selected problems from past exams

Lecture 12 : Recurrences DRAFT

Lecture Slides. Elementary Statistics Tenth Edition. by Mario F. Triola. and the Triola Statistics Series. Slide 1

Lecture 2: Asymptotic Notation CSCI Algorithms I

HW2 Solutions, for MATH441, STAT461, STAT561, due September 9th

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

CS 161 Summer 2009 Homework #2 Sample Solutions

Loop Invariants and Binary Search. Chapter 4.4, 5.1

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Note 10

Probability, Random Processes and Inference

CSE548, AMS542: Analysis of Algorithms, Spring 2014 Date: May 12. Final In-Class Exam. ( 2:35 PM 3:50 PM : 75 Minutes )

CSC Discrete Math I, Spring Discrete Probability

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Discrete Structures for Computer Science

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Notes. Combinatorics. Combinatorics II. Notes. Notes. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

Review Of Topics. Review: Induction

Intro to Stats Lecture 11

Searching. Sorting. Lambdas

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Analysis of Algorithms. Randomizing Quicksort

Discrete Probability

Chapter 15. Probability Rules! Copyright 2012, 2008, 2005 Pearson Education, Inc.

Unit 8: Statistics. SOL Review & SOL Test * Test: Unit 8 Statistics

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Announcements. Read Section 2.1 (Sets), 2.2 (Set Operations) and 5.1 (Mathematical Induction) Existence Proofs. Non-constructive

Lecture Slides. Elementary Statistics Eleventh Edition. by Mario F. Triola. and the Triola Statistics Series 4.1-1

Probability- describes the pattern of chance outcomes

Transcription:

CSE 1 Practice Exam for Midterm Fall 017 These practice problems should help prepare you for the second midterm, which is on monday, November 11 This is longer than the actual exam will be, but good practice for the exam 1 Sorting and Searching Give the number of comparisons that will be performed by each sorting algorithm if the input array of length n happens to be of the form [1,,, n 3, n, n, n 1] (ie, sorted except for the last two elements) Note: On the real exam, you would be given pseudocode for any algorithms you are asked to analyze, though it is a very good idea to be comfortable with how these algorithms work to save time on the exam For now, you can refer to the textbook and/or lecture slides for pseudocode (a) MinSort (SelectionSort) (b) BubbleSort (c) InsertionSort (a) MinSort (SelectionSort) Solution: n(n 1) MinSort does the same amount of work on all inputs It does n 1 comparisons to find the minimum of the list before swapping it to the front Then it does n comparisons on the next pass to find the minimum of the remaining elements, and so on, until it eventually does 1 comparison to find the minimum of the last remaining two elements The total number of comparisons is 1++ +(n 1) (b) BubbleSort Solution: n(n 1) BubbleSort does the same amount of work on all inputs It does n 1 comparisons on the first pass, then n comparisons on the next pass, and so on The total number of comparisons is 1 + + + (n 1) (c) InsertionSort Solution: n +n 4 On this input, InsertionSort will do two comparisons when j = (comparing a with a 1 and then with itself) It will then do three comparisons when j = 3, and so on, up to n 1 comparisons when j = n 1 This is because each new number a j being inserted into the sorted list is greater than all the numbers that have been inserted before it, and InsertionSort compares from the beginning of the list each time Finally, when j = n, InsertionSort will do n 1 comparisons since a n is bigger than all but one of the elements that have been inserted before it It takes n 1 comparisons (comparing a n with all the other elements except itself) in order to determine to place a n in the second to last position The total number of comparisons is + 3 + + (n 1) + (n 1) We can simplify this sum to n +n 4 Note that this is just one less comparison than the worst case of InsertionSort, which would be if all the list elements were in sorted order The analysis for the worst case of InsertionSort is done in Example 6 on page of Rosen Best and Worst Case Suppose we are adding two n-digit integers, using the usual algorithm learned in grade school The primary operation here is the number of single-digit additions that must be performed For example, to add 48 plus 34, we would do three single-digit additions: 1 In the ones place, add 8 + 4 = 1 In the tens place, add 4 + 3 = 7 3 In the tens place, add 7 + 1 = 8 (a) If n = 5, give an example of two n-digit numbers that would be a best-case input to the addition algorithm, in the sense that they would cause the fewest single-digit additions possible Solution: One example is 11111 plus 1

(b) In the best case, how many single-digit additions does this algorithm make when adding two n-digit numbers? Solution: In each column, there is at least one addition, and the best case is when there is only this one addition (no carrying) Therefore the total number of single-digit additions could be as low as the number of columns, or n (c) In the best case, when adding two n-digit numbers, describe the number of single-digit additions in Θ notation Solution: n is Θ(n), so the best case is linear in the number of digits (d) If n = 5, give an example of two n-digit numbers that would be a worst-case input to the addition algorithm, in the sense that they would cause the most single-digit additions possible Solution: One example is 99999 plus 88888 (e) In the worst case, how many single-digit additions does this algorithm make when adding two n-digit numbers? Solution: In the rightmost column, there are two numbers to add, so that is one single-digit addition In all of the other n 1 columns, there could be 3 numbers to add (from carrying), so that is two single-digit additions each Therefore the total number of single-digit additions could be as high as 1 + (n 1) = n 1 (f) In the worst case, when adding two n-digit numbers, describe the number of single-digit additions in Θ notation Solution: n 1 is Θ(n), so the worst case is linear in the number of digits 3 Iterative Algorithms and Loop Invariants In the following problem, we are given a list A = a 1,, a n of salaries of employees at our firm and two integers L and H with 0 L H We wish to compute the average salary of employees who earn between L and H (inclusive), and the number of such employees If there are no employees in the range, we say that 0 is the average salary This is an iterative algorithm which takes as input A, L, and H and returns an ordered pair (avg, N) where avg is the average salary of employees in the range, and N is the number of employees in the range AverageInRange(A : list of n integers, L, H : integers with 0 L H) 1 sum := 0 N := 0 3 for i := 1 to n 4 if L a i H then 5 sum := sum + a i 6 N + + 7 if N = 0 then 8 return (0, 0) 9 return (sum/n, N) (a) State a loop invariant that can be used to show the algorithm AverageInRange is correct Solution: After t times through the for loop, sum is the total salary of those people among a 1,, a t who make between L and H, and N is the number of such people (b) Prove your loop invariant from part (a) Solution: For the base case, let t = 0 Before the loop starts, sum and N are both 0 This is correct because there are no people among a 1,, a t, as it is the empty set, so N should be 0, and we said that when there are no employees in the range, their average salary is 0 Now suppose that after t times through the for loop, sum is the total salary of those people among a 1,, a t who make between L and H, and N is the number of such people On the t + 1st time through the loop, i = t + 1

Case 1: If a t+1 falls in the range from L to H, then we increment sum by a t+1 and N by 1 Thus, if the value of sum before this iteration was the total salary of those people among a 1,, a t who make between L and H, now it is the total salary of those people among a 1,, a t+1 who make between L and H Similarly, if N before this iteration counted the total number of people among a 1,, a t with salaries in the range of L to H and we increased it by one, now N counts the number of such people among a 1,, a t+1 Case : If a t+1 does not fall in the range of L to H, the algorithm does nothing In this case, the total salary of those people among a 1,, a t who make between L and H is the same as the total salary of those people among a 1,, a t+1 who make between L and H Similarly, the number of such people among a 1,, a t is the same as the number of such people among a 1,, a t+1 (c) Conclude from the loop invariant that the algorithm AverageInRange is correct Solution: Letting t = n in the loop invariant says that after the algorithm terminates, sum is the total salary of those people among a 1,, a n who make between L and H, and N is the number of such people The algorithm returns (sum/n, N), which is the correct average salary and number of people (d) Describe the running time of this algorithm in Θ notation, assuming that comparisons and arithmetic operations take constant time Justify your answer Solution: This is a linear time algorithm, Θ(n) The operations inside the for loop take constant time, and the for loop runs n times All other work outside the loop is also constant time 3

4 Recursive Algorithms In the following problem, we are given a list A = a 1,, a n of salaries of employees at our firm and two integers L and H with 0 L H We wish to compute the average salary of employees who earn between L and H (inclusive), and the number of such employees If there are no employees in the range, we say that 0 is the average salary This is a recursive algorithm which takes as input A, L, and H and returns an ordered pair (avg, N) where avg is the average salary of employees in the range, and N is the number of employees in the range RecAIR(A : list of n integers, L, H : integers with 0 L H) 1 if n = 0 then return (0, 0) 3 B := a 1, a,, a n 1 4 (avg, N) := RecAIR(B, L, H) 5 if L a n H then 6 return ((avg N + a n )/(N + 1), N + 1) 7 else 8 return (avg, N) (a) Prove by induction on n that for any input, the algorithm correctly returns the average salary and number of employees in the range Solution: For the base case, if n = 0, the algorithm returns 0 for the average and 0 for the number of employees in the range Both are correct, because we said that when there are no employees in the range, the average salary is 0 Now suppose the algorithm returns the correct average salary avg and number of employees N when there are n 1 employees When there are n employees, either the nth employee is in the salary range from L to H or not Case 1: If a n falls in the the salary range L to H, the algorithm returns the total number of employees in the range as N +1 Assuming by the inductive hypothesis that N of the first n 1 employees are in the range, then this is correct Also, the algorithm returns for the average (avg N + a n )/(N + 1) in this case Assuming by the inductive hypothesis that avg is the average salary of the first n 1 employees in the range, and N is the number of such employees, then the total salary of all of the first n employees who fall in the range is avg N + a n and the number of such employees is N + 1, so the average salary comes from dividing these numbers Case : If a n does not fall in the the salary range L to H, then the number of employees among the first n who are in the range is the same as the number of employees among the first n 1 who are in the range By the inductive hypothesis, we assume this number is N, which is what the algorithm returns in this case for the number Also, the algorithm returns for the average avg, which is also correct by the inductive hypothesis This is true because if a n is not in the range, the average salary among the first n 1 employees in the range is the same as the average salary among the first n employees in the range, and this number is avg (b) Write down a recurrence for the time taken by this algorithm, assuming that comparisons and arithmetic operations take constant time Assume also that removing an element from a list (line 3) takes constant time Solution: T (0) = c, T (n) = T (n 1) + d, where c and d are constants (c) Use your answer from part (b) to determine the running time of this algorithm in Θ notation Justify your answer mathematically Solution: We can do this by unraveling or by guess and check Let s do it by unraveling 4

T (n) = T (n 1) + d = T (n ) + d = T (n 3) + 3d = T (n k) + kd = T (0) + nd = c + nd We choose a value of k = n to end at the base case of T (0) The result is a linear function in n, c + dn, so the algorithm is Θ(n) Notice that it s the same order as the iterative version in the previous problem, so neither is substantially more efficient than the other (d) Write down a recurrence for the time taken by this algorithm, assuming that comparisons and arithmetic operations take constant time Assume now that removing an element from a list (line 3) takes linear time Solution: T (0) = c, T (n) = T (n 1) + dn, where c and d are constants (e) Use your answer from part (d) to determine the running time of this algorithm in Θ notation Justify your answer mathematically Solution: We can again do this by unraveling or by guess and check Let s do it by unraveling T (n) = T (n 1) + dn = T (n ) + d(n 1) + dn = T (n 3) + d(n ) + d(n 1) + dn = T (n k) + d(n k + 1) + d(n k + ) + + d(n 1) + dn = T (0) + d(1 + + + (n 1) + n) = c + dn(n + 1)/ 5 Probability We choose a value of k = n to end at the base case of T (0) The result is a quadratic function in n, since the higest power if we expand c + dn(n + 1)/ is n, so the algorithm is Θ(n ) (a) I have 10 shirts, 6 pairs of pants, and 3 jackets Every day I dress at random, picking one of each category What is the probability that today I am wearing at least one garment I was wearing yesterday? Solution:Let us instead compute the complement, since this is a question about whether there exists a match between today s clothing and yesterday s Let A be the event that I am wearing all different clothes from yesterday 5

We can see that P (A) = (9/10) (5/6) (/3) The reason for this is that today I can wear any of the other garments that I didn t wear yesterday Thus the probability of wearing at least one of the same garments as yesterday is 1 P (A) = 1 (9/10) (5/6) (/3) (b) A permutation of size n is a rearrangement of the numbers {1,,, n} in any order A rise in a permutation occurs when a larger number immediately follows a smaller one For example, if n = 5, the permutation 1 3 4 5 has three rises What is the expected number of rises in a permutation of size n? Solution: We want E[X] where X is the number of rises in the permutation However, the number of rises in the permutation is the total number of rises that start at each position So for i = 1 to n 1, define X i to be 1 if there is a rise starting at position i and 0 otherwise Thus, using linearity of expectation, E[X] = E[X 1 + X + + X n 1 ] = E[X 1 ] + E[X ] + + E[X n 1 ] If we think of a permutation as a random rearrangement of the numbers from 1 to n, no position in a permutation is more likely to have a rise than any other This means each X i has the same distribution and the same expected value With probability 1/ there is a rise at position i and with probability 1/ there is a fall, so E[X i ] = 1 1/ + 0 1/ = 1/ Thus, E[X] = (n 1) 1/ (c) There are n teams in a sports league Over the course of a season, each team plays every other team exactly once If the outcome of each game is a fair random coin flip, and there are no ties, what is the probability that some team wins all of its games? Solution: As there n teams in the league, each team plays n 1 games, so for a team to win all of its games it has to win all n 1 games Every game to be won has a probability of 1/ Define i events, E i is the event that team i is undefeated The event that some team is undefeated is E 1 +E + +E n, so we want P (E 1 +E + +E n ) But the events E i are disjoint subsets of the sample space since we can t have two undefeated teams, which means we can add their probabilities together and instead compute P (E 1 ) + P (E ) + + P (E n ), by the sum rule The probability that team i wins all of its games is P (E i ) = (1/) n 1 As there are n teams the total probability that some team is undefeated is n (1/) n 1 (d) Suppose that one person in 10,000 people has a rare genetic disease There is an excellent test for the disease; 999% of people with the disease test positive and only 00% who do not have the disease test positive What is the probability that someone who tests positive has the genetic disease? What is the probability that someone who tests negative does not have the disease? Solution: Define the event F to mean Has the disease and E to mean Tests positive Then F C is Doesn t have the disease and E C is Tests negative We are given that We will use Bayes Theorem, which says that P (F E) = P (E F ) = 0999 P (E C F ) = 0001 P (E F C ) = 0000 P (E C F C ) = 09998 P (F ) = 1/10000 = 00001 P (F C ) = 09999 P (E F )P (F ) P (E F )P (F ) + P (E F C )P (F C ) Plugging in the values above gives that the probability that someone who tests positive actually has the disease is 6

P (E F )P (F ) P (F E) = P (E F )P (F ) + P (E F C )P (F C ) 0999 00001 = 0999 00001 + 0000 09999 = 0333133533 Thus, there is approximately a one third chance of having the disease even if you test positive, since the disease is so rare Now if we complement each event in Bayes Theorem we get P (F C E C ) = P (E C F C )P (F C ) P (E C F C )P (F C ) + P (E C F )P (F ), which we can use to tell us the probability that someone who tests negative does not have the disease Plugging in the values above gives P (F C E C P (E C F C )P (F C ) ) = P (E C F C )P (F C ) + P (E C F )P (F ) 09998 09999 = 09998 09999 + 0001 00001 = 099999989997 Thus, it is almost certain that you don t have the disease if you test negative, since the disease is so rare 7