Lec-2 & 3 Program Efficency & Complexity-1
Lec-2 & 3 Program Efficency & Complexity-1
Lec-2 & 3
1/31
Questions that will be answered
• What is a “good” or "efficient" program?
• How to measure the efficiency of a program?
• How to analyse a simple program?
• How to compare different programs?
• What is the big-O notation?
• What is the impact of input on program performance?
• What are the standard program analysis techniques?
• Do we need fast machines or fast algorithms?
2/31
Which is better?
The running time of a program.
3/31
Measuring Efficiency
Ways of measuring efficiency:
4/31
Measuring Efficiency
5/31
A Simple Example
6/31
A Simple Example
Analysis of Sum
1.) Describe the size of the input in terms of one ore more
parameters:
- Input to Sum is an array of N ints, so size is N.
2.) Then, count how many steps are used for an input of that size:
- A step is an elementary operation such as
+, <, =, A[i]
7/31
A Simple Example
Analysis of Sum (2)
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A
8/31
Analysis: A Simple Example
How 5N+3 Grows
Estimated running time for different values of N:
N = 10 => 53 steps
N = 100 => 503 steps
N = 1,000 => 5003 steps
N = 1,000,000 => 5,000,003 steps
9/31
Analysis: A Simple Example
What Dominates?
What about the 5 in 5N+3? What about the +3?
• As N gets large, the +3 becomes insignificant
•5 is inaccurate, as different operations require varying
amounts of time
10/31
Analysis: A Simple Example
Asymptotic Complexity
11/31
Comparing Functions
f(N) = O(g(N))
12/31
Comparing Functions
100n2 Vs 5n3, which one is better?
250000
200000
150000
100000
50000
0
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
100n2 10 40 90 16 25 36 49 64 81 10 12 14 16 19 22 25 28 32 36 40 44 48 52 57 62 67 72 78 84 90 96 1E 1E1E
5n3 5 40 13 32 62 10 17 25 36 50 66 86 10 13 16 20 24 29 34 40 46 53 60 69 78 87 98 1E1E 1E 1E2E 2E2E
13/31
Comparing Functions
100n2 Vs 5n3, which one is better?
Differenec of functions
20000
10000
0
-10000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
-20000
-30000
-40000
-50000
-60000
-70000
-80000
-90000
14/31
Comparing Functions
Why is this useful?
As inputs get larger, any algorithm of a smaller order will
be more efficient than an algorithm of a larger order
0.05 N2 = O(N2)
Time (steps)
3N = O(N)
Input (size)
N = 60
15/31
Comparing Functions
What is the relationship between the following?
f1(n)
f2(n)
f3(n)
F(n)
f4(n)
16/31
Big-O Notation
Important:
• Big-O is not a function!
• Never read = as "equals"
• Examples:
5N + 3 = O(N)
37N5 + 7N2 - 2N + 1 = O(N5)
17/31
Big-O Notation
350000
100n2
300000
5n3
250000
100n2 + 5n3
200000
5n4
150000
100000
50000
0
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
18/31
Size does matter
Common Orders of Growth
Let N be the input size, and b and k be constants
Increasing Complexity
O(logbN) = O(log N) Logarithmic Time
O(N) Linear Time
O(N log N)
O(N2) Quadratic Time
O(N3) Cubic Time
...
O(kN) Exponential Time
19/31
Size does matter
What happens if we double the input size N?
N log2N 5N N log2N N2 2N
8 3 40 24 64 256
16 4 80 64 256 65536
32 5 160 160 1024 ~109
64 6 320 384 4096 ~1019
128 7 640 896 16384 ~1038
256 8 1280 2048 65536 ~1076
21/31
Standard Analysis Techniques
Constant time statements
Simplest case: O(1) time statements
• Assignment statements of simple data types
int x = y;
• Arithmetic operations:
x = 5 * y + 4 - z;
• Array referencing:
A[j] = 5;
• Array assignment:
j, A[j] = 5;
• Most conditional tests:
if (x < 12) ...
22/31
Standard Analysis Techniques
Analyzing Loops
Any loop has two parts:
1. How many iterations are performed?
2. How many steps per iteration?
23/31
Standard Analysis Techniques
Analyzing Loops (2)
What about this for-loop?
24/31
Standard Analysis Techniques
Analyzing Loops (3)
What about while-loops?
Determine how many times the loop will be executed:
25/31
Standard Analysis Techniques
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; k>0; k--)
sum += k+j;
int j,k;
for (j=0; j < N; j++)
for (k=0; k < j; k++)
sum += k+j;
27/31
Standard Analysis Techniques
Digression
When doing Big-O analysis, we sometimes have to compute
a series like:
1 + 2 + 3 + ... + (N-1) + N
Remember Gauss:
28/31
Standard Analysis Techniques
Sequence of Statements
For a sequence of statements, compute their complexity functions
individually and add them up
SUM RULE
29/31
Standard Analysis Techniques
Conditional Statements
What about conditional statements such as
if (condition)
statement1;
else
statement2;
where statement1 runs in O(N) time and statement2 runs
in O(N2) time?
31/31