HOMEWORK FILE SOLUTIONS

Size: px
Start display at page:

Download "HOMEWORK FILE SOLUTIONS"

Transcription

1 Data Structures Course (CSCI-UA 102) Professor Yap Spring 2012 HOMEWORK FILE February 13, 2012 SOLUTIONS 1 Homework 2: Due on Thu Feb 16 Q1. Consider the following function called crossproduct: int crossproduct(int[] A, int[] B){ int sum = 0; for (int i=0; i<a.length; i++) for (int j=0; j<b.length; j++) sum += A[i]*B[j]; return sum; } (a) Let the complexity of this function be T(n,m) where the lengths of A and B are n and m, respectively. Determine the complexity T(n, m) of this function exactly, not just asymptotically. For this purpose, make reasonable assumptions about the cost of each step. (b) Next, state the Θ-order of T(n,m) this means you write T(n,m) = Θ(f(n,m)) where f(n,m) is as simple as possible. (c) Finally prove your statement in (b). (d) Write a class called CrossProduct that has the above method, and also a main method to test the function. Your main function should just test the following two arrays A= [1,2,3,4], B= [2,4,6]. But sure to initialize the arrays to these values in the most effective way (look up if you do not know). Your Makefile must have a target called crossproduct (with short form cp) to compile AND run this class. (e) Can you improve the complexity of the method by computing the result in a different way? Indicate how to do this and give a Θ-order of the complexity if you can. Q2. The largest value of a Java int is 2,147,483,647. This value is Integer.MAX_VALUE in the wrapper class Integer. (a) First, use our rule of thumb (2 10 is 1000) to show why this is a roughly what you expect. (b) Next, explain why it is exactly this value, and not 2,147,483,646 or 2,147,483,648 (c) Now, we want to determine the largest value of n such that F n is less than MAX VALUE. You are told that for n > 10, F n is close to φ n where φ = (1+ 5)/ is the golden ratio. You may use a pocket calculator this part, but DO NOT use a program that computes F n. YOU MUST TELL US DETAILS ABOUT HOW YOU DETERMINE n. (d) Suppose we use the primitive type double. A Java double uses has a budget of 64 bits: 53 bits for mantissa, 1 bit for sign and 11 bits for exponent (that is a total of 65 not 64 bits the mysterious extra bit comes from the implicit leading bit in the mantissa). Using your rule of thumb, how many digits 1

2 do you expect the largest representable integer should be? (e) (THIS PART IS NOT REQUIRED) Confirm your answer for (d) experimentally, and tell us your experiment. Q3. We have given you code for two classes MyArrayList and MyLinkedList, both implementing the List ADT. Suppose we want to introduce a new method to the List ADT and to modify an original method as follows: The new method public void append( List<T> alist ) will modifies this list by appending the elements of alist to the end of itself, and alist becomes empty. E.g., if this list originally contains (1,3,5) and alist contains (a,b,c,d) then after calling append, this will contains (1,3,5,a,b,c,d). You need to implement this new method for MyLinkedList only, but rename your class to NewLinkedList. In MyArrayList, we want you to modify the behavior of remove() so that whenever the number of items in the list is less than half of the the length of the current array, we get a new array whose length is half the length of current array. Of course, it should not be less than the DEFAULT_CAPACITY Note that this choice half of the length of the current array is known to be a bad choice (why?), but it will serve our purpose. Rename your class to NewArrayList. You are to submit a Makefile that has targets called app and rem to test the above two modifications. 2 Homework 1: Due on Wed Feb 1 Q1. (4 Points for each part) Recall the big-oh and big-theta notations (the book s definition is in Section 2.1, which also has the big-omega notation). Let f(n) = 2n 100. Show that f(n) = Θ(n). In general, to show f(n) = Θ(g(n)), you need to explicitly tell us the 3 implicit positive constants C,c,n 0 in this notation. Show that f(n) = O(g(n)) and g(n) = O(h(n)) implies f(n) = O(h(n)). Note that showing f = O(g) only need 2 implicit constants, so it is a bit easier than showing f = Θ(g). Let f(n) = n 2. Show that it is NOT true that f(n) = O(n). Show that it is NOT true that 2 n = O(n k ), for any fixed value of k. Note that a function f(n) is called polynomial if f(n) = O(n k ) for some constant k > 0. So this shows 2 n is non-polynomial. 2

