SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.

Slides:



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

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 3 Growth of Functions
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)
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
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)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 – Fall
Elementary Data Structures and Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
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.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Cost Algorithm Complexity. Algorithm Cost.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
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
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
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,
Analysis of Algorithms
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
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 – Fall
Algorithm Analysis (Big O)
Scalability for Search Scaling means how a system must grow if resources or work grows –Scalability is the ability of a system, network, or process, to.
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.
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.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
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.
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.
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
Complexity Analysis (Part I)
Chapter 2 Algorithm Analysis
Mathematical Foundation
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to Algorithms
Searching, Sorting, and Asymptotic Complexity
Fundamentals of the Analysis of Algorithm Efficiency
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
Asymptotic complexity Searching/sorting
Asymptotic complexity
Estimating Algorithm Performance
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009

What Makes a Good Algorithm? 2  Suppose you have two possible algorithms or data structures that basically do the same thing; which is better?  Well… what do we mean by better?  Faster?  Less space?  Easier to code?  Easier to maintain?  Required for homework?  How do we measure time and space for an algorithm?

Sample Problem: Searching 3 /** return true iff v is in a */ static boolean find(int[] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) return true; } return false; }  Determine if sorted array a contains integer v  First solution: Linear Search (check each element) static boolean find(int[] a, int v) { for (int x : a) { if (x == v) return true; } return false; }

Sample Problem: Searching 4 static boolean find (int[] a, int v) { int low= 0; int high= a.length - 1; while (low <= high) { int mid = (low + high)/2; if (a[mid] == v) return true; if (a[mid] < v) low= mid + 1; else high= mid - 1; } return false; } Second solution: Binary Search Keep true: all occurrences of v are in b[low..high] Still returning true iff v is in a

Linear Search vs Binary Search 5 Which one is better?  Linear: easier to program  Binary: faster… isn’t it? How do we measure speed?  Experiment?  Proof?  What inputs do we use?  Simplifying assumption #1: Use size of input rather than input itself  For sample search problem, input size is n+1 where n is array size  Simplifying assumption #2: Count number of “basic steps” rather than computing exact times

One Basic Step = One Time Unit 6 Basic step:  Input/output of scalar value  Access value of scalar variable, array element, or object field  assign to variable, array element, or object field  do one arithmetic or logical operation  method invocation (not counting arg evaluation and execution of method body)  For conditional: number of basic steps on branch that is executed  For loop: (number of basic steps in loop body) * (number of iterations)  For method: number of basic steps in method body (include steps needed to prepare stack-frame)

Runtime vs Number of Basic Steps 7 Is this cheating?  The runtime is not the same as number of basic steps  Time per basic step varies depending on computer, compiler, details of code… Well … yes, in a way  But the number of basic steps is proportional to the actual runtime Which is better?  n or n 2 time?  100 n or n 2 time?  10,000 n or n 2 time? As n gets large, multiplicative constants become less important Simplifying assumption #3: Ignore multiplicative constants

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  Example: (n 2 + n) is O(n 2 )  We know n ≤ n 2 for n ≥1  So n 2 + n ≤ 2 n 2 for n ≥1  So by definition, n 2 + n is O(n 2 ) for c=2 and N=1 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

Big-O Examples 10 Claim: 100 n + log n is O(n) We know log n ≤ n for n ≥ 1 So 100 n + log n ≤ 101 n for n ≥ 1 So by definition, 100 n + log n is O(n) for c = 101 and N = 1 Claim: log B n is O(log A n) since log B n is (log B A)(log A n) Question: Which grows faster: n or log n?

Big-O Examples 11 Let f(n) = 3n 2 + 6n – 7  f(n) is O(n 2 )  f(n) is O(n 3 )  f(n) is O(n 4 )  … g(n) = 4 n log n + 34 n – 89  g(n) is O(n log n)  g(n) is O(n 2 ) h(n) = 20·2 n + 40n h(n) is O(2 n ) a(n) = 34  a(n) is O(1) Only the leading term (the term that grows most rapidly) matters

