Complexity Analysis (Part I)

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

College of Information Technology & Design
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
MATH 224 – Discrete Mathematics
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
HST 952 Computing for Biomedical Scientists Lecture 10.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
What is an Algorithm? (And how do we analyze one?)
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Complexity Analysis (Part I)
Complexity Analysis (Part I)
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Chapter 1 Algorithm Analysis
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Analysis of Algorithms
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Introduction to Complexity Analysis Motivation Average, Best, and Worst Case Complexity Analysis Asymptotic Analysis.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
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 Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Algorithm Analysis 1.
Mathematical Foundation
Introduction to Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Unit 2 Introduction to Complexity Analysis
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Big-O notation.
Lecture – 2 on Data structures
Lecture 3 of Computer Science II
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Course Description Algorithms are: Recipes for solving problems.
Objective of This Course
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
Analysis of Algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
GC 211:Data Structures Algorithm Analysis Tools
Searching, Sorting, and Asymptotic Complexity
Analysis of Algorithms
Revision of C++.
Analysis of Algorithms
Algorithmic complexity
Course Description Algorithms are: Recipes for solving problems.
Complexity Analysis (Part I)
Analysis of Algorithms
Analysis of Algorithms
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
Presentation transcript:

Complexity Analysis (Part I) Motivations for Complexity Analysis. Example of Basic Operations Average, Best, and Worst Cases. Simple Complexity Analysis Examples.

Motivations for Complexity Analysis There are often many different algorithms which can be used to solve the same problem. Thus, it makes sense to develop techniques that allow us to: compare different algorithms with respect to their “efficiency” choose the most efficient algorithm for the problem The efficiency of any algorithmic solution to a problem is a measure of the: Time efficiency: the time it takes to execute. Space efficiency: the space (primary or secondary memory) it uses. We will focus on an algorithm’s efficiency with respect to time.

Machine independence The evaluation of efficiency should be as machine independent as possible. It is not useful to measure how fast the algorithm runs as this depends on which particular computer, OS, programming language, compiler, and kind of inputs are used in testing Instead, we count the number of basic operations the algorithm performs. we calculate how this number depends on the size of the input. A basic operation is an operation which takes a constant amount of time to execute. Hence, the efficiency of an algorithm is the number of basic operations it performs. This number is a function of the input size n.

Example of Basic Operations: Arithmetic operations: *, /, %, +, - Assignment statements of simple data types. Reading of primitive types writing of a primitive types Simple conditional tests: if (x < 12) ... method call (Note: the execution time of the method itself may depend on the value of parameter and it may not be constant) a method's return statement Memory Access We consider an operation such as ++ , += , and *= as consisting of two basic operations. Note: To simplify complexity analysis we will not consider memory access (fetch or store) operations.

Best, Average, and Worst case complexities We are usually interested in the worst case complexity: what are the most operations that might be performed for a given problem size. We will not discuss the other cases -- best and average case. Best case depends on the input Average case is difficult to compute So we usually focus on worst case analysis Easier to compute Usually close to the actual running time Crucial to real-time systems (e.g. air-traffic control)

Best, Average, and Worst case complexities Example: Linear Search Complexity Best Case : Item found at the beginning: One comparison Worst Case : Item found at the end: n comparisons Average Case :Item may be found at index 0, or 1, or 2, . . . or n - 1 Average number of comparisons is: (1 + 2 + . . . + n) / n = (n+1) / 2 Worst and Average complexities of common sorting algorithms Method Worst Case Average Case Selection sort n2 Inserstion sort Merge sort n log n Quick sort

Simple Complexity Analysis: Loops We start by considering how to count operations in for-loops. We use integer division throughout. First of all, we should know the number of iterations of the loop; say it is x. Then the loop condition is executed x + 1 times. Each of the statements in the loop body is executed x times. The loop-index update statement is executed x times.

Simple Complexity Analysis: Loops (with <) In the following for-loop: The number of iterations is: (n – k ) / m The initialization statement, i = k, is executed one time. The condition, i < n, is executed (n – k) / m + 1 times. The update statement, i = i + m, is executed (n – k) / m times. Each of statement1 and statement2 is executed (n – k) / m times. for (int i = k; i < n; i = i + m){ statement1; statement2; }

Simple Complexity Analysis : Loops (with <=) In the following for-loop: The number of iterations is: (n – k) / m + 1 The initialization statement, i = k, is executed one time. The condition, i <= n, is executed (n – k) / m + 2 times. The update statement, i = i + m, is executed (n – k) / m + 1 times. Each of statement1 and statement2 is executed (n – k) / m + 1 times. for (int i = k; i <= n; i = i + m){ statement1; statement2; }

Simple Complexity Analysis: Loop Example Find the exact number of basic operations in the following program fragment: There are 2 assignments outside the loop => 2 operations. The for loop actually comprises an assignment (i = 0) => 1 operation a test (i < n) => n + 1 operations an increment (i++) => 2 n operations the loop body that has three assignments, two multiplications, and an addition => 6 n operations Thus the total number of basic operations is 6 * n + 2 * n + (n + 1) + 3 = 9n + 4 double x, y; x = 2.5 ; y = 3.0; for(int i = 0; i < n; i++){ a[i] = x * y; x = 2.5 * x; y = y + a[i]; }

Simple Complexity Analysis: Examples Suppose n is a multiple of 2. Determine the number of basic operations performed by of the method myMethod(): Solution: The number of iterations of the loop: for(int i = 1; i < n; i = i * 2) sum = sum + i + helper(i); is log2n (A Proof will be given later) Hence the number of basic operations is: 1 + 1 + (1 + log2 n) + log2 n[2 + 4 + 1 + 1 + (n + 1) + n[2 + 2] + 1] + 1 = 3 + log2 n + log2 n[10 + 5n] + 1 = 5 n log2 n + 11 log2 n + 4 static int myMethod(int n){ int sum = 0; for(int i = 1; i < n; i = i * 2) sum = sum + i + helper(i); return sum; } static int helper(int n){ int sum = 0; for(int i = 1; i <= n; i++) sum = sum + i; return sum; }

Simple Complexity Analysis: Loops With Logarithmic Iterations In the following for-loop: (with <) The number of iterations is: (Logm (n / k) ) In the following for-loop: (with <=) The number of iterations is:  (Logm (n / k) + 1)  for (int i = k; i < n; i = i * m){ statement1; statement2; } for (int i = k; i <= n; i = i * m){ statement1; statement2; }