3 SOLUTION To show that f(n) = 2n 100 = Θ(n), we split this into two parts: (i) First show that f(n) = O(n). Note that f(n) = 2n 100 2n for n 0. Hence we can choose C = 2 and n 0 = 0 in the definition of f(n) = O(n). (ii) Next we show that f(n) = Ω(n). Observe f(n) = n+(n 100) n for n 100. So c = 1 and n 0 = 100 will do. Show that f(n) = O(g(n)) and g(n) = O(h(n)) implies f(n) = O(h(n)). By definition, there is some constant C > 0 and n 0 such that f(n) C g(n) for all n n 0. Similarly, there is some constant C > 0 and n 1 such that g(n) C h(n) for all n n 1. Let n 2 = max{n 0,n 1 }. Then for all n n 2, we have f(n) C g(n) C C h(n). If we choose C = C C, then f(n) C h(n) for all n n 2. This proves f = O(h). Let f(n) = n 2. Show that it is NOT true that f(n) = O(n). If f(n) = O(n), this means there is some C > 0 such that f(n) C n for n large enough. That is, n 2 C n for n large enough. But if we choose n > C, then n 2 = n n > C n, contradiction. To show 2 n O(n k ) for any k, we show the stronger claim that 2 n = Ω(n k ). It is a bit tricky to show if you do not use Calculus tools (and we do want to avoid them). We will bootstrap ourselves, each time by induction. Initially assume that n and k are integers. First we show that 2 n n for all n 2. This is clear for n = 1. Suppose n 2. Then 2 n = 2(2 n 1 ) 2(n 1) (by induction hypothesis). But 2(n 1) = n+(n 2) n (since n 2). Fix any C > 1. Next we show that 2 n Cn (eventually). First, choose n 0 such that 2 n0 2C. Then 2 2n0 = 2 n0 2 n0 (2C)(n 0 ) (by previous part). Hence 2 2n0 C(2n 0 ). This proves our claim for n = 2n 0. Then for n > 2n 0, 2 n = 2(2 n 1 ) 2(C(n 1)) (by induction hypothesis). Thus 2 n C(2n 2) Cn (ev.) Now we show that 2 n Cn k (ev.) for any k 2 and C > 1. We may choose C > 2 k. By induction hypothesis, we know that 2 n Cn k 1 (ev.) for any C > 1. First choose n 0 such that 2 n0 Cn0 k 1 (induction hypothesis). Then 2 2n0 = 2 n0 2 n0 (Cn0 k 1 )(Cn0 k 1 C 2 n k 0 C(2n 0 ) k since C 2 k. So we have show our basis case when n = 2n 0. For n > 2n 0, we have 2 n = 2(2 n 1 ) 2(C(n 1) k ) = 2C(n k kn k 1 + ) obtained by expanding (n 1) k. We may write (n 1) k = n k p(n) where p(n) is a polynomial of degree k 1. Then 2 n 2C(n k p(n)) = Cn k +C(n k 2p(n)). But n k 2p(n) 0 (ev). Therefore 2 n Cn k (ev). We have now shown that 2 n Cn k (ev) for integer values of n and k, and for any C > 1. If these are not necessarily integers, we use the fact that 2 n 2 n C n k (ev.) (as proved above) C(n/2) k = C n k (C = C2 k ) I have since discovered a much simpler proof: Use the fact that 2 n = n ( n ) i=0 i (why is this true? use the interpretation that 2 n is the number of subsets of a set of size n). Then 2 n > ( n k) (n/2) k /k! (if n > 2k). But (n/2) k /k! = C n k if we choose C = 1/(2 k k!). Q2. (9 Points) In class, we described a Java class Fib that has a method fib(n) to compute F n, the nth 3

4 Fibonacci number. Recall that F 0 = 0,F 1 = 1 and F n+1 = F n + F n 1 for n 1. This definition is slightly different than the book s (p. 6), but the difference is unimportant. Please download the file Fib.java from our pickup site to study this class. Let T(n) denote the number of steps used by fib(n). You can interpret steps quite liberally, counting any constant time (i.e., O(1)) operation as one step. Observe (by studying the program f ib(n)) that { 1 if n 1 T(n) T(n 1)+T(n 2) else. (a) Show that T(n) 2 n/2. (b) Conclude that T(n) is non-polynomial. HINT: you may assume that T(n) T(m) whenever n m. SOLUTION (a) Note that the claim T(n) 2 n/2 is false when n = 1. But we claim that it is true eventually. Indeed, we only need n 2. For n = 2, it is true because T(2) = 2 2 2/2. For n = 3, it is true because T(3) = 3 2 3/2. Inductively, we use the observation that T(n) T(n 2) + T(n 2) = 2 T(n 2). Repeating this, T(n) 2 (2 T(n 4)) = 2 2 T(n 4). So T(n) 2 k T(n 2k). How large can k be? Well, about n/2. More precisely, we need n 2k = 3 or n 2k = 4. Thus, k = (n 3)/2 or k = (n 4)/2. So T(n) 2 (n i)/2 T(i) where i = 3 or i = 4. But we know that T(i) 2 i/2. Thus T(n) 2 (n i)/2)2i =2 n/2. (b) To say that T(n) is polynomial means T(n) = O(n k ) for some k > 0. But we already know from the previous question that 2 n C n k (ev.) for any k > 0. So (ev.) where C = C 2 k. T(n) 2 n/2 C (n/2) k C n k Q3. (30 Points) In class, we discussed two problems with the current implementation of f ib(n): (1) The use of int means that the output would be wrong for n > 60. The solution is to use the Java class Integer. (2) The fact that the recursive fib calls itself twice meant that this is an exponential time algorithm (see previous question). We can empirically see this by trying to compute F 45. We also sketched a version that only requires one recursive call (this one would be polynomial time). Please implement such an improved version that incorporates (1) and (2). The name ofyourjavaclassshouldbe calledfibonacci,andit should have(at least) twomethods: Integer fib(integer n) that computes F n, and a main method that reads an optional number n from the command line. If n is not given, then it defaults to 20. It should print the value fib(n). Your main method should also accept a second optional number m, and this means you compute a consecutive sequence of m Fibonacci numbers, starting from fib(n). The default value of m is 1. E.g., if we call java Fibonacci 4 2 then you should print F 4 = 3 and F 5 = 5. Download a Makefile from the pickup site, and put it in the same directory as Fibonacci.java. Modify it for your current assignment so that the following 12 behavior is achieved: > make compile --- this should call "javac Fibonacci.java" > make c --- this should be the same as "make compile" > make p=fib c --- this should be the same as "make c" but using Fib.java > make run --- this should call "java Fibonacci" > make r --- this should call "make run" > make --- same as "make c; make r" > make p=fib --- same as "make" but using Fib.java > make arg= same as "java Fibonacci 12" 4

5 > make arg=12 arg2=3 --- same as "java Fibonacci 12 3" > make test --- this should call "java Fibonacci 20 10" > make test1 --- this should call "java Fibonacci 30 10" > make test2 --- this should call "java Fibonacci 80" Note that we must use three variables in the Makefile, p, arg, arg2. To do this, you should read up our tutorial on make, and in particular how to use variables. Also, our sample Makefile already illustrates this. Here is one way to creat the TAR file to send us (either in Cygwin or in MacOS Windows or in Linux): > tar cvf hw1-chee-yap.tar README Q-1.pdf Q-2.pdf Makefile Fibonacci.java where > is just the command prompt (so do not type it!). SOLUTION The solution files Fibonacci.java and Makefile-hw1 are both found in our pickup site. NOTE that although the make file s name is non-standard, you can still use it by telling the make program which make file to use, as follows: 5

Data Structures Course (CSCI-UA 102) Professor Yap Spring 2012 HOMEWORK FILE. May 1, 2012

Data Structures Course (CSCI-UA 102) Professor Yap Spring 2012 HOMEWORK FILE. May 1, 2012 Data Structures Course (CSCI-UA 102) Professor Yap Spring 2012 HOMEWORK FILE May 1, 2012 Homework Instructions (H10 is new!) H1. Please read carefully. There is penalty for non-compliance. H2. This homework

More information

Complexity of Algorithms

Complexity of Algorithms CSCE 222 Discrete Structures for Computing Complexity of Algorithms Dr. Hyunyoung Lee Based on slides by Andreas Klappenecker 1 Overview Example - Fibonacci Motivating Asymptotic Run Time Analysis Asymptotic

More information

Complexity of Algorithms. Andreas Klappenecker

Complexity of Algorithms. Andreas Klappenecker Complexity of Algorithms Andreas Klappenecker Example Fibonacci The sequence of Fibonacci numbers is defined as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... F n 1 + F n 2 if n>1 F n = 1 if n =1 0 if n =0 Fibonacci

More information

MIDTERM EXAM (HONORS SECTION)

MIDTERM EXAM (HONORS SECTION) Data Structures Course (V22.0102.00X) Professor Yap Fall 2010 MIDTERM EXAM (HONORS SECTION) October 19, 2010 SOLUTIONS Problem 1 TRUE OR FALSE QUESTIONS (4 Points each) Brief justification is required

More information

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

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

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

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +... Design and Analysis of Algorithms nd August, 016 Problem Sheet 1 Solutions Sushant Agarwal Solutions 1. A d-ary tree is a rooted tree in which each node has at most d children. Show that any d-ary tree

More information

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;

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; 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; } if (n

More information

Solution for Homework set 3

Solution for Homework set 3 TTIC 300 and CMSC 37000 Algorithms Winter 07 Solution for Homework set 3 Question (0 points) We are given a directed graph G = (V, E), with two special vertices s and t, and non-negative integral capacities

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

More information

Lecture 2: Algorithm Analysis

Lecture 2: Algorithm Analysis ECE4050/CSC5050 Algorithms and Data Structures Lecture 2: Algorithm Analysis 1 Mathematical Background Logarithms Summations Recursion Induction Proofs Recurrence Relations 2 2 Logarithm Definition: 3

More information

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution Agenda The worst algorithm in the history of humanity 1 Asymptotic notations: Big-O, Big-Omega, Theta An iterative solution A better iterative solution The repeated squaring trick Fibonacci sequence 2

More information

Elementary maths for GMT. Algorithm analysis Part I

Elementary maths for GMT. Algorithm analysis Part I Elementary maths for GMT Algorithm analysis Part I Algorithms An algorithm is a step-by-step procedure for solving a problem in a finite amount of time Most algorithms transform input objects into output

More information

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

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the

More information

Data Structures and Algorithms Key to Homework 1

Data Structures and Algorithms Key to Homework 1 Data Structures and Algorithms Key to Homework 1 January 31, 2005 15 Define an ADT for a set of integers (remember that a set may not contain duplicates) Your ADT should consist of the functions that can

More information

Data Structures and Algorithms. Part 2

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

More information

Introduction to Computer Science

Introduction to Computer Science Introduction to Computer Science Program Analysis Ryan Stansifer Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901 http://www.cs.fit.edu/ ryan/ 24 April 2017

More information

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

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017 8/3/07 Analysis Introduction to Analysis Model of Analysis Mathematical Preliminaries for Analysis Set Notation Asymptotic Analysis What is an algorithm? An algorithm is any well-defined computational

More information

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University U Kang 1 In This Lecture Learn how to evaluate algorithm efficiency Learn the concept of average case, best case,

More information

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS) 0.1 Welcome http://cs161.stanford.edu My contact info: Jessica Su, jtysu at stanford dot edu, office hours Monday 3-5 pm in Huang basement TA office hours: Monday, Tuesday, Wednesday 7-9 pm in Huang basement

