Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.

Slides:



Advertisements
Similar presentations
Lecture: Algorithmic complexity
Advertisements

ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Algorithm Analysis
Introduction to Analysis of Algorithms
Algorithm Analysis. Analysis of Algorithms / Slide 2 Introduction * Data structures n Methods of organizing data * What is Algorithm? n a clearly specified.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 1. 2 Big Oh and other notations Introduction Classifying functions by their asymptotic growth Theta, Little oh,
Data Structures CS 310. Abstract Data Types (ADTs) An ADT is a formal description of a set of data values and a set of operations that manipulate the.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Cpt S 223 – Advanced Data Structures
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
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 Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Asymptotic Notations Iterative Algorithms and their analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
INTRODUCTION DATA STRUCTURES AND ALGORITHMS
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithm Analysis O Ω.
Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
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 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Analysis 1.
Lecture 15.
Chapter 2 Algorithm Analysis
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Analysis of Algorithms
Analysis of Algorithms
Review Bernard Chen Spring 2006.
Algorithm Analysis CSE 2011 Winter September 2018.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Analysis Neil Tang 01/22/2008
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Analysis of Algorithms
Programming and Data Structure
Presentation transcript:

Chapter 6 Algorithm Analysis Bernard Chen Spring 2006

Why Algorithm analysis Generally, we use a computer because we need to process a large amount of data. When we run a program on large amounts of input, besides to make sure the program is correct, we must be certain that the program terminates within a reasonable amount of time.

6.1 What is Algorithm Analysis? Algorithm: A clearly specified finite set of instructions a computer follows to solve a problem. Algorithm analysis: a process of determining the amount of time, resource, etc. required when executing an algorithm.

Big Oh Notation Big Oh notation is used to capture the most dominant term in a function, and to represent the growth rate. Also called asymptotic upper bound. Ex: 100n n =>O(n 3 ) 100n 3 + 2n n =>O(n 5 )

Upper and lower bounds of a function

Functions in order of increasing growth rate FunctionName CConstant LogNLogarithmic Log 2 NLog-squared NLinear NlogN N2N2 Quaratic N3N3 Cubic 2n2n Exponential

Functions in order of increasing growth rate

6.2 Examples of Algorithm Running Times Min element in an array :O(n) Closest points in the plane, ie. Smallest distance pairs: n(n-1)/2 => O(n 2 ) Colinear points in the plane, ie. 3 points on a straight line: n(n-1)(n-2)/6 => O(n 3 )

6.3 The Max. Contiguous Subsequence Given (possibly negative) integers A1, A2,.., An, find (and identify the sequence corresponding to) the max. value of sum of Ak where k = i -> j. The max. contiguous sequence sum is zero if all the integer are negative. {-2, 11, -4, 13, -5, 2} =>20 {1, -3, 4, -2, -1, 6} => 7

