Written Homework #1: Analysis of Algorithms

Size: px
Start display at page:

Download "Written Homework #1: Analysis of Algorithms"

Transcription

1 Written Homework #1: Analysis of Algorithms CIS 121 Fall 2016 Due: Thursday, September 15th, 2015 before 10:30am (You must submit your homework online via Canvas. A paper copy is not required) Learning Goals The goals of this assignment are to understand different methods of analyzing algorithms. The skills that you will practice in this homework are: Analyzing programs by timing them on different sizes of input, and building a mathematical model (an equation) that predicts the run time for other sizes of inputs. Using the doubling test and log ratio to predict the order of growth, by taking the slope in a power-law relationship. Understanding approximate models and orders of growth. Analyzing the orders of growth of different code snippets. Learning the definition of different common notations that we use to classify algorithms. Note that your textbook focuses on a particular approximate model, called Tilde notation, which is not as common as big O, big Theta, and big Omega. For the parts of this homework assignment that deal with O, Theta and Omega, you will need to do research and draw on sources other than your textbook. Practice solving recurrences. If you ve forgotten how to do these, there is a textbook called Mathematics for Computer Science by Eric Lehman and Tom Leighton that is linked from the Resources section of the course homepage. It has a chapter on recurrences that you should read. You should read Chapter 1.4 in your textbook to help you understand how to answer questions 2-7. For the remainder of the assignment, the textbook will be useful in helping you understand the fundamentals of approximate models, but you will need to use external resources to go into greater detail about the definitions of O, Theta and Omega. 1 Partner 0 points Did you work with a partner? If so, please say who you worked with. Give their name and their PennKey username. Remember that it is fine to discuss the problems with your partner, but you should write up your answers independently. Doing so improves your understanding and retention of the concepts. 1

