CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Fundamentals of Python: From First Programs Through Data Structures
§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers.
Chapter 1 – Basic Concepts
Chapter 2: Algorithm Analysis
Introduction to Analysis of Algorithms
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Cpt S 223 – Advanced Data Structures
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
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.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
CS Data Structures Chapter 1 Basic Concepts.
Analysis of Algorithms
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Complexity of Algorithms
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
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.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithm Analysis O Ω.
CE 221 Data Structures and Algorithms Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – Izmir University of Economics.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
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. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Chapter 2 Algorithm Analysis Mathematical Background Figure 2.1 Typical growth rates.
Vishnu Kotrajaras, PhD.1 Data Structures
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
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.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Data Structures 1st Week
Analysis of Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Programming and Data Structure
Searching, Sorting, and Asymptotic Complexity
Revision of C++.
Performance analysis of algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Presentation transcript:

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria: (1) Input There are zero or more quantities that are externally supplied. (2) Output At least one quantity is produced. (3) Definiteness Each instruction is clear and unambiguous. (4) Finiteness If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after finite number of steps. (5) Effectiveness Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in(3); it also must be feasible. 1/26

Note: A program is written in some programming language, and does not have to be finite (e.g. an operation system). An algorithm can be described by human languages, flow charts, some programming languages, or pseudo- code. 〖 Example 〗 Selection Sort : Sort a set of n  1 integers in increasing order. From those integers that are currently unsorted, find the smallest and place it next in the sorted list. Where and how are they stored? Where? for ( i = 0; i < n; i++) { E xamine list[i] to list[n  1] and suppose that the smallest integer is at list[min]; I nterchange list[i] and list[min]; } Sort = Find the smallest integer + Interchange it with list[i]. Algorithm in pseudo-code 2/26

§1 What to Analyze  Machine & compiler-dependent run times.  Time & space complexities : machine & compiler- independent. Assumptions:  instructions are executed sequentially  each instruction is simple, and takes exactly one time unit  integer size is fixed and we have infinite memory Typically the following two functions are analyzed: T avg (N) & T worst (N) -- the average and worst case time complexities, respectively, as functions of input size N. If there is more than one input, these functions may have more than one argument. 3/26

§1 What to Analyze 〖 Example 〗 Matrix addition void add ( int a[ ][ MAX_SIZE ], int b[ ][ MAX_SIZE ], int c[ ][ MAX_SIZE ], int rows, int cols ) { int i, j ; for ( i = 0; i < rows; i++ ) for ( j = 0; j < cols; j++ ) c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ]; } /* rows + 1 */ /* rows(cols+1) */ /* rows  cols */ T( rows, cols ) = 2 rows  cols + 2 rows + 1 Q: What shall we do if rows >> cols ? A: Exchange rows and cols. 4/ 26

〖 Example 〗 Iterative function for summing a list of numbers float sum ( float list[ ], int n ) { /* add a list of numbers */ float tempsum = 0; int i ; for ( i = 0; i < n; i++ ) tempsum += list [ i ] ; return tempsum; } /* count = 1 */ /* count ++ */ /* count ++ for last execution of for */ /* count ++ */ T sum ( n ) = 2n + 3 〖 Example 〗 Recursive function for summing a list of numbers float rsum ( float list[ ], int n ) { /* add a list of numbers */ if ( n ) return rsum(list, n  1) + list[n  1]; return 0; } /* count ++ */ T rsum ( n ) = 2n + 2 But it takes more time to compute each step. §1 What to Analyze 5/ 26

§1 What to Analyze Is it really necessary to count the exact number of steps ? Uhhh... I don’t think so. Why? Because it drives me crazy! So it’s too complicated sometimes. But does it worth the effort? Take the iterative and recursive programs for summing a list for example --- if you think 2n+2 is less than 2n+3, try a large n and you’ll be surprised ! I see... Then what’s the point of this T p stuff? Good question ! Let’s ask the students... 6/ 26

§2 Asymptotic Notation ( , , , o ) The point of counting the steps is to predict the growth in run time as the N change, and thereby compare the time complexities of two programs. So what we really want to know is the asymptotic behavior of T p. Suppose T p1 ( N ) = c 1 N 2 + c 2 N and T p2 ( N ) = c 3 N. Which one is faster? No matter what c 1, c 2, and c 3 are, there will be an n 0 such that T p1 ( N ) > T p2 ( N ) for all N > n 0. I see! So as long as I know that T p1 is about N 2 and T p2 is about N, then for sufficiently large N, P2 will be faster! 7/ 26

