Chapter 5 Algorithm Analysis 1CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
The Growth of Functions
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Data Structures Performance Analysis.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Analysis (Big O)
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
1 Complexity Lecture Ref. Handout p
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Mathematics Review and Asymptotic Notation
The Growth of Functions Rosen 2.2 Basic Rules of Logarithms log z (xy) log z (x/y) log z (x y ) If x = y If x < y log z (-|x|) is undefined = log z (x)
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CISC 235: Topic 1 Complexity of Iterative Algorithms.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
Algorithm Analysis (Big O)
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Topics Section 9.2 Complexity of algorithms Efficiency of algorithms
The Growth of Functions
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Computation.
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Chapter 12: Analysis of Algorithms
Algorithm Efficiency Chapter 10.
CSC 205 Java Programming II
CS 201 Fundamental Structures of Computer Science
Applied Discrete Mathematics Week 6: Computation
Analysis of Algorithms
Presentation transcript:

Chapter 5 Algorithm Analysis 1CSCI 3333 Data Structures

Analysis of Algorithms An algorithm is “a clearly specified set of instructions the computer will follow to solve a problem”. Algorithm analysis is “the process of determining the amount of resources, such as time and space, that a given algorithm will require”. The resources required by an algorithm often depends on the size of the data. Example: To search a particular value in an array of 10,000,000 integers is going to take more time and memory space than to search an array of 1,000 integers. CSCI 3333 Data Structures2

Performance factors How fast a program would run depends on many factors: – Processor speed – Amount of available memory – Construction of the compiler – Quality of the program – The size of the data – The efficiency of the algorithm(s) CSCI 3333 Data Structures3

4 To find a correlation between the size of the data, N, and the cost of running an algorithm given the data. -The correlation is typically represented as a function. Approach for algorithm analysis

Example algorithm analysis Given a program p that implements an algorithm a, which connects to a file server and download a file specified by the user: – Initial network connection to the server: 2 sec. – Download speed: 160 K/sec – Cost formula: T(N) = N/ (seconds) – If data size is 8,000K, T (8000) = 52 sec. – If data size is 1,000,000K, T = 6,252 sec. Q: How would the download time be reduced? A ? CSCI 3333 Data Structures5

Outline Review of functions Functions representing the cost of algorithms Example algorithm analysis: MaxSumTest.java CSCI 3333 Data Structures6

7 Functions Intuition: a function takes input and produces one output: e.g., f(x) = x 2, f(x) = sin(x) Formalism: – Domain type:D f – Range type:R f – [Mapping] Graph: G f = { | x  D f, f(x)  R f }  D f x R f – For every x  D f there is at most one pair  G f Graphs of sample functions: – Let D = {1,2,3,4,5}. f(x) = x 2, x  D. – f(x) = 1/x, x  R.

8 Example: f(x) = x 2

9 Functional Property For every x there is at most one y such that y=f(x) [y=1/x] There is an x such that more than one y satisfy y=f(x) [x 2 +y 2 =25] Example: x=0, y 1 =5, y 2 =-5 There is an x such that more than one y satisfy y=f(x) [x 2 +y 2 =25] Example: x=0, y 1 =5, y 2 =-5

10 Domain & Range

11 Why is the efficiency of an algorithm important? Concerns: – Efficient use of resources (processor time, memory space) – Response time / user perception Typical solutions: – Use a formula instead of recursion – Use looping instead of recursion – Use single loop instead of multiple loops – Reduce disk access – …

12 Efficiency of Algorithms Example: Implement the following recursively defined sequence as an algorithm/function a 1 = 1 a k = a k-1 + k,,  k > 1 //Note: Checking for error input is omitted in the codes a) As a recursively defined function Function f (int k) { if (k == 1) return 1; else return f(k-1) + k; } c) As a simple formula Function h (int k) { return k*(k+1)/2; } b) As a loop Function g (int k) { int sum = 0; while (k > 0) { sum = sum + k; k = k -1; } return sum; }

13 Notations Let f and g be real-valued functions defined on the set of nonnegative real numbers. f(x) is Ο(g(x)) : f is of order at most g iff there exist positive real numbers a and b s.t. |f(x)| ≤ b|g(x)| for all real numbers x > a. Informally: the growth rate of f(x) ≤ the growth rate of g(x), when x > a. f(x) is Ο(g(x)) : f is of order at most g f(x) is Θ(g(x)) : f is of order g f(x) is Ω(g(x)) : f is of order at least g

