CMSC 132: Algorithmic Complexity
Big o notation
Represents an upper bound on the number of steps performed by an algorithm, for a sufficiently large input size- the intrinsic efficiency of the algorithm for large inputs
Efficiency of BST Operations
Search: - Balanced O(log(n)) - Degenerate O(n) Insertion: - Balanced: O(log(n)) - Degenerate: O(n) Deletion: -Balanced O(log(n)) - Degenerate: O(n)
Average case
The average case is the number of steps required for the "typical" case - Most useful metric Average case- average over all possible inputs, assuming that all inputs have the same likelihood Expected case- a weighted average over all possible inputs, based on the likelihood of each input
Best case and worst case
The best case is the smallest number of steps required The worst case is the largest number of steps required
Critical section of algorithm
The execution time of an algorithm's critical section is what dominates its overall execution time Operation central to the functioning of the algorithm Contained inside most deeply nested loops/recursive calls Executed as often as any other part of the algorithm
O(n)
Time(2n)/Time(n)
Observations
Any O(log(n)) algorithm is faster than O(n) Any O(n) algorithm is faster than O(n2) Asymptotic complexity is a fundamental measure of efficiency
Efficiency of searching an array or list
Array= O(n) Linked list= O(n) Does not make a difference if the array is sorted or ordered Binary search (only works with sorted arrays)= O(log(n))
Determining asymptotic complexity- informal
As n increases, the highest-order term of a function (the function represents the running time of a program, in terms of the input size n) dominates its value. Lower order terms can be ignored
Asymptotic analysis
Asymptotic analysis - Mathematically analyze an algorithm's efficiency, and express its running time as a function of the input size (which is called n) - Notation: running time is O(f(n)), meaning time is on the order of some function f(n) Advantages - Measures intrinsic efficiency - Programming language, compiler, and processor are irrelevant
Benchmarking
Code the algorithm up as a program Run the program on some inputs (often on well- known standard inputs) Measure the time and memory space it uses (often compare them against other known results for those inputs) Advantage: -Gives precise information for a given configuration (implementation, hardware, inputs) Drawbacks: - Is affected by the specific configuration - Is affected by special cases (biased inputs) - Does not measure intrinsic efficiency
Algorithm efficiency
Efficiency has to do with the amount of resources used by an algorithm Two approaches to measuring efficiency are: - Benchmarking - Asymptotic analysis - Benchmarking
Analyzing algorithms
Goal: to determine the asymptotic complexity of an algorithm 1. Ignore the less frequently executed parts of the algorithm 2. Find the critical section of the algorithm 3. Determine how many times the critical section is executed as a function of the input problem size (n)
Height of Trees
Height of degenerate tree O(n)
Linked List Efficiency (Insertion, deletion, indexing)
Insertion: O(1) Deletion: O(1) Indexing: O(n) Does not make difference if sorted
Array Efficiency (Insertion, deletion, indexing)
Insertion: O(n) Deletion: O(n) Indexing: O(1) Does not make difference if sorted