§2 Asymptotic Notation 【 Definition 】 T (N) = O( f (N) ) if there are positive constants c and n 0 such that T (N)  c  f (N) for all N  n 0. 【 Definition 】 T (N) =  ( g(N) ) if there are positive constants c and n 0 such that T (N)  c  g(N) for all N  n 0. 【 Definition 】 T (N) =  ( h(N) ) if and only if T (N) = O( h(N) ) and T (N) =  ( h(N) ). Note:  2N + 3 = O( N ) = O( N k  1 ) = O( 2 N ) =  We shall always take the smallest f (N).  2 N + N 2 =  ( 2 N ) =  ( N 2 ) =  ( N ) =  ( 1 ) =  We shall always take the largest g(N). 【 Definition 】 T (N) = o( p(N) ) if T (N) = O( p(N) ) and T (N)   ( p(N) ). 8/ 26

§2 Asymptotic Notation Rules of Asymptotic Notation  If T 1 (N) = O( f (N) ) and T 2 (N) = O( g(N) ), then (a) T 1 (N) + T 2 (N) = max( O( f (N)), O( g(N)) ), (b) T 1 (N) * T 2 (N) = O( f (N) * g(N) ).  If T (N) is a polynomial of degree k, then T (N) =  ( N k ).  log k N = O(N) for any constant k. This tells us that logarithms grow very slowly. Note: When compare the complexities of two programs asymptotically, make sure that N is sufficiently large. For example, suppose that T p1 ( N ) = 10 6 N and T p2 ( N ) = N 2. Although it seems that  ( N 2 ) grows faster than  ( N ), but if N < 10 6, P2 is still faster than P1. Note: When compare the complexities of two programs asymptotically, make sure that N is sufficiently large. For example, suppose that T p1 ( N ) = 10 6 N and T p2 ( N ) = N 2. Although it seems that  ( N 2 ) grows faster than  ( N ), but if N < 10 6, P2 is still faster than P1. 9/ 26

§2 Asymptotic Notation 10/ 26

§2 Asymptotic Notation 2n2n n2n2 n log n n Log n f n 11/ 26

§2 Asymptotic Notation  s = microsecond = seconds ms = millisecond = seconds sec = seconds min = minutes yr = years hr = hours d = days n 12/ 26

〖 Example 〗 Matrix addition void add ( int a[ ][ MAX_SIZE ], int b[ ][ MAX_SIZE ], int c[ ][ MAX_SIZE ], int rows, int cols ) { int i, j ; for ( i = 0; i < rows; i++ ) for ( j = 0; j < cols; j++ ) c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ]; } /*  (rows) */ /*  (rows  cols ) */ T( rows, cols ) =  (rows  cols ) §2 Asymptotic Notation 13/ 26

§2 Asymptotic Notation General Rules  FOR LOOPS: The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations.  NESTED FOR LOOPS: The total running time of a statement inside a group of nested loops is the running time of the statements multiplied by the product of the sizes of all the for loops.  CONSECUTIVE STATEMENTS: These just add (which means that the maximum is the one that counts).  IF / ELSE: For the fragment if ( Condition ) S1; else S2; the running time is never more than the running time of the test plus the larger of the running time of S1 and S2. 14/ 26

§2 Asymptotic Notation  RECURSIONS: 〖 Example 〗 Fibonacci number: Fib(0) = Fib(1) = 1, Fib(n) = Fib(n  1) + Fib(n  2) long int Fib ( int N ) { if ( N <= 1 ) return 1; else return Fib( N  1 ) + Fib( N  2 ); } /* O( 1 ) */ /* T ( N ) */ /*T(N  1)*//*T(N  2)*/ T(N) = T(N  1) + T(N  2) + 2  Fib(N) Proof by induction T(N) grows exponentially Q: Why is it so bad? 15/ 26

§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers are negative. Algorithm 1 int MaxSubsequenceSum ( const int A[ ], int N ) { int ThisSum, MaxSum, i, j, k; /* 1*/ MaxSum = 0; /* initialize the maximum sum */ /* 2*/ for( i = 0; i < N; i++ ) /* start from A[ i ] */ /* 3*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */ /* 4*/ ThisSum = 0; /* 5*/ for( k = i; k <= j; k++ ) /* 6*/ ThisSum += A[ k ]; /* sum from A[ i ] to A[ j ] */ /* 7*/ if ( ThisSum > MaxSum ) /* 8*/ MaxSum = ThisSum; /* update max sum */ } /* end for-j and for-i */ /* 9*/ return MaxSum; } T( N ) = O( N 3 ) Detailed analysis is given on p / 26

Algorithm 2 §3 Compare the Algorithms int MaxSubsequenceSum ( const int A[ ], int N ) { int ThisSum, MaxSum, i, j; /* 1*/ MaxSum = 0; /* initialize the maximum sum */ /* 2*/ for( i = 0; i < N; i++ ) { /* start from A[ i ] */ /* 3*/ ThisSum = 0; /* 4*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */ /* 5*/ ThisSum += A[ j ]; /* sum from A[ i ] to A[ j ] */ /* 6*/ if ( ThisSum > MaxSum ) /* 7*/ MaxSum = ThisSum; /* update max sum */ } /* end for-j */ } /* end for-i */ /* 8*/ return MaxSum; } T( N ) = O( N 2 ) 17/ 26

