ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall 2014 1.

Slides:



Advertisements
Similar presentations
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Advertisements

Analysis of Algorithms
Recitation on analysis of algorithms. runtimeof MergeSort /** Sort b[h..k]. */ public static void mS(Comparable[] b, int h, int k) { if (h >= k) return;
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Chapter 10 Algorithm Efficiency
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Complexity Analysis (Part I)
Complexity Analysis (Part II)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Algorithmic Complexity 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY CS2110 – Spring 2015 Lecture 10 size n … Constant time n ops n + 2 ops 2n + 2 ops n*n ops Pic: Natalie.
Algorithm Analysis (Big O)
Analysis of Performance
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Lecture 2 Computational Complexity
CS 3343: Analysis of Algorithms
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Analysis of Algorithms
CS 340Chapter 2: Algorithm Analysis1 Time Complexity The best, worst, and average-case complexities of a given algorithm are numerical functions of the.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
©Silberschatz, Korth and Sudarshan3.1 Algorithms Analysis Algorithm efficiency can be measured in terms of:  Time  Space  Other resources such as processors,
Asymptotic Notation (O, Ω, )
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Analysis of Algorithms CS342 S2004.
Recitation on analysis of algorithms. Formal definition of O(n) We give a formal definition and show how it is used: f(n) is O(g(n)) iff There is a positive.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 – Fall
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
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.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Chapter 2 Algorithm Analysis
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
Asymptotic complexity Searching/sorting
Asymptotic complexity
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall

Readings, Homework Issues 1. How to look at a program and calculate, formally or informally, its execution time. 2. Determine whether some function f(n) is O(g(n)) 2

Worst case for selection / insertion sorts 3 Selection sort b[0..n-1] //inv b[0..i-1] sorted, b[0..i-1] <= b[i..n-1] for (int i= 0; i < n; i= i+1) { int j= pos of min of b[i..n-1]; Swap b[i] and b[j] } Insertion sort b[0..n-1] //inv: b[0..i-1] sorted for (int i= 0; i < n; i= i+1) { Push b[i] down to its sorted; position in b[0..i] } Count swaps? Iteration i requires 1 swap. Total of n Iteration i requires i swaps. Total of … n-1 = (n-1) n / 2 Number of swaps is not the thing to count!

Worst case for selection / insertion sorts 4 Selection sort b[0..n-1] //inv b[0..i-1] sorted, b[0..i-1] <= b[i..n-1] for (int i= 0; i < n; i= i+1) { int j= pos of min of b[i..n-1]; Swap b[i] and b[j] } Insertion sort b[0..n-1] //inv: b[0..i-1] sorted for (int i= 0; i < n; i= i+1) { Push b[i] down to its sorted; position in b[0..i] } Count array element comparisons ALWAYS n-i comparisons Total of (n-1) n / 2 Iteration i requires i comparisons in worst case, 1 in best case. Total of … n-1 = (n-1) n / 2 (worst case)

Find first occurrence of r in s (indexOf) 5 /** = position of first occurrence of r in s (-1 if not in) */ public static int find(String r, String s) { int nr= r.length(); int ns= s.length(); // inv: r is not in s[0..i-1+nr-1] for (int i= 0; i < ns – nr; i= i+1) { if (s.substring(i, i+nr).equals(r)) return i; } return -1; } How much time does this take O(nr) Executed how many times --worst case?ns – nr + 1 Therefore worst-case time is O(nr *(ns –nr + 1)) nr = 1: O(ns). nr = ns: O(ns). nr = ns/2: O(ns*ns)

Dealing with nested loops int c = 0; for (int i= 0; i < n; i++) { for (int j= 0; j < n; j++) { if ((j % 2) == 0) { for (int k= i; k < n; k++) c= c+1; } else { for (int h= 0; h < j; h++) c= c+1; } 6 n iterations True n*n/2 times Loop is executed n/2 times, with i = 0, 1, 2, …, n-1 It has n-i iterations. That’s … n = n*(n+1)/2 its. That’s O(n*n*n)

Dealing with nested loops int i= 0; int c= 0; while (i < n) { int k= i; while (k < n && b[k] == 0) { c= c+1; k= k + 1; } i= k+1; } 7 What is the execution time? It is O(n). It looks at Each element of b[0..n-1] ONCE.

Using Big-O to Hide Constants We say f(n) is order of g(n) if f(n) is bounded by a constant times g(n) Notation: f(n) is O(g(n)) Roughly, f(n) is O(g(n)) means that f(n) grows like g(n) or slower, to within a constant factor "Constant" means fixed and independent of n 8 Formal definition: f(n) is O(g(n)) if there exist constants c and N such that for all n ≥ N, f(n) ≤ c·g(n)

A Graphical View 9 To prove that f(n) is O(g(n)):  Find N and c such that f(n) ≤ c g(n) for all n  N  Pair (c, N) is a witness pair for proving that f(n) is O(g(n)) c·g(n) f(n) N 9

Big-O Examples 10 Let f(n) = 3n 2 + 6n – 7 Prove that f(n) is O(n 2 ) f(n) is O(g(n)) if there exist constants c and N such that for all n ≥ N, f(n) ≤ c·g(n) 3n 2 + 6n – 7 <= c n 2 ? What c? what N? For n >= 1, n <= n 2 3n 2 + 6n – 7 < 3n 2 + 6n = 1 = 9n 2 Choose N = 1 and c = 9

Big-O Examples 11 Let f(n) = 3n 2 + 6n + 7 Prove that f(n) is O(n 2 ) f(n) is O(g(n)) if there exist constants c and N such that for all n ≥ N, f(n) ≤ c·g(n) 3n 2 + 6n + 7 <= c n 2 ? What c? what N? For n >= 1, n <= n 2 3n 2 + 6n + 7 = 1 = 9n = 7 = 10n 2 Choose N = 7 and c = 10

Big-O Examples 12 Let f(n) = 3n 2 + 6n - 7 Prove that f(n) is O(n 3 ) f(n) is O(g(n)) if there exist constants c and N such that for all n ≥ N, f(n) ≤ c·g(n) 3n 2 + 6n – 7 < 3n 2 + 6n = 1 = 9n 2 = 1 Choose N = 1 and c = 9 So, f(n) is O(n 2 ) O(n 3 ), O(n 4 ), …

Big-O Examples 13 T(0) = 1 T(n) = 2 * T(n-1) Give a closed formula (no recursion) for T(n) T(0) = 1 T(1) = 2 T(2) = 4 T(3) = 8 One idea: Look at all small cases and find a pattern T(n) = 2^n

Big-O Examples 14 For quicksort in best case, i.e. two partitions are same size. T(0) = 1 T(1) = 1 T(n) = K*n + 2 * T(n/2) // The Kn is to partition array T(0) = K //Simplify computation: assume K > 1 T(1) = K // And use K instead of 1 T(2^1) = T(2) = 2K + 2K = 4K T(2^2) = T(4) = 4K + 2(4K) = 12K = 3*(2^2)K T(2^3) = T(8) = 8K + 2(12K) = 32K = 4*(2^3)K T(2^4) = T(16) = 16K + 2(32K) = 80K = 5*(2^4)K T(2^n) = (n+1)*(2^n)*KT(m) = log(2m)*m*K