Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Data Structures Data Structures Topic #7. Today’s Agenda How to measure the efficiency of algorithms? Discuss program #3 in detail Review for the midterm.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
Algorithm Efficiency and Sorting
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
The Efficiency of Algorithms
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
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.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ 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)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Analysis of Algorithms
Measuring the Efficiency of Algorithms Analysis of algorithms Provides tools for contrasting the efficiency of different methods of solution Time efficiency,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Algorithm Efficiency Chapter 10 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CS1020 Data Structures and Algorithms I Lecture Note #11 Analysis of Algorithms.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Algorithm Efficiency and Sorting Data Structure & Algorithm.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Efficiency of Algorithms. Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter()
Algorithm Analysis (Big O)
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
1 Algorithms Searching and Sorting Algorithm Efficiency.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
CS 302 Data Structures Algorithm Efficiency.
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
Algorithm Efficiency Chapter 10.
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency: Searching and Sorting Algorithms
Algorithm Efficiency Chapter 10
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency and Sorting
Presentation transcript:

Chapter 10 A Algorithm Efficiency

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms –Provides tools for contrasting the efficiency of different methods of solution A comparison of algorithms –Should focus of significant differences in efficiency –Should not consider reductions in computing costs due to clever coding tricks

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-3 Determining the Efficiency of Algorithms Three difficulties with comparing programs instead of algorithms –How are the algorithms coded? –What computer should you use? –What data should the programs use? Algorithm analysis should be independent of –Specific implementations –Computers –Data

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-4 The Execution Time of Algorithms Counting an algorithm's operations is a way to access its efficiency –An algorithm’s execution time is related to the number of operations it requires –Examples Traversal of a linked list The Towers of Hanoi Nested Loops

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-5 Algorithm Growth Rates An algorithm’s time requirements can be measured as a function of the problem size An algorithm’s growth rate –Enables the comparison of one algorithm with another –Examples Algorithm A requires time proportional to n 2 Algorithm B requires time proportional to n Algorithm efficiency is typically a concern for large problems only

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-6 Algorithm Growth Rates Figure 10.1 Time requirements as a function of the problem size n

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-7 Order-of-Magnitude Analysis and Big O Notation Definition of the order of an algorithm Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n 0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥ n 0 Growth-rate function –A mathematical function used to specify an algorithm’s order in terms of the size of the problem Big O notation –A notation that uses the capital letter O to specify an algorithm’s order –Example: O(f(n))

© 2004 Pearson Addison-Wesley. All rights reserved10 A-8

© 2004 Pearson Addison-Wesley. All rights reserved10 A-9

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-10 Order-of-Magnitude Analysis and Big O Notation Figure 10.3a A comparison of growth-rate functions: a) in tabular form

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-11 Order-of-Magnitude Analysis and Big O Notation Figure 10.3b A comparison of growth-rate functions: b) in graphical form

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-12 Order-of-Magnitude Analysis and Big O Notation Order of growth of some common functions O(1) < O(log 2 n) < O(n) < O(n * log 2 n) < O(n 2 ) < O(n 3 ) < O(2 n ) Properties of growth-rate functions –You can ignore low-order terms –You can ignore a multiplicative constant in the high-order term –O(f(n)) + O(g(n)) = O(f(n) + g(n))

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-13 Order-of-Magnitude Analysis and Big O Notation Worst-case and average-case analyses –An algorithm can require different times to solve different problems of the same size Worst-case analysis –A determination of the maximum amount of time that an algorithm requires to solve problems of size n Average-case analysis –A determination of the average amount of time that an algorithm requires to solve problems of size n

Algorithm Growth Rate Analysis 14 l Sequential Statements – If the orders of code segments p 1 and p 2 are f 1 (n) and f 2 (n), respectively, then the order of p 1 ;p 2 = O(f 1 (n) + f 2 (n)) = O(max(f 1 (n), f 2 (n))) l For Loops (simple ones) – Use  to figure out total number of iterations – Shortcut l While Loops – No particular way, might be able to count l Recursion – Recurrence relation

15 Important Recurrences l Constant operation reduces problem size by one  T n = T n-1 + c Solution: T n = O(n)  linear  Examples: find largest version 2 and linear search  find_max2(myArray, n) // finds largest value in myArray of size n if (n == 1) return myArray[0]; x = find_max2(myArray, n-1); if (myArray[n-1] > x) return myArray[n-1]; else return x;  linear_search(myArray, n, key) // returns index of key if found in myArray of size n, -1 otherwise if (n == 0) return -1; index = linear_search(myArray, n-1, key); if (index != -1) return index; if (myArray[n-1] == key) return n-1; else return -1;

16 Important Recurrences l A pass through input reduces problem size by one  T n = T n-1 + n (or T n = T n-1 + (n – 1)) Solution: T n = O(n 2 )  quadratic  Example: insertion sort  insertion_sort(myArray, n) // sort myArray of size n if (n == 1) return; insertion_sort(myArray, n-1) find the right position to insert myArray[n-1] // it takes O(n) l Constant operation divides problem into two subproblems of half-size  T n = 2T n/2 + c Solution: T n = O(n)  linear  Example: find largest version 1  find_max1(myArray, i, j) // find largest value in myArray[i.. j] if (i == j) return myArray[i]; x = find_max2(myArray, i, (i+j)/2); y = find_max2(myArray, (i+j)/2+1, j); return (x > y)? x : y;

17 Important Recurrences l Constant operation reduces problem size by half  T n = T n/2 + c Solution: T n = O(lg n)  logarithmic  Example: binary search l A pass through input divides problem into 2 subproblems of input size reduced by half  T n = 2T n/2 + n Solution: T n = O(n lg n)  n log n  Example: merge sort

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-18 Keeping Your Perspective Throughout the course of an analysis, keep in mind that you are interested only in significant differences in efficiency When choosing an ADT’s implementation, consider how frequently particular ADT operations occur in a given application Some seldom-used but critical operations must be efficient

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-19 Keeping Your Perspective If the problem size is always small, you can probably ignore an algorithm’s efficiency Weigh the trade-offs between an algorithm’s time requirements and its memory requirements Compare algorithms for both style and efficiency Order-of-magnitude analysis focuses on large problems

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-20 The Efficiency of Searching Algorithms Sequential search –Strategy Look at each item in the data collection in turn, beginning with the first one Stop when –You find the desired item –You reach the end of the data collection

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-21 The Efficiency of Searching Algorithms Sequential search –Efficiency Worst case: O(n) Average case: O(n) Best case: O(1)

© 2004 Pearson Addison-Wesley. All rights reserved 10 A-22 The Efficiency of Searching Algorithms Binary search –Strategy To search a sorted array for a particular item –Repeatedly divide the array in half –Determine which half the item must be in, if it is indeed present, and discard the other half –Efficiency Worst case: O(log 2 n) For large arrays, the binary search has an enormous advantage over a sequential search