§3 Compare the Algorithms Algorithm 3 Divide and Conquer 4 33 5 22 11 26 22 conquer divide T ( N/2 ) O( N ) T ( N ) = 2 T( N/2 ) + c N, T(1) = O(1) = 2 [2 T( N/2 2 ) + c N/2] + c N = 2 k O(1) + c k N where N/2 k = 1 = O( N log N ) Also true for N  2 k The program can be found on p / 26

§3 Compare the Algorithms Algorithm 4 On-line Algorithm int MaxSubsequenceSum( const int A[ ], int N ) { int ThisSum, MaxSum, j; /* 1*/ ThisSum = MaxSum = 0; /* 2*/ for ( j = 0; j < N; j++ ) { /* 3*/ ThisSum += A[ j ]; /* 4*/ if ( ThisSum > MaxSum ) /* 5*/ MaxSum = ThisSum; /* 6*/ else if ( ThisSum < 0 ) /* 7*/ ThisSum = 0; } /* end for-j */ /* 8*/ return MaxSum; } T( N ) = O( N ) A[ ] is scanned once only. 11 3 22 4 66 16 11 113  2 4  6 At any point in time, the algorithm can correctly give an answer to the subsequence problem for the data it has already read. 19/ 26

§3 Compare the Algorithms NA NA O( N )O(N log N)O( N 2 )O( N 3 ) Time 4321 Algorithm N =10 N =100 N =1,000 N =10,000 N =100,000 Input Size Running times of several algorithms for maximum subsequence sum (in seconds) Note: The time required to read the input is not included. 20/ 26

§4 Logarithms in the Running Time 〖 Example 〗 Binary Search : Given: A [0]  A [1]  ……  A [N  1] ; X Task: Find X Output: i if X = = A [ i ]  1 if X is not found lowhighmid X ~ A [ mid ] low high = mid  1 highlow= mid + 1 <>== mid 21/ 26

int BinarySearch ( const ElementType A[ ], ElementType X, int N ) { int Low, Mid, High; /* 1*/ Low = 0; High = N - 1; /* 2*/ while ( Low <= High ) { /* 3*/ Mid = ( Low + High ) / 2; /* 4*/ if ( A[ Mid ] < X ) /* 5*/ Low = Mid + 1; else /* 6*/ if ( A[ Mid ] > X ) /* 7*/ High = Mid - 1; else /* 8*/ return Mid; /* Found */ } /* end while */ /* 9*/ return NotFound; /* NotFound is defined as -1 */ } §4 Logarithms in the Running Time T(N) = ? T worst ( N ) = O( log N ) Very useful in case the data are static and is in sorted order (e.g. find words from a dictionary). 22/ 26

§4 Logarithms in the Running Time 〖 Example 〗 Euclid’s Algorithm: computing the greatest common divisor (Gcd). unsigned int Gcd ( unsigned int M, unsigned int N ) { unsigned int Rem; /* 1*/ while ( N > 0 ) { /* 2*/ Rem = M % N; /* 3*/ M = N; /* 4*/ N = Rem; } /* end while */ /* 5*/ return M; } T(N) = ? 【 Theorem 】 If M > N, then ( M mod N ) < M / 2. T( N ) = O( log N ) T(N) < 1 + T ( M/2 ) < 2 + T ( N/2 ) … < k + T ( N/2 k ) 23/ 26

§4 Logarithms in the Running Time 〖 Example 〗 Exponentiation: computing X N. long int Pow ( long int X, unsigned int N ) { long int P = 1; while ( N -- ) P *= X; return P; } long int Pow ( long int X, unsigned int N ) { /* 1*/ if ( N == 0 ) /* 2*/ return 1; /* 3*/ if ( N == 1 ) /* 4*/ return X; /* 5*/ if ( IsEven( N ) ) /* 6*/ return Pow( X * X, N / 2 ); else /* 7*/ return Pow( X * X, N / 2 ) * X; } T( N ) = O( N ) T( N ) = O( log N ) Is it necessary ? return Pow( Pow(X, 2), N / 2 ); return Pow( Pow(X, N / 2), 2); return Pow(X, N / 2) * Pow(X, N / 2); 24/ 26

§5 Checking Your Analysis Method 1 When T(N) = O(N), check if T(2N)/T(N)  2 When T(N) = O(N 2 ), check if T(2N)/T(N)  4 When T(N) = O(N 3 ), check if T(2N)/T(N)  8 … Method 2 When T(N) = O( f (N) ), check if Read the example given on p.28 (Figures 2.12 & 2.13). 25/ 26

Laboratory Project 1 Performance Measurement Detailed requirements can be downloaded from Courseware Download Real Programmers don't comment their code. If it was hard to write, it should be hard to understand and harder to modify. I will not read and grade any program which has less than 30% lines commented. Don’t forget to sign you names and duties at the end of your report. Due: Thursday, September 21 st, 2006 at 10:00pm 26/ 26