4 Hafta
4 Hafta
COMP 2002
1
• An algorithm is a set of instructions to be followed to solve
a problem.
2
• An algorithm is a set of instructions to be followed to solve
a problem.
• Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect the runtime?
The idea is not to find the exact computational time required by the algorithm since this
time is to be affected by other factors:
– How are the algorithms coded?
• Comparing running times means comparing the implementations.
• We should not compare implementations, because they are sensitive to programming
style that may cloud the issue of which algorithm is inherently more efficient.
– What computer should we use?
• We should compare the efficiency of the algorithms independently of a particular
computer.
– What data should the program use?
• Any analysis must be independent of specific data.
5
Asymptotic Algorithm Analysis
6
The Execution Time of Algorithms
• Each operation in an algorithm (or a program) has a cost.
➔ Each operation takes a certain of time.
A sequence of operations:
➔ Total Cost = c1 + c2
7
The Execution Time of Algorithms (cont.)
Example: Simple If-Statement
Cost Times
if (n < 0) c1 1
absval = -n c2 1
else
absval = n; c3 1
8
The Execution Time of Algorithms (cont.)
Example: Simple Loop
Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
9
The Execution Time of Algorithms (cont.)
Example: Nested Loop
Cost Times
i=1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
j=1; c4 n
while (j <= n) { c5 n*(n+1)
sum = sum + i; c6 n*n
j = j + 1; c7 n*n
}
i = i + 1; c8 n
}
10
The Execution Time of Algorithms (cont.)
Example: Function Calls
Find the running time of each function call. Be careful about their analyses if
these are recursive functions.
Recurrence relation:
int factorial( int n ) { T(n) = T(n - 1) + c
if ( n <= 1 ) T(1) = c
return 1;
return n * factorial(n - 1); Solving it with the substitution method
} T(n) = T(n - 1) + c
T(n) = T(n - 2) + c + c
T(n) = T(n - 3) + c + c + c
...
T(n) = T(n - k) + k c
T(n) = T(1) + (n - 1) c
11
T(n) = n c
General Rules for Estimation
• Loops: The running time of a loop is at most the running time
of the statements inside of that loop times the number of
iterations.
• If/Else: Never more than the running time of the test plus the
larger of running times of S1 and S2.
12
Algorithm Growth Rates
• We measure an algorithm’s time requirement as a function of the
problem size.
– Problem size depends on the application: e.g. number of elements in a list for a
sorting algorithm, the number users for a social network search.
• So, for instance, we say that (if the problem size is n)
– Algorithm A requires 5*n2 time units to solve a problem of size n.
– Algorithm B requires 7*n time units to solve a problem of size n.
• The most important thing to learn is how quickly the algorithm’s
time requirement grows as a function of the problem size.
– Algorithm A requires time proportional to n2.
– Algorithm B requires time proportional to n.
• An algorithm’s proportional time requirement is known as growth
rate.
• We can compare the efficiency of two algorithms by comparing
their growth rates.
13
Compare f(N) = N, g(N) = 1000N and h(N) = N2
14
Algorithm Growth Rates (cont.)
15
Common Growth Rates
log2 N Log-squared
N Linear
N log N Log-linear
N2 Quadratic
N3 Cubic
2N Exponential
16
Running Times for Small Inputs
17
Running Times for Large Inputs
18
Asymptotic notations: Big O Notation
19
Asymptotic notations: Big O Notation
20
Asymptotic notations: Big O Notation
Definition:
Algorithm A is order g(n) – denoted as O(g(n)) –
if constants k and n0 exist such that A requires no more than k*g(n) time
units to solve a problem of size n ≥ n0. ➔ T(n) ≤ k*g(n) for all n ≥ n0
T(n) = O( g(n) )
• g(n) is an upper-bound on T(n)
• The growth rate of T(n) is less than or equal to that of g(n)
• T(n) grows at a rate no faster than g(n)
21
Asymptotic notations: Big O Notation
Thus, the algorithm requires no more than k*n2 time units for n ≥ n0 ,
So it is O(n2)
22
Asymptotic notations: Big O Notation
23
Asymptotic notations: Big O Notation
• Show 2x + 17 is O(2x)
• Hence k = 2 and n0 = 5
24
Asymptotic notations: Big O Notation
• Show 2x + 17 is O(3x)
• 2x + 17 ≤ k3x
• Easy to see that rhs grows faster than lhs over time ! k=1
• However when x is small 17 will still dominate ! skip over
some smaller values of x by using n0 = 3
• Hence k = 1 and n0 = 3
25
Asymptotic notations: Big Omega Notation
Definition:
Algorithm A is omega g(n) – denoted as Ω(g(n)) –
if constants k and n0 exist such that A requires more than k*g(n) time
units to solve a problem of size n ≥ n0. ➔ T(n) ≥ k*g(n) for all n ≥ n0
T(n) = Ω( g(n) )
• g(n) is a lower-bound on T(n)
• The growth rate of T(n) is greater than or equal to that of g(n)
• T(n) grows at a rate no slower than g(n)
26
Asymptotic notations: Big Omega Notation
Show that
• 2 n2 = Ω( n2 ) → n2 is also a lower-bound on 2 n2
27
Asymptotic notations: Big Theta Notation
Definition:
Algorithm A is theta g(n) – denoted as (g(n)) –
if constants k1, k2 and n0 exist such that k1*g(n) ≤ T(n) ≤ k2*g(n) for all n ≥ n0
T(n) = Θ( g(n) )
• g(n) is a tight-bound on T(n)
• The growth rate of T(n) is equal to that of g(n)
Although using big-theta would be more precise, big-O answers are typically given.
28
𝚹
Asymptotic notations: Big Theta Notation
29
𝚹
A Comparison of Growth-Rate Functions
30
Growth-Rate Functions
O(1) Time requirement is constant, and it is independent of the problem’s size.
O(log2n) Time requirement for a logarithmic algorithm increases slowly
as the problem size increases.
O(n) Time requirement for a linear algorithm increases directly with the size
of the problem.
O(n*log2n) Time requirement for a n*log2n algorithm increases more rapidly than
a linear algorithm.
O(n2) Time requirement for a quadratic algorithm increases rapidly with the
size of the problem.
O(n3) Time requirement for a cubic algorithm increases more rapidly with the
size of the problem than the time requirement for a quadratic algorithm.
O(2n) As the size of the problem increases, the time requirement for an
exponential algorithm increases too rapidly to be practical.
31
Growth-Rate Functions
• If an algorithm takes 1 second to run with the problem size 8,
what is the time requirement (approximately) for that algorithm
with the problem size 16?
• If its order is:
O(1) ➔ T(n) = 1 second
O(log2n) ➔ T(n) = (1*log216) / log28 = 4/3 seconds
O(n) ➔ T(n) = (1*16) / 8 = 2 seconds
O(n*log2n) ➔ T(n) = (1*16*log216) / 8*log28 = 8/3 seconds
O(n2) ➔ T(n) = (1*162) / 82 = 4 seconds
O(n3) ➔ T(n) = (1*163) / 83 = 8 seconds
O(2n) ➔ T(n) = (1*216) / 28 = 28 seconds = 256 seconds
32
Properties of Growth-Rate Functions
1. We can ignore low-order terms in an algorithm’s growth-rate
function.
– If an algorithm is O(n3+4n2+3n), it is also O(n3).
– We only use the higher-order term as algorithm’s growth-rate function.
33
Properties of Growth-Rate Functions
• If g(n) = 2 n2,
g(n) = O(n4), g(n) = O(n3), g(n) = O(n2) are all technically correct.
• If h(n) = 2 n2 + 100 n
Do NOT say h(n) = O(2 n2 + 100 n) or h(n) = O(2 n2) or h(n) = O(n2 + n).
34
Growth-Rate Functions – Example1
Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
35
Growth-Rate Functions – Example2
Cost Times
i=1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
j=1; c4 n
while (j <= n) { c5 n*(n+1)
sum = sum + i; c6 n*n
j = j + 1; c7 n*n
}
i = i +1; c8 n
}
x=x+1;
n i j
T(n) =
∑∑∑1
i=1 j= 1 k=1
37
Growth-Rate Functions – Example4
38
Algorithm Analysis
However, after coding the algorithm, if it runs much more slowly than the
algorithm analysis suggests, there may be an implementation inefficiency.
39
What to Analyze
• An algorithm can require different times to solve different problems
of the same size.
– Eg. Searching an item in a list of n elements using sequential search. ➔ Cost: 1,2,…,n
• The best case for my algorithm is n=1 because that is the fastest.”
WRONG!
• Best case is defined for the input of size n that is fastest among all
inputs of size n.
41
A Common Misunderstanding
• Worst case refers to the worst input from among the choices for
possible inputs of a given size.
42
Sequential Search
public static int sequential(int[] arr, int N, int target)
{
for(int i = 0 ; i < N ; i++)
if( arr[i] == target)
return i;
return -1;
}
Find the worst case, best case, and average case upper-bounds.