More information

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

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs Big-Oh 1 asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs

More information

CS S-02 Algorithm Analysis 1

CS S-02 Algorithm Analysis 1 CS245-2008S-02 Algorithm Analysis 1 02-0: Algorithm Analysis When is algorithm A better than algorithm B? 02-1: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster 02-2:

More information

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms DATA STRUCTURES AND ALGORITHMS Asymptotic analysis of the algorithms Summary of the previous lecture Algorithm definition Representation of the algorithms: Flowchart, Pseudocode Description Types of the

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

Introduction to Data Structure

Introduction to Data Structure Introduction to Data Structure CONTENTS 1.1 Basic Terminology 1. Elementary data structure organization 2. Classification of data structure 1.2 Operations on data structures 1.3 Different Approaches to

More information

Choice of C++ as Language

Choice of C++ as Language EECS 281: Data Structures and Algorithms Principles of Algorithm Analysis Choice of C++ as Language All algorithms implemented in this book are in C++, but principles are language independent That is,

More information

Recall from Last Time: Big-Oh Notation

Recall from Last Time: Big-Oh Notation CSE 326 Lecture 3: Analysis of Algorithms Today, we will review: Big-Oh, Little-Oh, Omega (Ω), and Theta (Θ): (Fraternities of functions ) Examples of time and space efficiency analysis Covered in Chapter

