Data Structure Algorithm Analysis TA: Abbas Sarraf

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
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
Complexity Analysis (Part I)
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Lecture 3. kf(n) is O(f(n)) for any positive constant k f(n) + g(n) is O(f(n)) if g(n) is O(f(n)) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
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.
Analysis of Algorithm.
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.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
Algorithmic Analysis. Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount.
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Program Performance & Asymptotic Notations CSE, POSTECH.
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.
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. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Analysis of Algorithms Aaron Tan
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
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.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
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.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Vishnu Kotrajaras, PhD.1 Data Structures
ALGORITHM ANALYSIS Analysis of Resource consumption by processor –Time –Memory –Number of processors –Power Proof that the algorithm works correctly Debasis.
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Analysis of Algorithms
Programming and Data Structure
Presentation transcript:

Data Structure Algorithm Analysis TA: Abbas Sarraf

Objectives How to predict an algorithm’s performance How well an algorithm scales up How to compare different algorithms for a problem

Asymptotic Notations Big-O, “bounded above by”: T(n) = O(f(n)) – For some c and N, T(n)  c·f(n) whenever n > N. Big-Omega, “bounded below by”: T(n) =  (f(n)) – For some c>0 and N, T(n)  c·f(n) whenever n > N. – Same as f(n) = O(T(n)). Big-Theta, “bounded above and below”: T(n) =  (f(n)) – T(n) = O(f(n)) and also T(n) =  (f(n))

By Pictures Big-Oh (most commonly used) – bounded above Big-Omega – bounded below Big-Theta – exactly 0 N

Examples  What is the big-O of 2 n n + 5 ?  T(n) = 2 n n + 5; f(n) = n 2  T(n) <= c f(n) ? 2 n n + 5 <= c n 2 ?  for c = 3, n0 = 1001,check: 2( ) (1001) + 5 = ;3( ) =  answer: O(n 2 ) 6. An algorithm actually takes n 5 + n 3 + 7, we say its complexity is O(n 5 ). ( proof in the book, p. 54 ) 7. This is only for addition! Obviously if the algorithm takes n 5 * n 3 its complexity is O(n 8 )!

Cost sum = 0; ---> 1 sum = sum + next; ---> 1 Total Cost: 2 Cost for( int i = 1; i 1 + n+1 + n = 2n+2 sum = sum++; ---> n Total Cost: 3n + 2 Cost k = 0 ---> 1 for( int i = 0; i 2n+2 for( int j = 0; j n(2n+2) = 2n 2 +2n k++; ---> n 2 Total Cost: 3n 2 + 4n + 3

n Both algorithms are O(n).

n Time complexity: O  n 3  int maxSum = 0; for( int i = 0; i < a.size( ); i++ ) for( int j = i; j < a.size( ); j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum; Example

2n2n n2n2 n log n n log n n n log n n2n2 n3n3 n3n3 2n2n

Class O(1) Function/order – Constant time Examples – Find the ith element in an array. – A[i] Remarks – The running time of the algorithm doesn't depend on the value of n.

Class O(log a n) Function/order – Logarithmic time Examples – binary search Remarks – Typically achieved by dividing the problem into smaller segments and only looking at one input element in each segment. – Binary Search: Every time you go through the recursion (binary search uses recursion), the problem is reduced in half. So the most amount of times you can go through the recursion is log 2 n.

Class O(n) Function/order – Linear time Examples – Find the minimum element, printing, listing Remarks – Typically achieved by examining each element in the input once. – The running time of the algorithm is proportional to the value of n.

Class O(n * log(n)) Examples – heapsort, mergesort Remarks – Typically achieved by dividing the problem into subproblems, solving the subproblems independently, and then combining the results. Unlike the log N algorithms, each element in the subproblems must be examined.

Class O(n 2 ) Function/order – Quadratic time Examples – bubblesort, insertion sort Remarks – Typically achieved by examining all pairs of data elements – Comparisons : ( n) = n(n + 1)/2 = n 2 /2 + n/2 = O(n 2 )

Example -> what if “if”?! sum = 0; for (i = 0; i < n; i++) { if (is_even(i)) { for (j = 0; j < n; j++) sum++; } else sum = sum + n; } O( n 2 ) : outer loop is O(n) inside the loop: if “true” clause executed for half the values of n -> O(n),if “false” clause executed for other half -> O(1);the innermost loop is O(n),so the complexity is n(n + 1) = O( n 2 )

Average, Best, and Worst-Case Insertion Sort:

we let t j be the number of times the while loop test in line 5 is executed for that value of j.

Insertion Sort Best Case: ( t j = 1 ) T(n) = c 1 n + c 2 (n - 1) + c 4 (n - 1) + c 5 (n - 1) + c 8 (n - 1) = (c 1 + c 2 + c 4 + c 5 + c 8 )n - (c 2 + c 4 + c 5 + c 8 ). = O(n)

Insertion Sort Worst Case? – t j = i Average? (Probabilistic ) – E{ t j } = i / 2 (how?)

Average, Best, and Worst-Case On which input instances should the algorithm’s performance be judged? Average case: – Real world distributions difficult to predict Best case: – Seems unrealistic Worst case: – Gives an absolute guarantee – We will use the worst-case measure.

Example: Given A 1,…,A n, find the maximum value of A i +A i+1 +···+A j 0 if the max value is negative n Time complexity: O  n 3  int maxSum = 0; for( int i = 0; i < a.size( ); i++ ) for( int j = i; j < a.size( ); j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum;

Algorithm 2 Idea: Given sum from i to j-1, we can compute the sum from i to j in constant time. This eliminates one nested loop, and reduces the running time to O(n 2 ). into maxSum = 0; for( int i = 0; i < a.size( ); i++ ) int thisSum = 0; for( int j = i; j < a.size( ); j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum;

Algorithm 3 Time complexity clearly O(n) But why does it work? I.e. proof of correctness. 2, 3, -2, 1, -5, 4, 1, -3, 4, -1, 2 int maxSum = 0, thisSum = 0; for( int j = 0; j < a.size( ); j++ ) { thisSum += a[ j ]; if ( thisSum > maxSum ) maxSum = thisSum; else if ( thisSum < 0 ) thisSum = 0; } return maxSum;

Proof of Correctness Max subsequence cannot start or end at a negative Ai. More generally, the max subsequence cannot have a prefix with a negative sum. Ex: Thus, if we ever find that Ai through Aj sums to < 0, then we can advance i to j+1 – Proof. Suppose j is the first index after i when the sum becomes < 0 – The max subsequence cannot start at any p between i and j. Because A i through A p-1 is positive, so starting at i would have been even better.

Algorithm 3 int maxSum = 0, thisSum = 0; for( int j = 0; j < a.size( ); j++ ) { thisSum += a[ j ]; if ( thisSum > maxSum ) maxSum = thisSum; else if ( thisSum < 0 ) thisSum = 0; } return maxSum The algorithm resets whenever prefix is < 0. Otherwise, it forms new sums and updates maxSum in one pass