Algorithm Analysis
Recursive Algorithm
Alg. that breaks problem into smaller subproblems and applies the alg. itself to solve smaller subproblems o Must have a base case → Completes w/o applying itself to smaller subproblem → Ensures that recursive alg. eventually terminates o Not synonymous w/error-checking case.
Worst case
Algorithm does the max possible number of operations
Best case
Algorithm does the min possible number of operations
Growth of Functions and Complexity
Algorithm w/runtime complexity T(N) → Lower/Upper bound enclose all possible runtimes for alg. - Asymptotic Notation
Constant Time Operations
Always operates in the same amount of time regardless of input - Any constant # of constant time ops collectively 1 constant time ops
Runtime complexity of any recursive func. split into 2 parts:
- Operations done directly by functions - Operations done by recursive calls made by function
Analyzing the time complexity of recursive algorithms
- Recurrence Relations - Recursion trees
Lower bound
A function f(N) that is ≤ the best case T(N), for all values of N ≥ 1
Upper bound
A function f(N) that is ≥ the worst case T(N), for all values of N ≥ 1
Operations
Any statement(s) w/constant runtime O(1) → Precisely counting # of constant operations in a finite sequence is not needed
Asymptotic Notation
Classification of runtime complexity: O, Ω, Θ
Big O Notation
Describing how function (running time of alg.) generally behaves in relation to input size o All functions that have the same growth → Highest order term of the function in Big O
Runtime Growth Analysis
Efficiency of alg is most critical factor large inputs (Greater runtime) - For large N, difference in computational time varies greatly w/rate of growth of function
Recurrence Relations: Recurrence relation
Function f(N) is defined in terms of the same function operating on a value < N.
Recursive Function
Function that calls itself → To implement recursive algorithms
Algorithm Analysis
How runtime of an alg. scales as input size increases 1. Determine how many ops the alg. executes for input size N 2. Big O Notation for func. is determined - Focus on worse-case runtime
Determine Big O Notation
If f(N) = Sum of several terms → Highest order is kept and others are discarded. Ex: 5 + 13·N + 7·N^2 to O(N^2) - All constants are omitted.
Efficient algorithm aims to:
Lower algorithm runtime
Algorithm Efficiency
Measured by algorithm computational complexity → Used to identify/avoid using algorithms w/ - Long runtimes or high memory usage
Nested loops
N*N (according to number of nests)
Ω notation
Provides a growth rate for an algorithm's lower bound.
O notation
Provides a growth rate for an algorithm's upper bound.
Θ notation
Provides growth rate that is both upper and lower bound.
Root node
Represents k operations inside the first function call.
computational complexity
Resources used by the algorithm → resources: runtime and memory usage.
Recurrence Relations
Runtime complexity T(N) of a recursive function → function T on both sides of the equation. Ex: Binary search T(N) = O(1) + T(N / 2) → recurrence relation Using O-notation to express runtime complexity of recursive function requires solving the recurrence relations
Worst case runtime
Runtime complexity for an input results in longest execution
Recursive Algorithm: Binary search
Searches a sorted list for a key by comparing the key to the middle element in the list and searching half of the remaining list until key is found.
Auxiliary space complexity
Space complexity w/o input data → S(N) = K - w/k as constant
Recursion trees
Tool for solving recurrences: → Root node → Recursive ops. are represented below the node - 1st also does k operations. → # of nodes corresponds to # of recursive calls
Space Complexity
function S(N) → # of fixed-size memory units used by the alg. for an input of size N - Ex: S(N) = N + k Includes input data & additional memory allocated by the alg. - Auxiliary space complexity
Runtime complexity
function T(N) → # of constant-time operations performed by input of size N - Varies based on the input - Identify best and worst-case scenarios
Algorithm
method to solve a computational problem
Recursive Algorithm: Fibonacci Sequence
n = (n-1) + (n-2)
Analysis of Algorithm
runtime = # of constant time operations