Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.

Slides:



Advertisements
Similar presentations
 O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop.
Advertisements

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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 (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
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.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Complexity Analysis Chapter 1.
Analysis of Algorithms
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis (Big O)
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Searching Topics Sequential Search Binary Search.
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.
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.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithm Analysis (not included in any exams!)
Building Java Programs
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Programming and Data Structure
Revision of C++.
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithmic complexity
Analysis of Algorithms
Presentation transcript:

Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu

How to measure algorithm efficiency? Space utilization: amount of memory required Time efficiency: amount of time required to accomplish the task As space is not a problem nowadays Time efficiency is more emphasized But, in embedded computers or sensor nodes, space efficiency is still important

Time efficiency Time efficiency depends on : size of input speed of machine quality of source code quality of compiler These vary from one platform to another So, we cannot express time efficiency meaningfully in real time units such as seconds!!!

Algorithm efficiency But, we can count the number of times instructions are executed This gives us a measurement of efficiency of an algorithm So we measure computing time T(n) as T(n) = number of times the instructions are executed T(n): computing time of an algorithm for input of size n

Example: calculating a mean Task # times executed 1. Initialize the sum to Initialize index i to While i < n do following n+1 4. a) Add x[i] to sumn 5. b) Increment i by 1n 6. Return mean = sum/n1 Total 3n + 4

Computing time order of magintude As number of inputs increases  T(n) = 3n + 4 grows at a rate proportional to n Thus T(n) has the "order of magnitude" n The computing time of an algorithm on input of size n,  T(n) said to have order of magnitude f(n),  written T(n) is O(f(n)) if … there is some constant C such that  T(n) < C  f(n) for all sufficiently large values of n

Big Oh Notation Another way of saying this: The complexity of the algorithm is O(f(n)). Example: For the Mean-Calculation Algorithm:

Mean-Calculation Algorithm [1] int sum = 0; [2] int i = 0; [3] While(i < n) { [4] sum += a[i]; [5] i++; } [6] return sum/n;

Big Oh Notation Another way of saying this: The complexity of the algorithm is O(f(n)). Example: For the Mean-Calculation Algorithm: T(n) is O(n) Note that constants and multiplicative factors are ignored. Simple function f(n)!!!

Measure T(n)… Not only depends on the input size, but also depends on the arrangement of the input items Best case: not informative Average value of T: difficult to determine Worst case: is thus used to measure an algorithm’s performance

Get a taste int search (int a[n], int x) { // pseudocode for search x for (int i=0; i<n; i++) if (a[i]==x) return i; return -1; } // assuming the elements in a[] are unique Complexity analysis T(n): can you figure it out ? -- best case: ? -- average value: ? -- worst case: ?

Get a Taste Answer: Best case: O(1) Average case: O(n) Worst case: O(n) How to get this???

Simple Selection Sorting Algorithm,p554 // Algorithm to sort x[0]…x[n-1] into ascending order 1. for (int i=0; i<n-1; i++) { //On the ith pass, first find the smallest element in the sublist x[i],…,x[n-1]. */ 2. int spos = i; 3. int smallest = x[spos]; 4. for (int j=i+1; j<n; j++) { 5. if (x[j] < smallest) { 6. spos = j; 7. smallest = x[j]; } // end if } // end for 8. x[spos] = x[i]; 9. X[i] = smallest }// end for

What’s the worst case T(n)? T(n) = O(n 2 ) How do we get that? Let’s try!

Exercise n = 0; sum = 0; cin >> x; while (x != -999) { n++; sum += x; cin >> x; } Mean = sum / n;

Exercise Explain why, if T(n) is O(n), then it is also correct to say T(n) is O(n^2).

Simplifying the complexity analysis Non-trivial computation in the preceding example Simplifying the Big-O estimating Identify the statement executed most often and determine its execution count (statement 4) Ignore items with lower degree Only the highest power of n that affects Big-O estimate Big-O estimate gives an approximate measure of the computing time of an algorithm for large inputs

Binary Search Algorithm int bin_search(int a[], int item) // a [0… n-1] bool found = false; int first = 0; int last = n-1; while (first <= last and !found) { int loc = (first + last) / 2; if (item < a[loc]) last = loc – 1; else if (item > a[loc]) first = loc + 1; else found = !found; } //end while } //end bin_search

Binary Search Algorithm See p556. Step 1: identify the statement executed most often Step 2: determine the execution count for that statement (the worst case!!!) T(n)= O(log 2 n)

Big-Oh notation f(n) is usually simple: n, n 2, n 3,... 2^n 1, log 2 n n log 2 n log 2 log 2 n Note graph of common computing times

Common Computing Time Functions log 2 log 2 nlog 2 nnn log 2 nn2n2 n3n3 2n2n E E E+091.8E E E+186.7E

How about recursive algorithms? double power(double x, unsigned n) { if (n==0) return 1.0; return x * power(x, n-1); }

How to compute Big-Oh? Recurrence relation: expresses the computing time for input of size n in terms of smaller- sized inputs How to solve recurrence relations? Using telescoping principle: repeatedly apply the relation until the anchor case is reached

Exercises (1) for (int j = 4; j < n; ++j) { cin >> val; for (int i = 0; i < j; ++i) { b = b * val; for (int k = 0; k < n; ++k) c = b + c; }

Exercises (2) for (int i = 0; i < n * n; ++i) { sum = sum/n; for (int j = 0; j < i; ++j) j >> cout; }

Exercise 3 for (int i = 1; i<n-1; i++) { temp = a[i]; for (int j = i-1; j >= 0; j--) if (temp < a[j]) a[j+1] = a[j]; else break; a[j+1] = temp; }

Exercise 4 double fun(double x, unsigned n) { if (n==0) return 1.0; return x*fun(x, n/2); }

Review of Lecture How to measure algorithm efficiency? How to compute time efficiency? Big-Oh notation Given an algorithms, tell me the Big-Oh notation Recursive: telescoping principle