Homework 1 Solutions MA 522 Fall 2017

Similar documents
Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Lecture 5. Counting Sort / Radix Sort

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

CS473-Algorithms I. Lecture 2. Asymptotic Notation. CS 473 Lecture 2 1

Algorithm. Counting Sort Analysis of Algorithms

Examples and Applications of Binary Search

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Algorithm Design Techniques. Divide and conquer Problem

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

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

Analysis of Algorithms

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CSE 417: Algorithms and Computational Complexity

Lecture 1: Introduction and Strassen s Algorithm

Merge Sort. Alexandra Stefan

Computational Geometry

6.854J / J Advanced Algorithms Fall 2008

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Order statistics. Order Statistics. Randomized divide-andconquer. Example. CS Spring 2006

How do we evaluate algorithms?

CMPT 125 Assignment 2 Solutions

Big-O Analysis. Asymptotics

Data Structures Week #9. Sorting

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Big-O Analysis. Asymptotics

Data Structures and Algorithms. Analysis of Algorithms

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Data Structures and Algorithms Part 1.4

Fundamental Algorithms

Analysis of Algorithms

Algorithms Chapter 3 Growth of Functions

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

2. ALGORITHM ANALYSIS

Alpha Individual Solutions MAΘ National Convention 2013

The isoperimetric problem on the hypercube

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

Module 8-7: Pascal s Triangle and the Binomial Theorem

Arithmetic Sequences

Section 7.2: Direction Fields and Euler s Methods

Random Graphs and Complex Networks T

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

UNIT 4C Iteration: Scalability & Big O. Efficiency

quality/quantity peak time/ratio

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Recursive Estimation

MR-2010I %MktBSize Macro 989. %MktBSize Macro

COMP Parallel Computing. PRAM (1): The PRAM model and complexity measures

1.2 Binomial Coefficients and Subsets

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Thompson s Group F (p + 1) is not Minimally Almost Convex

LU Decomposition Method

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CIS 121. Introduction to Trees

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

. Written in factored form it is easy to see that the roots are 2, 2, i,

Array Applications. Sorting. Want to put the contents of an array in order. Selection Sort Bubble Sort Insertion Sort. Quicksort Quickersort

Lecture 18. Optimization in n dimensions

Mathematical Stat I: solutions of homework 1

The Adjacency Matrix and The nth Eigenvalue

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Counting Regions in the Plane and More 1

Algorithm Efficiency

Pattern Recognition Systems Lab 1 Least Mean Squares

Protected points in ordered trees

New Results on Energy of Graphs of Small Order

Python Programming: An Introduction to Computer Science

Design and Analysis of Algorithms Notes

Inductive Definition to Recursive Function

Recursive Procedures. How can you model the relationship between consecutive terms of a sequence?

BOOLEAN MATHEMATICS: GENERAL THEORY

WebAssign Lesson 6-1b Geometric Series (Homework)

Parabolic Path to a Best Best-Fit Line:

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

The number n of subintervals times the length h of subintervals gives length of interval (b-a).

top() Applications of Stacks

1.8 What Comes Next? What Comes Later?

Practical Session 2 Constants Don t Matter!!!

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

Ones Assignment Method for Solving Traveling Salesman Problem

Package popkorn. R topics documented: February 20, Type Package

Transcription:

Homework 1 Solutios MA 5 Fall 017 1. Cosider the searchig problem: Iput A sequece of umbers A = [a 1,..., a ] ad a value v. Output A idex i such that v = A[i] or the special value NIL if v does ot appear i A (a) (0 poits) Write a pseudocode for LINEAR-SEARCH, which scas through the sequece, lookig for v. Usig a loop ivariat, prove that your algorithm is correct. (Make sure that your loop ivariat fulfills the three ecessary properties iitializatio, maiteace, termiatio.) Solutio: LINEAR-SEARCH(A, v) 1 for i 1 to legth[a] do if A[i] = v 3 the retur i retur NIL Correctess: Loop ivariat At the start of each iteratio of the for loop we have A[j] v for all j < i. Iitializatio Before the first loop iteratio the ivariat holds sice the statemet is empty. Maiteace The loop ivariat is maitaied at each iteratio, sice otherwise at the i-th iteratio there is some some j < i such that A[j] = v. However, i that case for the j-th iteratio of the loop the value j is retured, ad there is o i-th iteratio of the loop, a cotradictio. Termiatio Whe the loop termiates, there may be two cases: oe is that it termiates after i legth(a) iteratios, ad returs i, i which case the if coditioal esures that A[i] = v. The other case is that i exceeds legth(a), i this case by the loop ivariat we have that for all j legth(a) A[j] v, this returig NIL is correct. (b) (10 poits) How may elemets of the iput sequece eed to be checked o the avarage, assumig that the elemet beig searched for is equally likely to be ay elemet i the array? How about the worst case? What are the avarage-case ad worst-case ruig times of liear search i Θ otatio? Justify your aswer. 1

