Lec9 EEE13 2s1617
Lec9 EEE13 2s1617
INTRODUCTION TO ALGORITHM
COMPLEXITY
MOTIVATION
2. Distance
3. Price
MOTIVATION
CASE: MULTIPLICATION
2. Memory Usage
HOW?
THEORETICAL APPROCHES
void doSomethingTrivial()
{
int a = 0, b = 1, c = 2;
c = a + b;
printf("%d + %d = %d\n", a, b, c);
}
BIG-OH NOTATION
2
EXAMPLE: O(N ) - TIME TO COMPUTE SCALES QUADRATICALLY WITH INPUT SIZE
2
EXAMPLE: O(N ) - TIME TO COMPUTE SCALES QUADRATICALLY WITH INPUT SIZE
N
EXAMPLE: O(2 ) - TIME TO CALCULATE DOUBLES PER INCREASE IN INPUT SIZE
EXAMPLE: O(LOG(N)) - DIVIDE A LINEAR DATA SET INTO TWO EACH ITERATION
EXAMPLE: O(LOG(N)) - DIVIDE A LINEAR DATA SET INTO TWO EACH ITERATION
EXAMPLE: O(N*LOG(N))
▸ Merge Sort
▸ Heap Sort
GENERALIZATION
GROWTH OF FUNCTIONS
▸ T = f(N)
GENERALIZATION
GROWTH OF FUNCTIONS
int func1(int aList[], int N) {
int iSum;
int i;
iSum = 0; /* 1 */
i = 0; /* 1 */
while( i < N) { /* N+1 */
iSum += aList[i]; /* 3N */
i++; /* N */
}
return iSum; /* 1 */
} /* f(N) = 5N + 4 */
GENERALIZATION
GROWTH OF FUNCTIONS
void func2(int aList[], int N) {
int iSum;
int i;
int j;
for (i = 0; i < N; i++) { /*1 + N + 1 + N = 2N + 2*/
for (j = 0; j < N; j++) { /*N*(2N+2)*/
printf("%d\n", aList[j]*aList[i]);/*N*4N*/
}
/*f(N) = 6N2 + 4N + 2*/
GENERALIZATION
▸ g(N) = O(f(N))
GENERALIZATION
▸ T(N) = 5N + 4 = O(N)
Let there be constants C0 and N0 such that:
C0 N > 5N + 4, when N > N0
(Example: C0 = 6, N0 = 4)
GENERALIZATION
▸ Omega Notation
▸ Theta Notation
▸ Little-oh notation
Why? Worst-case does not always happen
READINGS
▸ https://www.interviewcake.com/article/java/big-o-notation-time-
and-space-complexity
▸ https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-
notation/
▸ https://justin.abrah.ms/computer-science/big-o-notation-
explained.html
▸ http://bigocheatsheet.com/
SEE YOU NEXT
WEEK :D