More information

Analysis of Algorithms. CSE Data Structures April 10, 2002

Analysis of Algorithms. CSE Data Structures April 10, 2002 Analysis of Algorithms CSE 373 - Data Structures April 10, 2002 Readings and References Reading Chapter 2, Data Structures and Algorithm Analysis in C, Weiss Other References 10-Apr-02 CSE 373 - Data Structures

More information

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

RUNNING TIME ANALYSIS. Problem Solving with Computers-II RUNNING TIME ANALYSIS Problem Solving with Computers-II Performance questions 4 How efficient is a particular algorithm? CPU time usage (Running time complexity) Memory usage Disk usage Network usage Why

More information

Introduction to Computer Science

Introduction to Computer Science Introduction to Computer Science Program Analysis Ryan Stansifer Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901 http://www.cs.fit.edu/ ryan/ 8 December 2017

More information

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Big-O-ology 1 CIS 675: Algorithms January 14, 2019 Big-O-ology 1 CIS 675: Algorithms January 14, 2019 1. The problem Consider a carpenter who is building you an addition to your house. You would not think much of this carpenter if she or he couldn t produce

More information

Data Structures Lecture 8

Data Structures Lecture 8 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 8 Recap What should you have learned? Basic java programming skills Object-oriented

More information

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018 Analysis of Algorithms & Big-O CS16: Introduction to Algorithms & Data Structures Spring 2018 Outline Running time Big-O Big-Ω and Big-Θ Analyzing Seamcarve Dynamic programming Fibonacci sequence 2 Algorithms

