ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

CSE Lecture 3 – Algorithms I
HST 952 Computing for Biomedical Scientists Lecture 10.
ALG0183 Algorithms & Data Structures Lecture 7 Big-Oh, Big-Omega, Big-Theta, Little-Oh 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.
Fundamentals of Python: From First Programs Through Data Structures
ALG0183 Algorithms & Data Structures Lecture 17 Quicksort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Efficiency of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Design and Analysis of Algorithms Chapter Analysis of Algorithms Dr. Ying Lu August 28, 2012
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Analysis of Performance
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
{ 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.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Lecture 2 Computational Complexity
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
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 211 Data Structures Lecture 13
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Algorithm Analysis Data Structures and Algorithms (60-254)
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
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.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Algorithm Analysis Lakshmish Ramaswamy. What Constitutes Good Software? Code correctness Good design Code reusability OO design can help us in achieving.
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.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Algorithm Analysis 1.
Lecture 15.
Big-Oh and Execution Time: A Review
Algorithm Efficiency Chapter 10.
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Presentation transcript:

ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni Chapter 2

Running time The running time of a piece of software depends on the speed of the machine, the quality of the compiler, the quality of the program, and on the algorithms made use of. – and possibly on the quality of any network and caching mechanisms Sorting 100,000 records takes longer than sorting 10 records. The running time of an algorithm is a function of the size of the input. Different algorithms to solve the same problem can vary dramatically in terms of running time. The most efficient algorithms are those whose running times grow linearily with the size of input. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2

Figure 5.1 Running times for small inputs. ©Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3

Figure 5.1 Running times for small inputs. Small values of N are generally not important. For N=20, the example algorithms all terminate within 5 ms. – “less than a blink of the eye” For N=25, the example algorithms all terminate within 10ms. Notice how the quadratic algorithm is better at small N than O(NlogN), but that it loses its advantage when N>50. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4

Figure 5.1 Running times for small inputs. Constants in growth functions can play a significant part when N is small. – E.g. The function T(N) = N + 2,500 is larger than N 2 when N is less than 50. Weiss: “Consequently, when input sizes are very small, a good rule of thumb is to use the simplest algorithm.” – A complex algorithm is more likely to be incorrectly implemented. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5

Figure 5.2 Running times for moderate inputs. ©Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6

Figure 5.2 Running times for moderate inputs. For an input size of 10,000 records, O(NlogN) takes roughly 10 times as much time as a linear algorithm, O(N). – The actual time difference depends on the constants in the growth functions. – log 2 (10,000) = , log 2 (1,000,000) = Weiss: “Quadaratic algorithms are almost always impractical when the input size is more than a few thousand, and cubic algorithms are impractical for input sizes as small as a few hundred.” – Simple sorting algorithms such as bubble sort are quadratic. Weiss: “... before we waste effort attempting to optimize code, we need to optimize the algorithm.” – Can a quadratic algorithm (O(N 2 )) be made sub-quadratic? 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7

Dominant terms in growth functions A cubic function is a function whose dominant term is some constant times N 3. 10N 3 + N N + 80 is a cubic function. – Ignoring the special case when N=1. – For N = 1,000 the function has a value of 10,001,040,080 of which 10,000,000,000 is due to the 10N 3 term. Lower terms represent only 0.01 percent of the total. For sufficiently large N, the value of a growth function is determined by its dominant term. The Big-Oh notation is used to describe the dominant term in a growth function. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8

Figure 5.3 © AddisonWesley Functions in order of increasing growth rates. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9

Minimum element in an array given an array of N items, find the smallest item variable min stores the minimum. min is initialized to the first item perform a sequential scan through the array and update min when appropriate 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 examples of algorithm running times A fixed amount of work is performed for each element in the array so the running time of the algorithm is linear or O(N).

Sequential search for an element in an array given an array of N items, find the position of the item specified In the best-case, the item searched for will be the first item in the array. – O(1) In the worst-case, the item searched for will not be present in the array. – O(N) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 examples of algorithm running times

Figure 2.9 Best-case step count for sequential search by Sartaj Sahni ©McGraw-Hill 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 s/e is the number of steps per execution of the statement. Frequency is how often each statement is executed. The time complexity is estimated as 4 steps. examples of algorithm running times

Figure 2.10 Worst-case step count for sequential search by Sartaj Sahni ©McGraw-Hill 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 s/e is the number of steps per execution of the statement. Frequency is how often each statement is executed. The time complexity is estimated as n + 3 steps. Since i has to be incremented to n before the loop terminates, the frequency of the for statement is n+1. examples of algorithm running times

Figure 2.11 Typical-case (x=a[j]) step count for sequential search by Sartaj Sahni ©McGraw-Hill 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 s/e is the number of steps per execution of the statement. Frequency is how often each statement is executed. The time complexity is estimated as j + 4 steps. examples of algorithm running times

Average-case analysis for sequential search by Sartaj Sahni Assume all the values in the array are distinct. Assume x, the item being searched for, has an equal probability of being any of the values. The average step count for a successful search will be the sum of the step counts for the n possible searches divided by n. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 Slightly more than half the the step count for an unsuccessful search. examples of algorithm running times

Closest pair of points Given n points in the x-y plane, what pair of points are closest together? A brute force solution requires calculating the distance between every pair of points and updating the current minimum distance as necessary. Each of the n points can be paired with n-1 points so the number of unique pairs is n(n-1)/2. A brute force solution takes quadratic time, O(N 2 ). (Other algorithms exist which take less than time.) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 examples of algorithm running times

In general, the time complexity of a loop is O(N), the time complexity of a loop within a loop is O(N 2 ), and the time complexity of a loop within a loop within a loop is O(N 3 ). 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 Closest pair of points Given n points in the x-y plane, what pair of points are closest together? pseudocode ( minDist = infinity for each p in P: for each q in P: if p ≠ q and dist(p,q) < minDist: minDist = dist(p,q) closestPair = (p,q) return closestPair examples of algorithm running times

Collinear points Given n points in the x-y plane, do any three form a straight line? A brute force solution requires examining all groups of three points. The number of different groups is n(n-1)(n-2)/6. A brute force solution takes cubic time, O(N 3 ). (Other algorithms exist which take less time.) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 examples of algorithm running times