H16 Complexiteit
H16 Complexiteit
H16 Complexiteit
q Worst case
q What is the maximum time taken by the algorithm?
(often when the input fulfills some special conditions)
q Average case
q How much time does the algorithm take when given random input?
(i.e., without special assumptions or restrictions on the input)
q Best case
q What is the minimum time taken by the algorithm?
(often when the input fulfills some special conditions)
What is the input size n?
measurements = []
for N in range(…):
# … (create input of size N)
start = time.perf_counter()
# … (do something with input)
end = time.perf_counter()
measurements.append((N, end-start))
Remember this:
Some examples:
T(n) = 25 We use Big-O to express
= O(1) an (asymptotic) least upper
bound on the time required by
an algorithm
T(n) = 25 n2 + 3000 n + 10
= O(n2)
O(n log n)
O(n)
O(log n)
O(1)
fast
Calculating with Big O
# Example
for i in range(3):
# body: O(n)
# overall: O(n)
Calculating with Big O
# Example
for i in range(n):
# body O(1)
for j in range(n*n):
# body O(1)
# Example 1: # Example 2:
for i in range(n/2): def helper(n):
# body: O(n2) # body: O(n)