Estimating Algorithm Performance

Slides:



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

Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
1 TCSS 342, Winter 2005 Lecture Notes Course Overview, Review of Math Concepts, Algorithm Analysis and Big-Oh Notation Weiss book, Chapter 5, pp
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Analysis (Big O)
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
TCSS 342 Lecture Notes Course Overview, Review of Math Concepts,
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Asymptotic Analysis-Ch. 3
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis (Big O)
CSE 373: Data Structures and Algorithms
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 373: Data Structures and Algorithms Lecture 4: Math Review/Asymptotic Analysis II 1.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 4.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
Algorithm Analysis 1.
Analysis of Algorithms
Chapter 2 Algorithm Analysis
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Algorithm Analysis (not included in any exams!)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis
CSC 413/513: Intro to Algorithms
Analysis of Algorithms
Introduction to Algorithms Analysis
Analysis of Algorithms
TCSS 342, Winter 2006 Lecture Notes
CS 3343: Analysis of Algorithms
Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Analysis of Algorithms
Chapter 2.
Analysis of Algorithms
Programming and Data Structure
Searching, Sorting, and Asymptotic Complexity
Performance Evaluation
CSC 380: Design and Analysis of Algorithms
At the end of this session, learner will be able to:
CSC 380: Design and Analysis of Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
CSE 373: Data Structures and Algorithms
Big Omega, Theta Defn: T(N) = (g(N)) if there are positive constants c and n0 such that T(N)  c g(N) for all N  n0 . Lingo: “T(N) grows no slower than.
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Estimating Algorithm Performance How to determine how much time an algorithm A uses to solve problem X? Depends on input; use input length as parameter. Determine function f(n) representing cost. Code it and use timer running on many inputs Analyze steps of algorithm, estimating amount of work each step takes (Algorithm Analysis). Coding often impractical at early design stages. Algorithm Analysis: determining how long an algorithm will take to solve a problem USING A MODEL. TCSS 342 Intro Alg. Anal. v 1.0

RAM Model for Algorithm Analysis Typically use a simple model for basic operation costs. RAM (Random Access Machine) model RAM model has all the basic operations: +, -, *, / , =, comparisons. fixed sized integers (e.g., 32-bit) infinite memory All (basic) operations take exactly one time unit to execute. analysis= determining run-time efficiency. model = estimate, not meant to represent everything in real-world TCSS 342 Intro Alg. Anal. v 1.0

Critique of the model Weaknesses: Strengths: not all operations take the same amount of time in a real machine does not account for page faults, disk accesses, etc. Strengths: simple easier to prove things about the model than the real machine can estimate algorithm behavior on any hardware/software model = approximation of real world. can predict run-time of algorithm on machine. TCSS 342 Intro Alg. Anal. v 1.0

Why models? model real world Idea: (Useful) statements using the model translate into useful statement about real computers TCSS 342 Intro Alg. Anal. v 1.0

Relative rates of growth Distinguish between fast- and slow-growing functions. 100n2 + 1000 n3 + 2n2 Motivation: we usually care only about algorithm performance when there are large number of inputs. We usually don’t care about small changes in run-time performance. (inaccuracy of estimates make small changes less relevant). Consider algorithms with slow growth rate better than those with fast growth rates. TCSS 342 Intro Alg. Anal. v 1.0

Big-Oh notation Defn: T(N) = O(f(N)) if there are positive constants c and n0 such that T(N)  c f(N) for all N  n0 . Idea: We are concerned with how the function grows when N is large. We are not picky about constant factors: coarse distinctions among functions. Lingo: “T(N) grows no faster than f(N).” Important (not in Lewis & Chase book). Write on board. (for next slide). TCSS 342 Intro Alg. Anal. v 1.0

Examples n = O(2n) ? 2n = O(n) ? n = O(n2) ? n2 = O(n) ? n = O(1) ? 10 log n = O(n) ? 214n + 34 = O(2n2 + 8n) ? TCSS 342 Intro Alg. Anal. v 1.0

Preferred big-Oh usage If f(N) = 5N, then f(N) = O(N5) f(N) = O(N3) f(N) = O(N)  preferred f(N) = O(N log N) Ignore constant factors and low order terms. T(N) = O(N), not T(N) = O(5N). T(N) = O(N3), not T(N) = O(N3 + N2 + N log N). Bad style: f(N)  O(g(N)). Wrong: f(N)  O(g(N)). TCSS 342 Intro Alg. Anal. v 1.0