2 2 Experimental analysis through timing experiments The table below contains observations of the running times of a program for different sized inputs. Problem size N Running time t(n) Using any method, plot the problem size N on x-axis against the running time t(n) on y-axis. Give a mathematical model for the data. Solution (0.5 pts) Any reasonable plot that plots the data points. (0.5) Model: T (N) = N 4 3 Applying the doubling test Using the data from the previous question, calculate the log ratio and predict what the running times will be be for N = {640, 1280, using the doubling test. Compare this value to your model. Solution (0.5 pts ea) Around the magnitude of 1800, 30000, 2.8e18. If a numerical difference inside of a reasonable range is made and the overall idea is there, full credit is given. Partial credit is given based on the amount of work shown otherwise. 2

3 4 Tilde approximation Let each cell in the function column represent a different g(n). Compute each tilde approximation f(n). Compare the ratio of g(n)/f(n) for each g(n) f(n). g(n)/f(n) for No. g(n) f(n) N = 1 N = 100 N = 10, 000 N = 1, 000, N N ( ) ( 1 N ) N 4. N(2N 1)(N 3)/7 5. lg N N lg N 6. 2N 2 + N lg 2 N N 3 + lg N lg N + N lg N + N + 5 Solution 3

4 g(n)/f(n) for No. g(n) f(n) N = 1 N = 100 N = 10, 000 N = 1, 000, N + 3 N N ( ) ( 1 N ) N N(2N 1)(N 3)/7 2N 3 / lg N N lg N N lg N N/A (0/0) N 2 + N + 4 2N lg 2 N N N 3 + lg N 6 N lg N + N N lg N + N + 5 2N Point breakdown. Per row: (1 pt) correct f(n), (0.25 pts ea) correct ratios for each N. Small rounding errors can be overlooked. 5 Orders of growth Given the 10 functions in the chart of the previous problem, order the g(n) by order of growth, with line breaks in between functions with different orders of growth. The first function should have the smallest order of growth, and the last should have the largest. (If two functions have the same order of growth, put them on the same line as each other. You can also refer to the functions by the number.) Remember that orders of growth make an additional simplification beyond what Tilde notation does. 4

5 6 Code analysis to illustrate common orders of growth (Part 1) How many times does this code print the word bar as a function of n? p u b l i c s t a t i c void foo ( i n t n ) { i n t max = 0 ; f o r ( i n t i = 1 ; i <= n ; i ++) { f o r ( i n t j = 1 ; j < n i ; j =2) { System. out. p r i n t l n ( bar ) ; Solution We know the inner loop runs lg(n i) times for i from 1 to n. Hence we can sum as follows: n 1 i=1 lg(n i) = lg(n 1) +... lg(1) = O(n lg(n)) 7 Code analysis to illustrate common orders of growth (Part 2) How many times does this code print the word bar as a function of n? p u b l i c s t a t i c void bar ( i n t n ) { i n t max = 0 ; f o r ( i n t i = 1 ; i <= n ; i++) { f o r ( i n t j = 1 ; j <= Math. l o g ( i )/Math. l o g ( 2 ) ; j++) { f o r ( i n t k = 0 ; k < Math. pow ( 2, j ) ; k++) { System. out. p r i n t l n ( bar ) ; Solution We know the innermost loop runs 2 j times and that the second nested loop runs lg(i) times. Hence, we can sum as follows: n lg(i) i=1 j=1 2j = n i=1 2 (i 1) = 2 ( (n 1)) = (n 1)(n) = O(n2 ) 8 Definitions of O, Theta, and Omega 1 Give formal definitions of O, Theta, and Omega. These are not covered in your textbook, so you should do research on your own to find their definitions. For this problem, it is fine for you to copy a definition from another source, so long as you (1) attribute the source material, and (2) also explain in your own words what the definition means. 5

6 9 Upper and lower bounds The running time T (n) of an algorithm P is at least O(n 2 ). Does this statement allow us to make any conclusions about the lower bound or upper bound of algorithm P? Justify your answer. Solution The statement says that T (n) O(n 2 ), but this gives us no information about the upper bound of T (n). For the lower bound, we know that T (n) O(n 2 ). However, there are several functions of the form f(n) where f(n) O(n 2 ). Thus, we have no real information about the lower bound either. Hence, the above statement tells us nothing about the running time T (n) of algorithm P. 6

7 10 Analyze the O, Ω, and Θ of the following algorithm. p u b l i c i n t maximize ( i n t [ ] p r i c e s ) { i n t max = 0 ; f o r ( i n t i = 0 ; i < p r i c e s. l e n gth ; i++) { f o r ( i n t j = i ; j < p r i c e s. l ength ; j++) { p r o f i t = p r i c e s [ j ] p r i c e s [ i ] ; i f ( p r o f i t > max) { max = p r o f i t ; Solution The algorithm loops through the array using two pointers and examines every possible pair of points. There are exactly ( n 2) unique pairs, resulting in a lower and upper bound of O(n 2 ). Thus, the algorithm is Θ(n 2 ). 11 When can we say that an algorithm is optimal? Give a definition of what it means for an algorithm to be optimal. Given an array of integers, we want to find the minimum value in this array. We can achieve this by performing a single pass through the array and keeping track of the smallest value seen so far. Is this algorithm optimal? If so, explain why it is. If not, give an algorithm with a better runtime. 12 Recurrences Solve the following recurrences and express your answers in Θ notation: a. T (n) = 2T (n 1) + 4 where T (0) = 1 b. T (n) = T (n/2) + n where T (0) = 1 Solution a. T (n) = 2T (n 1) + 4 T (n) = 2T (n 1) + 4 T (n) = 2[2T (n 2) + 4] + 4 T (n) = 4T (n 2) + 12 T (n) = 4[2T (n 3) + 4] + 12 T (n) = 8T (n 3) + 28 T (n) = 2 k T (n k) ( k 1) ( k 2) k 1 T (n) = 2 k T (n k) i i=0 Setting k = n, we obtain: T (n) = 2 n T (0) + n 1 i=0 4 2i. Thus, we have T (n) = 2 n + 4(2 n + 1) = 5 2 n + 4, and we can conclude that T (n) Θ(2 n ). 7

8 b. T (n) = T (n/2) + n T (n) = T (n/2) + n T (n) = [T (n/4) + n/2] + n T (n) = [T (n/8) + n/4] + n/2 + n k 1 T (n) = T (n/2 k 1 ) + n 2 i Setting k = lg n, we obtain: T (n) = T (0) + n lg n 1 1 i=0 2. i Thus, we have T (n) = 1 + n (1 1 ) = 1 + n (1 2 2 lg n 1 n ) Hence, we see that T (n) = n 1, and we can conclude that T (n) Θ(n). 13 Recurrences (Part 2) Convert the following code fragment to a recurrence and then derive its running time. Assume that the function divide is called as follows: divide(0, n, numbers) where numbers is an array whose values are unknown and n is the length of the array. Express your answer in Θ notation as a function of n. p u b l i c i n t d i v i d e ( i n t s t a r t, i n t end, i n t [ ] numbers ) { i f ( s t a r t >= end ) { return 0 ; i n t index = s t a r t + ( end s t a r t ) / 2 ; d i v i d e ( s t a r t, index, numbers ) ; d i v i d e ( index, end, numbers ) ; f o r ( i n t i = s t a r t ; i < end ; i++) { System. out. p r i n t l n ( numbers [ i ] ) ; return numbers. l e n g t h ; Solution Let T (n) be the running time of the code fragment. We observe that the function makes a recursive call to itself twice, but on only half of the input each time. We further observe that the for loop runs in n time for each iteration. Thus, our recurrence is: T (n) = 2 T (n/2) + n. We proceed to solve this recurrence as follows: T (n) = 2T (n/2) + n i=0 = 2[2T (n/4) + n] + n = 4T (n/4) + 2 n = 2 k T (n/2 k ) + k n Setting k = lg n, we have T (n) = 2 lg n T (0) + n lg n. Thus, T (n) = n + n lg n, so we can conclude that T (n) Θ(n lg n). 14 Extra Credit: Asymptotic analysis Prove that 2 n is O(n!) using induction. 8

9 Hint: Start with n = 4 as a base case and c = 1 as your constant. Solution Sketch of proof should look something like this: 1. Prove the base case(n = 4): 4! > Induction Hypothesis: Assume k! > 2 k for some k 4 3. Inductive Step: Prove that (k + 1)! > 2 k+1 : (k)! k+1 2 > 2 k Since we know k > 4, we know that k+1 2 is greater and we know by our IH that k! > 2 k. This means (k)! k+1 2 must be greater than 2 k. 15 Extra Credit: Big Oh Proofs Prove that f(n) is O(g(n)) if and only if g(n) is Ω(f(n)). Hint: First, prove that if (f(n)) is O(g(n)) then g(n) is Ω(f(n)). You can start by proving that if (f(n)) is O(g(n)) then g(n) is Ω(f(n)). If f(n) is O(g(n)), then what do we know about f(n) from the definition of big-o? We want to prove that g(n) is Ω(f(n)). What can we prove about g(n) from the definition of Ω? Given what we know about the relationship between f(n) and g(n), complete the proof. Solution a. Since f(n) O(g(n)) then from the definition of big-o, we know that c > 0 n 0 n n 0, f(n) c g(n). 1 Rearranging this inequality, we can write: c f(n) g(n), or equivalently, g(n) 1 c f(n). Setting c = 1 c, we have g(n) c f(n). Thus, from the definition of Ω, we can conclude that g(n) Ω(f(n)). This completes the proof. b. Since g(n) Ω(f(n)), from the definition of Ω, we know that c > 0 n 0 n n 0, g(n) c f(n). Rearranging this inequality, we can write 1 c g(n) f(n), or equivalently, f(n) 1 c g(n). Setting c = 1 c,we have g(n) c f(n). Thus, from the definition of big-o, we can conclude that g(n) O(f(n)). This completes the proof. 16 Extra Credit: Relation between O, Theta, and Omega Prove or disprove the following statements. f(n) O(g(n)) implies 2 f(n) O(2 g(n) ) The running time of an algorithm is Θ(g(n)) if and only if its worst-case running time is O(g(n)) and its best-case running time is Ω(g(n)). Solution False. Consider f(n) = 2n and g(n) = n. True. You must show a implies b and b implies a. For a implies b, we can get the definitions of big O and big Omega from big Theta. To show b implies a, we can combine the definitions of big O and big Omega to derive the exact definition of big Theta. 9

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

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 Fundamental Algorithms Homework #1 Set on June 25, 2009 Due on July 2, 2009 Problem 1. [15 pts] Analyze the worst-case time complexity of the following algorithms,and give tight bounds using the Theta

More information

CSC236 Week 4. Larry Zhang

CSC236 Week 4. Larry Zhang CSC236 Week 4 Larry Zhang 1 Announcements PS2 due on Friday This week s tutorial: Exercises with big-oh PS1 feedback People generally did well Writing style need to be improved. This time the TAs are lenient,

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 2 Asymptotic Notation 1 O-notation: Asymptotic upper bound f(n) = O(g(n)) if positive constants c, n 0 such that 0 f(n) cg(n), n n 0 f(n) = O(g(n)) cg(n) f(n) Asymptotic running

More information

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013 /4/3 Administrative Big O David Kauchak cs3 Spring 3 l Assignment : how d it go? l Assignment : out soon l CLRS code? l Videos Insertion-sort Insertion-sort Does it terminate? /4/3 Insertion-sort Loop

More information

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

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Xi Chen Columbia University We continue with two more asymptotic notation: o( ) and ω( ). Let f (n) and g(n) are functions that map

More information

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

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline CS 4407, Algorithms Growth Functions

More information

CS 4407 Algorithms Lecture 2: Growth Functions

CS 4407 Algorithms Lecture 2: Growth Functions CS 4407 Algorithms Lecture 2: Growth Functions Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline Growth Functions Mathematical specification of growth functions

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 14, 2019 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. COMP

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 10, 2018 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. 1 /

More information

3.1 Asymptotic notation

3.1 Asymptotic notation 3.1 Asymptotic notation The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbers N = {0, 1, 2,... Such

More information

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23 CIS 11 Data Structures and Algorithms with Java Spring 018 Big-Oh Notation Monday, January /Tuesday, January 3 Learning Goals Review Big-Oh and learn big/small omega/theta notations Discuss running time

More information

Data Structures and Algorithms. Asymptotic notation

Data Structures and Algorithms. Asymptotic notation Data Structures and Algorithms Asymptotic notation Estimating Running Time Algorithm arraymax executes 7n 1 primitive operations in the worst case. Define: a = Time taken by the fastest primitive operation

More information

CSCE 222 Discrete Structures for Computing. Review for the Final. Hyunyoung Lee

CSCE 222 Discrete Structures for Computing. Review for the Final. Hyunyoung Lee CSCE 222 Discrete Structures for Computing Review for the Final! Hyunyoung Lee! 1 Final Exam Section 501 (regular class time 8:00am) Friday, May 8, starting at 1:00pm in our classroom!! Section 502 (regular

More information

Review 3. Andreas Klappenecker

Review 3. Andreas Klappenecker Review 3 Andreas Klappenecker Final Exam Friday, May 4, 2012, starting at 12:30pm, usual classroom Topics Topic Reading Algorithms and their Complexity Chapter 3 Logic and Proofs Chapter 1 Logic and Proofs

More information

Homework 1 Solutions

Homework 1 Solutions CS3510 Design & Analysis of Algorithms Section A Homework 1 Solutions Released 4pm Friday Sep 8, 2017 This homework has a total of 4 problems on 4 pages. Solutions should be submitted to GradeScope before

More information

Big O (Asymptotic Upper Bound)

Big O (Asymptotic Upper Bound) Big O (Asymptotic Upper Bound) Linear search takes O(n) time. Binary search takes O(lg(n)) time. (lg means log 2 ) Bubble sort takes O(n 2 ) time. n 2 + 2n + 1 O(n 2 ), n 2 + 2n + 1 O(n) Definition: f

More information

Running Time Evaluation

Running Time Evaluation Running Time Evaluation Quadratic Vs. Linear Time Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 19 1 Running time 2 Examples 3 Big-Oh, Big-Omega, and Big-Theta Tools 4 Time

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 3: Growth of Functions (slides enhanced by N. Adlai A. DePano) Overview Order of growth of functions provides a simple

More information

Homework 1 Submission

Homework 1 Submission Homework Submission Sample Solution; Due Date: Thursday, May 4, :59 pm Directions: Your solutions should be typed and submitted as a single pdf on Gradescope by the due date. L A TEX is preferred but not

More information

EECS 477: Introduction to algorithms. Lecture 5

EECS 477: Introduction to algorithms. Lecture 5 EECS 477: Introduction to algorithms. Lecture 5 Prof. Igor Guskov guskov@eecs.umich.edu September 19, 2002 1 Lecture outline Asymptotic notation: applies to worst, best, average case performance, amortized

More information

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency Lecture 2 Fundamentals of the Analysis of Algorithm Efficiency 1 Lecture Contents 1. Analysis Framework 2. Asymptotic Notations and Basic Efficiency Classes 3. Mathematical Analysis of Nonrecursive Algorithms

More information

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan Lecture 2 More Algorithm Analysis, Math and MCSS By: Sarah Buchanan Announcements Assignment #1 is posted online It is directly related to MCSS which we will be talking about today or Monday. There are

More information

Asymptotic Analysis 1

Asymptotic Analysis 1 Asymptotic Analysis 1 Last week, we discussed how to present algorithms using pseudocode. For example, we looked at an algorithm for singing the annoying song 99 Bottles of Beer on the Wall for arbitrary

More information

The Time Complexity of an Algorithm

The Time Complexity of an Algorithm Analysis of Algorithms The Time Complexity of an Algorithm Specifies how the running time depends on the size of the input. Purpose To estimate how long a program will run. To estimate the largest input

More information

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu Analysis of Algorithm Efficiency Dr. Yingwu Zhu Measure Algorithm Efficiency Time efficiency How fast the algorithm runs; amount of time required to accomplish the task Our focus! Space efficiency Amount

More information

Analysis of Algorithms

Analysis of Algorithms October 1, 2015 Analysis of Algorithms CS 141, Fall 2015 1 Analysis of Algorithms: Issues Correctness/Optimality Running time ( time complexity ) Memory requirements ( space complexity ) Power I/O utilization

More information

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation: CS 124 Section #1 Big-Oh, the Master Theorem, and MergeSort 1/29/2018 1 Big-Oh Notation 1.1 Definition Big-Oh notation is a way to describe the rate of growth of functions. In CS, we use it to describe

More information

Ch01. Analysis of Algorithms

Ch01. Analysis of Algorithms Ch01. Analysis of Algorithms Input Algorithm Output Acknowledgement: Parts of slides in this presentation come from the materials accompanying the textbook Algorithm Design and Applications, by M. T. Goodrich

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Based

More information

Analysis of Algorithms

Analysis of Algorithms September 29, 2017 Analysis of Algorithms CS 141, Fall 2017 1 Analysis of Algorithms: Issues Correctness/Optimality Running time ( time complexity ) Memory requirements ( space complexity ) Power I/O utilization

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2 MA008 p.1/36 MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2 Dr. Markus Hagenbuchner markus@uow.edu.au. MA008 p.2/36 Content of lecture 2 Examples Review data structures Data types vs. data

More information

The Time Complexity of an Algorithm

The Time Complexity of an Algorithm CSE 3101Z Design and Analysis of Algorithms The Time Complexity of an Algorithm Specifies how the running time depends on the size of the input. Purpose To estimate how long a program will run. To estimate

More information

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

Lecture 3. Big-O notation, more recurrences!! Lecture 3 Big-O notation, more recurrences!! Announcements! HW1 is posted! (Due Friday) See Piazza for a list of HW clarifications First recitation section was this morning, there s another tomorrow (same

More information

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018 CS 61B More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018 Here is a review of some formulas that you will find useful when doing asymptotic analysis. ˆ N i=1 i = 1 + 2 + 3 + 4 + + N = N(N+1)

More information

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count Types of formulas for basic operation count Exact formula e.g., C(n) = n(n-1)/2 Algorithms, Design and Analysis Big-Oh analysis, Brute Force, Divide and conquer intro Formula indicating order of growth

More information

CS Non-recursive and Recursive Algorithm Analysis

CS Non-recursive and Recursive Algorithm Analysis CS483-04 Non-recursive and Recursive Algorithm Analysis Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/

More information

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation: CS 124 Section #1 Big-Oh, the Master Theorem, and MergeSort 1/29/2018 1 Big-Oh Notation 1.1 Definition Big-Oh notation is a way to describe the rate of growth of functions. In CS, we use it to describe

More information

Review 1. Andreas Klappenecker

Review 1. Andreas Klappenecker Review 1 Andreas Klappenecker Summary Propositional Logic, Chapter 1 Predicate Logic, Chapter 1 Proofs, Chapter 1 Sets, Chapter 2 Functions, Chapter 2 Sequences and Sums, Chapter 2 Asymptotic Notations,

More information

Principles of Algorithm Analysis

Principles of Algorithm Analysis C H A P T E R 3 Principles of Algorithm Analysis 3.1 Computer Programs The design of computer programs requires:- 1. An algorithm that is easy to understand, code and debug. This is the concern of software

More information

CS 350 Midterm Algorithms and Complexity

CS 350 Midterm Algorithms and Complexity It is recommended that you read through the exam before you begin. Answer all questions in the space provided. Name: Answer whether the following statements are true or false and briefly explain your answer

More information

Computational Complexity

Computational Complexity Computational Complexity S. V. N. Vishwanathan, Pinar Yanardag January 8, 016 1 Computational Complexity: What, Why, and How? Intuitively an algorithm is a well defined computational procedure that takes

More information

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

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline CS 4407, Algorithms Growth Functions

More information

Lecture 2: Asymptotic Notation CSCI Algorithms I

Lecture 2: Asymptotic Notation CSCI Algorithms I Lecture 2: Asymptotic Notation CSCI 700 - Algorithms I Andrew Rosenberg September 2, 2010 Last Time Review Insertion Sort Analysis of Runtime Proof of Correctness Today Asymptotic Notation Its use in analyzing

More information

COE428 Notes Week 4 (Week of Jan 30, 2017)

COE428 Notes Week 4 (Week of Jan 30, 2017) COE428 Lecture Notes: Week 4 1 of 9 COE428 Notes Week 4 (Week of Jan 30, 2017) Table of Contents Announcements...2 Answers to last week's questions...2 Review...3 Big-O, Big-Omega and Big-Theta analysis

More information

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0 Asymptotic Notation Asymptotic notation deals with the behaviour of a function in the limit, that is, for sufficiently large values of its parameter. Often, when analysing the run time of an algorithm,

More information

How many hours would you estimate that you spent on this assignment?

How many hours would you estimate that you spent on this assignment? The first page of your homework submission must be a cover sheet answering the following questions. Do not leave it until the last minute; it s fine to fill out the cover sheet before you have completely

More information

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

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University) CIS 121 Analysis of Algorithms & Computational Complexity Slides based on materials provided by Mary Wootters (Stanford University) Today Sorting: InsertionSort vs MergeSort Analyzing the correctness of

More information

Algorithms Design & Analysis. Analysis of Algorithm

Algorithms Design & Analysis. Analysis of Algorithm Algorithms Design & Analysis Analysis of Algorithm Review Internship Stable Matching Algorithm 2 Outline Time complexity Computation model Asymptotic notions Recurrence Master theorem 3 The problem of

More information

Ch 01. Analysis of Algorithms

Ch 01. Analysis of Algorithms Ch 01. Analysis of Algorithms Input Algorithm Output Acknowledgement: Parts of slides in this presentation come from the materials accompanying the textbook Algorithm Design and Applications, by M. T.

More information

CSC Design and Analysis of Algorithms. Lecture 1

CSC Design and Analysis of Algorithms. Lecture 1 CSC 8301- Design and Analysis of Algorithms Lecture 1 Introduction Analysis framework and asymptotic notations What is an algorithm? An algorithm is a finite sequence of unambiguous instructions for solving

More information

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

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!! CSCE 222 Discrete Structures for Computing Review for Exam 2 Dr. Hyunyoung Lee 1 Strategy for Exam Preparation - Start studying now (unless have already started) - Study class notes (lecture slides and

More information

Growth of Functions (CLRS 2.3,3)

Growth of Functions (CLRS 2.3,3) Growth of Functions (CLRS 2.3,3) 1 Review Last time we discussed running time of algorithms and introduced the RAM model of computation. Best-case running time: the shortest running time for any input

More information

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: The maximum-subarray problem Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: Brute force algorithm: At best, θ(n 2 ) time complexity 129 Can we do divide

More information

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. Lecture 1: Asymptotic Complexity 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. Announcements TA office hours officially start this week see

More information

Problem Set 1 Solutions

Problem Set 1 Solutions Introduction to Algorithms September 24, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 7 Problem Set 1 Solutions Exercise 1-1. Do Exercise

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 2.1 Notes Homework 1 will be released today, and is due a week from today by the beginning

More information

An analogy from Calculus: limits

An analogy from Calculus: limits COMP 250 Fall 2018 35 - big O Nov. 30, 2018 We have seen several algorithms in the course, and we have loosely characterized their runtimes in terms of the size n of the input. We say that the algorithm

More information

Asymptotic Analysis of Algorithms. Chapter 4

Asymptotic Analysis of Algorithms. Chapter 4 Asymptotic Analysis of Algorithms Chapter 4 Overview Motivation Definition of Running Time Classifying Running Time Asymptotic Notation & Proving Bounds Algorithm Complexity vs Problem Complexity Overview

More information

CS F-01 Algorithm Analysis 1

CS F-01 Algorithm Analysis 1 CS673-016F-01 Algorithm Analysis 1 01-0: Syllabus Office Hours Course Text Prerequisites Test Dates & Testing Policies Try to combine tests Grading Policies 01-1: How to Succeed Come to class. Pay attention.

More information

Data Structures and Algorithms Chapter 2

Data Structures and Algorithms Chapter 2 1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Section 4.3 Prof. Nathan Wodarz Math 209 - Fall 2008 Contents 1 Analysis of Algorithms 2 1.1 Analysis of Algorithms....................... 2 2 Complexity Analysis 4 2.1 Notation

More information

Data Structures and Algorithms Running time and growth functions January 18, 2018

Data Structures and Algorithms Running time and growth functions January 18, 2018 Data Structures and Algorithms Running time and growth functions January 18, 2018 Measuring Running Time of Algorithms One way to measure the running time of an algorithm is to implement it and then study

More information

Lecture 5: Loop Invariants and Insertion-sort

Lecture 5: Loop Invariants and Insertion-sort Lecture 5: Loop Invariants and Insertion-sort COMS10007 - Algorithms Dr. Christian Konrad 11.01.2019 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 1 / 12 Proofs by Induction Structure

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu What is an Algorithm?

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms Taking Stock IE170: Algorithms in Systems Engineering: Lecture 3 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 19, 2007 Last Time Lots of funky math Playing

More information

Cpt S 223. School of EECS, WSU

Cpt S 223. School of EECS, WSU Algorithm Analysis 1 Purpose Why bother analyzing code; isn t getting it to work enough? Estimate time and memory in the average case and worst case Identify bottlenecks, i.e., where to reduce time Compare

More information

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Divide and Conquer CPE 349. Theresa Migler-VonDollen Divide and Conquer CPE 349 Theresa Migler-VonDollen Divide and Conquer Divide and Conquer is a strategy that solves a problem by: 1 Breaking the problem into subproblems that are themselves smaller instances

More information

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004 Asymptotic Analysis Thomas A. Anastasio January 7, 004 1 Introduction As a programmer, you often have a choice of data structures and algorithms. Choosing the best one for a particular job involves, among

More information

CS 61B Asymptotic Analysis Fall 2017

CS 61B Asymptotic Analysis Fall 2017 CS 61B Asymptotic Analysis Fall 2017 1 More Running Time Give the worst case and best case running time in Θ( ) notation in terms of M and N. (a) Assume that slam() Θ(1) and returns a boolean. 1 public

More information

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz. Q1-3 Growable Arrays Continued Big-Oh and its cousins Submit Growable Array exercise Answer Q1-3 from today's in-class quiz. } Finish course intro } Growable Array recap } Big-Oh and cousins } After today,

More information

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2 1 Faculty of Mathematics Waterloo, Ontario Centre for Education in Mathematics and Computing Grade 11/12 Math Circles Fall 2014 - Nov. 5 Recurrences, Part 2 Running time of algorithms In computer science,

More information

Analysis of Algorithms

Analysis of Algorithms Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Analysis of Algorithms Input Algorithm Analysis

More information

Asymptotic Analysis Cont'd

Asymptotic Analysis Cont'd Cont'd Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Announcements We have class this Wednesday, Jan 18 at 12:30 That is, we have two sessions this Wednesday: at

More information

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms (2017F) Lecture4: Analysis of Algorithms Daijin Kim CSE, POSTECH dkim@postech.ac.kr Running Time Most algorithms transform input objects into output objects. The running time of an algorithm typically

More information

Arbitrary-Precision Division

Arbitrary-Precision Division Arbitrary-Precision Division Rod Howell Kansas State University September 8, 000 This paper presents an algorithm for arbitrary-precision division and shows its worst-case time compleity to be related

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 MA008 p.1/37 MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 Dr. Markus Hagenbuchner markus@uow.edu.au. MA008 p.2/37 Exercise 1 (from LN 2) Asymptotic Notation When constants appear in exponents

More information

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem. O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem. defn: v(n) = O(f(n)) constants c and n 0 such that v(n) c f(n) whenever n > n 0 Termed complexity:

More information

data structures and algorithms lecture 2

data structures and algorithms lecture 2 data structures and algorithms 2018 09 06 lecture 2 recall: insertion sort Algorithm insertionsort(a, n): for j := 2 to n do key := A[j] i := j 1 while i 1 and A[i] > key do A[i + 1] := A[i] i := i 1 A[i

More information

Lecture 6: Recurrent Algorithms:

Lecture 6: Recurrent Algorithms: Lecture 6: Recurrent Algorithms: Divide-and-Conquer Principle Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 20 1 Divide-and-conquer 2 Math induction 3 Telescoping 4 Examples 5 Pros and

More information

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS 380 ALGORITHM DESIGN AND ANALYSIS CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 2: Asymptotic Analysis, Insertion Sort Text Reference: Chapters 2, 3 Space and Time Complexity For a given problem, may be a variety of algorithms that you

More information

What is Performance Analysis?

What is Performance Analysis? 1.2 Basic Concepts What is Performance Analysis? Performance Analysis Space Complexity: - the amount of memory space used by the algorithm Time Complexity - the amount of computing time used by the algorithm

More information

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

5 + 9(10) + 3(100) + 0(1000) + 2(10000) = Chapter 5 Analyzing Algorithms So far we have been proving statements about databases, mathematics and arithmetic, or sequences of numbers. Though these types of statements are common in computer science,

More information

Advanced Analysis of Algorithms - Homework I (Solutions)

Advanced Analysis of Algorithms - Homework I (Solutions) Advanced Analysis of Algorithms - Homework I (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV {ksmani@csee.wvu.edu} 1 Problems 1. Given an array A of n integer elements, how would

More information

Review for Midterm 1. Andreas Klappenecker

Review for Midterm 1. Andreas Klappenecker Review for Midterm 1 Andreas Klappenecker Topics Chapter 1: Propositional Logic, Predicate Logic, and Inferences Rules Chapter 2: Sets, Functions (Sequences), Sums Chapter 3: Asymptotic Notations and Complexity

More information

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation: Data structures Exercise 1 solution Question 1 Let s start by writing all the functions in big O notation: f 1 (n) = 2017 = O(1), f 2 (n) = 2 log 2 n = O(n 2 ), f 3 (n) = 2 n = O(2 n ), f 4 (n) = 1 = O

More information

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

More information

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms - Using Asymptotic Bounds - Analysis of Algorithms - Using Asymptotic Bounds - Andreas Ermedahl MRTC (Mälardalens Real-Time Research Center) andreas.ermedahl@mdh.se Autumn 004 Rehersal: Asymptotic bounds Gives running time bounds

More information

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

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003 University of New Mexico Department of Computer Science Midterm Examination CS 361 Data Structures and Algorithms Spring, 2003 Name: Email: Print your name and email, neatly in the space provided above;

More information

Omega notation. Transitivity etc.

Omega notation. Transitivity etc. Omega notation Big-Omega: Lecture 2, Sept. 25, 2014 f () n (()) g n const cn, s.t. n n : cg() n f () n Small-omega: 0 0 0 f () n (()) g n const c, n s.t. n n : cg() n f () n 0 0 0 Intuition (works most

More information

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 1: Asymptotics, Recurrences, Elementary Sorting Lecture 1: Asymptotics, Recurrences, Elementary Sorting Instructor: Outline 1 Introduction to Asymptotic Analysis Rate of growth of functions Comparing and bounding functions: O, Θ, Ω Specifying running

More information

CPSC 221 Basic Algorithms and Data Structures

CPSC 221 Basic Algorithms and Data Structures CPSC 221 Basic Algorithms and Data Structures Asymptotic Analysis Textbook References: Koffman: 2.6 EPP 3 rd edition: 9.2 and 9.3 EPP 4 th edition: 11.2 and 11.3 Hassan Khosravi January April 2015 CPSC

More information

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3 Banner ID: Name: Midterm Exam CS 3110: Design and Analysis of Algorithms June 20, 2006 Group 1 Group 2 Group 3 Question 1.1 Question 2.1 Question 3.1 Question 1.2 Question 2.2 Question 3.2 Question 3.3

More information

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2 In-Class Soln 1 Let f(n) be an always positive function and let g(n) = f(n) log n. Show that f(n) = o(g(n)) CS 361, Lecture 4 Jared Saia University of New Mexico For any positive constant c, we want to

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 3 Asymptotic Notation O-, Ω-, Θ-, o-, ω-notation Divide and Conquer Merge Sort Binary Search Sofya Raskhodnikova and Adam Smith /5/0 Review Questions If input

More information

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary Complexity Analysis Complexity Theory Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary Output TRUE or FALSE Time and Space

More information

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras Module - 2 Lecture - 25 Measuring running time of a program

More information

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text. Chapter 1 Quicksort 1.1 Prerequisites You need to be familiar with the divide-and-conquer paradigm, standard notations for expressing the time-complexity of an algorithm, like the big-oh, big-omega notations.

More information

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014 CSE332: Data Abstrac0ons Sec%on 2 HyeIn Kim Spring 2014 Sec0on Agenda Recurrence Relations Asymptotic Analysis HW1, Project 1 Q&A Project 2 - Introduction - Working in a team - Testing strategies Recurrence

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/8.40J Lecture Prof. Piotr Indyk Welcome to Introduction to Algorithms, Spring 08 Handouts. Course Information. Calendar 3. Signup sheet (PLEASE return at the end of this

More information