More information

Solution to Graded Problem Set 4

Solution to Graded Problem Set 4 Graph Theory Applications EPFL, Spring 2014 Solution to Graded Problem Set 4 Date: 13.03.2014 Due by 18:00 20.03.2014 Problem 1. Let V be the set of vertices, x be the number of leaves in the tree and

More information

Outline and Reading. Analysis of Algorithms 1

Outline and Reading. Analysis of Algorithms 1 Outline and Reading Algorithms Running time ( 3.1) Pseudo-code ( 3.2) Counting primitive operations ( 3.4) Asymptotic notation ( 3.4.1) Asymptotic analysis ( 3.4.2) Case study ( 3.4.3) Analysis of Algorithms

More information

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm INTRODUCTION A. Preliminaries: Purpose: Learn the design and analysis of algorithms Definition of Algorithm: o A precise statement to solve a problem on a computer o A sequence of definite instructions

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

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

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming Chapter 1 Algorithms: Efficiency, Analysis, and dod Order Preface techniques for solving problems using computer approach or methodology but not programming language e.g., search Collie Collean in phone

More information

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

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1 Overview CSE 101: Design and Analysis of Algorithms Lecture 1 CSE 101: Design and analysis of algorithms Course overview Logistics CSE 101, Fall 2018 2 First, relevant prerequisites Advanced Data Structures

