Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.

Slides:



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

HST 952 Computing for Biomedical Scientists Lecture 10.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Lecture 3 Sept 3, 2010 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Lecture 5 Sept 9 Goals: Selection sorting Insertion sorting (completion) 2-d arrays Image representation Image processing examples.
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm Analysis (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
COMP s1 Computing 2 Complexity
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.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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
Mathematics Review and Asymptotic Notation
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
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
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.
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Fundamentals of Algorithms MCS - 2 Lecture # 8. Growth of Functions.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Data Structure Introduction.
Algorithm Analysis Data Structures and Algorithms (60-254)
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
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.
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.
Searching Topics Sequential Search Binary Search.
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.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
CS 3343: Analysis of Algorithms
Algorithm Analysis (not included in any exams!)
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007.
Time Complexity Lecture 15 Mon, Feb 27, 2006.
Estimating Algorithm Performance
Presentation transcript:

Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing examples

Selection Sort Selection Sorting Algorithm: During the j-th pass (j = 0, 1, …, n – 2), we will examine the elements of the array a[j], a[j+1], …, a[n- 1] and determine the index min of the smallest key. Swap a[min] and a[j]. selection_sort(int_array a) { if (a.size() == 1) return; n = a.size(); for (int j = 0; j < n – 1; ++j) { min = j; for (int k= j+1; k<=n-1; ++k) if (a[k] < a[min]) min = k; swap a[min] and a[j]; }

Algorithm analysis Analysis is the process of estimating the number of computational steps performed by a program (usually as a function of the input size). Useful to compare different approaches. Can be done before coding. Does not require a computer (paper and pencil). Order of magnitude (rough) estimate is often enough. We will introduce a notation to convey the estimates. (O notation).

Analysis of selection sorting Consider the program to find the min number in an array: min = 0; for (j = 1; j < n; ++j) if (A[j] > min) min = j; The number of comparisons performed is n – 1. loop starts with j = 1 and ends with j = n so the number of iterations = n – 1. In each iteration, one comparison is performed.

Selection sorting – analysis The inner loop: n – 1 comparisons during the first iteration of the inner loop n – 2 comparisons during the 2nd iteration of the inner loop comparison during the last iteration of the inner loop Total number of comparisons = … + (n – 1) = n(n – 1)/ 2 (best as well as the worst-case)

O (order) notation Definition: Let f(n) and g(n) be two functions defined on the set of integers. If there is a c > 0 such that f(n) <= c g(n) for all large enough n. Then, we say f(n) = O(g(n)). Example: n 2 + 2n – 15 is O(n 2 ) Rule: When the expression involves a sum, keep only the term with the highest power, drop the rest. You can also drop constant multiplying terms. (3n n + 1) (4 n – 5) is O(n 3 )

How to Measure Algorithm Performance What metric should be used to judge algorithms? –Length of the program (lines of code) since the personnel cost is related to this. –Ease of programming (bugs, maintenance) –Memory required  Running time (most important criterion) Running time is the dominant standard –Quantifiable and easy to compare –Often the critical bottleneck –Particularly important when real-time response is expected

Average, Best, and Worst-Case On which input instances should the algorithm’s performance be judged? Average case: –Real world distributions difficult to predict Best case: –Unrealistic –Rarely occurs in practice Worst case: (most commonly used) –Gives an absolute guarantee –Easier to analyze

Simplifying the Bound T(n) = c k n k + c k-1 n k-1 + c k-2 n k-2 + … + c 1 n + c o –too complicated –too many terms –Difficult to compare two expressions, each with 10 or 20 terms Do we really need all the terms? For approximation, we can drop all but the biggest term. When n is large, the first term (the one with the highest power) is dominant.

Simplifications Keep just one term! – the fastest growing term (dominates the runtime) No constant coefficients are kept –Constant coefficients affected by machines, languages, etc. Order of magnitude (as n gets large) is captured well by the leading term. –Example. T(n) = 10 n 3 + n n If n = 1,000, then T(n) = 10,001,040,800 error is 0.01% if we drop all but the n 3 term

O (order) notation - formally Definition: Let f(n) and g(n) be two functions defined on the set of integers. If there is a c > 0 such that f(n) <= c g(n) for all large enough n. Then, we say f(n) = O(g(n)). Example: n 2 + 2n – 15 is O(n 2 ) Rule: When the expression involves a sum, keep only the term with the highest power, drop the rest. You can also drop constant coefficients from this term. (3n n + 1) (4 n – 5) is O(n 3 )

Problem size vs. time taken Assume the computer does 1 billion ops per sec.

Basic rules and examples about magnitude and growth of functions Constant = O(1) refers to functions f such that there is a constant c: f(n) < c for all n. Ex: accessing an array element A[j] given j. log n grows much slower than n. log n < 30 when n is a 1 trillion. Ex: binary search on array of size n takes O(log n) time. Usually, query systems are expected to perform in O(log n) time to answer about or update database of size n.

Magnitudes and growth functions O(n) may be acceptable for off-line processing but not for on-line (real-time) processing. When the data is unstructured (un- preprocessed), it usually takes O(n) time to give any non-trivial answer. Ex: maximum in a given collection of keys, a key in the top 10% etc. Algorithms whose time complexity is O(2 n ) is totally impractical.