1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Fundamentals of Python: From First Programs Through Data Structures
I Advanced Algorithms Analysis. What is Algorithm?  A computer algorithm is a detailed step-by-step method for solving a problem by using a computer.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Cmpt-225 Algorithm Efficiency.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Elementary Data Structures and Algorithms
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Design and Analysis of Algorithms Chapter Analysis of Algorithms Dr. Ying Lu August 28, 2012
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
COMP s1 Computing 2 Complexity
COMPSCI 102 Introduction to Discrete Mathematics.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Growth of Functions CS 202 Epp, section ??? Aaron Bloomfield.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
1.2. Comparing Algorithms. Learning outcomes Understand that algorithms can be compared by expressing their complexity as a function relative to the size.
Analysis of Algorithms
Analysis of Algorithms. What is an algorithm? The ideas behind computer programs Stays the same no matter Which kind of hardware it is running on Which.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
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.
MS 101: Algorithms Instructor Neelima Gupta
Nattee Niparnan Dept. of Computer Engineering, Chulalongkorn University.
3.3 Complexity of Algorithms
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.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
Analysis of Algorithm. Why Analysis? We need to know the “behavior” of algorithms – How much resource (time/space) does it use So that we know when to.
Page 1 adapted after Dr. Menezes Analysis of Algorithms What does it mean to analyze ? Strictly speaking analysis is the separation of an intellectual.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Algorithm Analysis (Big O)
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
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.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
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.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
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
CMPT 438 Algorithms.
Design and Analysis of Algorithms
Algorithms - some definitions
Analysis of Algorithms
Introduction to Algorithms Analysis
Applied Discrete Mathematics Week 6: Computation
Estimating Algorithm Performance
Presentation transcript:

1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried out by computer programs  Examples include  Sorting  Determining if a student qualifies for financial aid  Determining the steps to set up a dating service

2 Algorithms  In some cases we have only one algorithm for a problem or the problem is so straightforward that there is no need to consider anything other than the obvious  Some other problems have many known algorithms  We obviously want to choose the "best" algorithm  Other problems have no known algorithm !

3 What is the "best" Algorithm?  Traditionally we focused on two questions  1. How fast does it run?  Early days, measured by timing the implementation of the algorithm  It was common to hear about a "new" SuperDuper Sort that could sort a list of 1 million integers in 17 seconds whereas Junk Sort requires 43 seconds 2. How much memory does it require?

4 Analysis of Algorithms  Programs depend on the operating systems, machine, compiler/interpreter used, etc.  Analysis of algorithms compare algorithms and not programs  It is based on the premise that the longer the algorithm takes the longer its implementation will run.  Sorting 1 million items ought to take longer than sorting 1000  But if we comparing algorithms (not yet implemented) how can we express it's performance?  How can we "measure" the performance of an algorithm?

5 Analysis of Algorithms  We want an expression that can be applied to any computer  This is only possible by stating the efficiency in terms of some critical operations  These operations depend on the problem  We could for instance say that in sorting algorithms it is the number of time two elements are compared

6 Analysis of Algorithm  In general we do analysis of algorithms using the RAM model (Random Access Machine)  Instructions are executed one after the other  There is no concurrency  Basic operations take the same time (constant time)  We normally say that each line (step) in the algorithm takes time 1 (one)

7 Analysis of Algorithms  But you could be asking: If each line takes a constant time then the whole algorithm (any algorithm) will take constant time, right?  Wrong!  Although some algorithms may take constant time, the majority of algorithms varies its number of steps based on the size of instances we're trying to solve.

8 Number of steps  It is easy to see that most of algorithms vary their number of steps for i = 1.. N a = a + 2 i = i + 1 for i = 1.. N a = a + 2 i = i + 1 for i = 1.. N a = a + 2 i = i + 2 for i = 1.. N a = a + 2 i = i + 2 So must also consider the number of steps it will take to process the number of items(N).

9 Analysis of Algorithms  Therefore the efficiency of an algorithm is always normally stated as a function of the problem size  We generally use the variable n to represent the problem size  On the implementation, we could find out that out SuperDuper Sort takes 0.6n n seconds on Pentium.  Plug a value for n and you have how long it takes

