3 Time Complexity(1)
3 Time Complexity(1)
Time Complexity
Dr. Rubi Quiñones
Computer Science Department
Southern Illinois University Edwardsville
Course Timeline
• Introduction to algorithms
ALGORITH
ANALYSIS
MIC
• Median findings and order statistics
• Time complexity
STRATEGIES • Activity selection problems
GREEDY
•
Advanc
Algorithm intractability
ed
ts
• Randomized algorithms 2
Time Complexity
• Running Time
• Asymptotic Notation
• Big oh
• Big omega
• Big theta
• Performance classification
• Complexity classes
• Standard analysis techniques
• Constant time statements
• Analyzing loops
• Analyzing nested loops
• Analyzing sequence of statements
• Analyzing conditional statements
• Iteration method
3
Review
• Algorithm: a finite set of statements that guarantees an optimal solution in finite interval of time
4
Review
• Algorithm: a finite set of statements that guarantees an optimal solution in finite interval of time
5
Factors
• Hardware
• Operating system
• Compiler
• Size of input
• Nature of input
• algorithms
6
Running Time of an Algorithm
• Depends on
• Input size
• Nature of input
• Generally, time grows with size of input, so running time of an algorithm is usually measured as a function
of input size
7
Running Time of an Algorithm
• Depends on
• Input size
• Nature of input
• Generally, time grows with size of input, so running time of an algorithm is usually measured as a function
of input size
• Running time is measured in terms of number of steps/primitive operations performed
8
Finding Running Time of an Algorithm
9
Example 1
How many steps does this algorithm take in relation to the input size? Ie, what is the complexity function?
Remember to take into consideration each operation. 10
Example 1
1 2 8
Once
3 4 5 6 7
Once per each iteration of for
loop, N iterations
1
Total: 5N+3
4
2 3
The complexity function of
5 6 7 the algorithm is f(N): 5N+3
8
1
As N grows, the number of steps grow in linear
4 proportion to N for this function “Sum”
2 3
5 6 7
8
F(N)=5N+3 12
Example 1
What happens to the +3 and 5 in f(N)=5N+3 as N gets larger?
13
Example 1
What happens to the +3 and 5 in f(N)=5N+3 as N gets larger?
+3 becomes insignificant
5 becomes inaccurate, different operations require varying amounts of time
14
Example 1
What happens to the +3 and 5 in f(N)=5N+3 as N gets larger?
+3 becomes insignificant
5 becomes inaccurate, different operations require varying amounts of time
15
Asymptotic Notation
16
Big Oh Notation
If f(N) and g(N) are two complexity functions, we say
f(N) = O(g(N))
“f(N) is order g(N)” or “f(N) is big-O of g(N)”
17
Example 2
Which function is better?
19
Example 2
Which function is better?
20
Example 3
21
Big Omega Notation
If we wanted to say “running time is at least…” we use omega
f(n) is Ω(g(n)) if there exist positive numbers c and n 0 such that 0<f(n)>=c (n) for all n>=n0
22
Big Theta Notation
If we wish to express tight bounds, we use the theta notation, 𝜃
Takes into consideration both big Oh and big Omega giving us the “average” complexity
23
What does it all mean?
If f(n)= 𝜃(g(n)) we say that f(n) and g(n) grow at the same rate, asymptotically
If f(n) = O(g(n)) and f(n) != Ω(g(n)), then we say that f(n) is asymptotically slower growing than g(n)
If f(n) = Ω(g(n)) and f(n) != O(g(n)), then we say that f(n) is asymptotically faster growing than g(n)
24
Which notation to use?
What is the common notation computer scientists use when designing algorithms?
25
Which notation to use?
What is the common notation computer scientists use when designing algorithms?
As computer scientists, we generally like to express our algorithms as big O since we would like to know the
upper bounds of our algorithms
Why?
26
Which notation to use?
What is the common notation computer scientists use when designing algorithms?
As computer scientists, we generally like to express our algorithms as big O since we would like to know the
upper bounds of our algorithms
Why?
If we know the worst case then we can aim to improve it and/or avoid it
27
Performance Classification
28
Complexity Classes
29
Size Does Matter
What happens if we double the input size of N?
This is especially important in high performance computing, parallelization computing, and supercomputing
30
Size Does Matter
Suppose a program has run time O(n!) and the run time for n=1 is 10 seconds.
31
Size Does Matter
Suppose a program has run time O(n!) and the run time for n=1 is 10 seconds.
32
Time Complexity
• Running Time
• Asymptotic Notation
• Big oh
• Big omega
• Big theta
• Performance classification
• Complexity classes
• Standard analysis techniques
• Constant time statements
• Analyzing loops
• Analyzing nested loops
• Analyzing sequence of statements
• Analyzing conditional statements
• Iteration method
33
Analyzing Loops - Linear Loops
34
Analyzing Nested Loops
Treat just like a single loop and evaluate each level of nesting as needed:
int j, k;
for (j=0; j<N; j++)
for (k=N; 0<k; k--)
sum += k+j;
35
Analyzing Nested Loops
Treat just like a single loop and evaluate each level of nesting as needed:
int j, k;
for (j=0; j<N; j++)
for (k=N; 0<k; k--)
sum += k+j;
36
Analyzing Nested Loops
What if the number of iterations of one loop depends on the counter of the other?
int j, k;
for (j=0; j<N; j++)
for (k=N; k<j; k++)
sum += k+j;
37
Analyzing Nested Loops
What if the number of iterations of one loop depends on the counter of the other?
int j, k;
for (j=0; j<N; j++) When doing Big O analysis, we sometimes have to
for (k=N; k<j; k++) compute a series like:
sum += k+j;
i.e. sum of the first n numbers. What is the complexity of
Analyze inner and outer loop together! this?
Number of iterations of the inner loop is: Gauss figured out that the sum of the first n numbers is
0+1+2+…+(N-1) = O(N^2) always:
𝑛
How did we get this answer? 𝑛 ∗ (𝑛 + 1) 𝑛2 + 𝑛
𝑖 = = = 𝑂 𝑛2
2 2
𝑖=1
38
Sequence of Statements
For a sequence of statements, compute their complexity functions individually and add them up
39
Conditional Statements
Lets say statement1 runs in O(n) time and statement2 runs in O(n^2) times
We use “worst case” complexity; among all inputs of size n, what is the maximum running time?
40
Time Complexity
• Running Time
• Asymptotic Notation
• Big oh
• Big omega
• Big theta
• Performance classification
• Complexity classes
• Standard analysis techniques
• Constant time statements
• Analyzing loops
• Analyzing nested loops
• Analyzing sequence of statements
• Analyzing conditional statements
• Iteration method
41
Deriving a Recurrence Equation
So far, all algorithms that we have been analyzing have been non recursive
However, if N>=2, then running time T(N) is the cost of each step taken plus time required to compute
power(x, n-1). (i.e., T(N)=2+T(N-1) for N>=2)
43
Summary
Algorithms can be classified according to their complexity (O notation)
only relevant for large input sizes
44
Time Complexity
• Running Time
• Asymptotic Notation
• Big oh
• Big omega
• Big theta
• Performance classification
• Complexity classes
• Standard analysis techniques
• Constant time statements
• Analyzing loops
• Analyzing nested loops
• Analyzing sequence of statements
• Analyzing conditional statements
• Iteration method
45
Course Timeline
• Introduction to algorithms
ALGORITH
ANALYSIS
MIC
• Median findings and order statistics
• Time complexity
STRATEGIES • Activity selection problems
GREEDY
•
Advanc
Algorithm intractability
ed
ts
• Randomized algorithms 46