Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright © 2008-2013.

Slides:



Advertisements
Similar presentations
CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?
Advertisements

Discrete Structures CISC 2315
CSCE 2100: Computing Foundations 1 Running Time of Programs
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
Chapter 10 Algorithm Efficiency
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
Analysis of Performance
Algorithm analysis and design Introduction to Algorithms week1
Asymptotic Notations Iterative Algorithms and their analysis
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.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
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.
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)
CMSC 341 Asymptotic Analysis. 8/3/07 UMBC CMSC 341 AA2-color 2 Complexity How many resources will it take to solve a problem of a given size?  time 
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
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.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Design and Analysis of Algorithms Chapter Asymptotic Notations* Dr. Ying Lu August 30, RAIK.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Asymptotic Analysis (based on slides used at UMBC)
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Time Complexity of Algorithms (Asymptotic Notations)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
Algorithm Analysis (Big O)
Algorithm Analysis: Running Time Big O and omega ( 
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
CSE 373: Data Structures and Algorithms Lecture 4: Math Review/Asymptotic Analysis II 1.
8/3/07CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Chapter 10 Algorithm Efficiency
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Analysis of Algorithms
Analysis of Algorithms
Chapter 2.
At the end of this session, learner will be able to:
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©

2 Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program design are two (sometimes conflicting) goals. 1.To design an algorithm that is easy to understand, code, debug. 2.To design an algorithm that makes efficient use of the computer’s resources.

3 Algorithm Efficiency (cont) Goal (1) is the concern of Software Engineering. Goal (2) is the concern of data structures and algorithm analysis. When goal (2) is important, how do we measure an algorithm’s cost?

4 How to Measure Efficiency? 1.Empirical comparison (run programs) 2.Asymptotic Algorithm Analysis Critical resources: Factors affecting running time: For most algorithms, running time depends on “size” of the input. Running time is expressed as T(n) for some function T on input size n.

5 Examples of Growth Rate Example 1. Position of largest value in "A“ */ static int largest(int[] A) { int currlarge = 0; // Position of largest for (int i=1; i<A.length; i++) if (A[currlarge] < A[i]) currlarge = i; // Remember pos return currlarge; // Return largest pos }

6 Examples (cont) Example 2: Assignment statement. Example 3: sum = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j++) sum++; }

7 Growth Rate Graph

8 Best, Worst, Average Cases Not all inputs of a given size take the same time to run. Sequential search for K in an array of n integers: Begin at first element in array and look at each element in turn until K is found Best case: Worst case: Average case:

9 Which Analysis to Use? While average time appears to be the fairest measure, it may be difficult to determine. When is the worst case time important?

10 Faster Computer or Algorithm? Suppose we buy a computer 10 times faster. n: size of input that can be processed in one second on old computer (in 1000 computational units) n’: size of input that can be processed in one second on new computer (in 10,000 computational units) T(n)T(n)nn’Changen’/n 10n1001,000n’ = 10n10 10n n’=  10n n 34n’ = n /n

11 Asymptotic Analysis: Big-oh Definition: For T(n) a non-negatively valued function, T(n) is in the set O(f(n)) if there exist two positive constants c and n 0 such that T(n) n 0. Use: The algorithm is in O(n 2 ) in [best, average, worst] case. Meaning: For all data sets big enough (i.e., n>n 0 ), the algorithm always executes in less than cf(n) steps in [best, average, worst] case.

12 Big-oh Notation (cont) Big-oh notation indicates an upper bound. Example: If T(n) = 3n 2 then T(n) is in O(n 2 ). Look for the tightest upper bound: While T(n) = 3n 2 is in O(n 3 ), we prefer O(n 2 ).

13 Big-Oh Examples Example 1: Finding value X in an array (average cost). Then T(n) = c s n/2. For all values of n > 1, c s n/2 <= c s n. Therefore, the definition is satisfied for f(n)=n, n 0 = 1, and c = c s. Hence, T(n) is in O(n).

14 Big-Oh Examples (2) Example 2: Suppose T(n) = c 1 n 2 + c 2 n, where c 1 and c 2 are positive. c 1 n 2 + c 2 n 1. Then T(n) n 0, for c = c 1 + c 2 and n 0 = 1. Therefore, T(n) is in O(n 2 ) by definition. Example 3: T(n) = c. Then T(n) is in O(1).

15 A Common Misunderstanding “The best case for my algorithm is n=1 because that is the fastest.” WRONG! Big-oh refers to a growth rate as n grows to . Best case is defined for the input of size n that is cheapest among all inputs of size n.

16 Big-Omega Definition: For T(n) a non-negatively valued function, T(n) is in the set  (g(n)) if there exist two positive constants c and n 0 such that T(n) >= cg(n) for all n > n 0. Meaning: For all data sets big enough (i.e., n > n 0 ), the algorithm always requires more than cg(n) steps. Lower bound.

17 Big-Omega Example T(n) = c 1 n 2 + c 2 n. c 1 n 2 + c 2 n >= c 1 n 2 for all n > 1. T(n) >= cn 2 for c = c 1 and n 0 = 1. Therefore, T(n) is in  (n 2 ) by the definition. We want the greatest lower bound.

18 Theta Notation When big-Oh and  coincide, we indicate this by using  (big-Theta) notation. Definition: An algorithm is said to be in  (h(n)) if it is in O(h(n)) and it is in  (h(n)).

19 A Common Misunderstanding Confusing worst case with upper bound. Upper bound refers to a growth rate. Worst case refers to the worst input from among the choices for possible inputs of a given size.

20 Simplifying Rules 1.If f(n) is in O(g(n)) and g(n) is in O(h(n)), then f(n) is in O(h(n)). 2.If f(n) is in O(kg(n)) for some constant k > 0, then f(n) is in O(g(n)). 3.If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)), then (f 1 + f 2 )(n) is in O(max(g 1 (n), g 2 (n))). 4.If f 1 (n) is in O(g 1 (n)) and f 2 (n) is in O(g 2 (n)) then f 1 (n)f 2 (n) is in O(g 1 (n)g 2 (n)).

21 Time Complexity Examples (1) Example 3.9: a = b; This assignment takes constant time, so it is  (1). Example 3.10: sum = 0; for (i=1; i<=n; i++) sum += n;

22 Time Complexity Examples (2) Example 3.11: sum = 0; for (j=1; j<=n; j++) for (i=1; i<=j; i++) sum++; for (k=0; k<n; k++) A[k] = k;

23 Time Complexity Examples (3) Example 3.12: sum1 = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j++) sum1++; sum2 = 0; for (i=1; i<=n; i++) for (j=1; j<=i; j++) sum2++;