/
Chapter 2 Analysis of Algorithms Chapter 2 Analysis of Algorithms

Chapter 2 Analysis of Algorithms - PowerPoint Presentation

jane-oiler
jane-oiler . @jane-oiler
Follow
343 views
Uploaded On 2019-12-17

Chapter 2 Analysis of Algorithms - PPT Presentation

Chapter 2 Analysis of Algorithms Chapter Scope Efficiency goals The concept of algorithm analysis BigOh notation The concept of asymptotic complexity Comparing various growth functions Java Software Structures 4th Edition LewisChase ID: 770691

lewis structures 4th edition structures lewis edition 4th software algorithm chase java loop growth count time complexity size dish

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Chapter 2 Analysis of Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Chapter 2Analysis of Algorithms

Chapter ScopeEfficiency goalsThe concept of algorithm analysisBig-Oh notation The concept of asymptotic complexity Comparing various growth functions Java Software Structures, 4th Edition, Lewis/Chase 2 - 2

Algorithm EfficiencyThe efficiency of an algorithm is usually expressed in terms of its use of CPU time The analysis of algorithms involves categorizing an algorithm in terms of efficiency An everyday example: washing dishesSuppose washing a dish takes 30 seconds and drying a dish takes an additional 30 secondsTherefore, n dishes require n minutes to wash and dry Java Software Structures, 4th Edition, Lewis/Chase 2 - 3 Dry time: n * 30 seconds Wash time: n * 30 seconds Total time : n * 60 seconds = n minutes

Algorithm EfficiencyNow consider a less efficient approach that requires us to re-dry all previously washed dishes after washing another one Dry time Wash time ( seconds ) Dish 1 30 * n 30 Dish 2 30 * (n-1) 30 … Dish n-1 30 * 2 30 Dish n 30 * 1 30 Java Software Structures, 4th Edition, Lewis/Chase 2 - 4

Problem Size For every algorithm we want to analyze, we need to define the size of the problem The dishwashing problem has a size n – number of dishes to be washed/dried For a search algorithm, the size of the problem is the size of the search poolFor a sorting algorithm, the size of the program is the number of elements to be sorted Java Software Structures, 4th Edition, Lewis/Chase 2 - 5

Growth FunctionsWe must also decide what we are trying to efficiently optimizetime complexity – CPU time space complexity – memory space CPU time is generally the focus A growth function shows the relationship between the size of the problem ( n) and the value optimized (time)Java Software Structures, 4th Edition, Lewis/Chase2 - 6

Asymptotic ComplexityThe growth function of the second dishwashing algorithm is T(n) = 15n 2 + 45n It is not typically necessary to know the exact growth function for an algorithmWe are mainly interested in the asymptotic complexity of an algorithm – the general nature of the algorithm as n increases Java Software Structures, 4th Edition, Lewis/Chase2 - 7

Asymptotic ComplexityAsymptotic complexity is based on the dominant term of the growth function – the term that increases most quickly as n increases The dominant term for the second dishwashing algorithm is n2:Java Software Structures, 4th Edition, Lewis/Chase 2 - 8

Big-Oh NotationThe coefficients and the lower order terms become increasingly less relevant as n increasesSo we say that the 2 nd dishwashing algorithm is order n2, which is written O(n2)This is called Big-Oh notation There are various Big-Oh categoriesTwo algorithms in the same category are generally considered to have the same efficiency, but that doesn't mean they have equal growth functions or behave exactly the same for all values of nJava Software Structures, 4th Edition, Lewis/Chase 2 - 9

Big-Oh CategoriesSome sample growth functions and their Big-Oh categories: Java Software Structures, 4th Edition, Lewis/Chase 2 - 10

Comparing Growth Functions You might think that faster processors would make efficient algorithms less important A faster CPU helps , but not relative to the dominant term Java Software Structures, 4th Edition, Lewis/Chase 2 - 11 Assuming the processor speed is increased tenfold after speedup

Comparing Growth FunctionsAs n increases, the various growth functions diverge dramatically: Java Software Structures, 4th Edition, Lewis/Chase 2 - 12

Comparing Growth FunctionsFor large values of n, the difference is even more pronounced: Java Software Structures, 4th Edition, Lewis/Chase 2 - 13

Analyzing Loop ExecutionFirst determine the order of the body of the loop , then multiply that by the number of times the loop will execute for ( int count = 0 ; count < n; count++) // some sequence of O(1) stepsn loop iterations/executions times O(1) operations results in a O(n) efficiencyJava Software Structures, 4th Edition, Lewis/Chase 2 - 14

Analyzing Loop ExecutionConsider the following loop: count = 1 ; while ( count < n ) { count *= 2; // some sequence of O(1) steps }The loop is executed log2n times, so the loop is O(log n) Java Software Structures, 4th Edition, Lewis/Chase 2 - 15 Count number of iterations 1 0 2 1 4 2 8 3 16 4 … … n log2n

Analyzing Nested LoopsWhen loops are nested, we multiply the complexity of the outer loop by the complexity of the inner loop for ( int count = 0 ; count < n; count++) for (int count2 = 0; count2 < n; count2++) { // some sequence of O(1) steps }Both the inner and outer loops have complexity of O(n )The overall efficiency is O(n 2)Java Software Structures, 4th Edition, Lewis/Chase2 - 16

Analyzing Method CallsThe body of a loop may contain a call to a method To determine the order of the loop body , the order of the method must be taken into account The overhead of the method call itself is generally ignoredJava Software Structures, 4th Edition, Lewis/Chase 2 - 17