Lecture 13
Complexity
reminder
Measuring efficiency
(how long it takes your program to run)
Algorithmic complexity
Algorithmic complexity is a very important topic in computer science. Knowing the complexity of algorithms allows you to answer questions such as:
Complexity
Cars
Automobiles are divided by size into
several categories:
These categories provide a quick idea what size car you’re talking about, without needing to mention actual dimensions.
�Similarly, it’s useful to have a shorthand way to say how efficient a computer algorithm is
Lists: One application
List: one application
Lists: Running time
Lists: Running time
Running time: What version of the problem are you analyzing
Analyzing the worst case
def find(lst, toFind):
for i in lst:
if (i == toFind):
return True
return False
How many instructions do you have to execute to find out if the element is in the list in the worst case, if n represents the length of the list?
Analyzing the worst case
def find (lst, toFind):
for i in lst:
if (i == toFind):
return True
return False
def slowFind (lst, toFind):
for i in lst:
print("looking for: " + str(toFind))
if (i == toFind):
return True
return False
Which method is faster?
A: find
B: slowFind
C: They are about the same
Analyzing the worst case
def find (lst, toFind):
for i in lst:
if (i == toFind):
return True
return False
def fastFind(lst, toFind):
return False
Which method is faster?
A: find
B: fastFind
C: They are about the same
Time Complexity
A way of showing how the runtime of function increases as the size of the input increases
Time: Comparing Implementations
def factors(n):
# Slow: Test each k from 1 through n
# Fast: Test each k from 1 to square root n
# For every k, n/k is also a factor!
Question: How many time does each implementation use division?
Def factors(n)
1: Test each k from 1 through n
2: Test each k from 1 to square root n. For every k, n/k is also a factor!
Time (number of divisions)
A: n for (1) n for (2)
B: n2 for (1) n0.5 for (2)
C: n0.5 for (1) n for (2)
D: n for (1) n0.5 for (2)
E: None of the above
Comparing Orders of Growth
Properties of Orders of Growth (O vs Theta)
LOGS
Comparing orders of growth (n is the problem size)
Order of Growth of Counting Factors
Order of Growth of Counting Factors
Order of Growth of Counting Factors
Abused Notation
VERY often Big-O is used instead of Big-Theta (in Computer Science).
Not as an upper bound!
Practice
What is the growth rate?
Let R(n) = 100000
What is the growth rate?
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What is the growth rate?
Let R(n) = 100000n + n2
What is the growth rate?
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What is the growth rate?
Let R(n) = 100000n + n2 + n log n
What is the growth rate?
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What can you say about these functions?
n, 2n, 3n, 4n, 5n
A: The next one is 6n
B: n is faster than 5n
C: 5n is faster than n
D: All have the same complexity: O(n)
E: Nothing, still waking up
Recognizing the complexity for the code
What is the growth rate?
sum = 0
for i in range(1, n+1):
sum = sum * i
print(i)
A: O(n)
B: O(sqrt(n))
C: O(1)
D: O(n^2)
E: O(log n)
What is the growth rate?
sum = 0
for i in range(1, n+1):
if (i%3 == 0):
sum = sum * i
print(i)
A: O(n)
B: O(1)
C: O(log n)
C: O(n^2)
E: Impossible to calculate
What is the growth rate?
sum = 0
for i in range(1, n+1):
for j in range(1, n+1):
sum = sum * i + j
A: O(n2)
B: O(2^n)
C: O(n)
D: O(log n)
E: O(1)
Useful formula
1 + 2 + 3 + 4 + …… + n =
What is the growth rate?
sum = 0
for i in range(1, n+1):
j = 1
while j <=i:
sum = sum * i + j
j = j + 1
A: O(n2)
B: O(1)
C: O(n)
D: O(log n)
E: O(n log n)
reminder
What is the growth rate?
sum = 0
j = n
while j>1:
print(j)
j = j//2
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What is the growth rate?
sum = 0
for i in range(1, n+1):
sum = sum * i
for j in range(1, n+1):
sum = sum + j
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What is the growth rate?
sum = 0
j = 1
for i in range(1, n+1):
while j <=i:
sum = sum * i + j
j = j + 1
A: O(n2)
B: O(1)
C: O(n)
D: O(log n)
E: O(n log n)
What is the growth rate?
sum = 0
j = n
while j<=n:
print(j)
j = j*2
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
What is the growth rate for f2(n)?
def f2(n):
for i in range(1, n+1):
f1(n)
A: O(1)
B: O(n)
C: O(log n)
D: O(n log n)
E: O(n2)
def f1(n):� sum = 0
for i in range(1, n+1):
sum = sum * i
print(i)