10 Analysis of Algorithms  But we're not yet independent of the machine.  Remember that we said that we said that the formula for the SuperDuper Sort is valid for a Pentium  We need to identify the most important aspect of the function that represents the running time of an algorithm  Which one is the "best"  f(n) = n  g(n) = n 2 + n

11 Asymptotic Analysis  Asymptotic analysis of an algorithm describes the relative efficiency of an algorithm as n gets very large.  In the example it is easy to see that for very large n, g(n) grows faster than f(n)  Take for instance the value n=  Remember that the goal here is to compare algorithms. In practice, if you're writing small programs, asymptotic analysis may not be that important  When you're dealing with small input size, most algorithms will do  When the input size is very large, things change

12 An simple comparison  Let's assume that you have 3 algorithms to sort a list  f(n) = n log 2 n  g(n) = n 2  h(n) = n 3  Let's also assume that each step takes 1 microsecond (10 -6 ) 1s

13 Higher order Term  Most of the algorithms discussed here will be given in terms of common functions: polynomials, logarithms, exponentials and product of these functions  Analyzing the table given earlier we can see that in an efficiency function we are interested in the term with higher order  If we have a function f(n) = n 3 + n 2, for the case when  n = the running time of the algorithm is 31.7 years hours  Its clear that a couple of hours does not make much difference if the program is to run for 31.7 years!

14 Higher order Term  In the case above we say that f(n) is O(n 3 ) meaning that f(n) is of the order n 3.  This is called big-O notation.  It disregards any constant multiplying the term of highest order and any term of smaller order  f(n) = n 3 is O(n 3 )

15 Common Functions  Constant (1): Very fast. Some hash table algorithms can look up one item from the table of n items in an average time which is constant (independent of the table size)  Logarithmic(lg2 of N): Also very fast. Typical of many algorithms that use (binary) trees.  Linear Time( n): Typical of fast algorithms on a single-processor computer. If all the input of size n has to be read.  Poly-logarithmic (n log n): Typical of the best sorting algorithms. Considered a good solution  Polynomial(n^2): When a problem of size n can be solved in time n k where k is a constant. Small n's (n <= 3) is OK.  Exponential (2 ^ N): These problems can not be done in a reasonable time - see next slide

16 Common Functions  Exponential: Are those that use time k n where k is a constant. Algorithms that grow on this rate are suitable only for small problems.  Unfortunately the best algorithms known to many problems use exponential time  Much of the work on developing algorithms today is focused on these problems because they take an huge amount of time to execute (even for reasonably small input size)  There is a large variation in the size of various exponential functions ( n and 2 n ). But for large n the functions become huge

17 Comparison of Algorithms Big-O Notation A notation that expresses computing time (complexity) as the term in a function that increases most rapidly relative to the size of a problem If f(N) = N N N + 50 then f(N) is 0(N 4 ). N represents the size of the problem.

18 Worst Case and Best Case  If we return to our original question of "how fast does a program run?" we can see that this question is not enough  Inputs vary in the way they are organized and this can influence the number of critical operations performed  Suppose that we are searching of an element in an ordered list  If the target key is the first in the list our function takes constant time  If the target key is not in the list our function takes O(n), where n is the size of the list

19 Worst Case and Best Case  The examples above are referred to as best case analysis and worst case analysis.  Which is the really relevant case?  Worst case is more important because it gives us a bound on how long the function might have to run

20 Average Case  In some situations neither the best nor the worst case analysis express well the performance of an algorithm  Average case analysis can be used if necessary  Still average case is uncommon because  It may be cumbersome to do an average analysis of non-trivial algorithms  In most cases the "order" of the average analysis is the same as the worst

21 Comparison of Rates of Growth N log2N N log2N N² N³ 2^N

22 Comparison of Linear and Binary Searches

23 Big-O Comparison of List Operations Operation Unsorted List Sorted List O(LgN)

24 Review Questions: 1.What problems arise when we "measure" the performance of an algorithm? What problems arise if we time it. 2. What is a “critical operation”? 3. How then do we measure the efficiency of an algorithm 1. The efficiency of an algorithm is stated as a function of the problem size. We generally use the variable N to represent the problem size 2. We must also consider the number of steps it will take to process the number of items(N). 4.What is big-O notation? What are Common Functions: Give an example Constant (1): Logarithmic(lg2 of N): Linear Time( n): Poly-logarithmic (n log n): Polynomial(n^2): Exponential (2^n) 5.What is Worst Case Analysis?