New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski
Algorithm Analysis What do we mean? Develop an understand of the characteristics of the algorithm for different input parameters. Why? ▫Predict performance/compare/choose algorithms Frequently more than one solution - Sorting ▫Insertion Sort ▫Bubble Sort ▫Merge Sort ▫Selection Sort ▫Quick Sort …….. ▫Understand how it works leads to improvement shorter, simpler, and more elegant algorithms
Computational Complexity What is Computation Complexity? ▫A way to analyze an algorithm ▫Takes into account how difficult and how efficient or fast it is. Usually evaluate ▫Amount of Time = Time Complexity ▫Amount of Memory= Space Complexity
Computational Complexity - Time TIME = Number of Operations ▫Independent of programming language and computer used ▫ ▫Algorithms are a sequence of steps or operations ▫The more steps/operations, the longer the algorithm takes to run ▫Measure of time = number of operations
Computational Complexity - Time What is an operation? ▫Arithmetic operations ▫Comparisons ▫Swaps ▫Copies or saves ▫Accessing a list ▫Number of times through a loop
Computational Complexity - Time What is an operation? ▫Sorting Comparing – each comparison is one operation Swapping/Copying (storing) – each swap is one operation ▫Searching Comparing – each comparison is one operation
Computational Complexity - Time How do we do it? ▫Calculation Look at the basic “loop’ that is used for each input Do not look at operations outside the “loop” Put a time value on each operation Look at how many times is the loop executed for each type/amount of input! Look at how much input you have. ▫Run the program Different types/amounts of inputs
Computational Complexity - Time Look at Best/Worst/Average case ▫Best-Case complexity: This is the complexity of solving the problem for the best input of size n. ▫Worst-case complexity: This is the complexity of solving the problem for the worst input of size n. ▫Average-case complexity: This is the complexity of solving the problem on an average.
Computational Complexity - Time Time Complexity Search Algorithms List Size n List Size 1000 BestAverageWorstBestAverageWorst Linear Search 1n/2n Binary Search 1~(1 + 1/n)log 2 n - 1~log 2 n
Computational Complexity - Memory One algorithm may require more memory. If memory is a scarce resource, then Space Complexity (memory used) is important Not as much of a problem as it used to be but data sets are getting huge. There is often a time-space-tradeoff ▫Want Low Computing time and Low Memory consumption. ▫One then has to make a compromise
What Do We Mean By Best? Usually look at Computational Complexity (efficiency) Time Memory Other things may be important Difficulty ▫Can we understand and implement it? ▫Can we modify it? Validation ▫Can we validate correctness of the code? Suitable ▫Does the code suit the problem?
Summary Algorithm Analysis: characteristics for different input. Why? Predict performance/compare/choose algorithms Understand how it works leads to improvement Computation Complexity? How difficult and efficient it is. Time Complexity – Amount of time = # of operations Space Complexity = Amount of memory Time-Space Tradeoff Other things to think about Difficulty - Can we understand/implement/modify it? Validation - Can we validate correctness of the code? Suitability - Does the code suit the problem?