More information

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions CS 173 [A]: Discrete Structures, Fall 01 Homework 8 Solutions This homework contains 4 problems worth a total of 35 points. It is due on Wednesday, November 14th, at 5pm. 1 Induction Proofs With Inequalities

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

More information

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity Algorithm Analysis Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity ITCS 2214:Data Structures 1 Mathematical Fundamentals Measuring Algorithm Efficiency Empirical

More information

CSE Winter 2015 Quiz 1 Solutions

CSE Winter 2015 Quiz 1 Solutions CSE 101 - Winter 2015 Quiz 1 Solutions January 12, 2015 1. What is the maximum possible number of vertices in a binary tree of height h? The height of a binary tree is the length of the longest path from

More information

A LISP programmer knows the value of everything, but the cost of nothing.

A LISP programmer knows the value of everything, but the cost of nothing. Chapter 6 Cost A LISP programmer knows the value of everything, but the cost of nothing. Alan Perlis The evaluation rules in Chapter 3 explain how to determine the value of every expression that can be

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-02 Algorithm Analysis David Galles Department of Computer Science University of San Francisco 02-0: Algorithm Analysis When is algorithm A better than algorithm

More information

CS:3330 (22c:31) Algorithms

CS:3330 (22c:31) Algorithms What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.

More information

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims Lecture 10: Asymptotic Complexity and What Makes a Good Algorithm? Suppose you have two possible algorithms or

More information

Quiz 1 Practice Problems

Quiz 1 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these

More information

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept 9/10/2018 Algorithms & Data Structures Analysis of Algorithms Siyuan Jiang, Sept. 2018 1 Email me if the office door is closed Siyuan Jiang, Sept. 2018 2 Grades have been emailed github.com/cosc311/assignment01-userid

More information

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics. Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms

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

ANALYSIS OF ALGORITHMS ANALYSIS OF ALGORITHMS Running Time Pseudo-Code Asymptotic Notation Asymptotic Analysis Mathematical facts T(n) n = 4 Input Algorithm Output 1 Average Case vs. Worst Case Running Time of an Algorithm An

More information

Classic Data Structures Introduction UNIT I

Classic Data Structures Introduction UNIT I ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero

More information

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018 CS 61B Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018 1 Asymptotic Notation 1.1 Order the following big-o runtimes from smallest to largest. O(log n), O(1), O(n n ), O(n 3 ), O(n log n),

More information

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

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview 400 lecture note #0 [.2,.3,.4] Analysis of Algorithms Complexity of Algorithms 0. Overview The complexity of an algorithm refers to the amount of time and/or space it requires to execute. The analysis

More information

Analysis of Algorithm. Chapter 2

Analysis of Algorithm. Chapter 2 Analysis of Algorithm Chapter 2 Outline Efficiency of algorithm Apriori of analysis Asymptotic notation The complexity of algorithm using Big-O notation Polynomial vs Exponential algorithm Average, best

More information

4.4 Algorithm Design Technique: Randomization

4.4 Algorithm Design Technique: Randomization TIE-20106 76 4.4 Algorithm Design Technique: Randomization Randomization is one of the design techniques of algorithms. A pathological occurence of the worst-case inputs can be avoided with it. The best-case

More information

Chapter 1 Programming: A General Overview

Chapter 1 Programming: A General Overview Chapter 1 Programming: A General Overview 2 Introduction This class is an introduction to the design, implementation, and analysis of algorithms. Examples: sorting large amounts of data organizing information

More information

O(n): printing a list of n items to the screen, looking at each item once.

O(n): printing a list of n items to the screen, looking at each item once. UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))

More information