Problem-Size Examples 12  Consisider a computing device that can execute 1000 operations per second; how large a problem can we solve? 1 second1 minute1 hour n100060,0003,600,000 n log n ,000 n2n n n3n n2n 91521

Commonly Seen Time Bounds 13 O(1)constantexcellent O(log n)logarithmicexcellent O(n)lineargood O(n log n)n log npretty good O(n 2 )quadraticOK O(n 3 )cubicmaybe OK O(2 n )exponentialtoo slow

Worst-Case/Expected-Case Bounds 14 We can’t possibly determine time bounds for all possible inputs of size n Simplifying assumption #4: Determine number of steps for either  worst-case or  expected-case  Worst-case  Determine how much time is needed for the worst possible input of size n  Expected-case  Determine how much time is needed on average for all inputs of size n

Simplifying Assumptions 15 Use the size of the input rather than the input itself – n Count the number of “basic steps” rather than computing exact time Ignore multiplicative constants and small inputs (order-of, big-O) Determine number of steps for either  worst-case  expected-case These assumptions allow us to analyze algorithms effectively

Worst-Case Analysis of Searching 16 Linear Search /** return true iff v is in a */ static bool find (int[] a, int v) { for (int x : a) { if (x == v) return true; } return false; } worst-case time: O(n) Binary Search static bool find (int[] a, int v) { int low= 0; int high= a.length – 1; while (low <= high) { int mid = (low + high)/2; if (a[mid] == v) return true; if (a[mid] < v) low= mid + 1; else high= mid - 1; } return false; } worst-case time: O(log n)

Comparison of Algorithms 17

Comparison of Algorithms 18

Comparison of Algorithms 19

Analysis of Matrix Multiplication 20 Multiply n-by-n matrices A and B: Convention, matrix problems measured in terms of n, the number of rows, columns  Input size is really 2n 2, not n  Worst-case time: O(n 3 )  Expected-case time:O(n 3 ) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { c[i][j] = 0; for (k = 0; k < n; k++) c[i][j] += a[i][k]*b[k][j]; }

Remarks 21 Once you get the hang of this, you can quickly zero in on what is relevant for determining asymptotic complexity  Example: you can usually ignore everything that is not in the innermost loop. Why? Main difficulty:  Determining runtime for recursive programs

Why Bother with Runtime Analysis? 22 Computers so fast that we can do whatever we want using simple algorithms and data structures, right? Not really – data- structure/algorithm improvements can be a very big win Scenario:  A runs in n 2 msec  A' runs in n 2 /10 msec  B runs in 10 n log n msec Problem of size n=10 3  A: 10 3 sec ≈ 17 minutes  A': 10 2 sec ≈ 1.7 minutes  B: 10 2 sec ≈ 1.7 minutes Problem of size n=10 6  A: 10 9 sec ≈ 30 years  A': 10 8 sec ≈ 3 years  B: 2·10 5 sec ≈ 2 days 1 day = 86,400 sec ≈ 10 5 sec 1,000 days ≈ 3 years

Algorithms for the Human Genome 23 Human genome = 3.5 billion nucleotides ~ 1 base-pair instruction/  sec  n 2 → years  n log n → hours  n → 1 hour

Limitations of Runtime Analysis Big-O can hide a very large constant  Example: selection  Example: small problems The specific problem you want to solve may not be the worst case  Example: Simplex method for linear programming Your program may not be run often enough to make analysis worthwhile  Example: one-shot vs. every day  You may be analyzing and improving the wrong part of the program  Very common situation  Should use profiling tools 24

Summary 25  Asymptotic complexity  Used to measure of time (or space) required by an algorithm  Measure of the algorithm, not the problem  Searching a sorted array  Linear search: O(n) worst-case time  Binary search: O(log n) worst-case time  Matrix operations:  Note: n = number-of-rows = number-of-columns  Matrix-vector product: O(n 2 ) worst-case time  Matrix-matrix multiplication: O(n 3 ) worst-case time  More later with sorting and graph algorithms