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.
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
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.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Performance
Chapter 1 Algorithm Analysis
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
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.
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
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
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.
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.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
3.3 Complexity of Algorithms
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
Algorithm Analysis (Big O)
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.
Searching Topics Sequential Search Binary Search.
Introduction to Complexity Analysis Motivation Average, Best, and Worst Case Complexity Analysis Asymptotic Analysis.
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.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
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.
Complexity Analysis (Part I)
Mathematical Foundation
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Unit 2 Introduction to Complexity Analysis
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Searching, Sorting, and Asymptotic Complexity
Analysis of Algorithms
Revision of C++.
Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms
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; }