6.001 Notes: Section 4.1

6.001 Notes: Section 4.1 6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,

More information

Introduction to Computers & Programming

Introduction to Computers & Programming 16.070 Introduction to Computers & Programming Asymptotic analysis: upper/lower bounds, Θ notation Binary, Insertion, and Merge sort Prof. Kristina Lundqvist Dept. of Aero/Astro, MIT Complexity Analysis

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time: 1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time: Input: A CNF formula ϕ with n variables x 1, x 2,..., x n. Output: True if there is an

More information

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula Homework 1 MTH 4590 Spring 2018 1. Chapter 1, # 1: Prove that for all sets,, C, the formula ( C) = ( ) ( C) is true. Proof : It suffices to show that ( C) ( ) ( C) and ( ) ( C) ( C). ssume that x ( C),

More information

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19 Big-O-ology Jim Royer January 16, 2019 CIS 675 CIS 675 Big-O-ology 1/ 19 How do you tell how fast a program is? Answer? Run some test cases. Problem You can only run a few test cases. There will be many

More information

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 1.4 ANALYSIS OF ALGORITHMS Algorithms F O U R T H E D I T I O N http://algs4.cs.princeton.edu introduction observations mathematical

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow CS 173, Running Time Analysis, Counting, and Dynamic Programming Tandy Warnow CS 173 September 25, 2018 Tandy Warnow Today Topics: Results from survey Midterm Very basic counting Analyzing running times

More information

TECH. Recurrence Equations vs. Recursive Procedures. Recursion and Induction. e.g. Fibonacci Function. The working of recursive procedure

TECH. Recurrence Equations vs. Recursive Procedures. Recursion and Induction. e.g. Fibonacci Function. The working of recursive procedure Recursion and Induction For advanced algorithm development, recursion is an essential design technique Recursive Procedures What is a Proof? Induction Proofs Proving Correctness of Procedures Recurrence

More information

https://cs.uiowa.edu/resources/events Searching an array Let R(N) be the running time to search for an integer in an unsorted array. Can we find an f(n) such that R N O(f N )? Searching an array Let R(N)

More information

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers CIS 194: Homework 6 Due Monday, February 25 Files you should submit: Fibonacci.hs This week we learned about Haskell s lazy evaluation. This homework assignment will focus on one particular consequence

More information

The Running Time of Programs

The Running Time of Programs The Running Time of Programs The 90 10 Rule Many programs exhibit the property that most of their running time is spent in a small fraction of the source code. There is an informal rule that states 90%

More information

Framework for Design of Dynamic Programming Algorithms

Framework for Design of Dynamic Programming Algorithms CSE 441T/541T Advanced Algorithms September 22, 2010 Framework for Design of Dynamic Programming Algorithms Dynamic programming algorithms for combinatorial optimization generalize the strategy we studied

More information

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS 1 Reference books: The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie Programming in C (3rd Edition) by Stephen G. Kochan. Data

More information

Data Structures (CS 102), Professor Yap Spring 2015 MIDTERM. April 19, 2015

Data Structures (CS 102), Professor Yap Spring 2015 MIDTERM. April 19, 2015 Data Structures (CS 102), Professor Yap Spring 2015 MIDTERM April 19, 2015 QUESTION 1. SHORT QUESTIONS (5 Points each part = 50 Points). (a) (5 Points for each error) This Java program has two errors.

More information

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments Applied Algorithmics COMP526 Lecturer: Leszek Gąsieniec, 321 (Ashton Bldg), L.A.Gasieniec@liverpool.ac.uk Lectures: Mondays 4pm (BROD-107), and Tuesdays 3+4pm (BROD-305a) Office hours: TBA, 321 (Ashton)

More information

Selection (deterministic & randomized): finding the median in linear time

Selection (deterministic & randomized): finding the median in linear time Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.1 Overview Given an unsorted array, how quickly can one find the median element? Can one do it more quickly than bysorting?

More information

Solutions to the Second Midterm Exam

