Asymptotic Notation (O, Ω, )

Slides:



Advertisements
Similar presentations
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Data Structures & Algorithms What The Course Is About s Data structures is concerned with the representation and manipulation of data. s All programs.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Data Structures Performance Analysis.
Insertion Sort for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];
Time Complexity s Sorting –Insertion sorting s Time complexity.
Elementary Data Structures and Algorithms
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1]
Algorithm Analysis (Big O)
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.
Algorithmic Complexity: Complexity Analysis of Time Complexity Complexities Nate the Great.
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
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.
Data Structures, Algorithms, & Applications
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Lecture 2 Computational Complexity
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.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Analysis of Algorithms
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Data Structures Engr. Umbreen sabir What The Course Is About s Data structures is concerned with the representation and manipulation of data. s All programs.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 5.
Asymptotic Notations By Er. Devdutt Baresary. Introduction In mathematics, computer science, and related fields, big O notation describes the limiting.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Data Structures and Algorithms Dr. Manuel E. Bermudez Alter ego to Dr. Sartaj Sahni.
Algorithm Analysis (Big O)
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Data Structures, Algorithms, & Applications Instructor: Babak Alipour Slides and book: Sartaj Sahni.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Big-O. Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1.
Analysis of Algorithms
Introduction to Algorithms
Source: wikipedia
Source:
Asymptotic Notations Algorithms Lecture 9.
Insertion Sort for (int i = 1; i < a.length; i++)
Asymptotes: Why? How to describe an algorithm’s running time?
CSC 413/513: Intro to Algorithms
Analysys & Complexity of Algorithms
Sorting Rearrange a[0], a[1], …, a[n-1] into ascending order.
More on Asymptotic Analysis and Performance Measurement
Insertion Sort for (int i = 1; i < n; i++)
Insertion Sort for (int i = 1; i < n; i++)
Presentation transcript:

Asymptotic Notation (O, Ω, ) Describes the behavior of the time or space complexity for large instance characteristics Common asymptotic functions 1 (constant), log n, n (linear) n log n, n2 , n3 2n ( exponential), n! where n usually refer to the number of instance of data (data的個數)

Common asymptotic functions

Common asymptotic functions Other types of complexity: programming complexity, debugging complexity, complexity of comprehension.

Common asymptotic functions

Common asymptotic functions

Big-O notation The big O notation provides an upper bound for the function f Definition: f(n) = O(g(n)) iff positive constants c and n0 exist such that f(n)  c g(n) for all n, nn0 e.g. f(n) = 10n2 + 4n + 2 then, f(n) = O(n2), or O(n3), O(n4), …

Ω(Omega) notation The Ω notation provides a lower bound for the function f Definition: f(n) = Ω(g(n)) iff positive constants c and n0 exist such that f(n)  c g(n) for all n, nn0 e.g. f(n) = 10n2 + 4n + 2 then, f(n) = Ω(n2), or Ω(n), Ω(1)

 notation The  notation is used when the function f can be bounded both from above and below by the same function g Definition: f(n) =  (g(n)) iff positive constants c1and c2, and an n0 exist such that c1g(n)  f(n)  c2g(n) for all n, nn0 e.g. f(n) = 10n2 + 4n + 2 then, f(n) = Ω(n2), and f(n)=O(n2), therefore, f(n)=  (n2)

Sorting Rearrange a[0], a[1], …, a[n-1] into ascending order. When done, a[0] <= a[1] <= … <= a[n-1] 8, 6, 9, 4, 3 => 3, 4, 6, 8, 9 Strictly speaking, nondecreasing order: 2, 2, 3, 6, 8, 8, 10.

Sort Methods Insertion Sort Bubble Sort Selection Sort Rank Sort Shell Sort Heap Sort Merge Sort Quick Sort First 5 are simple (conceptually and to program). First 4 done in chapters 2 and 3. Shaker sort and shell sort are in the enrichment section of the text’s Web site. The remaining three are done in the algorithm design chapters of the text.

Insert An Element Given a sorted list/sequence, insert a new element Result 3, 5, 6, 9, 14

Insert an Element 3, 6, 9, 14 insert 5 Compare new element (5) and last one (14) Shift 14 right to get 3, 6, 9, , 14 Shift 9 right to get 3, 6, , 9, 14 Shift 6 right to get 3, , 6, 9, 14 Insert 5 to get 3, 5, 6, 9, 14

Insert An Element // insert t into a[0:i-1] int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;

Insertion Sort Start with a sequence of size 1 Repeatedly insert remaining elements

Insertion Sort Sort 7, 3, 5, 6, 1 Start with 7 and insert 3 => 3, 7

Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] // code to insert comes here }

Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

Insertion Sort (C++)

Complexity Space/Memory Time Count a particular operation Count number of steps Asymptotic complexity Other types of complexity: programming complexity, debugging complexity, complexity of comprehension.

Comparison Count for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

Comparison Count Pick an instance characteristic … n, n = a.length for insertion sort Determine count as a function of this instance characteristic.

Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; How many comparisons are made?

Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; number of compares depends on a[]s and t as well as on n

Comparison Count Worst case count = maximum count Best case count = minimum count Average count

Worst-Case Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares

Worst-Case Comparison Count for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; total compares = 1 + 2 + 3 + … + (n-1) = (n-1)n/2

Step Count A step is an amount of computing that does not depend on the instance characteristic n 10 adds, 100 subtracts, 1000 multiplies can all be counted as a single step n adds cannot be counted as 1 step

Step Count s/e for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } for (int i = 1; i < a.length; i++) 1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0

Step Count s/e isn’t always 0 or 1 x = MyMath.sum(a, n); where n is the instance characteristic has a s/e count of n

Step Count i+ 1 i s/e steps for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } for (int i = 1; i < a.length; i++) 1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0 i+ 1 i

Step Count for (int i = 1; i < n ; i++) { 2i + 3} step count for is n step count for body of for loop is 2(1+2+3+…+n-1) + 3(n-1) = (n-1)n + 3(n-1) = (n-1)(n+3)

Asymptotic Complexity of Insertion Sort What does this mean?

Complexity of Insertion Sort Time or number of operations does not exceed c·n2 on any input of size n (n suitably large). Actually, the worst-case time is Θ(n2) and the best- case is Θ(n) So, the worst-case time is expected to quadruple each time n is doubled

Complexity of Insertion Sort Is O(n2) too much time? Is the algorithm practical?

Practical Complexities 109 instructions/second Teraflop computer the 32yr time becomes approx 10 days.

Impractical Complexities 109 instructions/second

Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2n to n3