Algorithm Analysis CS 201 Fundamental Structures of Computer Science.

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 10.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
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.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
The Efficiency of Algorithms
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Cpt S 223 – Advanced Data Structures
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.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Analysis of Performance
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.
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.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
{ 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)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
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.
CE 221 Data Structures and Algorithms Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – Izmir University of Economics.
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.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structure and Algorithm Analysis 02: Algorithm Analysis Hongfei Yan School of EECS, Peking University 3/12/2014.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithm Analysis (not included in any exams!)
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Analysis of Algorithms
Programming and Data Structure
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

Algorithm Analysis CS 201 Fundamental Structures of Computer Science

Introduction Once an algorithm is given for a problem and decided to be correct, an important step is to determine how much in the way of resources (such as time or space) the algorithm will require. In this chapter, we shall discuss –How to estimate the time required for a program

Mathematical background The idea of the following definitions is to establish a relative order among functions. Given two functions, there are usually points where one function is smaller than the other function. –It does not make sense to claim f(N) < g(N) –Let us compare f(N) = 1000N and g(N) = N 2 –Thus, we will compare their relative rates of growth

Mathematical background Definition 1:

Mathematical background Definition 2:

Mathematical background Definition 3:

Examples f(N)=N 3 grows faster than g(N)=N 2, so g(N) = O(f(N)) or f(N) =  (g(N)) f(N)=N 2 and g(N)=2N 2 grow at the same rate, so f(N) = O(g(N)) and f(N) =  (g(N)) If g(N)=2N 2, g(N)=O(N 4 ), g(N)=O(N 3 ), g(N)=O(N 2 ) are all technically correct, but the last one is the best answer. Do not say T(N)=O(2N 2 ) or T(N)=O(N 2 +N). The correct form is T(N)=O(N 2 ).

Mathematical background Rule 1: Rule 2: Rule 3:

Examples Determine which of f(N)=N logN and g(N)=N 1.5 grows faster.  Determine which of logN and N 0.5 grows faster.  Determine which of log 2 N and N grows faster.  Since N grows faster than any power of a log, g(N) grows faster than f(N).

Examples Consider the problem of downloading a file over the Internet. –Setting up the connection: 3 seconds –Download speed: 1.5 Kbytes/second If a file is N kilobytes, the time to download is T(N) = N/ , i.e., T(N) = O(N). –1500K file takes 1003 seconds –750K file takes 503 seconds If the connection speed doubles, both times decrease, but downloading 1500K still takes approximately twice the time downloading 750K.

Typical growth rates FunctionName cConstant log NLogarithmic log 2 NLog-squared NLinear N log N N2N2 Quadratic N3N3 Cubic 2N2N Exponential

Plots of various algorithms

Algorithm analysis The most important resource to analyze is generally the running time –We assume that simple instructions (such as addition, comparison, and assignment) take exactly one unit time Unlike the case with real computers –For example, I/O operations take more time compared to comparison and arithmetic operators –Obviously, we do not have this assumption for fancy operations such as matrix inversion, list insertion, and sorting. –We assume infinite memory –We do not include the time required to read the input

Algorithm analysis Typically, the size of the input is the main consideration –Worst-case performance represents a guarantee for performance on any possible input –Average-case performance often reflects typical behavior –Best-case performance is often of little interest Generally, it is focused on the worst-case analysis –It provides a bound for all inputs –Average-case bounds are much more difficult to compute

Algorithm analysis Although using Big-Theta would be more precise, Big-Oh answers are typically given. Big-Oh answer is not affected by the programming language –If a program is running much more slowly than the algorithm analysis suggests, there may be an implementation inefficiency e.g., This can occur in C++ when arrays are inadvertently copied in their entirety instead of passed with references.

Algorithm analysis If two programs are expected to take similar times, probably the best way to decide which is faster to code them both up and run them! On the other hand, we would like to eliminate the bad algorithmic ideas early by algorithm analysis –Although different instructions can take different amounts of time (these would correspond to constants in Big-Oh notation), we ignore this difference in our analysis and try to find the upper bound of the algorithm

A simple example Estimate the running time of the following function int sum(int n){ int partialSum = 0; for (int i = 0; i <= n; i++) partialSum += i * i * i; return partialSum; }

General rules Rule 1 – 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 Rule 2 – nested loops: Analyze these inside out. The total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the sizes of all the loops for (i = 0; i < n; i++) for (j = i; j < n; j++) k++;

General rules Rule 3 – consecutive statements: just add. for (i = 0; i < n; i++) a[i] = 0; for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i] += a[j] + i + j; Rule 4 – if/else: For the following, the running time is never more than that of the test plus the larger of that of S1 and S2 if (condition) S1 else S2

General rules If there are function calls, these must be analyzed first If there are recursive functions, be careful about their analyses. For some recursions, the analysis is trivial long factorial(int n){ if (n <= 1) return 1; return n * factorial(n-1); }

General rules Let us analyze the following recursion long fib(int n){ if (n <= 1) return 1; return fib(n-1) + fib(n-2); } By induction, it is possible to show that So the running time grows exponentially. By using a for loop, the running time can be reduced substantially

Sequential search index = -1; for (i = 0; i < N; i++) if (A[i] == value){ index = i; break; } Worst-case: It is the last element or it is not found –N iterations  O(N) Best-case: It is the first element –1 iteration  O(1) Average case: We may have N different cases –(N + 3) / 2 iterations  O(N)

Searching a value in a sorted array using binary search int binarySearch(int *A, int N, int value){ int low = 0, high = N; while (low <= high){ int mid = (low + high) / 2; if (a[mid] < value) low = mid + 1; else if (a[mid] > value) high = mid - 1; else return mid; } return -1; }

Iterative solution of raising an integer to a power long pow1(long x, long n){ long result = 1; for (int i = 1; i <= n; i++) result = result * x; return result; }

Recursive solution of raising an integer to a power long pow2(long x, long n){ if (n == 0) return 1; if (n == 1) return x; if (n % 2 == 0) return pow2(x*x,n/2); else return pow2(x*x,n/2) * x; }