Solutio: O avarage 1 (+1) = +1 elemets beig searched for. I the worst case it is. The avarage case ruig time is +1 = Θ() sice for all 1 we have 1 + 1 The worst case ruig time is Θ(). (c) (10 poits) If we assume that A is sorted, the we ca check the midpoit of the sequece agaist v ad elimiate half of the sequece from further cosideratio. BINARY SEARCH is a algorithm that repeats this procedure, halvig the size of the remaiig portio of the sequece each time. Write a pseudocode, either iterative or recursive, for biary search. Solutio:. BINARY-SEARCH(A, p, q, v) 1 if q < p the retur NIL 3 m p + (p q)/ if A[m] = v 5 the retur m 6 if A[m] > v 7 the BINARY-SEARCH(A, 1, m 1, v) else BINARY-SEARCH(A, m + 1, legth(a), v) (d) (10 poits) Use the master method to show that the solutio to the biary-search recurrece T () = T (/) + Θ(1) is T () = Θ(lg ), ad argue that the worst case ruig time for biary search is Θ(lg ). Solutio: We ca use the Master Theorem Case because from a = 1 ad b = we have log b a = 0 = 1, so for k = 0 f() = Θ(1) = Θ( log b a lg k ). This gives that T () = Θ( log b a lg k+1 ) = Θ(lg ). The ruig time of BINARY-SEARCH ca be computed from the followig recurrece equatio: T () = c + T ( ). Thus the above argumet shows that the ruig time is T () = Θ(lg ). (e) (10 poits) Observe that the while loop of the INSERTION SORT procedure (i class) uses a liear search to sca (backward) through the sorted subarray A[1... j 1]. Ca we use biary search istead to improve the overall worst-case ruig time of isertio sort to Θ( lg )?

Solutio: The while loop of procedure INSERTION-SORT scas backward through the sorted array A[1... j 1] to fid the appropriate place for A[j]. The hitch is that the loop ot oly searches for the proper place for A[ j ], but that it also moves each of the array elemets that are bigger tha A[ j ] oe positio to the right. These movemets ca take as much as Θ(j) time, which occurs whe all the j 1 elemets precedig A[j] are larger tha A[j]. We ca use biary search to improve the ruig time of the search to Θ(lg j), but biary search will have o effect o the ruig time of movig the elemets. Therefore, biary search aloe caot improve the worst-case ruig time of INSERTION-SORT to Θ( lg ).. (0 poits) Let f, g : N R be fuctios which are positive for some 0. Prove the equatlity O((f + g) ) = O(f + g ). (Hit: you eed to use the ieaquality betwee geometric ad arithmetic averages.) Solutio: First assume that h O((f + g) ), i.e. there exist c > 0 ad 1 such that 0 h() c(f() + g()) for all 1. Usig the ieaquality betwee geometric ad arithmetic averages we get that if 0 (so f ad g are positive) the thus (f() + g()) f()g(), (f() + g()) = f() + g() + f()g() f() + g() + (f() + g()) which implies that (f() + g()) f() + g(). Thus for max( 0, 1 ) we have that 0 h() c (f() + g() ), i.e. h O(f + g ). For the other directio, if h O(f + g ) i.e. there exist d > 0 ad such that 0 h() d(f() + g()) for all, the usig that for 0 f() + g() f() + g() + f()g() = (f() + g()) we have that for max( 0, ), 0 h() d(f() + g()), i.e. h O((f + g) ). 3. (0 poits) Give asymptotic upper ad lower boud for the recurrece T () = T (/) + T (/) + T (/) +. 3

Solutio: The solutio of the recurrece ca be computed by addig up the odes i the followig tree: 3 3 6..................... Note that if a ode would have a umber < 1 the we write 0 i that ode. Let Σ(i) deote the sum of the odes i lie i. The Σ(1) =, Σ() + + ( = 1 1 ) = 7....... Note that we may have iequality above, i case <. Similarly for sum of the (i + 1)-th row Σ(i + 1) Σ(i) + Σ(i) + Σ(i) ( = Σ(i) 1 1 ) = 7 Σ(i). Thus we get that Σ(i). Summig up the rows we get the followig upper boud: lg T () i=1 i=1 = 1 1 7 =. O the other had is a lower boud for the ruig time. Thus T () = Θ(). We ca also verify that T () = Θ() by the substitutio method. We show that T () = c for some suitable c > 0 costat. We have T () = T ( ) + T ( ) + T ( ) + = c + c + c + = 7 c + = ( 7 c + 1 ).

We eed to fid c such that ( ) 7 c + 1 = c. This equatios has the solutio c =, so we have that T () = c. 5