Solutions to the Second Midterm Exam CS/Math 240: Intro to Discrete Math 3/27/2011 Instructor: Dieter van Melkebeek Solutions to the Second Midterm Exam Problem 1 This question deals with the following implementation of binary search. Function

More information

EECS 477: Introduction to algorithms. Lecture 6

EECS 477: Introduction to algorithms. Lecture 6 EECS 477: Introduction to algorithms. Lecture 6 Prof. Igor Guskov guskov@eecs.umich.edu September 24, 2002 1 Lecture outline Finish up with asymptotic notation Asymptotic analysis of programs Analyzing

More information

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

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014 CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014 Previously, on CSE 373 We want to analyze algorithms for efficiency (in time and space) And do so generally

More information

THE PRINCIPLE OF INDUCTION. MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin

THE PRINCIPLE OF INDUCTION. MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin THE PRINCIPLE OF INDUCTION MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin The Principle of Induction: Let a be an integer, and let P(n) be a statement

More information

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

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session CSE 146 Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session Comparing Algorithms Rough Estimate Ignores Details Or really: independent of details What are some details

More information

Introduction to Analysis of Algorithms

Introduction to Analysis of Algorithms Introduction to Analysis of Algorithms Analysis of Algorithms To determine how efficient an algorithm is we compute the amount of time that the algorithm needs to solve a problem. Given two algorithms

More information

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for

More information

Assignment 1 (concept): Solutions

Assignment 1 (concept): Solutions CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Names and Identifiers A program (that is, a class) needs a name public class SudokuSolver {... 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations,

More information

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016 1 SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 Spring 2016 Time spent on A2 2 Histogram: [inclusive:exclusive) [0:1): 0 [1:2): 24 ***** [2:3): 84 ***************** [3:4): 123 *************************

More information

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

COE428 Lecture Notes Week 1 (Week of January 9, 2017) COE428 Lecture Notes: Week 1 1 of 10 COE428 Lecture Notes Week 1 (Week of January 9, 2017) Table of Contents COE428 Lecture Notes Week 1 (Week of January 9, 2017)...1 Announcements...1 Topics...1 Informal

More information

Introduction to the Analysis of Algorithms. Algorithm

Introduction to the Analysis of Algorithms. Algorithm Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational

More information

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

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

UNIT 1 ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS UNIT 1 ANALYSIS OF ALGORITHMS Analysis of Algorithms Structure Page Nos. 1.0 Introduction 7 1.1 Objectives 7 1.2 Mathematical Background 8 1.3 Process of Analysis 12 1.4 Calculation of Storage Complexity

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy. CIS 194: Homework 6 Due Wednesday, 4 March It s all about being lazy. Fibonacci numbers The Fibonacci numbers F n are defined as the sequence of integers, beginning with 1 and 1, where every integer in

More information

Lecture 5: Running Time Evaluation

Lecture 5: Running Time Evaluation Lecture 5: Running Time Evaluation Worst-case and average-case performance Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 13 1 Time complexity 2 Time growth 3 Worst-case 4 Average-case

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

by the evening of Tuesday, Feb 6

by the evening of Tuesday, Feb 6 Homework 1 Due 14 February Handout 6 CSCI 334: Spring 2018 Notes This homework has three types of problems: Self Check: You are strongly encouraged to think about and work through these questions, and

More information

Quiz 1 Practice Problems

Quiz 1 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these

More information

Elementary maths for GMT. Algorithm analysis Part II

Elementary maths for GMT. Algorithm analysis Part II Elementary maths for GMT Algorithm analysis Part II Algorithms, Big-Oh and Big-Omega An algorithm has a O( ) and Ω( ) running time By default, we mean the worst case running time A worst case O running

More information

APCS-AB: Java. Recursion in Java December 12, week14 1

APCS-AB: Java. Recursion in Java December 12, week14 1 APCS-AB: Java Recursion in Java December 12, 2005 week14 1 Check point Double Linked List - extra project grade Must turn in today MBCS - Chapter 1 Installation Exercises Analysis Questions week14 2 Scheme

More information