figure 1.11 asymptotic complexity of selected functions TCSS 342 Intro Alg. Anal. v 1.0

figure 1.12 Increase in problem size with a ten-fold increase in processor speed Book has errata, including on this figure. 2^n = exponential complexity. 2n TCSS 342 Intro Alg. Anal. v 1.0

Big Omega, Theta Defn: T(N) = (g(N)) if there are positive constants c and n0 such that T(N)  c g(N) for all N  n0 . Lingo: “T(N) grows no slower than g(N).” Defn: T(N) = (h(N)) if and only if T(N) = O(h(N)) and T(N) = (h(N)). Big-Oh, Omega, and Theta establish a relative order among all functions of N. TCSS 342 Intro Alg. Anal. v 1.0

Intuition, little-Oh Defn: T(N) = o(p(N)) if T(N) = O(p(N)) and T(N)  (p(N)). notation intuition O (Big-Oh)   (Big-Omega)   (Theta) = o (little-Oh) < TCSS 342 Intro Alg. Anal. v 1.0

More about Asymptotics Fact: If f(N) = O(g(N)), then g(N) = (f(N)). Proof: Suppose f(N) = O(g(N)). Then there exist constants c and n0 such that f(N)  c g(N) for all N  n0. Then g(N)  f(N) for all N  n0, and so g(N) = (f(N)). TCSS 342 Intro Alg. Anal. v 1.0

More terminology Suppose T(N) = O(f(N)). f(N) is an upper bound on T(N). T(N) grows no faster than f(N). Suppose T(N) = (g(N)). g(N) is a lower bound on T(N). T(N) grows at least as fast as g(N). If T(N) = o(h(N)), then we say that T(N) grows strictly slower than h(N). TCSS 342 Intro Alg. Anal. v 1.0

Facts about Big-Oh If T1(N) = O(f(N)) and T2(N) = O(g(N)), then T1(N) + T2(N) = O(f(N) + g(N)). T1(N) * T2(N) = O(f(N) * g(N)). If T(N) is a polynomial of degree k, then T(N) = (Nk). log k N = O(N), for any constant k. TCSS 342 Intro Alg. Anal. v 1.0

Techniques Algebra ex. f(N) = N / log N, g(N) = log N. same as asking which grows faster, N or log 2 N. Evaluate . limit is Big-Oh relation f(N) = o(g(N)) c  0 f(N) = (g(N))  g(N) = o(f(N)) no limit no relation TCSS 342 Intro Alg. Anal. v 1.0

Techniques, cont’d L’Hôpital’s rule: If and , then . example: f(N) = N, g(N) = log N. Use L’Hôpital’s rule. f’(N) = 1, g’(N) = 1/N.  g(N) = o(f(N)). TCSS 342 Intro Alg. Anal. v 1.0

Kinds of Algorithm Analysis Express the running time as f(N), where N is the size of the input. Worst case: your enemy gets to pick the input. Average case: need to assume a probability distribution on the inputs. Amortized: your enemy gets to pick the inputs/operations, but you only have to guarantee speed over a large number of operations. However, even with input size N, cost of an algorithm could vary on different input. TCSS 342 Intro Alg. Anal. v 1.0

Search avg. case, if found: n/2 accesses // return index if x is found // in array a; // or NOT_FOUND if not found public int SequentialSearch( int[] a, int x) { int i; for (i = 0; i < a.length; i++) { if (a[i] == x) return i; } return NOT_FOUND; worst case: O(n) avg. case, if found: n/2 accesses avg. case, if not found: n accesses  constant work  in loop TCSS 342 Intro Alg. Anal. v 1.0

Binary Search  constant work  in loop // Given a sorted array a and // a key x, return index if x is found // in array a // or NOT_FOUND if not found public int BinarySearch(int[] a, int x){ int low = 0; int high = a.length – 1; while (low <= high) { int mid = (low + high) / 2; if (a[mid] < x) { low = mid + 1; } else if (a[mid] > x) { high = mid – 1; } else { return mid; } return NOT_FOUND;  constant work  in loop TCSS 342 Intro Alg. Anal. v 1.0

Analysis Intuitive argument about the running time: number of elements to consider (high – low + 1) is halved each iteration high – low + 1 = n at start The algorithm iterates at most log2 n.  O(log n) steps to execute. … mid n-1 TCSS 342 Intro Alg. Anal. v 1.0