14 Big O Notation f is of the order of g, f(x) = O(g(x)), if and only if there exists a positive real number M and a real number x0 such that for all x, |f(x)| x0. (source:

15 Orders of Power Functions For any rational numbers r and s, if r <= s, then x r is O(x s ). Examples: x 2 is O(x 3 ) 100x is O(x 2 ) 500x 1/2 is O(x) 1000x is O(x 3 ) ? 100x 2 is O(x 2 ) ? 2x 4 + 3x is O(x 4 ) ? Hint: Focus on the dominant term.

16 Big Omega Ω Let f and g be real-valued functions defined on the set of nonnegative real numbers. f(x) is Ω(g(x)) : f is of order at least g iff there exist positive real numbers a and b s.t. b|g(x)| ≤ |f(x)| for all real numbers x > a. Examples: x 3 is Ω (x 2 ) x 2 is Ω (x) x is Ω (x 1/2 ) x is Ω (3x) ?

17 Big Theta Θ Let f and g be real-valued functions defined on the set of nonnegative real numbers. f(x) is Θ(g(x)) : f is of order g iff there exist positive real numbers a, b, and k s.t. a|g(x)| ≤ |f(x)| ≤ b|g(x)| for all real numbers x > k. Theorem (p.521) f is Ω(g) and f is O(g) iff f is Θ(g) Examples: 2x 4 + 3x is Θ(x 4 )

CSCI 3333 Data Structures18 The logarithm: For any B, N > 0, log B N = k if B K = N. log B N = Theorem 5.4: For any constant B > 1, log B N = O(log N). That is, the base does not matter. Proof? Next page

Proof of theorem 5.4: For any constant B > 1, log B N = O(log N). Let K = log B N B K = N (from the logarithm definition) Let C = log B 2 C = B B K = (2 C ) K (from ) log N = log B K (from ) So, log N = log (2 C ) K = CK (from, ) log N = C log B N log B N = (from ) Therefore, log B N = O(log N). CSCI 3333 Data Structures19

CSCI 3333 Data Structures20 Tools for drawing functions: e.g., Note: The default base for log( ) is 10.

CSCI 3333 Data Structures21

Note: log 2 X = logx / log2 CSCI 3333 Data Structures22

CSCI 3333 Data Structures23 Q: Linear or constant ?

CSCI 3333 Data Structures24

CSCI 3333 Data Structures25 The maximum contiguous subsequence sum problem Given (possibly negative) integers, A 1, A 2, …, A N, find (and identify the sequence corresponding to) the maximum value of. The maximum contiguous sub-sequence sum is zero if all the integers are negative.

CSCI 3333 Data Structures26

CSCI 3333 Data Structures27

1-28CSCI 3333 Data Structures

1-29CSCI 3333 Data Structures

1-30CSCI 3333 Data Structures

1-31CSCI 3333 Data Structures Prerequisite of binary search: The array to be searched must be pre-sorted. O (log N)

1-32CSCI 3333 Data Structures

1-33CSCI 3333 Data Structures Verifying an algorithm analysis Method: Check whether the empirically observed running time matches the running time predicted by the analysis. e.g., The program performs N binary searches given each N. Increasing … O(N) is an underestimate. Increasing … O(N) is an underestimate. Decreasing … O(N 2 ) is an overestimate. Decreasing … O(N 2 ) is an overestimate. Converging … O(N log N) is about right. Converging … O(N log N) is about right.

CSCI 3333 Data Structures34 Limitations of big-O analysis Not appropriate for small data size Large constants are ignored in the analysis, but they may affect the actual performance. e.g., 1000N vs 2 N log N Q: When will log N > 500? Cannot differentiate between memory access vs disk access Infinite memory is assumed Average-case running time can often be difficult to obtain.

Exercises Ex 5.20: For each of the following program fragments, give a Big-O analysis of the running time. CSCI 3333 Data Structures35

Exercises Ex 5.7: Solving a problem requires running an O(N 2 ) algorithm and then afterwards an O(N) algorithm. What is the total cost of solving the problem? Ex 5.8: Solving a problem requires running an O(N) algorithm, and then performing N binary searches on an N-element array, and then running another O(N) algorithm. What is the total cost of solving the problem? CSCI 3333 Data Structures36

Ex 5.14: An algorithm take 0.5 ms for input size 100. How long will it take for input size 500 (assuming that low-order terms are negligible) if the running time is as follow: a)linear: b)O(N logN): c)Quadratic: d)Cubic: CSCI 3333 Data Structures37 Exercises

Ex 5.16 CSCI 3333 Data Structures38 Exercises O(N)O(N log N)O(N^2)O(N^3) N1 (100)0.5 ms N2 (500)N2/N1*0.5N2*logN2/ N1*logN1 *0.5 N2^2/N1^ 2*0.5 N2^3/N1^ 3*0.5 Four separate questions: 1)If an algorithm with running time of O(N) takes 0.5 ms when N = 100, how much time would it take when N = 500? 2)… 3)… 4)… Hint: Use Excel