Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Introduction to Analysis of Algorithms
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
CSE 830: Design and Theory of Algorithms Dr. Eric Torng.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Data Structure & Algorithm Lecture 3 –Algorithm Analysis JJCAO.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Cost Algorithm Complexity. Algorithm Cost.
Analysis of Performance
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
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.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
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)
CS 3343: Analysis of Algorithms
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
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.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
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.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Time Complexity of Algorithms (Asymptotic Notations)
Algorithm Analysis Part of slides are borrowed from UST.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
Algorithm Analysis (Big O)
Spring 2015 Lecture 2: Analysis of Algorithms
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.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
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.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Linda Shapiro Winter 2015.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
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.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Chapter 2 Algorithm Analysis
Introduction to Algorithms
Big-O notation.
Searching, Sorting, and Asymptotic Complexity
Performance Evaluation
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Presentation transcript:

Analysis of Algorithms intro

 What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal definition ◦ Formal definition  Mathematical ◦ Application: examples ◦ Best, worst, average case

 Correct – meets specifications  Easy to understand  Easy to modify  Easy to write  Runs fast  Uses reasonable set of resources ◦ Time ◦ Space (main memory) ◦ Hard-drive space ◦ Peripherals ◦…◦…

 What kinds of things should we measure? ◦ CPU time ◦ memory used ◦ disk transfers ◦ network bandwidth  Mostly in this course, we focus on the first two, and especially on CPU time  One way to measure CPU time: profiling ◦ Run the program in a variety of situations / inputs ◦ Call System.currentTimeMillis()  What are the problems with profiling?

 Results from profiling depend on: ◦ Power of machine you use  CPU, RAM, etc ◦ State of machine you use  What else is running? How much RAM is available? … ◦ What inputs do you choose to run?  Size of input  Specific input

 Big-Oh is a mathematical definition that allows us to: ◦ Determine how fast a program is (in big-Oh terms) ◦ Share results with others in terms that are universally understood  Features of big-Oh ◦ Allows paper-and-pencil analysis ◦ Is much easier / faster than profiling ◦ Is a function of the size of the input ◦ Focuses our attention on big inputs ◦ Is machine independent

for (int i=0; i < a.length; i++) { if ( a[i].compareTo(soughtItem) > 0 ) return NOT_FOUND; // Explain why this is NOT cohesive. // NOT_FOUND must be …? else if ( a[i].compareTo(soughtItem) == 0 ) return i; } return NOT_FOUND; What should we count? Best case, worst case, average case? Q5

Does the following method actually create and return a copy of the string s ? public static String stringCopy(String s) { String result = ""; for (int i=0; i<s.length(); i++) result += s.charAt(i); return result; } What can we say about the running time of the method? (where N is the length of the string s) What should we count? How can we do the copy more efficiently? Don’t be too quick to make assumptions when analyzing an algorithm! Q6

 Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. --Martin Golding

Figure 5.1 Running times for small inputs Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.2 Running times for moderate inputs Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.3 Functions in order of increasing growth rate Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley a.k.a "log linear"

 We only really care what happens when N (the size of a problem) gets large  Is the function basically linear, quadratic, etc. ?  For example, when n is large, the difference between n 2 and n 2 – 3 is negligible

 Run-time of the algorithm of interest on a worst-case input of size n is: ◦ at most a constant times blah, for large n  Example: run-time of the linear search algorithm on a worst-case input of size n is: ◦ O(n)  Alternatives to: ◦ Run-time: space required, … ◦ Algorithm of interest: Problem of interest ◦ Worst-case input: Average-case, best-case ◦ At most: At least => Ω and “exactly” (i.e. one constant for at least and another for at most) => Θ

≥ In this course, we won't be so formal. We'll just say that f(N) is O(g(N) means that f(n) is eventually smaller than a constant times g(n). Q7 there exists a c such that for all n >= n0

 f(N) is O(g(N)) if there is a constant c such that for sufficiently large N, f(N) ≤ cg(N) ◦ Informally, as N gets large the growth rate of f is bounded above by the growth rate of g  f(N) is Ω(g(N)) if there is a constant c such that for sufficiently large N, f(N) ≥ cg(N) ◦ Informally, as N gets large the growth rate of f is bounded below by the growth rate of g  f(N) is Θ(g(N)) if f(N) is O(g(n)) and f(N) is Ω(g(N))  Informally, as N gets large the growth rate of f is the same as the growth rate of g

 consider the limit  What does it say about asymptotics if this limit is zero, nonzero, infinite?  We could say that knowing the limit is a sufficient but not necessary condition for recognizing big-oh relationships.  It will be all we need for all examples in this course. Q8

1. N and N 2 2. N 2 + 3N + 2 and N 2 3. N + sin(N) and N 4. log N and N 5. N log N and N 2 6. N a and N n 7. a N and b N (a < b) 8. log a N and log b N (a < b) 9. N! and N N Q9

 Give tightest bound you can ◦ Saying that 3N+2 is O(N 3 ) is true, but not as useful as saying it’s O(N) [What about Θ(N 3 ) ?]  Simplify: ◦ You could say: ◦ 3n+2 is O(5n-3log(n) + 17) ◦ and it would be technically correct… ◦ It would also be poor taste … and put me in a bad mood.  But… if I ask “true or false: 3n+2 is O(n 3 )”, what’s the answer? ◦ True! ◦ There may be “trick” questions like this on assignments and exams. ◦ But they aren’t really tricks, just following the big-Oh definition!

 Sorting and searching ◦ Why we study these  See project: SortingAndSearching ◦ Counting: Loops