Brute Force Algorithm O(n 3 ) template Comparable maxSubSum(const vector a, int & seqStart, int & seqEnd){ int n = a.size(); Comparable maxSum = 0; for(int i = 0; i < n; i++){ // for each possible start point for(int j = i; j < n; j++){ // for each possible end point Comparable thisSum = 0; for(int k = i; k <= j; k++) thisSum += a[k];//dominant term if( thisSum > maxSum){ maxSum = thisSum; seqStart = i; seqEnd = j; } return maxSum; } //A cubic maximum contiguous subsequence sum algorithm

O(n 3 ) Algorithm Analysis We do not need precise calculations for a Big-Oh estimate. In many cases, we can use the simple rule of multiplying the size of all the nested loops

O(N 2 ) algorithm An improved algorithm makes use of the fact that If we have already calculated the sum for the subsequence i, …, j-1. Then we need only one more addition to get the sum for the subsequence i, …, j. However, the cubic algorithm throws away this information. If we use this observation, we obtain an improved algorithm with the running time O(N 2 ).

O(N 2 ) Algorithm cont. template Comparable maxSubsequenceSum(const vector & a, int & seqStart, int &seqEnd){ int n = a.size(); Comparable maxSum = 0; for( int i = 0; i < n; i++){ Comparable thisSum = 0; for( int j = i; j < n; j++){ thisSum += a[j]; if( thisSum > maxSum){ maxSum = thisSum; seqStart = i; seqEnd = j; } return maxSum; }//figure 6.5

O(N) Algorithm template Comparable maxSubsequenceSum(const vector & a, int & seqStart, int &seqEnd){ int n = a.size(); Comparable thisSum = 0, maxSum = 0; int i=0; for( int j = 0; j < n; j++){ thisSum += a[j]; if( thisSum > maxSum){ maxSum = thisSum; seqStart = i; seqEnd = j; }else if( thisSum < 0) { i = j + 1; thisSum = 0; } return maxSum; }//figure 6.8

6.4 General Big-Oh Rules Def: (Big-Oh) T(n) is O(F(n)) if there are positive constants c and n0 such that T(n) = n0 Def: (Big-Omega) T(n) is Ω(F(n)) if there are positive constant c and n0 such that T(n) >= cF(n) when n >= n0 Def: (Big-Theta) T(n) is Θ(F(n)) if and only if T(n) = O(F(n)) and T(n) = Ω(F(n)) Def: (Little-Oh) T(n) = o(F(n)) if and only if T(n) = O(F(n)) and T(n) != Θ (F(n))

Figure 6.9 Mathematical ExpressionRelative Rates of Growth T(n) = O(F(n)) Growth of T(n) <= growth of F(n) T(n) = Ω(F(n)) Growth of T(n) >= growth of F(n) T(n) = Θ(F(n)) Growth of T(n) = growth of F(n) T(n) = o(F(n)) Growth of T(n) < growth of F(n)

Various growth rates

Worst-case vs. Average-case A worst-case bound is a guarantee over all inputs of size N. In an average-case bound, the running time is measured as an average over all of the possible inputs of size N. We will mainly focus on worst-case analysis, but sometimes it is useful to do average one.

6.6 Static Searching problem Static Searching Problem Given an integer X and an array A, return the position of X in A or an indication that it is not present. If X occurs more than once, return any occurrence. The array A is never altered.

Cont. Sequential search: =>O(n) Binary search (sorted data): => O(logn) Interpolation search (data must be uniform distributed): making guesses and search =>O(n) in worse case, but better than binary search on average Big-Oh performance, (impractical in general).

Sequential Search A sequential search steps through the data sequentially until an match is found. A sequential search is useful when the array is not sorted. A sequential search is linear O(n) (i.e. proportional to the size of input) Unsuccessful search --- n times Successful search (worst) ---n times Successful search (average) --- n/2 times

Binary Search If the array has been sorted, we can use binary search, which is performed from the middle of the array rather than the end. We keep track of low_end and high_end, which delimit the portion of the array in which an item, if present, must reside. If low_end is larger than high_end, we know the item is not present.

Binary Search 3-ways comparisons template int binarySearch(const vector & a, const Comparable & x){ int low = 0; int high = a.size() – 1; int mid; while(low < high) { mid = (low + high) / 2; if(a[mid] < x) low = mid + 1; else if( a[mid] > x) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 }//figure 6.11 binary search using three-ways comparisons

Binary Search 2-ways comparisons template int binarySearch(const vector & a, const Comparable & x){ int low, mid; int high = a.size() – 1; while(low < high) { mid = (low + high) / 2; if(a[mid] < x) low = mid + 1; else high = mid; } return (low == high && a[low] == x) ? low: NOT_FOUND; }//figure 6.12 binary search using two ways comparisons

6.7 Checking an Algorithm Analysis If it is possible, write codes to test your algorithm for various large n.

6.8 Limitations of Big-Oh Analysis Big-Oh is an estimate tool for algorithm analysis. It ignores the costs of memory access, data movements, memory allocation, etc. => hard to have a precise analysis. Ex:2nlogn vs. 1000n. Which is faster? => it depends on n

Common errors (Page 222) For nested loops, the total time is effected by the product of the loop size, for consecutive loops, it is not. Do not write expressions such as O(2N 2 ) or O(N 2 +2). Only the dominant term, with the leading constant removed is needed. More errors on page 222..

Summary Introduced some estimate tools for algorithm analysis. Introduced binary search.

In Class exercises Q6.14 Q6.15

Answers