Solution: Algorithmic Analysis with big O notation.
n
approaching infinite, rather than the small n(n)
is the size of an array (n = size )
A[0] = K.
A[]
with different kint SequentialSearch(int A[], int size, int K) {
for (int i = 0; i < size; i++>) {
if (A[i] == K)
return i;
}
return -1;
}
f(n)
n
is the size of the inputIf we compare two algorithms, f1(n) = 1000
, f2(n) = n
f2(n) and f5(n) = 0.5*n
- minor differences, can ignore minor differences between each group
k
is a constant
1, 1000, any k*1
0.5*n, n, any k*n
Time Complexity | Type |
---|---|
O(1) |
Constant time |
O(log2(n)) |
Logarithmic time |
O(n) |
Linear time |
O(n * log2(n)) |
Linearithmic Time |
O(n^2) |
Quadratic time |
O(n^3) |
Cubic time |
O(2^n) |
Exponential time |
O(n!) |
Factorial time |
Example
n^2 = O(n^2)
2*n^2 = O(n^2)
99999999*n^2 = O(n^2)
O(2^n)
and O(3^n)
are not the same orders. O(3^n)
> O(2^n)
O(n^k)
, k
is the largest power valuen! + 5*3^n 500 * 2^n n^3 + 12n^2 + 99 = O(n!)
f(n)
is classified as O(g(n))
if there exists two positive constants K
and n
- such that
|f(n)| < K|g(n) | for all n > n0
f(n) < K*g(n), for all n > n0
f(n) is O(g(n))
is sometimes written as f(n) = O(g(n))
f
may be smaller than g
f
may be similar to g
n
. Let's talk big (n approaching infinite)g
is NOT too much bigger than f
(finite constant K
times bigger is OK), f belongs to the same group as g
.f(n) = O(g(n))
, we mean f(n)
has the same order of complexity as g(n)
has
g(n) could have an order higher than f(n). OR they have the same order
We want a tigheter upper bound
O(1)
When code is simple, finite steps, they are all big O(n)
Ex. Sequentially implement list acces [i]
item
When pointer to the node is given: O(1)
Doubly-linked list deleted a node when a pointer to the node is given
Doubly linked list: insert a node when a pointer to the place is given
void LinkedList::insertFront(Node *newNode) {
if (newNode ==NULL) {
//check input
cout << "Warning! insert Front has newNode == NULL" << endl;
return;
}
if(head == NULL) {//special case: list is empty
head = newNode;
} else { //general case: list is not empty
newNode-> next = head;
head = newNode;
}
}
//constant steps
O(n)
A*N
timesExamples:
How many steps? (an + b)
steps (worst case)
b
: outside loop, constant stepsNode *ListSearch(Data Item value, Node *head) {
Node* tempNode = headl
while(tempNode ! = NULL) {
if (tempNode -> data == value) {
return tempNode;
}
tempNode = tempNode -> next;
}
return NULL;
}
for (int i = 1; i <=n; i+ =10) {
//some O(1) steps here within the loop
}
// some O(1) steps
n/10
timesa*n
times, a
is some constantk
levels of nested loops, then it is:for (int i = 1; i <= i; i+=1) {
for (int j = 1; j <=n; j+=2) {
//some O(1) steps
}
//some O(1) steps
}
(n)
times(an + b)
stepsTotal: (an + b)*n + c = an^2
(fix this after looking at sliedes)
for (int i = 1; i <= i; i+=1) {
for (int j = 1; j <=n; j+=2) {
for
}
//some O(1) steps
}
for (int i = 1; i<n; i*=2) {
//some O(1) steps
}
= (a log2n + b) steps: worst case O(log2n)
What if it is i*=3
?
O(log3n)
n
times, in the ith
reptition, it requires i
stepsTotal steps = 1 + 2 + 3 + ... + n = >
a, b, c
Highest one is O(n3)
int factorialRecusrive (int n) {
if (n<0) {
cout << "warning!" <<endl ;
return 0;
} else if (n == 0)
return 1;
else {
return (n*factorialRecursive(n-1));
}
}
T(n)
represents the total steps needed for inputT(n) = T(n -1) + a
: recurrence relationT(0) = b
: ending caseT(n) = T(n-1) = a
= (T(n-2) + a) + a
= ((T(n-3)+ a)+ a) + a
...
= T(n-k) + k*a
When k = n, we reach the ending case
T(n) = T(O) + n*a = b + a*n
,
that is O(n)
int factorialNonRecursive(int n) {
//non recursive version, need to use a loop
if (n < 0) {
cout <<"Warning! Factorial can only take non-negative input!" << endl;
return 0;
}
if (n == 0) return 1;
int result = 1;
for (int i = 1; i <= n; ) {
result = result *i;
}
return result;
}
int fibonnaciRecursive (int n) {
if (n < 0) {
cout << "Warning! Input <0." <<endl;
return 0;
}
else if (n == 0) return 0;
else if (n ==1) return 1;
else return fibonnaciRecursive(n-1) + fibbonacciRecursive(n-2);
}
T(n) = T(n-1) + T(n-2) + a
: Reccurence relationUseful website: https://developerinsider.co/big-o-notation-explained-with-examples/