Week 2: Complexity Analysis Flashcards
What is Complexity Analysis?
The time required for an algorithm to execute until completion.
What is a search problem?
In a standard search problem, we have the inputs: A sequence of numbers a1, a2, …, an A number to search, q
And the expected outputs: The index in a in which exists q, or NIL if q is not present.
What is a Linear search?
A linear search checks each element in the sequence sequentially.
This gives a worst case performance of O(n) if q = an or if q is not present in the sequence.
This gives an average case performance of n/2. A sorted sequence doesn’t affect the performance.
What is a Binary search?
For a binary search, the sequence, a, must be sorted. This gives a worst case performance of log2(n). If n is 10 for example, a maximum of 4 array members will need to be checked log2(10)= 3.3~.
How is an algorithm executed using a computational model?
Usually, a generic uniprocessor random-access machine (RAM) is used.
All memory is equally expensive to access.
All instructions are executed one after another, no concurrency.
All reasonable instructions take 1 unit time.
Function calls do not use one unit.
Constant word size.
How are the results a function of Input Size?
Sorting: number of input items
Multiplication: number of involved bits
Graph Algorithms: Number of nodes and edges in graph
Worst and Average Case
Worst case: Provides an upper bound on running time. A guarantee running time will never be worse than this.
Average case: Provides the expected running time Usually very useful. Care needs to be taken to define what ‘average’ means. It could mean the average input taken from the real world (a median or mode). It could mean the average running time taken from random equally likely inputs.
What is Insertion sort?
- Work left to right.
- Examine each item and compare it to the items on its left.
- Insert the item in the correct position on the array.
Worst time complexity is O(n2) if the sequence is sorted decreasing then there will be n number of comparisons and n number of swaps.
Code: InsertionSort(A, n) {for i = 2 to n{ key = A[i] j = i - 1 while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } } }
Big-O bound notation?
Big-O notation defines the upper bound for the time complexity of an algorithm.
f(n) is O(g(n)) if there exists positive constants c and n0 such that f(n) ≤ c • g(n) for all n ≥ n0.
Big-Ω bound notation?
Big-Ω defines the lower bound for the time complexity of an algorithm.
f(n) is Ω(g(n)) if there exists positive constants c and n0 such that 0 ≤ c • g(n) ≤ f(n) for all n ≥ n0
Big-Θ bound notation?
Big-Θ defines the asymptotic tight bound for the time complexity of an algorithm. f(n) is Θ(g(n)) if there exists positive constants c1, c2 and n0 such that c2 • g(n) for all n ≥ n0
If a program by Θ(g(n)) then it is both O(g(n)) and Ω(g(n))
Other notation?
o(g(n)) is like < g(n)
O(g(n)) is like ≤ g(n)
ω(g(n)) is like > g(n)
Ω(g(n)) is like ≥ g(n)
Θ(g(n)) is like = g(n)